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

tree shaking used in async modules, not found module #2804

Closed
rambo-panda opened this issue Mar 15, 2019 · 5 comments
Closed

tree shaking used in async modules, not found module #2804

rambo-panda opened this issue Mar 15, 2019 · 5 comments

Comments

@rambo-panda
Copy link

rambo-panda commented Mar 15, 2019

🐛 bug report

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

// cli command
{
  production: true,
  scopeHoist: true,
  hmr: false,
  minify: !true,
  cache: false,
  watch: false,
  detailedReport: true,
  target: "browser",
  sourceMaps: false
}

💻 Code Sample

// a.js
const c = () => import('./c.js')
console.log(c);

// b.js
const c = () => import('./c.js')
console.log(c);

// c.js
export default () => {
	console.log(111);
}

// main.js
const a = () => import("./a.js");
const b = () => import("./b.js");

console.log(a);
console.log(b);

😯 Current Behavior

// generated a.js
var $KX6I$var$c = function c() {
  return ($_$init(), $_$exports)([["c.a92336cc.js", "KQUl"], "KQUl"]);
};

// generated b.js
var $gffB$var$c = function c() {
  return ($_$init(), $_$exports)("KQUl");
};

When the js resource is loaded in the browser order main.js b.js, then throw NOT FOUND KQUI module error.

🤔 Expected Behavior

If you are loading modules asynchronously, you should also keep information about their assetsDeps.

💁 Possible Solution

https://github.com/parcel-bundler/parcel/blob/parcel-bundler%401.12.1/packages/core/parcel-bundler/src/scope-hoisting/concat.js#L201

🔦 Context

🌍 Your Environment

Software Version(s)
Parcel 1.12.1
Node 10.15.2
npm/Yarn 5.4.1
Operating System mac 10.14
@mischnic
Copy link
Member

mischnic commented Mar 16, 2019

Happens for me with this:
parcel build index.html --experimental-scope-hoisting

<script src="./index.js"></script>
// index.js
import("./a.js").then(() => {});
import("./b.js").then(() => {});


// a.js
import("./c.js").then(v => console.log("a", v.default));


// b.js
import("./c.js").then(v => console.log("b", v.default));


// c.js
export default 123;

@mischnic
Copy link
Member

I'd say this is once again caused by Parcel's deduplication of assets.
The bundle of a.js does have c.js listed as a childBundle, but b.js doesn't.

@rambo-panda
Copy link
Author

I'd say this is once again caused by Parcel's deduplication of assets.
The bundle of a.js does have c.js listed as a childBundle, but b.js doesn't.

That's it!

if (asset.parentBundle && !bundle.isolated) {
// If the asset is already in a bundle, it is shared. Move it to the lowest common ancestor.
if (asset.parentBundle !== bundle) {
let commonBundle = bundle.findCommonAncestor(asset.parentBundle);
// If the common bundle's type matches the asset's, move the asset to the common bundle.
// Otherwise, proceed with adding the asset to the new bundle below.
if (asset.parentBundle.type === commonBundle.type) {
this.moveAssetToBundle(asset, commonBundle);
return;

@mischnic
Copy link
Member

@devongovett This would require quite a deep change (removing the duplicate assets/bundles only at a later stage). Will this be easier with Parcel 2?

@mischnic
Copy link
Member

mischnic commented Jan 3, 2023

Should be fixed with Parcel 2

@mischnic mischnic closed this as completed Jan 3, 2023
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