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

How to disable .cjs file generation #91

Open
ivodolenc opened this issue Feb 7, 2023 · 6 comments
Open

How to disable .cjs file generation #91

ivodolenc opened this issue Feb 7, 2023 · 6 comments

Comments

@ivodolenc
Copy link

ivodolenc commented Feb 7, 2023

Assuming we're only targeting type: module packages, how to disable module.cjs file generation in /dist directory?

I tried this but the file is still generated:

// build.config.ts

import { defineBuildConfig } from 'unbuild'

export default defineBuildConfig({
  rollup: {
    emitCJS: false,
    commonjs: false
  }
})

Quick alternatives

Removes it after build via package script:

{
  "scripts": {
    "build:module": "nuxt-module-build && rm dist/module.cjs"
  }
}

Excludes it via package files option:

{
  "files": [
    "dist",
    "!dist/module.cjs"
  ],
}
@pi0
Copy link
Member

pi0 commented Feb 7, 2023

Hi. Generated cjs is just a wrapper rather than actual build. Why would you want to remove that?

@ivodolenc
Copy link
Author

ivodolenc commented Feb 7, 2023

Hi, thanks for fast reply.

You're right, my version is only ~230 bytes, but if it's not needed, maybe it would be good if there was an option to disable its generation?

If it's not easy to implement, I can use the mentioned alternative.

This is more of a question or asking for advice, I would make a discussion, but I see that the Discussion page is not activated on the repo.

@pi0
Copy link
Member

pi0 commented Feb 7, 2023

We can add an option to disable. For the context, this stub makes modules compatible with Nuxt 2. And really harmless as in Nuxt 3, we natively support and pick the mjs format.

@ivodolenc
Copy link
Author

Yep, I assumed it was for previous versions. Ok, it's not an issue or urgent, but thanks for the info.

@ivodolenc
Copy link
Author

Hi @pi0, sorry for tagging you here, but I just updated @nuxt/module-builder to the latest (0.5.1) version and noticed more extra files when exporting for production.

  • Module size before: unpacked size 28.6 kB
  • Module size after (with extra files): unpacked size 35.9 kB

I ran into some possibly related discussions here (problems with type declaration and nodeResolution in TS) so I believe that's the reason for the extra files. These are .d.ts, .d.mts, .d.cts formats.

The main problem is that this can increase the final version of the package a lot.

I plan to switch all my modules to ESM only so that all other formats (like .cjs, .d.cts) will be unnecessary.

It would be good if we could exclude them from the build, for example explicitly define which formats or extensions we want to generate.

Also, why add extra .d.mts files when .d.ts works perfectly fine for esm?

@peterbud
Copy link

peterbud commented Jun 5, 2024

I'd agree with @ivodolenc : it would be fantastic to have an option for "pure" ESM build, without CJS or similar.

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

No branches or pull requests

3 participants