rollup-plugin-preserve-shebang icon indicating copy to clipboard operation
rollup-plugin-preserve-shebang copied to clipboard

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

Open aloisklink opened this issue 3 years ago • 2 comments

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.

aloisklink avatar May 13 '22 00:05 aloisklink

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

otakustay avatar Jul 19 '22 06:07 otakustay

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?

radiovisual avatar Mar 09 '24 23:03 radiovisual