From 70b9530bd08de00fe24f22f22d9c637c592df87a Mon Sep 17 00:00:00 2001 From: ahnpnl Date: Wed, 31 Jul 2024 13:28:25 +0200 Subject: [PATCH] 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 --- e2e/__tests__/native-esm-ts.test.ts | 11 +++++-- .../__tests__/third-party.spec.ts | 16 ++++++++++ e2e/native-esm-ts/package-lock.json | 30 ++++++++++++++++++- e2e/native-esm-ts/package.json | 4 ++- .../third-parties/dirname-filename-esm.ts | 3 ++ .../third-parties/i18next-fs-backend.ts | 3 ++ src/legacy/compiler/ts-compiler.spec.ts | 3 +- src/legacy/compiler/ts-compiler.ts | 26 +++++++--------- 8 files changed, 76 insertions(+), 20 deletions(-) create mode 100644 e2e/native-esm-ts/__tests__/third-party.spec.ts create mode 100644 e2e/native-esm-ts/third-parties/dirname-filename-esm.ts create mode 100644 e2e/native-esm-ts/third-parties/i18next-fs-backend.ts diff --git a/e2e/__tests__/native-esm-ts.test.ts b/e2e/__tests__/native-esm-ts.test.ts index 223fbc3890..789836d714 100644 --- a/e2e/__tests__/native-esm-ts.test.ts +++ b/e2e/__tests__/native-esm-ts.test.ts @@ -1,4 +1,11 @@ +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', [], { @@ -6,6 +13,6 @@ test('runs TS test with native ESM', () => { }) expect(exitCode).toBe(0) - expect(json.numTotalTests).toBe(4) - expect(json.numPassedTests).toBe(4) + expect(json.numTotalTests).toBe(6) + expect(json.numPassedTests).toBe(6) }) diff --git a/e2e/native-esm-ts/__tests__/third-party.spec.ts b/e2e/native-esm-ts/__tests__/third-party.spec.ts new file mode 100644 index 0000000000..0e636a0368 --- /dev/null +++ b/e2e/native-esm-ts/__tests__/third-party.spec.ts @@ -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') + }) +}) diff --git a/e2e/native-esm-ts/package-lock.json b/e2e/native-esm-ts/package-lock.json index 28f3382210..e7873abfb1 100644 --- a/e2e/native-esm-ts/package-lock.json +++ b/e2e/native-esm-ts/package-lock.json @@ -5,7 +5,9 @@ "packages": { "": { "devDependencies": { - "@jest/globals": "^29.7.0" + "@jest/globals": "^29.7.0", + "dirname-filename-esm": "^1.1.2", + "i18next-fs-backend": "^2.3.2" } }, "node_modules/@ampproject/remapping": { @@ -1130,6 +1132,13 @@ "node": "^14.15.0 || ^16.10.0 || >=18.0.0" } }, + "node_modules/dirname-filename-esm": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/dirname-filename-esm/-/dirname-filename-esm-1.1.2.tgz", + "integrity": "sha512-mMhH2imzJN95KnX/iDAhC0QDG0VU833c88U3SXCaQzATu7YlO9YTFD/CF9Nn2xhSwFlV9iWEKb/YyGJ4TwCcPg==", + "dev": true, + "license": "MIT" + }, "node_modules/electron-to-chromium": { "version": "1.4.775", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.775.tgz", @@ -1305,6 +1314,13 @@ "node": ">=8" } }, + "node_modules/i18next-fs-backend": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/i18next-fs-backend/-/i18next-fs-backend-2.3.2.tgz", + "integrity": "sha512-LIwUlkqDZnUI8lnUxBnEj8K/FrHQTT/Sc+1rvDm9E8YvvY5YxzoEAASNx+W5M9DfD5s77lI5vSAFWeTp26B/3Q==", + "dev": true, + "license": "MIT" + }, "node_modules/imurmurhash": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/imurmurhash/-/imurmurhash-0.1.4.tgz", @@ -2853,6 +2869,12 @@ "integrity": "sha512-EjePK1srD3P08o2j4f0ExnylqRs5B9tJjcp9t1krH2qRi8CCdsYfwe9JgSLurFBWwq4uOlipzfk5fHNvwFKr8Q==", "dev": true }, + "dirname-filename-esm": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/dirname-filename-esm/-/dirname-filename-esm-1.1.2.tgz", + "integrity": "sha512-mMhH2imzJN95KnX/iDAhC0QDG0VU833c88U3SXCaQzATu7YlO9YTFD/CF9Nn2xhSwFlV9iWEKb/YyGJ4TwCcPg==", + "dev": true + }, "electron-to-chromium": { "version": "1.4.775", "resolved": "https://registry.npmjs.org/electron-to-chromium/-/electron-to-chromium-1.4.775.tgz", @@ -2981,6 +3003,12 @@ "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", "dev": true }, + "i18next-fs-backend": { + "version": "2.3.2", + "resolved": "https://registry.npmjs.org/i18next-fs-backend/-/i18next-fs-backend-2.3.2.tgz", + "integrity": "sha512-LIwUlkqDZnUI8lnUxBnEj8K/FrHQTT/Sc+1rvDm9E8YvvY5YxzoEAASNx+W5M9DfD5s77lI5vSAFWeTp26B/3Q==", + "dev": true + }, "imurmurhash": { "version": "0.1.4", "resolved": "https://registry.npmjs.org/imurmurhash/-/imurmurhash-0.1.4.tgz", diff --git a/e2e/native-esm-ts/package.json b/e2e/native-esm-ts/package.json index 18a131fb23..0084165bbc 100644 --- a/e2e/native-esm-ts/package.json +++ b/e2e/native-esm-ts/package.json @@ -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" } } diff --git a/e2e/native-esm-ts/third-parties/dirname-filename-esm.ts b/e2e/native-esm-ts/third-parties/dirname-filename-esm.ts new file mode 100644 index 0000000000..5fc8740896 --- /dev/null +++ b/e2e/native-esm-ts/third-parties/dirname-filename-esm.ts @@ -0,0 +1,3 @@ +import { filename } from 'dirname-filename-esm' + +export { filename as fileName } diff --git a/e2e/native-esm-ts/third-parties/i18next-fs-backend.ts b/e2e/native-esm-ts/third-parties/i18next-fs-backend.ts new file mode 100644 index 0000000000..591f37d98f --- /dev/null +++ b/e2e/native-esm-ts/third-parties/i18next-fs-backend.ts @@ -0,0 +1,3 @@ +import { default as Backend } from 'i18next-fs-backend' + +export const backend = new Backend() diff --git a/src/legacy/compiler/ts-compiler.spec.ts b/src/legacy/compiler/ts-compiler.spec.ts index ee12043408..659b234ef9 100644 --- a/src/legacy/compiler/ts-compiler.spec.ts +++ b/src/legacy/compiler/ts-compiler.spec.ts @@ -200,7 +200,7 @@ describe('TsCompiler', () => { useESM: true, supportsStaticESM: true, moduleValue: 'NodeNext', - expectedModule: ts.ModuleKind.NodeNext, + expectedModule: ts.ModuleKind.ESNext, expectedEsModuleInterop: true, }, { @@ -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: [], diff --git a/src/legacy/compiler/ts-compiler.ts b/src/legacy/compiler/ts-compiler.ts index d4e4a5c2db..9e8bcbd759 100644 --- a/src/legacy/compiler/ts-compiler.ts +++ b/src/legacy/compiler/ts-compiler.ts @@ -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, } }