Skip to content

Commit

Permalink
fix: support evaluating TypeScript files by default. fixes #27
Browse files Browse the repository at this point in the history
  • Loading branch information
satya164 committed Dec 15, 2018
1 parent 60174de commit db661f7
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/__fixtures__/sample-typescript.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = 27;
13 changes: 13 additions & 0 deletions src/__tests__/module.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,19 @@ it('imports JS files', () => {
expect(mod.exports.result).toBe('The answer is 42');
});

it('imports TypeScript files', () => {
const mod = new Module(path.resolve(__dirname, '../__fixtures__/test.ts'));

mod.transform = transform;
mod.evaluate(dedent`
import answer from './sample-typescript';
export const result = 'The answer is ' + answer;
`);

expect(mod.exports.result).toBe('The answer is 27');
});

it('imports JSON files', () => {
const mod = new Module(path.resolve(__dirname, '../__fixtures__/test.js'));

Expand Down
35 changes: 31 additions & 4 deletions src/babel/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class Module {
require: (id: string) => any;
exports: any;
extensions: string[];
transform: ?(text: string) => { code: string }
*/

Expand All @@ -58,10 +59,35 @@ class Module {
this.require.resolve = this.resolve.bind(this);
this.require.ensure = NOOP;
this.require.cache = cache;

// We support following extensions by default
this.extensions = ['.json', '.js', '.ts', '.tsx'];
}

resolve(id /* : string */) {
return NativeModule._resolveFilename(id, this);
const extensions = NativeModule._extensions;
const added = [];

try {
// Check for supported extensions
this.extensions.forEach(ext => {
if (ext in extensions) {
return;
}

// When an extension is not supported, add it
// And keep track of it to clean it up after resolving
// Use noop for the tranform function since we handle it
extensions[ext] = NOOP;
added.push(ext);
});

// Resolve the module using node's resolve algorithm
return NativeModule._resolveFilename(id, this);
} finally {
// Cleanup the extensions we added to restore previous behaviour
added.forEach(ext => delete extensions[ext]);
}
}

require(id /* : string */) {
Expand All @@ -86,15 +112,16 @@ class Module {
// we would end up in infinite loop with cyclic dependencies
cache[filename] = m;

if (/\.(js|json)$/.test(filename)) {
// For JS/JSON files, we need to read the file first
if (/\.(json|js|tsx?)$/.test(filename)) {
// For JS/TS/JSON files, we need to read the file first
const code = fs.readFileSync(filename, 'utf-8');

if (/\.json$/.test(filename)) {
// For JSON files, parse it to a JS object similar to Node
m.exports = JSON.parse(code);
} else {
// For JS files, evaluate the module
// For JS/TS files, evaluate the module
// The module will be transpiled using provided transform
m.evaluate(code);
}
} else {
Expand Down
1 change: 1 addition & 0 deletions tslint.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "dtslint/dtslint.json",
"rules": {
"file-name-casing": false,
"interface-over-type-literal": false,
"strict-export-declare-modifiers": false
}
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit db661f7

Please sign in to comment.