Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NEW Add support for .ts files #204

Merged
merged 2 commits into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading