-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Output mjs file extension #3067
Comments
Why has this merged feature not been released for two years? |
It's not merged |
This comment has been minimized.
This comment has been minimized.
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 |
Landed in swc-project/cli#286 Should close this issue |
No, after I tried, even the output file is mjs, but the code is still like |
In #8742 (comment) I describe possible workaround. You also need to use the `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"
} |
Is there an equivalent .swcrc option? |
I have a problem with ESM (mjs) modules, when i use something like this: // src/bootstrap.mjs
import 'reflect-metadata'
import {RegisterRoutes} from './routes.mjs'
import {errorRequestHandler} from './my_module/middleware/index.mjs'
import {getContainer} from './inversify.config.mjs' and build it with swc src --out-file-extension mjs --strip-leading-paths -D -d dist I get the following: // dist/bootstrap.mjs
import "reflect-metadata";
import { RegisterRoutes } from "./routes.mjs";
// This should be ".mjs" extension, no ".js"
import { errorRequestHandler } from "./my_module/middleware/index.js";
import { getContainer } from "./inversify.config.mjs"; if I run this, I get the following error
my configuration is {
"$schema": "https://swc.rs/schema.json",
"jsc": {
"parser": {
"syntax": "typescript",
"tsx": false,
"decorators": true,
"dynamicImport": true,
"resolveFully": true
},
"transform": {
"decoratorMetadata": true
},
"target": "es2022",
"baseUrl": "."
},
"module": {
"type": "es6",
"noInterop": true,
"resolveFully": true
},
"sourceMaps": true
}
My workaround is transform the import, but theres a catch, check for {
"$schema": "https://swc.rs/schema.json",
"jsc": {
"experimental": {
"plugins": [
[
"@swc/plugin-transform-imports",
{
"^(.*?)\\.mjs(x)?$": {
"skipDefaultConversion": true,
"transform": "{{matches.[1]}}.mjs "
}
}
] There's a space at the end of .mjs. This does the job (it's still a valid import), as it ensures it doesn’t end up returning // dist/bootstrap.mjs
import "reflect-metadata";
import { RegisterRoutes } from "./routes.mjs ";
import { errorRequestHandler } from "./my_module/middleware/index.mjs ";
import { getContainer } from "./inversify.config.mjs "; There's a better way to doing this?. Thanks. |
I wanted to echo what @sirzeta reported. We do have the |
I'll work take on this when I have some time for it. |
@kdy1 - I ended up cobbling together a solution locally to this problem since the above hack wasn't working for me and I figured that I may as well spend time debugging a fix instead of a hack. If you already have something you're working on or if you have a different fix in mind because my PR did the fix in the wrong spot or isn't up to par for rust in this project, etc, just let me know and I can bow out or make requested changes (whatever works best for you). Thanks! |
@kdy1 - one thing that I didn't do was add any documentation to the site for this option. Should that be added as well? |
Yeah, I guess so. Can you send a PR to https://github.com/swc-project/website ? |
Awesome. I added an attempt at the documentation here: swc-project/website#278 |
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
#2953
babel/babel#9144
The text was updated successfully, but these errors were encountered: