-
Notifications
You must be signed in to change notification settings - Fork 231
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
Fix/remove node globals #435
Conversation
@vweevers i used |
At fist glance, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the past we used https://github.com/calvinmetcalf/process-nextick-args instead. The reason we dropped was to remove a dependency (because there is a phobia of having too many dependencies).
next-tick
would not work for this module because it does not support positional arguments.
This module uses process.nextTick the goal of this PR is to NOT use process.nextTick. Btw where are nextTick positional arguments used? If this is really the case maybe we need to add a test for that because everything is green now. |
@@ -558,7 +560,7 @@ function emitReadable_(stream) { | |||
function maybeReadMore(stream, state) { | |||
if (!state.readingMore) { | |||
state.readingMore = true; | |||
process.nextTick(maybeReadMore_, stream, state); | |||
nextTick(maybeReadMore_, stream, state); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should not work with the polyfill. I think our browser testing is adding process.nextTick
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
its just using the first if here https://github.com/medikoo/next-tick/blob/master/index.js#L47-L49 because airtap is injecting everything, its hard to validate this with the current repo setup i will try to fix this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes exactly. However if those transform are not put in there, it won't work.
@mcollina can you have a look at the warning i wrote in the PR description |
I agree, there is something weird going on. @vweevers do you think we can disable node globals with airtap? |
@hugomrdias Are you sure that this line is called by any browser test?
If so, what is the actual value of readable-stream/lib/internal/streams/buffer_list.js Lines 18 to 19 in 7eded7b
|
@mcollina We can specify browserify options via browserify:
options:
detectGlobals: false Not sure if you mean globals (like |
yeah i did some more testing and this line is never called in the browser tests
and yes the |
it would be everything so we can be sure this works with webpack 5 or rollup without node plugin, etc. im not seeing a way to test this properly with the current setup, i can only validate this in ipfs/libp2p tests (where i can disable node stuff for the full test bundle) and report back any issue i find. |
This is not correct, we run different tests in the browser: https://github.com/nodejs/readable-stream/tree/master/test/browser. Also this is important readable-stream/test/browser.js Lines 1 to 28 in 7eded7b
I think we should update the browser test environment so that we do not rely on node core stuff. |
This should do the trick: browserify:
- options:
detectGlobals: false
builtins: false |
even if the tests code can run without node builtins how would tape run ? builtins: false gives me this Uncaught Error: Cannot find module '_process'
at o (_prelude.js:1)
at _prelude.js:1
at Object./Users/hugomrdias/code/readable-stream/node_modules/tape/index.js../lib/default_stream (index.js:151)
at o (_prelude.js:1)
at _prelude.js:1
at Object.<anonymous> (browser.js:13)
at Object./Users/hugomrdias/code/readable-stream/test/browser.js../browser/test-stream-big-packet (browser.js:82)
at o (_prelude.js:1)
at r (_prelude.js:1)
at _prelude.js:1 |
Ok, this is getting complicated. Before supporting this use case, we'd need a way to automatically verify this works at a very high level. I'm happy to switch frontend framework if it helps |
Alternatively, fix |
Bundlers that don’t inject node globals when bundling node modules are broken. The fix is to fix or replace your bundler, not attempt to add complexity to every module in the ecosystem. |
@ljharb That's a discussion to take up with Webpack. I can kinda see their point but it's putting a strain on the ecosystem and its maintainers. Wish I had the time to engage Webpack folks. |
Is there a flag or plugin that can be passed to webpack to do this? |
@vweevers those who it's putting a strain on, are who should be taking up that discussion. |
@mcollina There's no flag, because webpack 5 removes it: |
I see a couple of options:
|
Webpack users will already likely stay on v4, since v5's choices break the ecosystem. I think the stance that "a bundler for X to environment Y should work for any code written for X that can possibly work in environment Y" isn't an unreasonable one to take. |
I agree that's a reasonable stance. If we take it, I do believe further action is required. Because this PR and many others prove that "webpack users will already likely stay on v4" isn't true; webpack is actively encouraging folks to update modules like |
@vweevers i had some discussion on the webpack slack, and it looks like https://github.com/webpack/changelog-v5#automatic-nodejs-polyfills-removed tells you how to restore this behavior using the ProvidePlugin, without needing to alter any part of the ecosystem. |
@hugomrdias Could you try that out? https://webpack.js.org/plugins/provide-plugin/ |
if we want a concrete example of the problems of using different microtask shims (ironically, all written by me) see this issue defunctzombie/node-process#88 |
This PR changes webpack config to not include node globals and built-ins by default. For now theres still a --node flag to allow node globals if needed. `process` is still includes because `readable-stream` will only remove it in v4 nodejs/readable-stream#435 BREAKING CHANGE: browser code will NOT have node globals and builtins available.
This PR changes webpack config to not include node globals and built-ins by default. For now theres still a --node flag to allow node globals if needed. `process` is still includes because `readable-stream` will only remove it in v4 nodejs/readable-stream#435 closes ipfs/js-ipfs#2924 BREAKING CHANGE: browser code will NOT have node globals and builtins available.
|
As |
I would urge this package to also retain support for older nodes. |
@ljharb If I was able to make it support |
@martinheidegger i mean, concat-stream could still be changed to make that possible :-) but really the PRs should be sent to webpack. |
@ljharb Personally I am in favor of webpack enforcing a |
is there any update on this? seems like it would fix quite a few issues with webpack 5. |
Any non-node env will fail when stream is piped couple of times, like var _require2 = __webpack_require__(/*! util */ "?1dff"),
inspect = _require2.inspect; /***/ "?1dff":
/*!**********************!*\
!*** util (ignored) ***!
\**********************/
/***/ (() => {
/* (ignored) */
/***/ }), P.S. will fail in any other bundler I know, btw. |
This package is a dependency of microphone-stream which is not working when using Vite. I believe this is cause by the use of |
@reintroducing the proper vix remains that vite, webpack 5, and whoever else has made this incorrect choice but claims to be able to bundle node modules, needs to provide, or transform Vite (or anyone else) could very easily transform every |
@ljharb I've read the exchanges between you and others and engineers on the Vite team in various GH issues over the past few days while trying to find a way around this. it's an unfortunate reality that we are currently in but nonetheless a reality that exists. that being said, i cannot find a way to do this in Vite, nor have I found any sort of polyfill for it, so am I basically left with the reality that I either use webpack 4 with my codebase (that would be very unfortunate and not really an option for us realistically) or continue to use webpack 5/Vite and find a different way to achieve what I'm after in my particular code (basically have to find a replacement for microphone-stream)? I have been searching but can't find a way to polyfill this in vite (and realistically how to do it in webpack 5 is also not something I've come across successfully). |
I would suggest avoiding tools that fail to interoperate with the existing, decade-old ecosystem and conventions. |
I've been using this in my Vite config to work around a module that uses
|
@targos amazingly that worked...! is it really that simple? I feel like it can't be, but if it is, im kind of shocked. |
Lol, so after getting that working, I got to my next roadblock, which, in a production build of Vite, buffer.from is not available (as a result of another dependency of readable-stream, import NodeModulesPolyfills from '@esbuild-plugins/node-modules-polyfill';
optimizeDeps: {
esbuildOptions: {
plugins: [NodeModulesPolyfills()]
}
} I tried adding to that the following: import GlobalsPolyfills from '@esbuild-plugins/node-globals-polyfill'
optimizeDeps: {
esbuildOptions: {
plugins: [
GlobalsPolyfills({
buffer: true,
}),
NodeModulesPolyfills(),
],
},
}, but of course that didn't work. I'm just documenting all of this here in case someone else lands on this in a google search eventually and is facing these issues to save them some time. I feel like this is going to be a fun little game of cat and mouse until I can get the production build working for all dependencies. |
was finally able to get the production build working (with global and buffer) using the following config: import nodePolyfills from 'rollup-plugin-polyfill-node';
import NodeModulesPolyfills from '@esbuild-plugins/node-modules-polyfill';
resolve: {
alias: {
// don't need to add this to deps, it's included by @esbuild-plugins/node-modules-polyfill
buffer: 'rollup-plugin-node-polyfills/polyfills/buffer-es6',
},
},
optimizeDeps: {
esbuildOptions: {
plugins: [
NodeModulesPolyfills(),
],
},
},
build: {
rollupOptions: {
plugins: [nodePolyfills()],
},
}, While this works, i have absolutely ZERO confidence in it in not only its current state but when updates to Vite (or any of these plugins) are made in the future. |
This PR removes usage of node globals and adds buffer, events and next-tick to the dependencies. This will allow readable-stream to works with bundlers that don't inject node globals.
Some code is borrowed from this PR #414
Warning:
These lines should make the browsers tests fail
https://github.com/nodejs/readable-stream/blob/master/lib/internal/streams/buffer_list.js#L18-L19
https://github.com/nodejs/readable-stream/blob/master/lib/internal/streams/buffer_list.js#L200
because of this
https://github.com/nodejs/readable-stream/blob/master/package.json#L55
but they don't, so either airtap ignores
'util': false
or something else is happening that i didn’t catch.I need some feedback on how you guys would like to fix this, maybe use https://www.npmjs.com/package/browser-util-inspect or just mock inspect.