Skip to content

Commit

Permalink
Add TypeScript support
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnns authored Aug 13, 2023
2 parents 092e554 + 2d7729f commit a183ba7
Show file tree
Hide file tree
Showing 5 changed files with 242 additions and 59 deletions.
34 changes: 34 additions & 0 deletions lib/moduleEnv.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,22 +84,30 @@ function requireProxy(path) {

function registerExtensions() {
var originalJsExtension = require.extensions[".js"];
var originalTsExtension = require.extensions[".ts"];
var originalCoffeeExtension = require.extensions[".coffee"];

if (originalJsExtension) {
originalExtensions.js = originalJsExtension;
}
if (originalTsExtension) {
originalExtensions.ts = originalTsExtension;
}
if (originalCoffeeExtension) {
originalExtensions.coffee = originalCoffeeExtension;
}
require.extensions[".js"] = jsExtension;
require.extensions[".ts"] = tsExtension;
require.extensions[".coffee"] = coffeeExtension;
}

function restoreExtensions() {
if ("js" in originalExtensions) {
require.extensions[".js"] = originalExtensions.js;
}
if ("ts" in originalExtensions) {
require.extensions[".ts"] = originalExtensions.ts;
}
if ("coffee" in originalExtensions) {
require.extensions[".coffee"] = originalExtensions.coffee;
}
Expand Down Expand Up @@ -136,6 +144,32 @@ function jsExtension(module, filename) {
originalExtensions.js(module, filename);
}

function tsExtension(module, filename) {
var _compile = module._compile;

module._compile = function rewireCompile(content, filename) {
var noConstAssignMessage = linter.verify(content, eslintOptions).find(isNoConstAssignMessage);
var line;
var column;

if (noConstAssignMessage !== undefined) {
line = noConstAssignMessage.line;
column = noConstAssignMessage.column;
throw new TypeError(`Assignment to constant variable at ${ filename }:${ line }:${ column }`);
}
_compile.call(
this,
content
.replace(shebang, '') // Remove shebang declarations
.replace(matchConst, "$1let $2"), // replace const with let, while maintaining the column width
filename
);
};

restoreExtensions();
originalExtensions.ts(module, filename);
}

function coffeeExtension(module, filename) {
if (!coffee) {
throw new Error("Cannot rewire module written in CoffeeScript: Please install 'coffeescript' package first.");
Expand Down
Loading

0 comments on commit a183ba7

Please sign in to comment.