diff --git a/src/esm.ts b/src/esm.ts index ae08ada32..a00b2110e 100644 --- a/src/esm.ts +++ b/src/esm.ts @@ -72,7 +72,7 @@ export function registerAndCreateEsmHooks (opts?: RegisterOptions) { // If file has .ts, .tsx, or .jsx extension, then ask node how it would treat this file if it were .js const ext = extname(nativePath) - if (ext === '.ts' || ext === '.tsx' || ext === '.jsx') { + if (ext !== '.js' && !tsNodeInstance.ignored(nativePath)) { return defer(formatUrl(pathToFileURL(nativePath + '.js'))) } diff --git a/src/index.spec.ts b/src/index.spec.ts index 88de298d1..d9085e4cf 100644 --- a/src/index.spec.ts +++ b/src/index.spec.ts @@ -728,11 +728,38 @@ describe('ts-node', function () { it('should create generic compiler instances', () => { const service = create({ compilerOptions: { target: 'es5' }, skipProject: true }) const output = service.compile('const x = 10', 'test.ts') - expect(output).to.contain('var x = 10;') }) }) + describe('issue #1098', () => { + function testIgnored (ignored: tsNodeTypes.Register['ignored'], allowed: string[], disallowed: string[]) { + for (const ext of allowed) { + expect(ignored(join(__dirname, `index${ext}`))).equal(false, `should accept ${ext} files`) + } + for (const ext of disallowed) { + expect(ignored(join(__dirname, `index${ext}`))).equal(true, `should ignore ${ext} files`) + } + } + + it('correctly filters file extensions from the compiler when allowJs=false and jsx=false', () => { + const { ignored } = create({ compilerOptions: { }, skipProject: true }) + testIgnored(ignored, ['.ts', '.d.ts'], ['.js', '.tsx', '.jsx', '.mjs', '.cjs', '.xyz', '']) + }) + it('correctly filters file extensions from the compiler when allowJs=true and jsx=false', () => { + const { ignored } = create({ compilerOptions: { allowJs: true }, skipProject: true }) + testIgnored(ignored, ['.ts', '.js', '.d.ts'], ['.tsx', '.jsx', '.mjs', '.cjs', '.xyz', '']) + }) + it('correctly filters file extensions from the compiler when allowJs=false and jsx=true', () => { + const { ignored } = create({ compilerOptions: { allowJs: false, jsx: 'preserve' }, skipProject: true }) + testIgnored(ignored, ['.ts', '.tsx', '.d.ts'], ['.js', '.jsx', '.mjs', '.cjs', '.xyz', '']) + }) + it('correctly filters file extensions from the compiler when allowJs=true and jsx=true', () => { + const { ignored } = create({ compilerOptions: { allowJs: true, jsx: 'preserve' }, skipProject: true }) + testIgnored(ignored, ['.ts', '.tsx', '.js', '.jsx', '.d.ts'], ['.mjs', '.cjs', '.xyz', '']) + }) + }) + describe('esm', () => { this.slow(1000) diff --git a/src/index.ts b/src/index.ts index 5aa2ea23b..ac55811b2 100644 --- a/src/index.ts +++ b/src/index.ts @@ -834,14 +834,15 @@ export function create (rawOptions: CreateOptions = {}): Register { let active = true const enabled = (enabled?: boolean) => enabled === undefined ? active : (active = !!enabled) + const extensions = getExtensions(config) const ignored = (fileName: string) => { if (!active) return true - const relname = relative(cwd, fileName) - if (!config.options.allowJs) { - const ext = extname(fileName) - if (ext === '.js' || ext === '.jsx') return true + const ext = extname(fileName) + if (extensions.tsExtensions.includes(ext) || extensions.jsExtensions.includes(ext)) { + const relname = relative(cwd, fileName) + return !isScoped(relname) || shouldIgnore(relname) } - return !isScoped(relname) || shouldIgnore(relname) + return true } return { ts, config, compile, getTypeInfo, ignored, enabled, options }