Skip to content

Commit dbeee55

Browse files
marco-ippolitotargos
authored andcommitted
module: use sync cjs when importing cts
PR-URL: #60072 Fixes: #59963 Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com>
1 parent 9d9bd0f commit dbeee55

File tree

9 files changed

+38
-1
lines changed

9 files changed

+38
-1
lines changed

β€Žlib/internal/modules/esm/loader.jsβ€Ž

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,9 @@ class ModuleLoader {
504504
const loadResult = this.#loadSync(url, { format, importAttributes });
505505

506506
// Use the synchronous commonjs translator which can deal with cycles.
507-
const finalFormat = loadResult.format === 'commonjs' ? 'commonjs-sync' : loadResult.format;
507+
const finalFormat =
508+
loadResult.format === 'commonjs' ||
509+
loadResult.format === 'commonjs-typescript' ? 'commonjs-sync' : loadResult.format;
508510

509511
if (finalFormat === 'wasm') {
510512
assert.fail('WASM is currently unsupported by require(esm)');

β€Žtest/es-module/test-typescript-commonjs.mjsβ€Ž

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,3 +174,13 @@ test('expect failure of a .cts file requiring esm in node_modules', async () =>
174174
match(result.stderr, /ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING/);
175175
strictEqual(result.code, 1);
176176
});
177+
178+
test('cts -> require mts -> import cts', async () => {
179+
const result = await spawnPromisified(process.execPath, [
180+
fixtures.path('typescript/cts/issue-59963/a.cts'),
181+
]);
182+
183+
strictEqual(result.stderr, '');
184+
strictEqual(result.stdout, 'Hello from c.cts\n');
185+
strictEqual(result.code, 0);
186+
});

β€Žtest/es-module/test-typescript-module.mjsβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,3 +98,14 @@ test('execute .ts file importing a module', async () => {
9898
strictEqual(result.stdout, 'Hello, TypeScript!\n');
9999
strictEqual(result.code, 0);
100100
});
101+
102+
test('mts -> import cts -> require mts', async () => {
103+
const result = await spawnPromisified(process.execPath, [
104+
'--no-warnings',
105+
fixtures.path('typescript/mts/issue-59963/a.mts'),
106+
]);
107+
108+
strictEqual(result.stderr, '');
109+
strictEqual(result.stdout, 'Hello from c.mts\n');
110+
strictEqual(result.code, 0);
111+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const { message } = require("./b.mts");
2+
interface Foo {};
3+
console.log(message);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
interface Foo {};
2+
export { message } from "./c.cts";
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const message: string = "Hello from c.cts";
2+
module.exports = { message };
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { message } from "./b.cts";
2+
interface Foo {};
3+
console.log(message);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
const { message } = require("./c.mts");
2+
interface Foo {};
3+
module.exports = { message };
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const message: string = "Hello from c.mts";

0 commit comments

Comments
Β (0)