parcel icon indicating copy to clipboard operation
parcel copied to clipboard

`$parcel$export` ordering prevents variable inlining

Open mischnic opened this issue 3 years ago • 0 comments

🙋 feature request

😯 Current Behavior

The variables at the end cannot be inlined by minifiers, I suspect this is because between the variable use and the declaration, you could somehow invoke the callback (and get a ReferenceError or a TDZ error). Inlining the variables would change the behaviour by not throwing anymore

$parcel$export(module.exports, "A", function () { return A; });
$parcel$export(module.exports, "B", function () { return B; });
const A = "A", B = "B";

Minified:

$parcel$export(module.exports,"A",(function(){return e})),
$parcel$export(module.exports,"B",(function(){return r}));
const e="A",r="B";

🤔 Expected Behavior

We have the current order for a reason (circular imports), but this would inline everything:

const A = "A", B = "B";
$parcel$export(module.exports, "A", function () { return A; });
$parcel$export(module.exports, "B", function () { return B; });

Minified:

$parcel$export(module.exports,"A",(function(){return"A"}));
$parcel$export(module.exports,"B",(function(){return"B"}));

Note that Terser appears to only inline string and number literals. So there this mostly applies to modules containing many export const X = "SOME THING"; declarations

mischnic avatar Aug 05 '22 14:08 mischnic