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

Parcel 2: mainFields ordering leads to duplicated dependencies in bundle #4879

Closed
speigg opened this issue Jul 12, 2020 · 4 comments · Fixed by #6470
Closed

Parcel 2: mainFields ordering leads to duplicated dependencies in bundle #4879

speigg opened this issue Jul 12, 2020 · 4 comments · Fixed by #6470
Labels
💬 RFC Request For Comments

Comments

@speigg
Copy link

speigg commented Jul 12, 2020

🐛 bug report

When running parcel serve, by default the mainFields order (unexpectedly) appears to be:

["source", "browser", "main", "module"]

This ordering can lead to broken builds when importing standalone esm modules from packages that also include main and module entries. For example, the three package defines main entry as "/build/three.js" and module entry as "/build/three.module.js", whereas the standalone modules in "three/examples/jsm/**/*" import from`three.module.js".

// wrongly adds 'three/build/three.js' instead of  'three/build/three.module.js'
import * as THREE from 'three';

// adds 'three/build/three.module.js'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js';

So when the main entry is prioritized over module, both entries (one bundled and the other unbundled) end up being included in the same application. These duplicated dependencies can cause subtle and very hard to track down problems due to (for example) duplicate singleton instances being referenced (each with their own state) in different places as if they were the same instance. I would expect that only build/three.module.js is included.

More so, due to an inconsistency in behavior related to the scopeHoist option between parcel serve and parcel build, this problem only occurs when running parcel serve.
When running parcel build, the scopeHoist option is enabled which changes the mainFields order to the (more reasonable/expected):

["source", "browser", "module", "main"]

I would expect to have module preferred over main in all cases (by default), which would enable nested esm imports in threejs to work consistently.

@speigg speigg changed the title Parcel 2: Inconsistent and unexpected mainFields ordering leads to broken imports Parcel 2: Inconsistent and unexpected mainFields ordering leads to broken build Jul 12, 2020
@mischnic
Copy link
Member

in all cases (by default

You have probably seen the comment in the sourcecode?

// If scope hoisting is enabled, we can get smaller builds using esmodule input, so choose `module` over `main`.
// Otherwise, we'd be wasting time transforming esmodules to commonjs, so choose `main` over `module`.

@speigg
Copy link
Author

speigg commented Jul 14, 2020

@mischnic right, that's what I'm referring to when I say that the current behavior is to choose main over module when running parcel serve (since parce serve does not enable scope hoisting). I would expect the default behavior to be consistently that module always has priority over main whether running parcel serve or parcel build.

@mischnic mischnic added the 💬 RFC Request For Comments label Jul 27, 2020
@speigg speigg changed the title Parcel 2: Inconsistent and unexpected mainFields ordering leads to broken build Parcel 2: mainFields ordering leads to duplicated dependencies in bundle Aug 25, 2020
@github-actions github-actions bot added the Stale Inactive issues label Feb 21, 2021
@speigg
Copy link
Author

speigg commented Feb 23, 2021

bump

@github-actions github-actions bot removed the Stale Inactive issues label Feb 23, 2021
@wbinnssmith
Copy link
Contributor

@devongovett @mischnic maybe we should discuss this now that transformation is faster? Should probably back it up with performance metrics.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💬 RFC Request For Comments
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants