swc-project / swc-project/pkgs

@swc/cli seems to reuse config somewhere under the hood

Open
#97 12 comments 0 reactions 1 assignee View on GitHub

Nobody has claimed this yet.

Dominant language
TypeScript
Stars
86
Forks
44
Avg merge
3d 49m
Merged PRs (30d)
1

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.