Skip to content

Commit

Permalink
fix(compiler): support Node16/NodeNext value for target
Browse files Browse the repository at this point in the history
Fixes #4198
  • Loading branch information
ahnpnl committed Jul 8, 2024
1 parent bc966ff commit 7a0cb8e
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 51 deletions.
5 changes: 3 additions & 2 deletions e2e/native-esm-ts/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { pathsToModuleNameMapper } from '../../dist/index.js'
import { pathsToModuleNameMapper, createDefaultEsmPreset } from '../../dist/index.js'
import { createRequire } from 'module'

const require = createRequire(import.meta.url)
const tsConfig = require('./tsconfig.json')
const esmPreset = createDefaultEsmPreset()

/** @type {import('../../dist').JestConfigWithTsJest} */
export default {
extensionsToTreatAsEsm: ['.ts'],
...esmPreset,
resolver: '<rootDir>/mjs-resolver.ts',
moduleNameMapper: pathsToModuleNameMapper(tsConfig.compilerOptions.paths, {
prefix: '<rootDir>',
Expand Down
4 changes: 3 additions & 1 deletion examples/js-with-babel/tsconfig-esm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "ESNext"
"module": "ES2022",
"moduleResolution": "Bundler",
"esModuleInterop": true
}
}
6 changes: 3 additions & 3 deletions examples/js-with-babel/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"compilerOptions": {
"module": "CommonJS",
"target": "ES2015",
"allowJs": true
"module": "Node16",
"moduleResolution": "Node16",
"target": "ES2021"
},
"files": ["globals.d.ts"]
}
3 changes: 2 additions & 1 deletion examples/js-with-ts/tsconfig-esm.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "ESNext",
"module": "ES2022",
"moduleResolution": "Bundler",
"esModuleInterop": true
}
}
6 changes: 3 additions & 3 deletions examples/js-with-ts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"compilerOptions": {
"module": "CommonJS",
"target": "ES2015",
"allowJs": true
"module": "Node16",
"moduleResolution": "Node16",
"target": "ES2021"
},
"files": ["globals.d.ts"]
}
3 changes: 2 additions & 1 deletion examples/monorepo-app/tsconfig-esm.base.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "./tsconfig.base.json",
"compilerOptions": {
"module": "ESNext",
"module": "ES2022",
"moduleResolution": "Bundler",
"esModuleInterop": true
}
}
6 changes: 3 additions & 3 deletions examples/monorepo-app/tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
"declaration": false,
"downlevelIteration": true,
"experimentalDecorators": true,
"moduleResolution": "node",
"importHelpers": true,
"target": "ESNext",
"module": "CommonJS",
"module": "Node16",
"moduleResolution": "Node16",
"target": "ES2021",
"lib": ["ESNext", "dom"]
}
}
2 changes: 2 additions & 0 deletions examples/react-app/tsconfig-esm.spec.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"extends": "./tsconfig.spec.json",
"compilerOptions": {
"module": "ES2022",
"moduleResolution": "Bundler",
"esModuleInterop": true
}
}
5 changes: 3 additions & 2 deletions examples/react-app/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "CommonJS",
"moduleResolution": "Node"
"module": "Node16",
"moduleResolution": "Node16",
"target": "ES2021"
}
}
3 changes: 2 additions & 1 deletion examples/ts-only/tsconfig-esm.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "ESNext",
"module": "ES2022",
"moduleResolution": "Bundler",
"esModuleInterop": true
}
}
3 changes: 2 additions & 1 deletion examples/ts-only/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"module": "CommonJS",
"module": "Node16",
"moduleResolution": "Node16",
"target": "ES2021"
},
"files": ["globals.d.ts"]
Expand Down
3 changes: 2 additions & 1 deletion examples/type-module/tsconfig-esm.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "ESNext",
"module": "ES2022",
"moduleResolution": "Bundler",
"esModuleInterop": true
}
}
3 changes: 2 additions & 1 deletion examples/type-module/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"module": "CommonJS",
"module": "Node16",
"moduleResolution": "Node16",
"target": "ES2021"
},
"files": ["globals.d.ts"]
Expand Down
12 changes: 6 additions & 6 deletions src/legacy/compiler/__snapshots__/ts-compiler.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ exports[`TsCompiler getCompiledOutput isolatedModules false should compile codes
{
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"module": 99,
"module": 1,
}
`;

exports[`TsCompiler getCompiledOutput isolatedModules false should compile codes with useESM {"babelConfig": true, "supportsStaticESM": false, "useESM": true} 1`] = `
{
"allowSyntheticDefaultImports": true,
"allowSyntheticDefaultImports": undefined,
"esModuleInterop": true,
"module": 99,
"module": 1,
}
`;

Expand All @@ -52,14 +52,14 @@ exports[`TsCompiler getCompiledOutput isolatedModules true should transpile code
{
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"module": 99,
"module": 1,
}
`;

exports[`TsCompiler getCompiledOutput isolatedModules true should transpile code with config {"babelConfig": true, "supportsStaticESM": false, "useESM": true} 1`] = `
{
"allowSyntheticDefaultImports": true,
"allowSyntheticDefaultImports": undefined,
"esModuleInterop": true,
"module": 99,
"module": 1,
}
`;
57 changes: 32 additions & 25 deletions src/legacy/compiler/ts-compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,34 +146,41 @@ export class TsCompiler implements TsCompilerInstance {
return importedModulePaths
}

getCompiledOutput(fileContent: string, fileName: string, options: TsJestCompileOptions): CompiledOutput {
let moduleKind = this._initialCompilerOptions.module
let esModuleInterop = this._initialCompilerOptions.esModuleInterop
let allowSyntheticDefaultImports = this._initialCompilerOptions.allowSyntheticDefaultImports
const currentModuleKind = this._compilerOptions.module
const isEsmMode = this.configSet.useESM && options.supportsStaticESM
if (
(this.configSet.babelJestTransformer || (!this.configSet.babelJestTransformer && options.supportsStaticESM)) &&
this.configSet.useESM
) {
moduleKind =
!moduleKind ||
(moduleKind &&
![this._ts.ModuleKind.ES2015, this._ts.ModuleKind.ES2020, this._ts.ModuleKind.ESNext].includes(moduleKind))
? this._ts.ModuleKind.ESNext
: moduleKind
// Make sure `esModuleInterop` and `allowSyntheticDefaultImports` true to support import CJS into ESM
esModuleInterop = true
allowSyntheticDefaultImports = true
} else {
moduleKind = this._ts.ModuleKind.CommonJS
private getCompilerOptionsForModuleKind(compilerOptions: CompilerOptions, isEsm: boolean): CompilerOptions {
if (!isEsm) {
const moduleKind = compilerOptions.module ?? this._ts.ModuleKind.CommonJS
const allowedCjsModuleKinds = [
this._ts.ModuleKind.CommonJS,
this._ts.ModuleKind.Node16,
this._ts.ModuleKind.NodeNext,
].includes(moduleKind)
? moduleKind
: this._ts.ModuleKind.CommonJS

return {
...compilerOptions,
module: allowedCjsModuleKinds,
}
}
this._compilerOptions = {
...this._compilerOptions,
allowSyntheticDefaultImports,
esModuleInterop,

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

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

getCompiledOutput(fileContent: string, fileName: string, options: TsJestCompileOptions): CompiledOutput {
const moduleKind = this._initialCompilerOptions.module
const currentModuleKind = this._compilerOptions.module
const isEsmMode = this.configSet.useESM && options.supportsStaticESM
this._compilerOptions = this.getCompilerOptionsForModuleKind(this._compilerOptions, isEsmMode)
if (this._languageService) {
this._logger.debug({ fileName }, 'getCompiledOutput(): compiling using language service')

Expand Down

0 comments on commit 7a0cb8e

Please sign in to comment.