swc icon indicating copy to clipboard operation
swc copied to clipboard

Output mjs file extension

Open stefee opened this issue 3 years ago • 10 comments

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

stefee avatar Dec 18 '21 10:12 stefee

Why has this merged feature not been released for two years?

snowyu avatar Mar 14 '23 00:03 snowyu

It's not merged

kdy1 avatar Mar 14 '23 00:03 kdy1

Will this ever be solved :D I would really need this feature.

Tjerk-Haaye-Henricus avatar Jun 03 '23 20:06 Tjerk-Haaye-Henricus

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.

wintercounter avatar Nov 25 '23 23:11 wintercounter

Landed in https://github.com/swc-project/cli/pull/286

Should close this issue

himself65 avatar Feb 23 '24 22:02 himself65

No, after I tried, even the output file is mjs, but the code is still like import './xxx.js'

himself65 avatar Feb 24 '24 00:02 himself65

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 avatar Apr 05 '24 19:04 shimarulin

@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"
  }

josh-i386g avatar Aug 18 '24 05:08 josh-i386g