-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Add library bundler #9489
Merged
Merged
Add library bundler #9489
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
597c63b
WIP: add library bundler
devongovett 8a0305d
fix test
devongovett 0a83e4f
Merge branch 'v2' of github.com:parcel-bundler/parcel into library-bu…
devongovett e3f6450
add more tests and handle more cases
devongovett 2689ed6
prettier broke flow
devongovett dce335e
fix test
devongovett 308ae9a
Merge branch 'v2' into library-bundler
devongovett 1afee47
use string export syntax for invalid identifiers
devongovett 38e266c
Merge branch 'v2' into library-bundler
devongovett e19e12d
fix lint
devongovett f2e0203
Merge branch 'library-bundler' of github.com:parcel-bundler/parcel in…
devongovett 6bfedb3
fix
devongovett a1ed3c1
Merge branch 'v2' into library-bundler
mischnic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changes in this file are to handle the case where a dependency resolves to multiple bundles. In that case, we need to prioritize the one with the same type as the parent bundle. We also prioritize reference edges over bundle group resolution