Uh oh!
There was an error while loading. Please reload this page.
Fix/remove node globals - #435
Conversation
hugomrdias
commented
Apr 27, 2020
@vweevers i used |
vweevers
commented
Apr 27, 2020
At fist glance, |
mcollina
left a comment
There was a problem hiding this comment.
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.
hugomrdias
commented
Apr 28, 2020
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. |
| if (!state.readingMore) { | ||
| state.readingMore = true; | ||
| process.nextTick(maybeReadMore_, stream, state); | ||
| nextTick(maybeReadMore_, stream, state); |
There was a problem hiding this comment.
This should not work with the polyfill. I think our browser testing is adding process.nextTick.
There was a problem hiding this comment.
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.
Yes exactly. However if those transform are not put in there, it won't work.
hugomrdias
commented
Apr 28, 2020
@mcollina can you have a look at the warning i wrote in the PR description |
mcollina
commented
Apr 28, 2020
I agree, there is something weird going on. @vweevers do you think we can disable node globals with airtap? |
vweevers
commented
Apr 28, 2020
@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 |
vweevers
commented
Apr 28, 2020
@mcollina We can specify browserify options via browserify:
options:
detectGlobals: falseNot sure if you mean globals (like |
hugomrdias
commented
Apr 29, 2020
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. |
mcollina
commented
Apr 29, 2020
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. |
vweevers
commented
Apr 29, 2020
This should do the trick: browserify:
- options:
detectGlobals: falsebuiltins: false |
even if the tests code can run without node builtins how would tape run ? builtins: false gives me this Uncaught Error: Cannotfindmodule'_process'ato(_prelude.js:1)at_prelude.js:1atObject./Users/hugomrdias/code/readable-stream/node_modules/tape/index.js../lib/default_stream(index.js:151)ato(_prelude.js:1)at_prelude.js:1atObject.<anonymous>(browser.js:13)atObject./Users/hugomrdias/code/readable-stream/test/browser.js../browser/test-stream-big-packet(browser.js:82)ato(_prelude.js:1)atr(_prelude.js:1)at_prelude.js:1 |
mcollina
commented
Apr 30, 2020
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 |
vweevers
commented
May 10, 2020
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. |
vweevers
commented
May 11, 2020
@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. |
mcollina
commented
May 11, 2020
Is there a flag or plugin that can be passed to webpack to do this? |
ljharb
commented
May 11, 2020
@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: |
vweevers
commented
May 12, 2020
I see a couple of options:
|
ljharb
commented
May 12, 2020
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. |
vweevers
commented
May 12, 2020
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 |
ljharb
commented
May 12, 2020
@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. |
vweevers
commented
May 12, 2020
@hugomrdias Could you try that out? https://webpack.js.org/plugins/provide-plugin/ |
mcollina
commented
May 27, 2020
calvinmetcalf
commented
Jun 8, 2020
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#435closesipfs/js-ipfs#2924 BREAKING CHANGE: browser code will NOT have node globals and builtins available.
|
martinheidegger
commented
Jun 23, 2020
As |
ljharb
commented
Jun 23, 2020
I would urge this package to also retain support for older nodes. |
martinheidegger
commented
Jun 23, 2020
@ljharb If I was able to make it support |
ljharb
commented
Jun 23, 2020
@martinheidegger i mean, concat-stream could still be changed to make that possible :-) but really the PRs should be sent to webpack. |
martinheidegger
commented
Jun 23, 2020
@ljharb Personally I am in favor of webpack enforcing a |
kelly-tock
commented
Jan 5, 2021
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. |
reintroducing
commented
Feb 26, 2022
This package is a dependency of microphone-stream which is not working when using Vite. I believe this is cause by the use of |
ljharb
commented
Feb 26, 2022
@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 |
reintroducing
commented
Feb 27, 2022
@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). |
ljharb
commented
Feb 27, 2022
I would suggest avoiding tools that fail to interoperate with the existing, decade-old ecosystem and conventions. |
targos
commented
Feb 27, 2022
I've been using this in my Vite config to work around a module that uses |
reintroducing
commented
Feb 27, 2022
@targos amazingly that worked...! is it really that simple? I feel like it can't be, but if it is, im kind of shocked. |
reintroducing
commented
Feb 27, 2022
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, importNodeModulesPolyfillsfrom'@esbuild-plugins/node-modules-polyfill';
optimizeDeps: {esbuildOptions: {plugins: [NodeModulesPolyfills()]}}I tried adding to that the following: importGlobalsPolyfillsfrom'@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. |
reintroducing
commented
Feb 27, 2022
was finally able to get the production build working (with global and buffer) using the following config: importnodePolyfillsfrom'rollup-plugin-polyfill-node';importNodeModulesPolyfillsfrom'@esbuild-plugins/node-modules-polyfill';resolve: {alias: {// don't need to add this to deps, it's included by @esbuild-plugins/node-modules-polyfillbuffer: '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': falseor 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.