Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unsafe re-order and make .ts > .js #649

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ const args = arg({
'--pretty': Boolean,
'--skip-project': Boolean,
'--skip-ignore': Boolean,
'--unsafe-ext-order': Boolean,

// Aliases.
'-e': '--eval',
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -114,6 +117,7 @@ const service = register({
project,
skipIgnore,
skipProject,
unsafeExtOrder,
compiler,
ignoreDiagnostics,
compilerOptions,
Expand Down
40 changes: 37 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export interface Options {
project?: string
skipIgnore?: boolean | null
skipProject?: boolean | null
unsafeExtOrder?: boolean | null
compilerOptions?: object
ignoreDiagnostics?: Array<number | string>
readFile?: (path: string) => string | undefined
Expand Down Expand Up @@ -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'])
Expand Down Expand Up @@ -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
}
Expand All @@ -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.
*/
Expand Down