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 9, 2024
1 parent 0e34b7a commit 089bd16
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 33 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
1 change: 1 addition & 0 deletions examples/react-app/tsconfig-esm.spec.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "./tsconfig.spec.json",
"compilerOptions": {
"module": "ESNext",
"esModuleInterop": true
}
}
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 fixupCompilerOptionsForModuleKind(compilerOptions: CompilerOptions, isEsm: boolean): CompilerOptions {
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,
}
}
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.fixupCompilerOptionsForModuleKind(this._initialCompilerOptions, isEsmMode)
if (this._languageService) {
this._logger.debug({ fileName }, 'getCompiledOutput(): compiling using language service')

Expand Down

0 comments on commit 089bd16

Please sign in to comment.