Skip to content

Commit 08dc47d

Browse files
concisioncspotcode
andauthored
Fix #1098: ignored() bug: ESM loader will try to compile .cjs, .mjs… (#1103)
* Fix #1098: `ignored()` bug: ESM loader will try to compile .cjs, .mjs, and other unexpected file extensions * tests: Add test matrix for ignored() to ensure only specific extensions are sent to TypeScript compiler * feat: Changed ESM getFormat to use a Register.ignored() lookup * Add .d.ts extension to tests Co-authored-by: Andrew Bradley <cspotcode@gmail.com>
1 parent 54963b8 commit 08dc47d

File tree

3 files changed

+35
-7
lines changed

3 files changed

+35
-7
lines changed

src/esm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export function registerAndCreateEsmHooks (opts?: RegisterOptions) {
7272

7373
// If file has .ts, .tsx, or .jsx extension, then ask node how it would treat this file if it were .js
7474
const ext = extname(nativePath)
75-
if (ext === '.ts' || ext === '.tsx' || ext === '.jsx') {
75+
if (ext !== '.js' && !tsNodeInstance.ignored(nativePath)) {
7676
return defer(formatUrl(pathToFileURL(nativePath + '.js')))
7777
}
7878

src/index.spec.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -728,11 +728,38 @@ describe('ts-node', function () {
728728
it('should create generic compiler instances', () => {
729729
const service = create({ compilerOptions: { target: 'es5' }, skipProject: true })
730730
const output = service.compile('const x = 10', 'test.ts')
731-
732731
expect(output).to.contain('var x = 10;')
733732
})
734733
})
735734

735+
describe('issue #1098', () => {
736+
function testIgnored (ignored: tsNodeTypes.Register['ignored'], allowed: string[], disallowed: string[]) {
737+
for (const ext of allowed) {
738+
expect(ignored(join(__dirname, `index${ext}`))).equal(false, `should accept ${ext} files`)
739+
}
740+
for (const ext of disallowed) {
741+
expect(ignored(join(__dirname, `index${ext}`))).equal(true, `should ignore ${ext} files`)
742+
}
743+
}
744+
745+
it('correctly filters file extensions from the compiler when allowJs=false and jsx=false', () => {
746+
const { ignored } = create({ compilerOptions: { }, skipProject: true })
747+
testIgnored(ignored, ['.ts', '.d.ts'], ['.js', '.tsx', '.jsx', '.mjs', '.cjs', '.xyz', ''])
748+
})
749+
it('correctly filters file extensions from the compiler when allowJs=true and jsx=false', () => {
750+
const { ignored } = create({ compilerOptions: { allowJs: true }, skipProject: true })
751+
testIgnored(ignored, ['.ts', '.js', '.d.ts'], ['.tsx', '.jsx', '.mjs', '.cjs', '.xyz', ''])
752+
})
753+
it('correctly filters file extensions from the compiler when allowJs=false and jsx=true', () => {
754+
const { ignored } = create({ compilerOptions: { allowJs: false, jsx: 'preserve' }, skipProject: true })
755+
testIgnored(ignored, ['.ts', '.tsx', '.d.ts'], ['.js', '.jsx', '.mjs', '.cjs', '.xyz', ''])
756+
})
757+
it('correctly filters file extensions from the compiler when allowJs=true and jsx=true', () => {
758+
const { ignored } = create({ compilerOptions: { allowJs: true, jsx: 'preserve' }, skipProject: true })
759+
testIgnored(ignored, ['.ts', '.tsx', '.js', '.jsx', '.d.ts'], ['.mjs', '.cjs', '.xyz', ''])
760+
})
761+
})
762+
736763
describe('esm', () => {
737764
this.slow(1000)
738765

src/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -834,14 +834,15 @@ export function create (rawOptions: CreateOptions = {}): Register {
834834

835835
let active = true
836836
const enabled = (enabled?: boolean) => enabled === undefined ? active : (active = !!enabled)
837+
const extensions = getExtensions(config)
837838
const ignored = (fileName: string) => {
838839
if (!active) return true
839-
const relname = relative(cwd, fileName)
840-
if (!config.options.allowJs) {
841-
const ext = extname(fileName)
842-
if (ext === '.js' || ext === '.jsx') return true
840+
const ext = extname(fileName)
841+
if (extensions.tsExtensions.includes(ext) || extensions.jsExtensions.includes(ext)) {
842+
const relname = relative(cwd, fileName)
843+
return !isScoped(relname) || shouldIgnore(relname)
843844
}
844-
return !isScoped(relname) || shouldIgnore(relname)
845+
return true
845846
}
846847

847848
return { ts, config, compile, getTypeInfo, ignored, enabled, options }

0 commit comments

Comments
 (0)