diff --git a/src/test/index.spec.ts b/src/test/index.spec.ts index a9d7b91f9..ed9f19c94 100644 --- a/src/test/index.spec.ts +++ b/src/test/index.spec.ts @@ -103,6 +103,7 @@ test.beforeAll(async () => { test.suite('ts-node', (test) => { const cmd = `"${BIN_PATH}" --project "${PROJECT}"`; const cmdNoProject = `"${BIN_PATH}"`; + const cmdEsmLoaderNoProject = `node --loader ts-node/esm`; test('should export the correct version', () => { expect(VERSION).to.equal(require('../../package.json').version); @@ -367,6 +368,17 @@ test.suite('ts-node', (test) => { expect(stdout).to.contain('Hello World!'); }); + test('swc transpiler supports native ESM emit', async () => { + const { err, stdout } = await exec( + `${cmdEsmLoaderNoProject} ./index.ts`, + { + cwd: resolve(TEST_DIR, 'transpile-only-swc-native-esm'), + } + ); + expect(err).to.equal(null); + expect(stdout).to.contain('Hello file://'); + }); + test('should pipe into `ts-node` and evaluate', async () => { const execPromise = exec(cmd); execPromise.child.stdin!.end("console.log('hello')"); diff --git a/src/transpilers/swc.ts b/src/transpilers/swc.ts index c9528c512..9b7361a08 100644 --- a/src/transpilers/swc.ts +++ b/src/transpilers/swc.ts @@ -58,21 +58,23 @@ export function create(createOptions: SwcTranspilerOptions): Transpiler { function createSwcOptions(isTsx: boolean): swcTypes.Options { const swcTarget = targetMapping.get(target!) ?? 'es3'; const keepClassNames = target! >= /* ts.ScriptTarget.ES2016 */ 3; - const moduleConfig = { - noInterop: !esModuleInterop, - type: - module === ModuleKind.CommonJS - ? 'commonjs' - : module === ModuleKind.AMD - ? 'amd' - : module === ModuleKind.UMD - ? 'umd' - : undefined, - } as swcTypes.ModuleConfig; + const moduleType = + module === ModuleKind.CommonJS + ? 'commonjs' + : module === ModuleKind.AMD + ? 'amd' + : module === ModuleKind.UMD + ? 'umd' + : undefined; return { sourceMaps: sourceMap, // isModule: true, - module: moduleConfig, + module: moduleType + ? ({ + noInterop: !esModuleInterop, + type: moduleType, + } as swcTypes.ModuleConfig) + : undefined, swcrc: false, jsc: { externalHelpers: importHelpers, diff --git a/tests/transpile-only-swc-via-tsconfig/index.ts b/tests/transpile-only-swc-via-tsconfig/index.ts index e2f5a1e22..b7dfa09cd 100644 --- a/tests/transpile-only-swc-via-tsconfig/index.ts +++ b/tests/transpile-only-swc-via-tsconfig/index.ts @@ -7,3 +7,7 @@ class World {} parseInt(1101, 2); const x: number = `Hello ${World.name}!`; console.log(x); + +// test module type emit +import { readFileSync } from 'fs'; +readFileSync; diff --git a/tests/transpile-only-swc-via-tsconfig/tsconfig.json b/tests/transpile-only-swc-via-tsconfig/tsconfig.json index e0e3c9a90..5097d63ac 100644 --- a/tests/transpile-only-swc-via-tsconfig/tsconfig.json +++ b/tests/transpile-only-swc-via-tsconfig/tsconfig.json @@ -5,7 +5,7 @@ }, "compilerOptions": { "target": "ES2018", - "module": "ESNext", + "module": "CommonJS", "allowJs": true, "jsx": "react", "experimentalDecorators": true diff --git a/tests/transpile-only-swc/index.ts b/tests/transpile-only-swc/index.ts index e2f5a1e22..b7dfa09cd 100644 --- a/tests/transpile-only-swc/index.ts +++ b/tests/transpile-only-swc/index.ts @@ -7,3 +7,7 @@ class World {} parseInt(1101, 2); const x: number = `Hello ${World.name}!`; console.log(x); + +// test module type emit +import { readFileSync } from 'fs'; +readFileSync; diff --git a/tests/transpile-only-swc/tsconfig.json b/tests/transpile-only-swc/tsconfig.json index c1cf4bccd..78c35a9ca 100644 --- a/tests/transpile-only-swc/tsconfig.json +++ b/tests/transpile-only-swc/tsconfig.json @@ -1,7 +1,7 @@ { "compilerOptions": { "target": "ES2018", - "module": "ESNext", + "module": "CommonJS", "allowJs": true, "jsx": "react", "experimentalDecorators": true