Skip to content

Commit 39147c1

Browse files
module: use sync cjs when importing cts
PR-URL: #60072 Backport-PR-URL: #62029Fixes: #59963 Reviewed-By: Yongsheng Zhang <zyszys98@gmail.com> Reviewed-By: Geoffrey Booth <webadmin@geoffreybooth.com> Fixes: #61801
1 parent 12a2462 commit 39147c1

9 files changed

Lines changed: 38 additions & 1 deletion

File tree

‎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
constloadResult=this.#loadSync(url,{ format, importAttributes });
494494

495495
// Use the synchronous commonjs translator which can deal with cycles.
496-
constfinalFormat=loadResult.format==='commonjs' ? 'commonjs-sync' : loadResult.format;
496+
constfinalFormat=
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+
constresult=awaitspawnPromisified(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+
constresult=awaitspawnPromisified(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+
interfaceFoo{};
3+
console.log(message);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
interfaceFoo{};
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+
constmessage: 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+
interfaceFoo{};
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+
interfaceFoo{};
3+
module.exports={ message };
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exportconstmessage: string="Hello from c.mts";

0 commit comments

Comments
 (0)