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

Support .cjs files imported with @rollup/plugin-commonjs #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

aloisklink
Copy link

Add support for CommonJS files loaded with @rollup/plugin-commonjs.

If try to run rollup-plugin-preserve-shebang on a CommonJS file
(e.g. one that uses const x = require("..."); syntax instead of
import),
@rollup/plugin-commonjs
will append ?commonjs-entry to the end of the id.

This means that renderChunk() will fail to add the shebang back to the file, since the moduleId is different.

E.g. in transform(code, modId), modId = "...example.js"
However, in renderChunk(),
chunk.facadeModuleId = "...example.js?commonjs-entry".

In order to get the original module id, you need to remove ?commonjs-entry.

Add support for CommonJS files loaded with @rollup/plugin-commonjs.

If try to run rollup-plugin-preserve-shebang on a CommonJS file
(e.g. one that uses `const x = require("...");` syntax instead of
`import`),
[@rollup/plugin-commonjs](https://www.npmjs.com/package/@rollup/plugin-commonjs)
will append `?commonjs-entry` to the end of the id.

E.g. in `transform(code, modId)`, `modId = "...example.js"`
However, in `renderChunk()`,
`chunk.facadeModuleId = "...example.js?commonjs-entry"`.

In order to get the original module id, you need to remove `?commonjs-entry`.
@otakustay
Copy link

I encountered exactly the same issue, would be helpful if this PR can be merged.

@radiovisual
Copy link

I am not sure this is true anymore. I don't know what has changed since this was opened two year ago, but I just tested the scenario this PR attempts to fix (alongside my PR that adds integration tests #16) and I can't reproduce this. Is it still valid?

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