swc
swc copied to clipboard
Output mjs file extension
Describe the feature
A configuration option to output mjs files instead of js files (when using module type es6).
Babel plugin or link to the feature description
https://babeljs.io/docs/en/babel-cli#set-file-extensions
Additional context
https://github.com/swc-project/swc/discussions/2953
https://github.com/babel/babel/pull/9144
Why has this merged feature not been released for two years?
It's not merged
Will this ever be solved :D I would really need this feature.
When I transpile to modules, my dynamic imports will stop working in Node, because it either requires an extension to be defined, or the file must have .mjs extension. A simple import('./myModule') will fail without having an extension defined.
Landed in https://github.com/swc-project/cli/pull/286
Should close this issue
No, after I tried, even the output file is mjs, but the code is still like import './xxx.js'
In https://github.com/swc-project/swc/issues/8742#issuecomment-2040392007 I describe possible workaround. You also need to use the .mts extension of the sources to import them as .mjs.
`tsconfig.json'
⬇⬇⬇
{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext"
"...": "..."
}
}
// ======
// SOURCE
// Import from './pkgs.mts' to './ctx.mts'
// ======
import { ProjectPackages, getProjectPackages } from "~/pkgs.mjs";
export interface Ctx {
packages: ProjectPackages | null
}
//...
⬇ swc --out-file-extension mjs ... ⬇
import { getProjectPackages } from "./pkgs.mjs";
//...
@shimarulin thanks that works
"scripts": {
"swc-watcher": "npx swc src --out-dir dist --strip-leading-paths --watch --out-file-extension mjs",
"swc-runner": "node --watch-path=./dist/ ./dist/index.mjs"
}