Skip to content

Commit 4aff83c

Browse files
marco-ippolitojoyeecheung
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 041e977 commit 4aff83c

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
@@ -493,7 +493,9 @@ class ModuleLoader {
493493
const loadResult = this.#loadSync(url, { format, importAttributes });
494494

495495
// Use the synchronous commonjs translator which can deal with cycles.
496-
const finalFormat = loadResult.format === 'commonjs' ? 'commonjs-sync' : loadResult.format;
496+
const finalFormat =
497+
loadResult.format === 'commonjs' ||
498+
loadResult.format === 'commonjs-typescript' ? 'commonjs-sync' : loadResult.format;
497499

498500
if (finalFormat === 'wasm') {
499501
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
@@ -186,3 +186,13 @@ test('expect failure of a .cts file requiring esm in node_modules', async () =>
186186
match(result.stderr, /ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING/);
187187
strictEqual(result.code, 1);
188188
});
189+
190+
test('cts -> require mts -> import cts', async () => {
191+
const result = await spawnPromisified(process.execPath, [
192+
fixtures.path('typescript/cts/issue-59963/a.cts'),
193+
]);
194+
195+
strictEqual(result.stderr, '');
196+
strictEqual(result.stdout, 'Hello from c.cts\n');
197+
strictEqual(result.code, 0);
198+
});

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,14 @@ test('execute .ts file importing a module', async () => {
133133
strictEqual(result.stdout, 'Hello, TypeScript!\n');
134134
strictEqual(result.code, 0);
135135
});
136+
137+
test('mts -> import cts -> require mts', async () => {
138+
const result = await spawnPromisified(process.execPath, [
139+
'--no-warnings',
140+
fixtures.path('typescript/mts/issue-59963/a.mts'),
141+
]);
142+
143+
strictEqual(result.stderr, '');
144+
strictEqual(result.stdout, 'Hello from c.mts\n');
145+
strictEqual(result.code, 0);
146+
});
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)