diff --git a/src/bin.ts b/src/bin.ts index 1e8734229..e53cc1e9e 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -32,6 +32,7 @@ const args = arg({ '--pretty': Boolean, '--skip-project': Boolean, '--skip-ignore': Boolean, + '--unsafe-ext-order': Boolean, // Aliases. '-e': '--eval', @@ -62,6 +63,7 @@ const { '--type-check': typeCheck = DEFAULTS.typeCheck, '--pretty': pretty = DEFAULTS.pretty, '--skip-project': skipProject = DEFAULTS.skipProject, + '--unsafe-ext-order': unsafeExtOrder = DEFAULTS.unsafeExtOrder, '--skip-ignore': skipIgnore = DEFAULTS.skipIgnore } = args @@ -89,6 +91,7 @@ Options: --pretty Use pretty diagnostic formatter --skip-project Skip reading \`tsconfig.json\` --skip-ignore Skip \`--ignore\` checks + --unsafe-ext-order Un-Safe use TypeScript file first instead of JavaScript `) process.exit(0) @@ -114,6 +117,7 @@ const service = register({ project, skipIgnore, skipProject, + unsafeExtOrder, compiler, ignoreDiagnostics, compilerOptions, diff --git a/src/index.ts b/src/index.ts index 52a2d077c..c6503d50c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -65,6 +65,7 @@ export interface Options { project?: string skipIgnore?: boolean | null skipProject?: boolean | null + unsafeExtOrder?: boolean | null compilerOptions?: object ignoreDiagnostics?: Array readFile?: (path: string) => string | undefined @@ -104,6 +105,7 @@ export const DEFAULTS: Options = { project: process.env['TS_NODE_PROJECT'], skipIgnore: yn(process.env['TS_NODE_SKIP_IGNORE']), skipProject: yn(process.env['TS_NODE_SKIP_PROJECT']), + unsafeExtOrder: yn(process.env['TS_NODE_UNSAFE_EXT_ORDER']), ignoreDiagnostics: split(process.env['TS_NODE_IGNORE_DIAGNOSTICS']), typeCheck: yn(process.env['TS_NODE_TYPE_CHECK']), transpileOnly: yn(process.env['TS_NODE_TRANSPILE_ONLY']) @@ -374,9 +376,7 @@ export function register (opts: Options = {}): Register { const register: Register = { cwd, compile, getTypeInfo, extensions, ts } // Register the extensions. - extensions.forEach(extension => { - registerExtension(extension, ignore, register, originalJsHandler) - }) + registerHandler(opts, extensions, ignore, register, originalJsHandler) return register } @@ -390,6 +390,40 @@ function shouldIgnore (filename: string, ignore: RegExp[]) { return ignore.some(x => x.test(relname)) } +/** + * unsafe re-order and make .ts > .js + */ +function registerHandler ( + opts: Options, + extensions: string[], + ignore: RegExp[], + register: Register, + originalJsHandler: (m: NodeModule, filename: string) => any +) { + if (opts.unsafeExtOrder) { + // @todo a better way with options + ['.ts', '.tsx'] + .concat(Object.keys(require.extensions), extensions) + .filter(function (element, index, array: string[]) { + return array.indexOf(element) === index + }) + .forEach(function (ext) { + if (extensions.indexOf(ext) !== -1) { + registerExtension(ext, ignore, register, originalJsHandler) + } + + const old = require.extensions[ext] + + delete require.extensions[ext] + require.extensions[ext] = old + }) + } else { + extensions.forEach(function (ext) { + registerExtension(ext, ignore, register, originalJsHandler) + }) + } +} + /** * Register the extension for node. */