You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When building a directory: swc ./src -d ./dist --copy-files --config-file .swcrc --strip-leading-paths
There are files are explicitly using the .cts and .mts extensions (meaning commonjs + ESM, respectively).
When building these files (e.g. via tsc) it is expected to map to a .cjs and .mjs extend files.
However when building with SWC, all files are emitted with .js extensions.
Not only does this break existing resolution paths, it will change how the code is executed which is guaranteed to break (you cannot have both commonjs and ESM valid syntax in .js files)
If you have multiple files that only differ by extension, it will also omit some of the files as they all cannot be built under the same extension. In my experience so far, the written file is a non-deterministic race condition.
The index.cjs file will not be built at all. It will be written as the index.js file (if not overwritten by the true index.js file)
The esm.mjs file will instead be emitted as esm.js
And the index.js file will fail to execute, since it tries to load from a file that doesn't exist (if not overwritten by the index.cts)
Version
@swc/cli: 0.5.2 @swc/core: 1.10.1
Additional context
I believe this is coming from the --out-file-extension option. Despite the fact that I have not set it, it comes with a default of js.
Trying to build both ESM and CJS at the same time will also end up running into this issue: #9790
Currently SWC tries to build the entire directory as ESM or CJS, it never infer the proper output based on extension.
The text was updated successfully, but these errors were encountered:
Describe the bug
When building a directory:
swc ./src -d ./dist --copy-files --config-file .swcrc --strip-leading-paths
There are files are explicitly using the
.cts
and.mts
extensions (meaning commonjs + ESM, respectively).When building these files (e.g. via
tsc
) it is expected to map to a.cjs
and.mjs
extend files.However when building with SWC, all files are emitted with
.js
extensions.Not only does this break existing resolution paths, it will change how the code is executed which is guaranteed to break (you cannot have both commonjs and ESM valid syntax in .js files)
If you have multiple files that only differ by extension, it will also omit some of the files as they all cannot be built under the same extension. In my experience so far, the written file is a non-deterministic race condition.
Input code
Config
Playground link (or link to the minimal reproduction)
https://github.com/JacobLey/issue-recreator/tree/swc-extensions
SWC Info output
Operating System:
Platform: linux
Arch: arm64
Machine Type: aarch64
Version: #60 SMP Mon Nov 18 05:48:29 UTC 2024
CPU: (14 cores)
Models: unknown
Expected behavior
From the input code, three files should be built:
Actual behavior
The
index.cjs
file will not be built at all. It will be written as theindex.js
file (if not overwritten by the trueindex.js
file)The
esm.mjs
file will instead be emitted asesm.js
And the
index.js
file will fail to execute, since it tries to load from a file that doesn't exist (if not overwritten by theindex.cts
)Version
@swc/cli: 0.5.2 @swc/core: 1.10.1
Additional context
I believe this is coming from the
--out-file-extension
option. Despite the fact that I have not set it, it comes with a default ofjs
.Trying to build both ESM and CJS at the same time will also end up running into this issue: #9790
Currently SWC tries to build the entire directory as ESM or CJS, it never infer the proper output based on extension.
The text was updated successfully, but these errors were encountered: