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

Do not wrap duplicated assets when they are in different targets #9348

Merged
merged 1 commit into from
Oct 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably have a target.id at some point, but this is how the default bundler does it already.

).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',
);
}
});
}
});