diff --git a/README.md b/README.md index 5c1cd44b3..bed60d1c6 100644 --- a/README.md +++ b/README.md @@ -136,7 +136,7 @@ _Environment variable denoted in parentheses._ * `--skip-project` Skip project config resolution and loading (`TS_NODE_SKIP_PROJECT`, default: `false`) * `--skip-ignore` Skip ignore checks (`TS_NODE_SKIP_IGNORE`, default: `false`) * `--log-error` Logs errors of types instead of exit the process (`TS_NODE_LOG_ERROR`, default: `false`) -* `--prefer-ts-exts` Changes the order of file extensions used when matching file imports so that `.ts` files are preferred over `.js` (`TS_PREFER_TS_EXTS`, default: `false`) +* `--prefer-ts-exts` Re-order file extensions so that TypeScript imports are preferred (`TS_NODE_PREFER_TS_EXTS`, default: `false`) ### Programmatic Only Options diff --git a/src/bin.ts b/src/bin.ts index 1c5241e07..dd6e01586 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -109,6 +109,13 @@ const cwd = process.cwd() const code = args['--eval'] const isPrinted = args['--print'] !== undefined +/** + * Eval helpers. + */ +const EVAL_FILENAME = `[eval].ts` +const EVAL_PATH = join(cwd, EVAL_FILENAME) +const EVAL_INSTANCE = { input: '', output: '', version: 0, lines: 0 } + // Register the TypeScript compiler instance. const service = register({ files, @@ -139,13 +146,6 @@ if (version >= 2) { // Require specified modules before start-up. if (args['--require']) (Module as any)._preloadModules(args['--require']) -/** - * Eval helpers. - */ -const EVAL_FILENAME = `[eval].ts` -const EVAL_PATH = join(cwd, EVAL_FILENAME) -const EVAL_INSTANCE = { input: '', output: '', version: 0, lines: 0 } - // Prepend `ts-node` arguments to CLI for child processes. process.execArgv.unshift(__filename, ...process.argv.slice(2, process.argv.length - args._.length)) process.argv = [process.argv[1]].concat(args._.length ? resolve(cwd, args._[0]) : []).concat(args._.slice(1)) diff --git a/src/index.spec.ts b/src/index.spec.ts index c9bf83ee4..e0905cadc 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -326,6 +326,12 @@ describe('ts-node', function () { expect(m.example()).to.equal('hello') }) + it('should work with `require.cache`', function () { + const { example1, example2 } = require('../tests/require-cache') + + expect(example1).to.not.equal(example2) + }) + it('should use source maps', function (done) { try { require('../tests/throw') diff --git a/src/index.ts b/src/index.ts index ef786cca8..82aadb328 100644 --- a/src/index.ts +++ b/src/index.ts @@ -420,7 +420,7 @@ function shouldIgnore (filename: string, ignore: RegExp[]) { * * @param {string} ext */ -function refreshRequireExtension (ext: string) { +function reorderRequireExtension (ext: string) { const old = require.extensions[ext] // tslint:disable-line delete require.extensions[ext] // tslint:disable-line require.extensions[ext] = old // tslint:disable-line @@ -436,21 +436,17 @@ function registerExtensions ( register: Register, originalJsHandler: (m: NodeModule, filename: string) => any ) { - if (opts.preferTsExts) { - extensions.unshift( - '.ts', - '.tsx', - ...Object.keys(require.extensions), // tslint:disable-line - ) + // Register new extensions. + for (const ext of extensions) { + registerExtension(ext, ignore, register, originalJsHandler) } - // @todo a better way with options - Array.from(new Set(extensions)) - .forEach(ext => { - registerExtension(ext, ignore, register, originalJsHandler) + if (opts.preferTsExts) { + // tslint:disable-next-line + const preferredExtensions = new Set([...extensions, ...Object.keys(require.extensions)]) - if (ext in require.extensions) refreshRequireExtension(ext) // tslint:disable-line - }) + for (const ext of preferredExtensions) reorderRequireExtension(ext) + } } /** diff --git a/tests/require-cache.ts b/tests/require-cache.ts new file mode 100644 index 000000000..4fbdd5047 --- /dev/null +++ b/tests/require-cache.ts @@ -0,0 +1,7 @@ +const moduleName = require.resolve('./module') + +const { example: example1 } = require(moduleName) +delete require.cache[moduleName] +const { example: example2 } = require(moduleName) + +export { example1, example2 } diff --git a/tsconfig.json b/tsconfig.json index 447b83c08..486a70bae 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "target": "es5", + "target": "es2015", "lib": ["es2015"], "rootDir": "src", "outDir": "dist",