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

module type nodenext does not output commonJS for commonJS files #9790

Open
NotWoods opened this issue Dec 10, 2024 · 4 comments
Open

module type nodenext does not output commonJS for commonJS files #9790

NotWoods opened this issue Dec 10, 2024 · 4 comments
Labels

Comments

@NotWoods
Copy link

Describe the bug

In TypeScript, nodenext modules will output either ES modules or CommonJS depending on the module type determined by Node.js.

  • .cts -> CommonJS
  • .mts -> ESM
  • package.json with type: commonjs or no type field -> CommonJS
  • package.json with type: module -> ESM

As far as I can tell, in SWC nodenext always outputs an ES module.

Input code

export default function hello() {
  console.log('hello world');
}

Config

{
  "module": {
    "type": "nodenext"
  },
  "jsc": {
    "parser": {
      "syntax": "typescript"
    }
  }
}

Playground link (or link to the minimal reproduction)

https://stackblitz.com/edit/vitejs-vite-ycvjv6ru?file=.swcrc

SWC Info output

Operating System:
    Platform: linux
    Arch: x64
    Machine Type: undefined
    Version: Ubuntu 20.04.0 LTS Tue Dec 10 2024 11:27:16 GMT-0800 (Pacific Standard Time)
    CPU: (8 cores)
        Models: Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz

Binaries:
    Node: 18.20.3
    npm: 10.2.3
    Yarn: 1.22.19
    pnpm: 8.15.6

Relevant Packages:
    @swc/core: 1.10.1
    @swc/helpers: N/A
    @swc/types: 0.1.17
    typescript: 5.6.3

SWC Config:
    output: N/A
    .swcrc path: N/A

Next.js info:
    output: N/A

Expected behavior

CommonJS should be outputted for CommonJS files in nodenext, or documentation should be added to indicate that nodenext is not designed to match TypeScript's nodenext.

Actual behavior

Only ES modules are outputted

Version

1.10.1

Additional context

https://www.typescriptlang.org/docs/handbook/modules/reference.html#the-moduleresolution-compiler-option

@NotWoods NotWoods added the C-bug label Dec 10, 2024
@magic-akari
Copy link
Member

After reading the TS Handboook, here are my findings:

  1. nodenext supports converting cts to cjs, not as a feature, but by directly using the CommonJS pattern, and the nodenext option does not do more or different things than CommonJS.
  2. TSC recognizes file extensions and the type field in package.json during emitting, unlike SWC Core. These tasks may need to be handled in SWC CLI if necessary.

@JacobLey
Copy link

JacobLey commented Dec 16, 2024

I'm running into this as well.

Because SWC always tries to interpret commonjs files as ESM (even if they have the .cts extension, which is explicitly commonjs, irrelevant to whatever is in package.json) it makes some syntax invalid.

e.g.

// index.cts
export = { foo: 'bar' }

Will fail with

Export assignment cannot be used when targeting ECMAScript modules.

Despite the fact that this is the recommended format for exporting CJS files, with verbatimModuleSyntax

Which is explicitly endorsed by SWC as the preferred way to format code: https://swc.rs/docs/migrating-from-tsc#verbatimmodulesyntax-true
Given what I have experienced so far, I don't believe it is possible to build a directory with SWC that uses verbatimModuleSyntax and .cts extended files...

I don't necessarily expect SWC to respect my package.json's type field (although that would be nice, and I'd be open to a CLI/config option to mirror this instead), but I would expect it to respect the file extension.
But based on separate issues: #9800 It looks like SWC might ignore file extensions altogether...

@kdy1
Copy link
Member

kdy1 commented Dec 16, 2024

I think we should handle .cts extension

@GiveMe-A-Name
Copy link

May i have a try?

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

No branches or pull requests

5 participants