Skip to content

Commit a183ba7

Browse files
authored
Add TypeScript support
2 parents 092e554 + 2d7729f commit a183ba7

File tree

5 files changed

+242
-59
lines changed

5 files changed

+242
-59
lines changed

lib/moduleEnv.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,22 +84,30 @@ function requireProxy(path) {
8484

8585
function registerExtensions() {
8686
var originalJsExtension = require.extensions[".js"];
87+
var originalTsExtension = require.extensions[".ts"];
8788
var originalCoffeeExtension = require.extensions[".coffee"];
8889

8990
if (originalJsExtension) {
9091
originalExtensions.js = originalJsExtension;
9192
}
93+
if (originalTsExtension) {
94+
originalExtensions.ts = originalTsExtension;
95+
}
9296
if (originalCoffeeExtension) {
9397
originalExtensions.coffee = originalCoffeeExtension;
9498
}
9599
require.extensions[".js"] = jsExtension;
100+
require.extensions[".ts"] = tsExtension;
96101
require.extensions[".coffee"] = coffeeExtension;
97102
}
98103

99104
function restoreExtensions() {
100105
if ("js" in originalExtensions) {
101106
require.extensions[".js"] = originalExtensions.js;
102107
}
108+
if ("ts" in originalExtensions) {
109+
require.extensions[".ts"] = originalExtensions.ts;
110+
}
103111
if ("coffee" in originalExtensions) {
104112
require.extensions[".coffee"] = originalExtensions.coffee;
105113
}
@@ -136,6 +144,32 @@ function jsExtension(module, filename) {
136144
originalExtensions.js(module, filename);
137145
}
138146

147+
function tsExtension(module, filename) {
148+
var _compile = module._compile;
149+
150+
module._compile = function rewireCompile(content, filename) {
151+
var noConstAssignMessage = linter.verify(content, eslintOptions).find(isNoConstAssignMessage);
152+
var line;
153+
var column;
154+
155+
if (noConstAssignMessage !== undefined) {
156+
line = noConstAssignMessage.line;
157+
column = noConstAssignMessage.column;
158+
throw new TypeError(`Assignment to constant variable at ${ filename }:${ line }:${ column }`);
159+
}
160+
_compile.call(
161+
this,
162+
content
163+
.replace(shebang, '') // Remove shebang declarations
164+
.replace(matchConst, "$1let $2"), // replace const with let, while maintaining the column width
165+
filename
166+
);
167+
};
168+
169+
restoreExtensions();
170+
originalExtensions.ts(module, filename);
171+
}
172+
139173
function coffeeExtension(module, filename) {
140174
if (!coffee) {
141175
throw new Error("Cannot rewire module written in CoffeeScript: Please install 'coffeescript' package first.");

0 commit comments

Comments
 (0)