From 3c6e0b0046199489b7e05e97bba533e430793e07 Mon Sep 17 00:00:00 2001 From: Ron Spickenagel Date: Mon, 3 Jun 2024 16:27:21 -0400 Subject: [PATCH] squash: Corrected ts-node issue (detail) ts-node 'compile' was output *.ts transformers as cjs, even if they were esm code. --- projects/patch/src/plugin/esm-intercept.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/projects/patch/src/plugin/esm-intercept.ts b/projects/patch/src/plugin/esm-intercept.ts index eab5fab..9296e92 100644 --- a/projects/patch/src/plugin/esm-intercept.ts +++ b/projects/patch/src/plugin/esm-intercept.ts @@ -83,7 +83,15 @@ namespace tsp { if (tsExtensions.includes(resolvedPathExt)) { if (!builtFiles.has(resolvedPath)) { const tsCode = fs.readFileSync(resolvedPath, 'utf8'); - const jsCode = registerConfig.tsNodeInstance!.compile(tsCode, resolvedPath); + + // NOTE - I don't know why, but if you supply a *.ts file to tsNode.compile it will be output as cjs, + // regardless of the tsConfig properly specifying ESNext for module and target. Notably, this issue seems + // to have started with TS v5.5, + // + // To work around, we will tell ts-node that it's an "mjs" file. + const newPath = resolvedPath.replace(/\.ts$/, '.mts'); + + const jsCode = registerConfig.tsNodeInstance!.compile(tsCode, newPath); const outputFileName = getHash() + '.mjs'; const outputFilePath = path.join(getTmpDir('esm'), outputFileName); fs.writeFileSync(outputFilePath, jsCode, 'utf8');