Skip to content

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

Open
aloisklink wants to merge 1 commit into
developit:masterfrom
aloisklink:patch-1
Open

Support .cjs files imported with @rollup/plugin-commonjs#15
aloisklink wants to merge 1 commit into
developit:masterfrom
aloisklink:patch-1

Conversation

@aloisklink
Copy link
Copy Markdown

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
Copy Markdown

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

@radiovisual
Copy link
Copy Markdown

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

Development

Successfully merging this pull request may close these issues.

3 participants