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

Scope Hoisting: Don't insert unused requires that aren't registered anywhere #7764

Merged
merged 24 commits into from
Apr 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
61b4334
working repro
gorakong Feb 26, 2022
a7de06a
Merge branch 'v2' into gkong/unused-module
gorakong Feb 26, 2022
d9656ec
Merge branch 'v2' of github.com:parcel-bundler/parcel into gkong/unus…
gorakong Feb 26, 2022
2f50d7e
Merge branch 'v2' into gkong/unused-module
Mar 1, 2022
0f23e46
fix test regressions
gorakong Mar 1, 2022
0dcb66a
Merge branch 'v2' of github.com:parcel-bundler/parcel into gkong/unus…
gorakong Mar 1, 2022
e969630
Merge branch 'gkong/unused-module' of github.com:parcel-bundler/parce…
gorakong Mar 1, 2022
f6b08cf
add comment
gorakong Mar 2, 2022
4fb446d
Merge branch 'v2' into gkong/unused-module
gorakong Mar 10, 2022
e092f85
add extra condition + comments
gorakong Mar 15, 2022
e9baf94
Merge branch 'v2' of github.com:parcel-bundler/parcel into gkong/unus…
gorakong Mar 15, 2022
4d9fdc4
Merge branch 'gkong/unused-module' of github.com:parcel-bundler/parce…
gorakong Mar 15, 2022
d13f685
remove redundant condition
gorakong Mar 23, 2022
82c141e
Merge branch 'v2' of github.com:parcel-bundler/parcel into gkong/unus…
gorakong Mar 24, 2022
74a7da4
clean up comment
gorakong Mar 24, 2022
4dfff12
Merge branch 'v2' into gkong/unused-module
gorakong Mar 29, 2022
1dd7f56
Merge branch 'v2' into gkong/unused-module
Mar 31, 2022
84ff85a
Merge branch 'v2' into gkong/unused-module
gorakong Apr 5, 2022
1c09c52
Merge branch 'v2' into gkong/unused-module
Apr 7, 2022
79540d5
Merge branch 'v2' into gkong/unused-module
mischnic Apr 14, 2022
33d3555
Merge branch 'v2' of github.com:parcel-bundler/parcel into gkong/unus…
gorakong Apr 14, 2022
c0988bb
check instead for when no parcelRequire.register was generated
gorakong Apr 14, 2022
18c6ee7
Merge branch 'v2' of github.com:parcel-bundler/parcel into gkong/unus…
gorakong Apr 21, 2022
1548499
Merge branch 'gkong/unused-module' of github.com:parcel-bundler/parce…
gorakong Apr 21, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {foo} from './library';

output = foo;
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const unusedFoo = 'unusedFoo';
export { foo } from './other';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const foo = 'foo';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"sideEffects": false
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export { foo, unusedFoo } from './foo';

eval('')
12 changes: 12 additions & 0 deletions packages/core/integration-tests/test/scope-hoisting.js
Original file line number Diff line number Diff line change
Expand Up @@ -5336,4 +5336,16 @@ describe('scope hoisting', function () {
let output = await run(b);
assert.strictEqual(output, 'bar foo bar');
});

it("not insert unused requires that aren't registered anywhere", async function () {
let b = await bundle(
path.join(
__dirname,
'/integration/scope-hoisting/es6/unused-require/index.js',
),
);

let output = await run(b);
assert.strictEqual(output, 'foo');
});
});
13 changes: 11 additions & 2 deletions packages/packagers/js/src/ScopeHoistingPackager.js
Original file line number Diff line number Diff line change
Expand Up @@ -760,9 +760,18 @@ ${code}
let staticExports = resolvedAsset.meta.staticExports !== false;
let publicId = this.bundleGraph.getAssetPublicId(resolvedAsset);

// If the rsolved asset is wrapped, but imported at the top-level by this asset,
// If the resolved asset is wrapped, but imported at the top-level by this asset,
// then we hoist parcelRequire calls to the top of this asset so side effects run immediately.
if (isWrapped && dep && !dep?.meta.shouldWrap && symbol !== false) {
if (
isWrapped &&
dep &&
!dep?.meta.shouldWrap &&
symbol !== false &&
// Only do this if the asset is part of a different bundle (so it was definitely
// parcelRequire.register'ed there), or if it is indeed registered in this bundle.
(!this.bundle.hasAsset(resolvedAsset) ||
!this.shouldSkipAsset(resolvedAsset))
) {
let hoisted = this.hoistedRequires.get(dep.id);
if (!hoisted) {
hoisted = new Map();
Expand Down