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

Running through rollup for node causes issues with dynamic require #6

Open
rowanwins opened this issue Apr 25, 2020 · 11 comments
Open

Comments

@rowanwins
Copy link

Thanks for a neat little library!

I was attempting at incorporating it into a library of mine which I bundle using rollup and I get an issue

Error: Dynamic requires are not currently supported by rollup-plugin-commonjs

which was related to this line of your library

From what I can tell it looks like it's to do with requiring the URL of the worker?

Anyway just thought I'd flag it as other users of rollup may run into the same issue.

Cheers
Rowan

@tunnckoCore
Copy link

True. I think we can replace it with dynamic import import(mod);. The whole idea behind that require is evaluation/execution of that mod.

@guybedford
Copy link
Collaborator

Short of directly publishing ESM, one pattern I've used to solve this issue before is to use a Promise.resolve(require()) pattern in CommonJS and then have the CJS to ESM transform specifically know it can replace this with a dynamic import.

I've created a Rollup issue for this here - rollup/plugins#341.

@developit
Copy link
Owner

@rowanwins are you bundling for web via rollup, or for Node? For web none of that code should be processed. It just requires using the node-resolve plugin with the default browser:true option (or mainFields:['browser','module','main']).

@rowanwins
Copy link
Author

I was trying to two seperate bundles so I could support both. My browser bundle is working fine, but my node bundle is where I run into the issue.

@rowanwins rowanwins changed the title Running through rollup causes issues with dynamic require Running through rollup for node causes issues with dynamic require Apr 28, 2020
@developit
Copy link
Owner

developit commented May 3, 2020

Just looping back here - it doesn't seem like the require() referenced here should actually be processed by Rollup. It's strictly used to inject code into the Worker at runtime, and there's no way for Rollup to statically analyze the require() statement there anyway since its in a different JavaScript context.

@guybedford - I wonder, is there a way to exempt a require() statement from processing by rollup's commonjs plugin? Maybe just switching it to module.require() or global.require()?

@guybedford
Copy link
Collaborator

@developit perhaps try eval('require')(...) here?

@gerdstolpmann
Copy link

I was recently bundling with webpack, and also ran into this issue. The solve was to use eval as @guybedford suggests.

@adjenks
Copy link

adjenks commented Sep 2, 2022

I just ran into this issue as well. My error looked like this:

[!] (plugin commonjs--resolver) Error: invalid import "import(mod)". It cannot be statically analyzed. Variable dynamic imports must start with ./ and be limited to a specific directory. For example: import(./foo/${bar}.js).
node_modules/web-worker/cjs/node.js
Error: invalid import "import(mod)". It cannot be statically analyzed. Variable dynamic imports must start with ./ and be limited to a specific directory. For example: import(./foo/${bar}.js).
at dynamicImportToGlob (/node_modules/@rollup/plugin-dynamic-import-vars/dist/index.js:103:11)

Your solution to label set it to browser:true fixed it for me @developit. I had a suspicion that something like that would be the solution, but I wasn't quite sure where the setting was to disable processing of dependencies. Thanks!

@adjenks
Copy link

adjenks commented Sep 2, 2022

Maybe not, I thought it fixed it but now the code builds but doesn't resolve modules correctly in the browser :(

Addendum: In the end I rolled back the "openlayers" library to a version that doesn't use the "web-workers" library at all. That's my solution for now.

@adjenks
Copy link

adjenks commented May 18, 2023

I still have to lock openlayers to an old version to work around this.

@adjenks
Copy link

adjenks commented May 18, 2023

Okay, I fixed it by adding this to my rollup config plugins section:

		commonjs({
			dynamicRequireTargets: ['**/node_modules/web-worker/cjs/node.js']
		}),

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

6 participants