Skip to content

Commit

Permalink
Allow library bundles to be shared between targets in the same package (
Browse files Browse the repository at this point in the history
  • Loading branch information
devongovett authored Mar 20, 2024
1 parent 9f08a86 commit b6281c7
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 5 deletions.
4 changes: 3 additions & 1 deletion packages/bundlers/library/src/LibraryBundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,7 @@ export default (new Bundler({
function getBundleKey(asset, target) {
// Group by type and file path so CSS generated by macros is combined together by parent JS file.
// Also group by environment/target to ensure bundles cannot be shared between packages.
return `${asset.type}:${asset.filePath}:${asset.env.id}:${target.distDir}`;
return `${asset.type}:${asset.filePath}:${asset.env.id}:${
target.loc?.filePath ?? target.distDir
}`;
}
9 changes: 9 additions & 0 deletions packages/core/core/src/requests/TargetRequest.js
Original file line number Diff line number Diff line change
Expand Up @@ -935,6 +935,15 @@ export class TargetResolver {
if (customTargets.length >= 2) {
distDir = path.join(distDir, targetName);
}
invariant(pkgMap != null);
invariant(typeof pkgFilePath === 'string');
loc = {
filePath: pkgFilePath,
...getJSONSourceLocation(
pkgMap.pointers[`/targets/${targetName}`],
'key',
),
};
} else {
if (typeof distPath !== 'string') {
let contents: string =
Expand Down
56 changes: 52 additions & 4 deletions packages/core/core/test/TargetRequest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,19 @@ describe('TargetResolver', () => {
loc: undefined,
sourceType: 'module',
},
loc: undefined,
loc: {
filePath: relative(
path.join(CUSTOM_TARGETS_DISTDIR_FIXTURE_PATH, 'package.json'),
),
start: {
line: 3,
column: 5,
},
end: {
line: 3,
column: 10,
},
},
},
],
);
Expand Down Expand Up @@ -1317,7 +1329,19 @@ describe('TargetResolver', () => {
loc: undefined,
sourceType: 'module',
},
loc: undefined,
loc: {
filePath: relative(
path.join(DEFAULT_DISTPATH_FIXTURE_PATHS.one, 'package.json'),
),
start: {
line: 3,
column: 5,
},
end: {
line: 3,
column: 20,
},
},
},
],
);
Expand Down Expand Up @@ -1355,7 +1379,19 @@ describe('TargetResolver', () => {
loc: undefined,
sourceType: 'module',
},
loc: undefined,
loc: {
filePath: relative(
path.join(DEFAULT_DISTPATH_FIXTURE_PATHS.two, 'package.json'),
),
start: {
line: 3,
column: 5,
},
end: {
line: 3,
column: 20,
},
},
},
{
name: 'browserLegacy',
Expand Down Expand Up @@ -1383,7 +1419,19 @@ describe('TargetResolver', () => {
loc: undefined,
sourceType: 'module',
},
loc: undefined,
loc: {
filePath: relative(
path.join(DEFAULT_DISTPATH_FIXTURE_PATHS.two, 'package.json'),
),
start: {
line: 10,
column: 5,
},
end: {
line: 10,
column: 20,
},
},
},
],
);
Expand Down
73 changes: 73 additions & 0 deletions packages/core/integration-tests/test/library-bundler.js
Original file line number Diff line number Diff line change
Expand Up @@ -418,4 +418,77 @@ describe('library bundler', function () {
assert.deepEqual(Object.keys(foo), ['default']);
assert.deepEqual(foo.default, {'foo-bar': 'foo'});
});

it('should allow bundles to be reused between targets in the same package', async function () {
await fsFixture(overlayFS, dir)`
yarn.lock:
.parcelrc:
{
"extends": "@parcel/config-default",
"bundler": "@parcel/bundler-library"
}
package.json:
{
"engines": { "node": "*" },
"targets": {
"a": {
"source": "src/a.js",
"distDir": "a",
"outputFormat": "esmodule",
"isLibrary": true
},
"b": {
"source": "src/b.js",
"distDir": "b",
"outputFormat": "esmodule",
"isLibrary": true
}
}
}
src/a.js:
import shared from './shared';
export default shared + '-a';
src/b.js:
import shared from './shared';
export default shared + '-b';
src/shared.js:
export default 'shared';
`;

let b = await bundle(dir, {
inputFS: overlayFS,
mode: 'production',
});

assertBundles(b, [
{
assets: ['a.js'],
},
{
assets: ['b.js'],
},
{
assets: ['shared.js'],
},
]);

let res: any = await runBundle(
b,
nullthrows(b.getBundles().find(b => b.name === 'a.js')),
);

assert.equal(res.default, 'shared-a');

let res2: any = await runBundle(
b,
nullthrows(b.getBundles().find(b => b.name === 'b.js')),
);

assert.equal(res2.default, 'shared-b');
});
});

0 comments on commit b6281c7

Please sign in to comment.