rollup/rollup

Module execution order can be incorrect when having multiple entrypoints

sapphi-red opened this issue · 5 comments

Rollup Version

4.12.1

Operating System (or Browser)

StackBlitz / REPL

Node Version (if applicable)

No response

Link To Reproduction

Expected Behaviour

// main.js
import './1-hash.js'
import './2-hash.js'
import './3-hash.js'

// sub.js
import './1-hash.js'
import './3-hash.js'

// 1-hash.js
console.log('1');

// 2-hash.js
console.log('2');

// 3-hash.js
console.log('3');

Actual Behaviour

// main.js
import './3-hash.js'

console.log('2');

// sub.js
import './3-hash.js'

// 3-hash.js
console.log('1');

console.log('3');

Additional Information

Maybe this is a duplicate of #3888. I made a different issue as this might be easier to fix than the repro in #3888. Feel free to close this one if it's better to be merged in to #3888.

Yes, I am aware of this and it is indeed a duplicate. One problem is that fixing this would involve creating quite a few more chunks, and we already have a lot of chunks in complicated projects. So I wonder if this "strict execution order" should be an opt-in or an opt-out for users via an option. What would you think?
Another thought would be to track which modules have side effects and only ensure correct execution order between modules with side effects. Then maybe we could just enable it for everyone and hope that people do not have too many side effects in their modules.

In Vite, we transform each HTML into a module with <link rel=stylesheet href=/foo.css /> transformed to import './foo.css'. We set moduleSideEffects: 'no-treeshake' to CSS files and HTML files. Then, we inject the CSS tag rely on the generated chunk order. So in this specific case, opt-in for each module would work for Vite.

I guess in most cases, users would want to apply it only to some modules like modules injecting polyfills.

For polyfills, there is usually not a problem if you ensure that all static entries first import the polyfill. See also https://rollupjs.org/faqs/#how-do-i-add-polyfills-to-a-rollup-bundle . But it would still be nice to fix this properly.

Ah, that's true.

If rollup goes with the latter way (= track which modules have side effects), Vite would need to inject a dummy statement that has side effects and remove that later in renderChunk. So it'd be nice if there's a way to opt-in per module even with the latter way.


Maybe starting with an new option that opt-in the behavior is good. Then, I guess we can know how much the number of chunks will increase. Also we can try if experimentalMinChunkSize can eliminate the increase.

Setting moduleSideEffects: 'no-treeshake' is always considered one hell of a side effect, so those would be properly ordered 😉