Skip to content

Commit c45c296

Browse files
authored
Do not wrap duplicated assets when they are in different targets (#9348)
1 parent e0909fe commit c45c296

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

packages/core/core/src/BundleGraph.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1122,8 +1122,12 @@ export default class BundleGraph {
11221122
}
11231123

11241124
isAssetReferenced(bundle: Bundle, asset: Asset): boolean {
1125-
// If the asset is available in multiple bundles, it's referenced.
1126-
if (this.getBundlesWithAsset(asset).length > 1) {
1125+
// If the asset is available in multiple bundles in the same target, it's referenced.
1126+
if (
1127+
this.getBundlesWithAsset(asset).filter(
1128+
b => b.target.distDir === bundle.target.distDir,
1129+
).length > 1
1130+
) {
11271131
return true;
11281132
}
11291133

packages/core/integration-tests/test/javascript.js

+42
Original file line numberDiff line numberDiff line change
@@ -7551,5 +7551,47 @@ describe('javascript', function () {
75517551

75527552
assert.equal(await result.output, 2);
75537553
});
7554+
7555+
it('should not wrap assets that are duplicated in different targets', async function () {
7556+
const dir = path.join(__dirname, 'multi-target-duplicates');
7557+
overlayFS.mkdirp(dir);
7558+
7559+
await fsFixture(overlayFS, dir)`
7560+
shared/index.js:
7561+
export default 2;
7562+
7563+
packages/a/package.json:
7564+
{
7565+
"source": "index.js",
7566+
"module": "dist/module.js"
7567+
}
7568+
7569+
packages/a/index.js:
7570+
import shared from '../../shared';
7571+
export default shared + 2;
7572+
7573+
packages/b/package.json:
7574+
{
7575+
"source": "index.js",
7576+
"module": "dist/module.js"
7577+
}
7578+
7579+
packages/b/index.js:
7580+
import shared from '../../shared';
7581+
export default shared + 2;
7582+
`;
7583+
7584+
let b = await bundle(path.join(dir, '/packages/*'), {
7585+
inputFS: overlayFS,
7586+
});
7587+
7588+
for (let bundle of b.getBundles()) {
7589+
let contents = await outputFS.readFile(bundle.filePath, 'utf8');
7590+
assert(
7591+
!contents.includes('parcelRequire'),
7592+
'should not include parcelRequire',
7593+
);
7594+
}
7595+
});
75547596
}
75557597
});

0 commit comments

Comments
 (0)