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

Conversation

gorakong
Copy link
Member

@gorakong gorakong commented Feb 26, 2022

↪️ Pull Request

In getSymbolResolution, unused modules can end up in an asset's hoisted requires. A parcelRequire is then inserted for the unused module, and results in a Cannot find module runtime error (see example below).

Proposed fix is to check that an asset's exportSymbols are actually used before inserting a parcelRequire for it.

Tested on a large app.

💻 Examples

$parcel$export(module.exports, "foo", () => (parcelRequire("jDXqh")).foo);

var $jDXqh = parcelRequire("jDXqh");
var $5oNdt = parcelRequire("5oNdt");                      // 5oNdt isn't registered anywhere
eval('');

});
parcelRequire.register("jDXqh", function(module, exports) {

$parcel$export(module.exports, "foo", () => $e4cfc0f7eb90214f$export$6a5cdcad01c973fa);
const $e4cfc0f7eb90214f$export$6a5cdcad01c973fa = 'foo';

});

✔️ PR Todo

  • Added/updated unit tests for this change
  • Filled out test instructions (In case there aren't any unit tests)

@height
Copy link

height bot commented Feb 26, 2022

Link Height tasks by mentioning a task ID in the pull request title or commit messages, or description and comments with the keyword link (e.g. "Link T-123").

💡Tip: You can also use "Close T-X" to automatically close a task when the pull request is merged.

@parcel-benchmark
Copy link

parcel-benchmark commented Feb 26, 2022

Benchmark Results

Kitchen Sink 🚨

Timings

Description Time Difference
Cold FAILED -0.00ms
Cached FAILED -0.00ms

Cold Bundles

No bundles found, this is probably a failed build...

Cached Bundles

No bundles found, this is probably a failed build...

React HackerNews ✅

Timings

Description Time Difference
Cold 9.32s +17.00ms
Cached 464.00ms -17.00ms

Cold Bundles

No bundle changes detected.

Cached Bundles

No bundle changes detected.

AtlasKit Editor 🚨

Timings

Description Time Difference
Cold FAILED -0.00ms
Cached FAILED -0.00ms

Cold Bundles

No bundles found, this is probably a failed build...

Cached Bundles

No bundles found, this is probably a failed build...

Three.js ✅

Timings

Description Time Difference
Cold 6.58s -115.00ms
Cached 274.00ms +12.00ms

Cold Bundles

No bundle changes detected.

Cached Bundles

No bundle changes detected.

Click here to view a detailed benchmark overview.

@gorakong gorakong marked this pull request as ready for review March 1, 2022 22:56
@wbinnssmith
Copy link
Contributor

The test looks great, but I'll defer to @devongovett or @mischnic to have a look at the scope hoisting logic.

@wbinnssmith
Copy link
Contributor

What's the state of this PR?

@mischnic
Copy link
Member

mischnic commented Apr 14, 2022

So just checking resolvedAssetUsedSymbols.has(exportSymbol) would also fix the integration test. (And that is also what I would have assumed to work). Can you add tests (or extend the existing one by adding more library folders) so that these conditions are also convered? (Related to #7764 (comment)) Ah, that breaks existing tests. I see...

@mischnic
Copy link
Member

This very long condition made me suspicious and indeed the following version passes all test (you probably want to test this in your app though).
The new condition also better corresponds to the cases where it shouldn't insert the statement, which is when no parcelRequire.register was generated (= skipped in this bundle and also not imported from another bundle)

  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();
      this.hoistedRequires.set(dep.id, hoisted);
    }

    hoisted.set(
      resolvedAsset.id,
      `var $${publicId} = parcelRequire(${JSON.stringify(publicId)});`,
    );
  }

@gorakong
Copy link
Member Author

@devongovett updated with @mischnic's suggestion and tested on a large app -- everything looks good 👍

@devongovett devongovett merged commit 2f39f29 into v2 Apr 21, 2022
@devongovett devongovett deleted the gkong/unused-module branch April 21, 2022 01:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants