-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aeaba41
commit 75182a0
Showing
12 changed files
with
706 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{ | ||
"name": "@parcel/bundler-library", | ||
"version": "2.11.0", | ||
"license": "MIT", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"funding": { | ||
"type": "opencollective", | ||
"url": "https://opencollective.com/parcel" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/parcel-bundler/parcel.git" | ||
}, | ||
"main": "lib/LibraryBundler.js", | ||
"source": "src/LibraryBundler.js", | ||
"engines": { | ||
"node": ">= 12.0.0", | ||
"parcel": "^2.12.0" | ||
}, | ||
"dependencies": { | ||
"@parcel/plugin": "2.12.0", | ||
"nullthrows": "^1.1.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// @flow strict-local | ||
import {Bundler} from '@parcel/plugin'; | ||
import nullthrows from 'nullthrows'; | ||
|
||
// This bundler plugin is designed specifically for library builds. It outputs a bundle for | ||
// each input asset, which ensures that the library can be effectively tree shaken and code | ||
// split by an application bundler. | ||
export default (new Bundler({ | ||
bundle({bundleGraph}) { | ||
// Collect dependencies from the graph. | ||
// We do not want to mutate the graph while traversing, so this must be done first. | ||
let dependencies = []; | ||
bundleGraph.traverse((node, context) => { | ||
if (node.type === 'dependency') { | ||
let dependency = node.value; | ||
if (bundleGraph.isDependencySkipped(dependency)) { | ||
return; | ||
} | ||
dependencies.push([ | ||
dependency, | ||
nullthrows(dependency.target ?? context), | ||
]); | ||
if (dependency.target) { | ||
return dependency.target; | ||
} | ||
} | ||
}); | ||
|
||
// Create bundles for each asset. | ||
let bundles = new Map(); | ||
for (let [dependency, target] of dependencies) { | ||
let assets = bundleGraph.getDependencyAssets(dependency); | ||
if (assets.length === 0) { | ||
continue; | ||
} | ||
|
||
let parentAsset = bundleGraph.getAssetWithDependency(dependency); | ||
let parentBundle; | ||
if (parentAsset) { | ||
let parentKey = getBundleKey(parentAsset, target); | ||
parentBundle = bundles.get(parentKey); | ||
} | ||
let bundleGroup; | ||
|
||
// Create a separate bundle group/bundle for each asset. | ||
for (let asset of assets) { | ||
let key = getBundleKey(asset, target); | ||
let bundle = bundles.get(key); | ||
if (!bundle) { | ||
bundleGroup ??= bundleGraph.createBundleGroup(dependency, target); | ||
bundle = bundleGraph.createBundle({ | ||
entryAsset: asset, | ||
needsStableName: dependency.isEntry, | ||
target, | ||
bundleBehavior: dependency.bundleBehavior ?? asset.bundleBehavior, | ||
}); | ||
bundleGraph.addBundleToBundleGroup(bundle, bundleGroup); | ||
bundles.set(key, bundle); | ||
} | ||
|
||
if (!bundle.hasAsset(asset)) { | ||
bundleGraph.addAssetToBundle(asset, bundle); | ||
} | ||
|
||
// Reference the parent bundle so we create dependencies between them. | ||
if (parentBundle) { | ||
bundleGraph.createBundleReference(parentBundle, bundle); | ||
bundleGraph.createAssetReference(dependency, asset, bundle); | ||
} | ||
} | ||
} | ||
}, | ||
optimize() {}, | ||
}): 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}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/core/integration-tests/test/integration/formats/esm-cjs/a.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
exports.test = true; | ||
exports['foo-bar'] = true; |
Empty file.
Oops, something went wrong.