Skip to content

Commit 70b9530

Browse files
committed
fix: revert support implementation for Node16/NodeNext
- 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
1 parent 86398c7 commit 70b9530

File tree

8 files changed

+76
-20
lines changed

8 files changed

+76
-20
lines changed

e2e/__tests__/native-esm-ts.test.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
1+
import path from 'path'
2+
13
import { json as runWithJson } from '../run-jest'
4+
import { runNpmInstall } from '../utils'
5+
6+
beforeAll(() => {
7+
runNpmInstall(path.join(__dirname, '..', 'native-esm-ts'))
8+
})
29

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

815
expect(exitCode).toBe(0)
9-
expect(json.numTotalTests).toBe(4)
10-
expect(json.numPassedTests).toBe(4)
16+
expect(json.numTotalTests).toBe(6)
17+
expect(json.numPassedTests).toBe(6)
1118
})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import path from 'node:path'
2+
3+
import { it, describe, expect } from '@jest/globals'
4+
5+
import { fileName } from '../third-parties/dirname-filename-esm.js'
6+
import { backend } from '../third-parties/i18next-fs-backend.js'
7+
8+
describe('Third parties', () => {
9+
it('should work with i18next-fs-backend', () => {
10+
expect(backend).toBeDefined()
11+
})
12+
13+
it('should work with dirname-filename-esm', () => {
14+
expect(path.basename(fileName(import.meta))).toBe('third-party.spec.ts')
15+
})
16+
})

e2e/native-esm-ts/package-lock.json

+29-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

e2e/native-esm-ts/package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
22
"type": "module",
33
"devDependencies": {
4-
"@jest/globals": "^29.7.0"
4+
"@jest/globals": "^29.7.0",
5+
"dirname-filename-esm": "^1.1.2",
6+
"i18next-fs-backend": "^2.3.2"
57
}
68
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { filename } from 'dirname-filename-esm'
2+
3+
export { filename as fileName }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { default as Backend } from 'i18next-fs-backend'
2+
3+
export const backend = new Backend()

src/legacy/compiler/ts-compiler.spec.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ describe('TsCompiler', () => {
200200
useESM: true,
201201
supportsStaticESM: true,
202202
moduleValue: 'NodeNext',
203-
expectedModule: ts.ModuleKind.NodeNext,
203+
expectedModule: ts.ModuleKind.ESNext,
204204
expectedEsModuleInterop: true,
205205
},
206206
{
@@ -252,6 +252,7 @@ describe('TsCompiler', () => {
252252

253253
expect(usedCompilerOptions.module).toBe(expectedModule)
254254
expect(usedCompilerOptions.esModuleInterop).toBe(expectedEsModuleInterop)
255+
expect(usedCompilerOptions.moduleResolution).toBe(ts.ModuleResolutionKind.Node10)
255256
expect(output).toEqual({
256257
code: updateOutput(jsOutput, fileName, sourceMap),
257258
diagnostics: [],

src/legacy/compiler/ts-compiler.ts

+11-15
Original file line numberDiff line numberDiff line change
@@ -147,31 +147,27 @@ export class TsCompiler implements TsCompilerInstance {
147147
}
148148

149149
private fixupCompilerOptionsForModuleKind(compilerOptions: CompilerOptions, isEsm: boolean): CompilerOptions {
150+
const moduleResolution = this._ts.ModuleResolutionKind.Node10
150151
if (!isEsm) {
151-
const moduleKind = compilerOptions.module ?? this._ts.ModuleKind.CommonJS
152-
const moduleKindValue = [
153-
this._ts.ModuleKind.CommonJS,
154-
this._ts.ModuleKind.Node16,
155-
this._ts.ModuleKind.NodeNext,
156-
].includes(moduleKind)
157-
? moduleKind
158-
: this._ts.ModuleKind.CommonJS
159-
160152
return {
161153
...compilerOptions,
162-
module: moduleKindValue,
154+
module: this._ts.ModuleKind.CommonJS,
155+
moduleResolution,
163156
}
164157
}
165158

166-
const moduleKind = compilerOptions.module ?? this._ts.ModuleKind.ESNext
167-
const esModuleInterop = [this._ts.ModuleKind.Node16, this._ts.ModuleKind.NodeNext].includes(moduleKind)
168-
? true
169-
: compilerOptions.esModuleInterop
159+
let moduleKind = compilerOptions.module ?? this._ts.ModuleKind.ESNext
160+
let esModuleInterop = compilerOptions.esModuleInterop
161+
if ([this._ts.ModuleKind.Node16, this._ts.ModuleKind.NodeNext].includes(moduleKind)) {
162+
esModuleInterop = true
163+
moduleKind = this._ts.ModuleKind.ESNext
164+
}
170165

171166
return {
172167
...compilerOptions,
173168
module: moduleKind,
174-
esModuleInterop: esModuleInterop ?? false,
169+
esModuleInterop,
170+
moduleResolution,
175171
}
176172
}
177173

0 commit comments

Comments
 (0)