Skip to content

Commit

Permalink
Simplify extension ordering for preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Jun 15, 2019
1 parent 611d013 commit 53e470d
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 7 additions & 7 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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))
Expand Down
6 changes: 6 additions & 0 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
22 changes: 9 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
}

/**
Expand Down
7 changes: 7 additions & 0 deletions tests/require-cache.ts
Original file line number Diff line number Diff line change
@@ -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 }
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es5",
"target": "es2015",
"lib": ["es2015"],
"rootDir": "src",
"outDir": "dist",
Expand Down

0 comments on commit 53e470d

Please sign in to comment.