Skip to content

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

Open

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

Activity

  1. CodeMan62 commented on Apr 23, 2025

    @CodeMan62

    Donny/강동윤 (@kdy1) if you want i can take a look and try to reproduce and propose a fix with your permission!

  2. kdy1 commented on Apr 23, 2025

    @kdy1
    Member

    It would be nice, thank you!

  3. CodeMan62 commented on Apr 24, 2025

    @CodeMan62

    Donny/강동윤 (@kdy1) the issue is realted to this repo https://github.com/swc-project/pkgs so we have to fix this in pkgs repo so should I create the PR directly in pkgs or something else??

  4. kdy1 commented on Apr 24, 2025

    @kdy1
    Member

    You should send a PR to pkgs repo I guess

  5. CodeMan62 commented on Apr 25, 2025

    @CodeMan62

    Done Donny/강동윤 (@kdy1) but there are some problems we should discuss in that PR

  6. thomasmattheussen commented on May 6, 2025

    @thomasmattheussen
    Author

    Great! Thanks for fixing this so quickly! हिमांशु (@CodeMan62) Donny/강동윤 (@kdy1) 🙌
    Any idea when this will be released?

  7. kdy1 commented on May 6, 2025

    @kdy1
    Member

    I published a new version

  8. CodeMan62 commented on May 6, 2025

    @CodeMan62

    Thomas Mattheussen (@thomasmattheussen) the new version is out check out if you see the bug is still there or see any bug or issue just create a PR I would love to work on that ❤

  9. thomasmattheussen commented on May 7, 2025

    @thomasmattheussen
    Author

    I think something went wrong with publishing. I get this error now:

    node:internal/modules/esm/resolve:204
      const resolvedOption = FSLegacyMainResolve(pkgPath, packageConfig.main, baseStringified);
                             ^
    
    Error: Cannot find package '/Users/tsmn02/Desktop/chameleon-another-alt/packages/chameleon-react/node_modules/@swc/cli/lib/swc/index.js' imported from /Users/tsmn02/Desktop/chameleon-another-alt/packages/chameleon-react/tools/compile/swc.mjs
        at legacyMainResolve (node:internal/modules/esm/resolve:204:26)
        at packageResolve (node:internal/modules/esm/resolve:778:12)
        at moduleResolve (node:internal/modules/esm/resolve:854:18)
        at defaultResolve (node:internal/modules/esm/resolve:984:11)
        at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:685:12)
        at #cachedDefaultResolve (node:internal/modules/esm/loader:634:25)
        at ModuleLoader.resolve (node:internal/modules/esm/loader:617:38)
        at ModuleLoader.getModuleJobForImport (node:internal/modules/esm/loader:273:38)
        at ModuleJob._link (node:internal/modules/esm/module_job:135:49) {
      code: 'ERR_MODULE_NOT_FOUND'
    }
    
    Node.js v22.14.0
     ELIFECYCLE  Command failed with exit code 1.
    

    I assume this is due to #96

    When I check my node_modules folder, the /lib folder is not there in the @swc/cli package.

    Image
  10. thomasmattheussen commented on May 8, 2025

    @thomasmattheussen
    Author

    I think something went wrong with publishing.

    Okay, that seems fixed with 0.7.5 👍

    हिमांशु (@CodeMan62) But now I also get this issue: #101

  11. kdy1 commented on May 12, 2025

    @kdy1
    Member

    Reopening as #95 is reverted.

  12. CodeMan62 commented on May 12, 2025

    @CodeMan62

    will investigate it again and make sure to fix it Donny/강동윤 (@kdy1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions