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

Polyfill common Node.js packages for webpack 5 #15948

Closed
2 tasks done
Timer opened this issue Aug 6, 2020 · 8 comments · Fixed by #16022
Closed
2 tasks done

Polyfill common Node.js packages for webpack 5 #15948

Timer opened this issue Aug 6, 2020 · 8 comments · Fixed by #16022
Assignees
Milestone

Comments

@Timer
Copy link
Member

Timer commented Aug 6, 2020

Webpack 5 stopped polyfilling Node.js packages, however, their usage is prolific throughout the npm ecosystem.

We should provide default polyfills for the most common usages:

@Timer Timer mentioned this issue Aug 6, 2020
13 tasks
@Timer Timer modified the milestones: iteration 7, iteration 6 Aug 6, 2020
@armandabric
Copy link
Contributor

I'm giving a try to Webpack 5 in my project and the build in failing because of the crypto polyfill missing:

ModuleNotFoundError: Module not found: Error: Can't resolve 'crypto' in '/app/node_modules/crypto-random-string'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.

If you want to include a polyfill, you need to:
	- add an alias 'resolve.alias: { "crypto": "crypto-browserify" }'
	- install 'crypto-browserify'
If you don't want to include a polyfill, you can use an empty module like this:
	resolve.alias: { "crypto": false }

Should it be add in the default polyfill? If not, is there a workaround without having to touch the webpack configuration?

@timneutkens
Copy link
Member

So overall we're going to automatically polyfill these cases still to provide backwards compat, however you'll really want to figure out where it comes from and get rid of the dependency given that it hogs in around 300kb of JS.

@ljosberinn
Copy link
Contributor

Would it be possible to add build warnings for polyfilled modules?

Right now, when polyfills are missing, it will just crash and tell you on which specific Node module its bailing. Good time to find out where you're pulling in something potentially unwanted, I learnt that the hard way, but it was very helpful info.

However, I fear in the future, if Next silently polyfills it, we end up in basically the same situation as now: manually having to check individual packages or worse, not ever even thinking about it because it "just works".

Relevant discussion here: #15999

@timneutkens
Copy link
Member

Would it be possible to add build warnings for polyfilled modules?

Right now, when polyfills are missing, it will just crash and tell you on which specific Node module its bailing. Good time to find out where you're pulling in something potentially unwanted, I learnt that the hard way, but it was very helpful info.

However, I fear in the future, if Next silently polyfills it, we end up in basically the same situation as now: manually having to check individual packages or worse, not ever even thinking about it because it "just works".

Relevant discussion here: #15999

That's the plan yeah. Initially I'm just going to add the polyfills so that backwards compat is preserved.

@kodiakhq kodiakhq bot closed this as completed in #16022 Aug 10, 2020
kodiakhq bot pushed a commit that referenced this issue Aug 10, 2020
…#16022)

This adds the following Node.js core polyfills only when the import is used:

- `path`
- `stream`
- `vm`
- `crypto`
- `buffer`

Fixes #15948

We'll have a separate issue about adding warnings for the usage of these modules in the browser, some polyfills like crypto are quite heavy and generally not needed for most applications (included accidentally through node_modules).
@jerome-diver
Copy link

Warning message has wrong syntax. If user is not expert (and then new) with Webpack, this will push him to loose time to this kind of details neglected. I know it is difficult for expert to explain themself the easy way by choose appropriate linked existing words and syntax, but it is something good for the project if the doc and warning message are clearer as possible.
thank you to care about that.

@ghost

This comment has been minimized.

@uladkasach
Copy link

uladkasach commented Dec 21, 2020

Any tips on a good way to track down which module may be pulling in the heavy crypto polyfill? I'm facing tracking that down now

@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants