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

[commonjs] Allow injecting dynamicRequireTargets code in a custom file #809

Closed
nicolo-ribaudo opened this issue Feb 21, 2021 · 1 comment

Comments

@nicolo-ribaudo
Copy link
Contributor

nicolo-ribaudo commented Feb 21, 2021

  • Rollup Plugin Name: commonjs
  • Rollup Plugin Version: 17.1.0

Feature Use Case

I need to use @rollup/plugin-babel for older browsers support and @rollup/plugin-commonjs for my dependencies. One of my dependencies uses dynamic requrie calls, so enabled the dynamicRequireTargets option.

However, I couldn't come up with a working configuration:

  • rollupCommonJs({
    	dynamicRequireTargets: [
    		"./node_modules/regenerate-unicode-properties/**/*.js",
    	],
    }),
    rollupBabel()
    doesn't work, because @rollup/plugin-commonjs doesn't understand the experimental syntax before that it's compiled by Babel
  • rollupBabel(),
    rollupCommonJs({
    	dynamicRequireTargets: [
    		"./node_modules/regenerate-unicode-properties/**/*.js",
    	],
    })
    doesn't work, because @rollup/plugin-babel expects all its input to be ESM and not CJS (and it's clearly documented)
  • rollupCommonJs({
      include: [ /node_modules/ ],
    	dynamicRequireTargets: [
    		"./node_modules/regenerate-unicode-properties/**/*.js",
    	],
    }),
    rollupBabel()
    doesn't work, because @rollup/plugin-commonjs injects require() calls for the dynamically imported files in the entrypoint, so I cannot only include /node_modules/ and not my source files.
  • I also tried duplicating the commonjs plugin like this:
    rollupCommonJs({
      include: [ /node_modules/ ],
    	dynamicRequireTargets: [
    		"./node_modules/regenerate-unicode-properties/**/*.js",
    	],
    }),
    rollupBabel(),
    rollupCommonJs({
      transformMixedEsModules: true
    }),
    I was hoping that it would have converted CJS dependencies to ESM for Babel, and then injected and converted require() calls after transpiling everything so that the CJS plugin doesn't see unknown syntax. However, for some reason this consumes too much memory and makes every Node.js version I tried segfault.

Feature Proposal

I am actually asking for two features:

  1. rollupCommonJs should throw an error and make the build file if it cannot inject the require() calls. When we found out that our bundle didn't include the dynamically imported files we added the dynamicRequireTargets option thinking that it would have fixed it, and I only accidentally discovered that it was still not including those modules (all our tests where passing because when dynamicRequireTargets is enabled Rollup delegates to Node's native require()).

  2. It should be possible to inject the preload code in a file that is different from the entrypoint.
    To fix our build, I created an empty dynamic-require-entrypoint.cjs file, imported that file in our app, and hacked @rollup/plugin-commonjs so that it injects require() calls in that file rather than in the entrypoint: Make sure that Rollup's dynamicRequireTargets are included babel/babel#12839. I think that @rollup/plugin-commonjs should provide an option for this.

@stale stale bot added the x⁷ ⋅ stale label Apr 23, 2021
@stale
Copy link

stale bot commented Apr 24, 2021

Hey folks. This issue hasn't received any traction for 60 days, so we're going to close this for housekeeping. If this is still an ongoing issue, please do consider contributing a Pull Request to resolve it. Further discussion is always welcome even with the issue closed. If anything actionable is posted in the comments, we'll consider reopening it.

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

No branches or pull requests

1 participant