Skip to content
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

Closed
stefee opened this issue Dec 18, 2021 · 16 comments · Fixed by #9784
Closed

Output mjs file extension #3067

stefee opened this issue Dec 18, 2021 · 16 comments · Fixed by #9784
Assignees
Milestone

Comments

@stefee
Copy link

stefee commented Dec 18, 2021

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

@kdy1 kdy1 added this to the Planned milestone Mar 18, 2022
@kdy1 kdy1 modified the milestones: Planned, v1.2.199 Jun 11, 2022
@kdy1 kdy1 removed this from the Planned milestone Dec 21, 2022
@snowyu
Copy link

snowyu commented Mar 14, 2023

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

@kdy1
Copy link
Member

kdy1 commented Mar 14, 2023

It's not merged

@kdy1 kdy1 closed this as not planned Won't fix, can't repro, duplicate, stale Mar 14, 2023
@kdy1 kdy1 reopened this Mar 14, 2023
@Tjerk-Haaye-Henricus

This comment has been minimized.

@wintercounter
Copy link

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.

@himself65
Copy link

Landed in swc-project/cli#286

Should close this issue

@himself65
Copy link

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

@shimarulin
Copy link

In #8742 (comment) 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";
//...

@josh-i386g
Copy link

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

@Clashsoft
Copy link

Is there an equivalent .swcrc option?

@sirzeta
Copy link

sirzeta commented Oct 12, 2024

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

Error: Directory import '/dist/my_module/middleware' is not supported resolving ES modules imported from /dist/bootstrap.mjs

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 {{matches.[1]}}.mjs.

{
  "$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 "./my_module/middleware/index.js". This makes my app happy to run but with annoying spaces.

// 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.

@hanseltime
Copy link
Contributor

I wanted to echo what @sirzeta reported. We do have the out-file-extension command, but it when used with resolveFully the resolve fully seems unaware of the extension to use and creates the broken links. It would be nice to have this supported in swc/core

@kdy1 kdy1 added this to the Planned milestone Dec 4, 2024
@kdy1 kdy1 self-assigned this Dec 4, 2024
@kdy1
Copy link
Member

kdy1 commented Dec 4, 2024

I'll work take on this when I have some time for it.

@hanseltime
Copy link
Contributor

@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).

#9784

Thanks!

@kdy1 kdy1 closed this as completed in #9784 Dec 9, 2024
@kdy1 kdy1 closed this as completed in e04c7b3 Dec 9, 2024
@kdy1 kdy1 modified the milestones: Planned, v1.10.1 Dec 9, 2024
@hanseltime
Copy link
Contributor

@kdy1 - one thing that I didn't do was add any documentation to the site for this option. Should that be added as well?

@kdy1
Copy link
Member

kdy1 commented Dec 10, 2024

Yeah, I guess so. Can you send a PR to https://github.com/swc-project/website ?

@hanseltime
Copy link
Contributor

hanseltime commented Dec 10, 2024

Awesome. I added an attempt at the documentation here: swc-project/website#278

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

Successfully merging a pull request may close this issue.