Skip to content

Commit

Permalink
Do not wrap duplicated assets when they are in different targets (#9348)
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett authored Oct 29, 2023
1 parent e0909fe commit c45c296
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 2 deletions.
8 changes: 6 additions & 2 deletions packages/core/core/src/BundleGraph.js
Original file line number Diff line number Diff line change
Expand Up @@ -1122,8 +1122,12 @@ export default class BundleGraph {
}

isAssetReferenced(bundle: Bundle, asset: Asset): boolean {
// If the asset is available in multiple bundles, it's referenced.
if (this.getBundlesWithAsset(asset).length > 1) {
// If the asset is available in multiple bundles in the same target, it's referenced.
if (
this.getBundlesWithAsset(asset).filter(
b => b.target.distDir === bundle.target.distDir,
).length > 1
) {
return true;
}

Expand Down
42 changes: 42 additions & 0 deletions packages/core/integration-tests/test/javascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -7551,5 +7551,47 @@ describe('javascript', function () {

assert.equal(await result.output, 2);
});

it('should not wrap assets that are duplicated in different targets', async function () {
const dir = path.join(__dirname, 'multi-target-duplicates');
overlayFS.mkdirp(dir);

await fsFixture(overlayFS, dir)`
shared/index.js:
export default 2;
packages/a/package.json:
{
"source": "index.js",
"module": "dist/module.js"
}
packages/a/index.js:
import shared from '../../shared';
export default shared + 2;
packages/b/package.json:
{
"source": "index.js",
"module": "dist/module.js"
}
packages/b/index.js:
import shared from '../../shared';
export default shared + 2;
`;

let b = await bundle(path.join(dir, '/packages/*'), {
inputFS: overlayFS,
});

for (let bundle of b.getBundles()) {
let contents = await outputFS.readFile(bundle.filePath, 'utf8');
assert(
!contents.includes('parcelRequire'),
'should not include parcelRequire',
);
}
});
}
});

0 comments on commit c45c296

Please sign in to comment.