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

Inlining shared, small modules can yield non-standard behavior #4302

Closed
surma opened this issue Mar 10, 2020 · 4 comments · Fixed by #4786
Closed

Inlining shared, small modules can yield non-standard behavior #4302

surma opened this issue Mar 10, 2020 · 4 comments · Fixed by #4786

Comments

@surma
Copy link

surma commented Mar 10, 2020

🐛 bug report

If you have 2 <script> tags that share a dependency, that dependency will get chunked out if it is above a certain size (is this size threshold documented somewhere?).

However, this can yield non-standard behavior compared to using normal ES modules. For example, exporting an object, all importers will get a reference to the same object. So one function change the object, other importers should see those changes.

🎛 Configuration (.babelrc, package.json, cli command)

Please see repro repo

🤔 Expected Behavior

obj.js should land in its own chunk.

😯 Current Behavior

obj.js is inlined into both component-1.js and component-2.js.

💁 Possible Solution

See “Expected Behavior”.

🔦 Context

E.g: A module exporting a global config object that gets augmented by other modules.

💻 Code Sample

Please see repro repo

🌍 Your Environment

Software Version(s)
Parcel 2.0.0-alpha.3.2
Node v12.8.0
npm/Yarn 6.12.1
Operating System MacOS 10.15.3 (19D76)
@mischnic
Copy link
Member

mischnic commented Mar 10, 2020

The second script tag would have to import that shared module from the first script tag (a mechanism for that exists already).

But sharing local variables between the script tags is still something we wouldn't support (what do you think?)

(How I think we could implement it) I think taking all the script tags as one bundlegroup and each script tag as a bundle would automatically cause that behavior. Currently we completely ignore that fact that these bundles are actually related (so two script tags behave just like two worker calls - which is quite a simplification).

that dependency will get chunked out if it is above a certain size (is this size threshold documented somewhere?).

Here (that plugin handles all the chunking): https://github.com/parcel-bundler/parcel/blob/v2/packages/bundlers/default/src/DefaultBundler.js#L10

@surma
Copy link
Author

surma commented Mar 10, 2020

But sharing local variables between the script tags is still something we wouldn't support (what do you think?)

The reason I bring this up is because it would behave differently with 2 <script type="module"> tags. The parcel output logs 1 1, while the source with native ES modules logs 1 2.

It feels unexpected (and undesirable) to me that behavior would change because I ran parcel.

@mischnic
Copy link
Member

I agree that running Parcel shouldn't change the behaviour of the code.

@devongovett
Copy link
Member

Should be fixed by #4786. Parcel will now deduplicate common modules between multiple script tags.

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

Successfully merging a pull request may close this issue.

3 participants