Rollupjs: function is being called before definition bundled code?

Viewed 109

When running my bundle, I get this error:

can't access property "call", Readable$1 is undefined

So I looked in the bundled code, and noticed this on line 71481:

function Duplex$2(options) {
  if (!(this instanceof Duplex$2)) return new Duplex$2(options);
  Readable$1.call(this, options);

The on line 71463 (correctly before usage):

var Readable$1 = _stream_readable;

But then on line 72148 (so Readable$1 is actually undefined at this point, which links to the above error):

var _stream_readable = Readable;
/*<replacement>*/

If I manually move this definition to before var Readable$1 = _stream_readable; then it works. My question is, how do I fix rollup mixing up the order?

1 Answers

Ok so it's a known issue with rollup, and readable-stream, as pointed out here: https://github.com/rollup/rollup/issues/4231 and documented in the various issues listed: https://github.com/rollup/rollup/issues/4231#issuecomment-937772340

I managed to solve the issue this way: https://github.com/nodejs/readable-stream/issues/348#issuecomment-937971694, basically:

npm i readable-stream@npm:vite-compatible-readable-stream

The relevant concept is detailed here: https://github.com/nodejs/readable-stream/issues/348#issuecomment-473804926

Related