Skip to content

Commit

Permalink
P2X-1023 exclude inline assests from PRL (#9194)
Browse files Browse the repository at this point in the history
* Remove inline assets from PRL count

* Add test case for inline assets not counting toward PRL

* Remove graph logging statements

* Clean up changes

* Refactor and simplify test case

* Add invariant statement to prevent flow from complaining
  • Loading branch information
irismoini authored Aug 17, 2023
1 parent 5478f4a commit 5d54a4f
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/bundlers/default/src/DefaultBundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -1044,7 +1044,15 @@ function createIdealGraph(

// We should include "bundle reuse" as shared bundles that may be removed but the bundle itself would have to be retained
let bundleIdsInGroup = getBundlesForBundleGroup(bundleId); //get all bundlegrups this bundle is an ancestor of
if (bundleIdsInGroup.length > config.maxParallelRequests) {

// Filter out inline assests as they should not contribute to PRL
let numBundlesContributingToPRL = bundleIdsInGroup.reduce((count, b) => {
let bundle = nullthrows(bundleGraph.getNode(b));
invariant(bundle !== 'root');
return count + (bundle.bundleBehavior !== 'inline');
}, 0);

if (numBundlesContributingToPRL > config.maxParallelRequests) {
let sharedBundleIdsInBundleGroup = bundleIdsInGroup.filter(b => {
let bundle = nullthrows(bundleGraph.getNode(b));
// shared bundles must have source bundles, we could have a bundle
Expand All @@ -1054,7 +1062,6 @@ function createIdealGraph(
);
});

let numBundlesInGroup = bundleIdsInGroup.length;
// Sort the bundles so the smallest ones are removed first.
let sharedBundlesInGroup = sharedBundleIdsInBundleGroup
.map(id => ({
Expand All @@ -1071,7 +1078,7 @@ function createIdealGraph(
// Remove bundles until the bundle group is within the parallel request limit.
while (
sharedBundlesInGroup.length > 0 &&
numBundlesInGroup > config.maxParallelRequests
numBundlesContributingToPRL > config.maxParallelRequests
) {
let bundleTuple = sharedBundlesInGroup.pop();
let bundleToRemove = bundleTuple.bundle;
Expand Down Expand Up @@ -1122,7 +1129,7 @@ function createIdealGraph(
bundleGraph.removeEdge(sourceBundleId, bundleIdToRemove);
}
}
numBundlesInGroup--;
numBundlesContributingToPRL--;
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions packages/core/integration-tests/test/bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,40 @@ import assert from 'assert';
import {bundle, assertBundles, findAsset} from '@parcel/test-utils';

describe('bundler', function () {
it('should not count inline assests towards parallel request limit', async function () {
// Shared bundle should not be removed in this case
let b = await bundle(
path.join(__dirname, 'integration/inlined-assests/local.html'),
{
mode: 'production',
defaultTargetOptions: {
shouldScopeHoist: false,
},
},
);

assertBundles(b, [
{
assets: ['local.html'],
},
{
assets: ['buzz.js'],
},
{
assets: [
'inline-module.js',
'local.html',
'bundle-url.js',
'cacheLoader.js',
'js-loader.js',
],
},
{
assets: ['esmodule-helpers.js'],
},
]);
});

it('should not create a shared bundle from an asset if that asset is shared by less than minBundles bundles', async function () {
let b = await bundle(
path.join(__dirname, 'integration/min-bundles/index.js'),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export default 7;
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import('./buzz');

export default 10;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!doctype html>
<html>
<body>
<script type="module">
import './inline-module';
</script>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"@parcel/bundler-default": {
"minBundles": 1,
"minBundleSize": 200,
"maxParallelRequests": 2
}
}
Empty file.

0 comments on commit 5d54a4f

Please sign in to comment.