Skip to content

Commit

Permalink
fix: revert support implementation for Node16/NodeNext
Browse files Browse the repository at this point in the history
- For CJS mode, `module` is always `CommonJS`
- For ESM mode, `module` with value `Node16/NodeNext` will use `ESNext`, otherwise use the value provided by test `tsconfig`
- `moduleResolution` is always `Node10`

Fixes #4468
Fixes #4473
  • Loading branch information
ahnpnl committed Aug 1, 2024
1 parent 86398c7 commit 70b9530
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 20 deletions.
11 changes: 9 additions & 2 deletions e2e/__tests__/native-esm-ts.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import path from 'path'

import { json as runWithJson } from '../run-jest'
import { runNpmInstall } from '../utils'

beforeAll(() => {
runNpmInstall(path.join(__dirname, '..', 'native-esm-ts'))
})

test('runs TS test with native ESM', () => {
const { exitCode, json } = runWithJson('native-esm-ts', [], {
nodeOptions: '--experimental-vm-modules --no-warnings',
})

expect(exitCode).toBe(0)
expect(json.numTotalTests).toBe(4)
expect(json.numPassedTests).toBe(4)
expect(json.numTotalTests).toBe(6)
expect(json.numPassedTests).toBe(6)
})
16 changes: 16 additions & 0 deletions e2e/native-esm-ts/__tests__/third-party.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import path from 'node:path'

import { it, describe, expect } from '@jest/globals'

import { fileName } from '../third-parties/dirname-filename-esm.js'
import { backend } from '../third-parties/i18next-fs-backend.js'

describe('Third parties', () => {
it('should work with i18next-fs-backend', () => {
expect(backend).toBeDefined()
})

it('should work with dirname-filename-esm', () => {
expect(path.basename(fileName(import.meta))).toBe('third-party.spec.ts')
})
})
30 changes: 29 additions & 1 deletion e2e/native-esm-ts/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion e2e/native-esm-ts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"type": "module",
"devDependencies": {
"@jest/globals": "^29.7.0"
"@jest/globals": "^29.7.0",
"dirname-filename-esm": "^1.1.2",
"i18next-fs-backend": "^2.3.2"
}
}
3 changes: 3 additions & 0 deletions e2e/native-esm-ts/third-parties/dirname-filename-esm.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { filename } from 'dirname-filename-esm'

export { filename as fileName }
3 changes: 3 additions & 0 deletions e2e/native-esm-ts/third-parties/i18next-fs-backend.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { default as Backend } from 'i18next-fs-backend'

export const backend = new Backend()
3 changes: 2 additions & 1 deletion src/legacy/compiler/ts-compiler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ describe('TsCompiler', () => {
useESM: true,
supportsStaticESM: true,
moduleValue: 'NodeNext',
expectedModule: ts.ModuleKind.NodeNext,
expectedModule: ts.ModuleKind.ESNext,
expectedEsModuleInterop: true,
},
{
Expand Down Expand Up @@ -252,6 +252,7 @@ describe('TsCompiler', () => {

expect(usedCompilerOptions.module).toBe(expectedModule)
expect(usedCompilerOptions.esModuleInterop).toBe(expectedEsModuleInterop)
expect(usedCompilerOptions.moduleResolution).toBe(ts.ModuleResolutionKind.Node10)
expect(output).toEqual({
code: updateOutput(jsOutput, fileName, sourceMap),
diagnostics: [],
Expand Down
26 changes: 11 additions & 15 deletions src/legacy/compiler/ts-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,31 +147,27 @@ export class TsCompiler implements TsCompilerInstance {
}

private fixupCompilerOptionsForModuleKind(compilerOptions: CompilerOptions, isEsm: boolean): CompilerOptions {
const moduleResolution = this._ts.ModuleResolutionKind.Node10
if (!isEsm) {
const moduleKind = compilerOptions.module ?? this._ts.ModuleKind.CommonJS
const moduleKindValue = [
this._ts.ModuleKind.CommonJS,
this._ts.ModuleKind.Node16,
this._ts.ModuleKind.NodeNext,
].includes(moduleKind)
? moduleKind
: this._ts.ModuleKind.CommonJS

return {
...compilerOptions,
module: moduleKindValue,
module: this._ts.ModuleKind.CommonJS,
moduleResolution,
}
}

const moduleKind = compilerOptions.module ?? this._ts.ModuleKind.ESNext
const esModuleInterop = [this._ts.ModuleKind.Node16, this._ts.ModuleKind.NodeNext].includes(moduleKind)
? true
: compilerOptions.esModuleInterop
let moduleKind = compilerOptions.module ?? this._ts.ModuleKind.ESNext
let esModuleInterop = compilerOptions.esModuleInterop
if ([this._ts.ModuleKind.Node16, this._ts.ModuleKind.NodeNext].includes(moduleKind)) {
esModuleInterop = true
moduleKind = this._ts.ModuleKind.ESNext
}

return {
...compilerOptions,
module: moduleKind,
esModuleInterop: esModuleInterop ?? false,
esModuleInterop,
moduleResolution,
}
}

Expand Down

0 comments on commit 70b9530

Please sign in to comment.