diff --git a/packages/core/core/src/BundleGraph.js b/packages/core/core/src/BundleGraph.js index a5af4ecf59e..c70b582a977 100644 --- a/packages/core/core/src/BundleGraph.js +++ b/packages/core/core/src/BundleGraph.js @@ -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; } diff --git a/packages/core/integration-tests/test/javascript.js b/packages/core/integration-tests/test/javascript.js index 1c63553f088..fb377b85990 100644 --- a/packages/core/integration-tests/test/javascript.js +++ b/packages/core/integration-tests/test/javascript.js @@ -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', + ); + } + }); } });