Skip to content
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

Module execution order can be incorrect when having multiple entrypoints #5423

Open
sapphi-red opened this issue Mar 9, 2024 · 5 comments
Open

Comments

@sapphi-red
Copy link
Contributor

sapphi-red commented Mar 9, 2024

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.

@lukastaegert
Copy link
Member

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.

@sapphi-red
Copy link
Contributor Author

sapphi-red commented Mar 12, 2024

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.

@lukastaegert
Copy link
Member

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.

@sapphi-red
Copy link
Contributor Author

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.

@lukastaegert
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants