-
-
Notifications
You must be signed in to change notification settings - Fork 72
Require node Buffer global to allow use with Webpack 5 #130
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
Conversation
Webpack 5 removed automatic support for node polyfills so you now can't access the node `Buffer` global in the browser any more - you have to `require` it first. I've added Feross' [Buffer polyfill](https://www.npmjs.com/package/buffer) as a dependency and `require`d it in the non-cli/test files that use Buffer directly. If running on node you'll get node's Buffer, if it's the browser you get the polyfill. It's not worth replacing `Buffer` with `Uint8Array`s entirely because there's no equivalent API to [Buffer.allocUnsafe](https://nodejs.org/api/buffer.html#buffer_static_method_buffer_allocunsafe_size) so it'd be a big performance hit.
|
I think we need In theory, I'm ok with there being some overhead for node users so that web users have a better experience, but let's make sure we've got a full solution and then see what that overhead is. |
|
Fair point about |
|
Also, I'd really like node users to get the native node versions, not the polyfills. I'd also like to think about testing before merging something here. Do we need to test on different browser versions? How do we merge coverage data? |
|
Actually I think this module is ok on
If you're on node, |
If you're on node there's no change so there's not much to test. If you're in the browser you were using buffer via the webpack polyfill before v5 anyway.. |
|
I've looked at this some more, and since I'm using const webpack = require('webpack')
module.exports = {
resolve: {
fallback: {
process: 'process/browser',
stream: 'stream-browserify'
}
},
plugins: [
new webpack.ProvidePlugin({
Buffer: ['buffer', 'Buffer'],
process: 'process/browser'
})
]
}with the following dependencies added:
Given that, I propose that since you're going to need |
|
After thinking about this more, the Buffer part is easy to put in, has no impact on node users, and is one less thing for me to explain to web users. I'll review this patch and probably take it. I wonder what else we can do to reduce the overhead? Is there already a fork of the |
|
I am working up a patch for #131 as well. That's mostly-done, just needs a bit more cleanup. |
|
Apparently, the folks who make the |
|
Update: I've got a version of |
|
See: nodejs/readable-stream#414. nodejs/readable-stream#435, nodejs/readable-stream#450, nodejs/readable-stream#448, etc. We're depending on them because stream-browserify uses them. I think I might have a work-around, but it's going to take several more days. |
|
See https://github.com/hildjj/node-cbor/tree/require-buffer if you want to track my work that integrates this PR. |
|
Okay, after a marathon refactor into a real monorepo, I think I have everything working on #133, which includes this PR. Web folks will now depend on Can I get another set of eyeballs on this, please? At least someone to download the branch, build it, and see if you can get |
|
This got swept up in #133. |
Webpack 5 removed automatic support for node polyfills so you now can't access the node
Bufferglobal in the browser any more - you have torequireit first.I've added Feross' Buffer polyfill as a dependency and
required it in the non-cli/test files that use Buffer directly.If running on node you'll get node's Buffer, if it's the browser you get the polyfill.
It's not worth replacing
BufferwithUint8Arrays entirely because there's no equivalent API to Buffer.allocUnsafe so it'd be a big performance hit.