-
Notifications
You must be signed in to change notification settings - Fork 36
Description
Describe the bug
When invoking swcDir() programmatically in the same process for two separate builds (ESM then CommonJS), the second call seems to reuse some parts of the config. As a result, even though I pass swcOptions.module.outFileExtension: 'cjs' on the second run, all import-path rewrites still use .mjs. If I comment out the ESM build, the CJS build works correctly—but running both back-to-back, the first invocation “wins” and contaminates the second somehow.
Input code
import path from "node:path";
import { swcDir } from "@swc/cli";
import { toMerged } from "es-toolkit";
export async function compileSwc() {
const defaultOptions = {
cliOptions: {
outDir: path.join("dist", "es"),
filenames: ["src"],
extensions: [".js", ".jsx", ".cjs", ".mjs"],
stripLeadingPaths: true,
},
swcOptions: {
jsc: {
parser: {
syntax: "ecmascript",
},
baseUrl: path.resolve("src"),
},
module: { type: "es6", resolveFully: true, outFileExtension: "mjs" },
},
};
// Config for ES6 modules output
const swcModulesConfig = defaultOptions;
// Merge options for CommonJS output
const swcCommonJSConfig = toMerged(defaultOptions, {
cliOptions: {
outDir: path.join("dist", "cjs"),
outFileExtension: "cjs",
},
swcOptions: {
module: { type: "commonjs", outFileExtension: "cjs" },
},
});
console.log("Compiling esm.");
await swcDir(swcModulesConfig);
console.log("Compiling CommonJS.");
await swcDir(swcCommonJSConfig);
}Config
Link to the code that reproduces this issue
https://codesandbox.io/p/devbox/w9lwrg?file=%2Fdist%2Fcjs%2Findex.cjs
SWC Info output
No response
Expected behavior
In the dist/cjs folder the import paths should be rewritten to use '.cjs' extensions
Actual behavior
In the dist/cjs folder the import paths are rewritten with '.mjs' extensions
Version
"@swc/cli": "^0.7.3", "@swc/core": "^1.11.21"
Additional context
No response