Skip to content

Commit

Permalink
squash: Corrected ts-node issue (detail)
Browse files Browse the repository at this point in the history
ts-node 'compile' was output *.ts transformers as cjs, even if they were esm code.
  • Loading branch information
nonara committed Jun 3, 2024
1 parent 38c722d commit 3c6e0b0
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion projects/patch/src/plugin/esm-intercept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down

0 comments on commit 3c6e0b0

Please sign in to comment.