Skip to content

Commit

Permalink
Fix #1098: ignored() bug: ESM loader will try to compile .cjs, .mjs… (
Browse files Browse the repository at this point in the history
#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>
  • Loading branch information
concision and cspotcode committed Aug 12, 2020
1 parent 54963b8 commit 08dc47d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/esm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')))
}

Expand Down
29 changes: 28 additions & 1 deletion src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
11 changes: 6 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit 08dc47d

Please sign in to comment.