Skip to content

Commit d270d95

Browse files
committed
fix
1 parent 49341d0 commit d270d95

File tree

4 files changed

+88
-39
lines changed

4 files changed

+88
-39
lines changed

src/index.ts

+19-6
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,9 @@ export function create(rawOptions: CreateOptions = {}): Service {
751751
'Transformers function is unavailable in "--transpile-only"'
752752
);
753753
}
754-
let customTranspiler: Transpiler | undefined = undefined;
754+
let createTranspiler:
755+
| ((compilerOptions: TSCommon.CompilerOptions) => Transpiler)
756+
| undefined;
755757
if (transpiler) {
756758
if (!transpileOnly)
757759
throw new Error(
@@ -762,11 +764,21 @@ export function create(rawOptions: CreateOptions = {}): Service {
762764
const transpilerOptions =
763765
typeof transpiler === 'string' ? {} : transpiler[1] ?? {};
764766
const transpilerPath = projectLocalResolveHelper(transpilerName, true);
765-
const transpilerFactory: TranspilerFactory = require(transpilerPath).create;
766-
customTranspiler = transpilerFactory({
767-
service: { options, config, projectLocalResolveHelper },
768-
...transpilerOptions,
769-
});
767+
const transpilerFactory = require(transpilerPath)
768+
.create as TranspilerFactory;
769+
createTranspiler = function (compilerOptions) {
770+
return transpilerFactory({
771+
service: {
772+
options,
773+
config: {
774+
...config,
775+
options: compilerOptions,
776+
},
777+
projectLocalResolveHelper,
778+
},
779+
...transpilerOptions,
780+
});
781+
};
770782
}
771783

772784
/**
@@ -1277,6 +1289,7 @@ export function create(rawOptions: CreateOptions = {}): Service {
12771289
const compilerOptions = { ...config.options };
12781290
if (overrideModuleType !== undefined)
12791291
compilerOptions.module = overrideModuleType;
1292+
let customTranspiler = createTranspiler?.(compilerOptions);
12801293
return (code: string, fileName: string): SourceOutput => {
12811294
let result: _ts.TranspileOutput;
12821295
if (customTranspiler) {

src/test/index.spec.ts

+40-33
Original file line numberDiff line numberDiff line change
@@ -1197,39 +1197,46 @@ test.suite('ts-node', (test) => {
11971197
expect(stdout).toBe('');
11981198
});
11991199

1200-
async function runModuleTypeTest(project: string, ext: string) {
1201-
const { err, stderr, stdout } = await exec(
1202-
`${CMD_ESM_LOADER_WITHOUT_PROJECT} ./module-types/${project}/test.${ext}`,
1203-
{
1204-
env: {
1205-
...process.env,
1206-
TS_NODE_PROJECT: `./module-types/${project}/tsconfig.json`,
1207-
},
1208-
}
1209-
);
1210-
expect(err).toBe(null);
1211-
expect(stdout).toBe(`Failures: 0\n`);
1212-
}
1213-
1214-
test('moduleTypes should allow importing CJS in an otherwise ESM project', async (t) => {
1215-
// A notable case where you can use ts-node's CommonJS loader, not the ESM loader, in an ESM project:
1216-
// when loading a webpack.config.ts or similar config
1217-
const { err, stderr, stdout } = await exec(
1218-
`${CMD_TS_NODE_WITHOUT_PROJECT_FLAG} --project ./module-types/override-to-cjs/tsconfig.json ./module-types/override-to-cjs/test-webpack-config.cjs`
1219-
);
1220-
expect(err).toBe(null);
1221-
expect(stdout).toBe(``);
1222-
1223-
await runModuleTypeTest('override-to-cjs', 'cjs');
1224-
if (semver.gte(process.version, '14.13.1'))
1225-
await runModuleTypeTest('override-to-cjs', 'mjs');
1226-
});
1227-
1228-
test('moduleTypes should allow importing ESM in an otherwise CJS project', async (t) => {
1229-
await runModuleTypeTest('override-to-esm', 'cjs');
1230-
// Node 14.13.0 has a bug(?) where it checks for ESM-only syntax *before* we transform the code.
1231-
if (semver.gte(process.version, '14.13.1'))
1232-
await runModuleTypeTest('override-to-esm', 'mjs');
1200+
test.suite('moduleTypes', (test) => {
1201+
suite('with vanilla ts transpilation', 'tsconfig.json');
1202+
suite('with third-party-transpiler', 'tsconfig-swc.json');
1203+
function suite(name: string, tsconfig: string) {
1204+
test.suite(name, (test) => {
1205+
test('supports CJS webpack.config.ts in an otherwise ESM project', async (t) => {
1206+
// A notable case where you can use ts-node's CommonJS loader, not the ESM loader, in an ESM project:
1207+
// when loading a webpack.config.ts or similar config
1208+
const { err, stdout } = await exec(
1209+
`${CMD_TS_NODE_WITHOUT_PROJECT_FLAG} --project ./module-types/override-to-cjs/${tsconfig} ./module-types/override-to-cjs/test-webpack-config.cjs`
1210+
);
1211+
expect(err).toBe(null);
1212+
expect(stdout).toBe(``);
1213+
});
1214+
test('should allow importing CJS in an otherwise ESM project', async (t) => {
1215+
await run('override-to-cjs', tsconfig, 'cjs');
1216+
if (semver.gte(process.version, '14.13.1'))
1217+
await run('override-to-cjs', tsconfig, 'mjs');
1218+
});
1219+
test('should allow importing ESM in an otherwise CJS project', async (t) => {
1220+
await run('override-to-esm', tsconfig, 'cjs');
1221+
// Node 14.13.0 has a bug(?) where it checks for ESM-only syntax *before* we transform the code.
1222+
if (semver.gte(process.version, '14.13.1'))
1223+
await run('override-to-esm', tsconfig, 'mjs');
1224+
});
1225+
});
1226+
}
1227+
async function run(project: string, config: string, ext: string) {
1228+
const { err, stderr, stdout } = await exec(
1229+
`${CMD_ESM_LOADER_WITHOUT_PROJECT} ./module-types/${project}/test.${ext}`,
1230+
{
1231+
env: {
1232+
...process.env,
1233+
TS_NODE_PROJECT: `./module-types/${project}/${config}`,
1234+
},
1235+
}
1236+
);
1237+
expect(err).toBe(null);
1238+
expect(stdout).toBe(`Failures: 0\n`);
1239+
}
12331240
});
12341241
}
12351242

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"ts-node": {
3+
"swc": true,
4+
"moduleTypes": {
5+
"webpack.config.ts": "cjs",
6+
// Test that subsequent patterns override earlier ones
7+
"src/cjs-subdir/**/*": "esm",
8+
"src/cjs-subdir": "cjs",
9+
"src/cjs-subdir/esm-exception.ts": "esm"
10+
}
11+
},
12+
"compilerOptions": {
13+
"module": "ES2015"
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"ts-node": {
3+
"swc": true,
4+
"moduleTypes": {
5+
// Test that subsequent patterns override earlier ones
6+
"src/esm-subdir/**/*": "cjs",
7+
"src/esm-subdir": "esm",
8+
"src/esm-subdir/cjs-exception.ts": "cjs"
9+
}
10+
},
11+
"compilerOptions": {
12+
"module": "CommonJS"
13+
}
14+
}

0 commit comments

Comments
 (0)