-
-
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
874dddf
commit 597c63b
Showing
6 changed files
with
171 additions
and
4 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.11.0" | ||
}, | ||
"dependencies": { | ||
"@parcel/plugin": "2.11.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,41 @@ | ||
// @flow strict-local | ||
import {Bundler} from '@parcel/plugin'; | ||
import nullthrows from 'nullthrows'; | ||
|
||
export default (new Bundler({ | ||
bundle({bundleGraph}) { | ||
let bundles = new Map(); | ||
bundleGraph.traverse((node, context) => { | ||
if (node.type === 'dependency') { | ||
let dependency = node.value; | ||
let parentAsset = bundleGraph.getAssetWithDependency(dependency); | ||
let assets = bundleGraph.getDependencyAssets(dependency); | ||
|
||
// Create a separate bundle group/bundle for each asset. | ||
for (let asset of assets) { | ||
let target = nullthrows(dependency.target ?? context); | ||
let bundleGroup = bundleGraph.createBundleGroup(dependency, target); | ||
let bundle = bundleGraph.createBundle({ | ||
entryAsset: asset, | ||
needsStableName: !parentAsset, | ||
target, | ||
}); | ||
bundleGraph.addAssetToBundle(asset, bundle); | ||
bundleGraph.addBundleToBundleGroup(bundle, bundleGroup); | ||
|
||
// Reference the parent bundle so we create dependencies between them. | ||
let parentBundle = parentAsset && bundles.get(parentAsset); | ||
if (parentBundle) { | ||
bundleGraph.createBundleReference(parentBundle, bundle); | ||
} | ||
bundles.set(asset, bundle); | ||
} | ||
|
||
if (dependency.target) { | ||
return dependency.target; | ||
} | ||
} | ||
}); | ||
}, | ||
optimize() {}, | ||
}): Bundler); |
Empty file.
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,92 @@ | ||
// @flow strict-local | ||
import assert from 'assert'; | ||
import path from 'path'; | ||
import { | ||
bundle, | ||
run, | ||
overlayFS, | ||
outputFS, | ||
fsFixture, | ||
assertBundles, | ||
} from '@parcel/test-utils'; | ||
|
||
describe('library bundler', function () { | ||
let count = 0; | ||
let dir; | ||
beforeEach(async () => { | ||
dir = path.join(__dirname, 'libraries', '' + ++count); | ||
await overlayFS.mkdirp(dir); | ||
}); | ||
|
||
after(async () => { | ||
await overlayFS.rimraf(path.join(__dirname, 'libraries')); | ||
}); | ||
|
||
it('should support named imports', async function () { | ||
await fsFixture(overlayFS, dir)` | ||
yarn.lock: | ||
.parcelrc: | ||
{ | ||
"extends": "@parcel/config-default", | ||
"bundler": "@parcel/bundler-library" | ||
} | ||
package.json: | ||
{ | ||
"module": "dist/out.js" | ||
} | ||
index.js: | ||
export * from './foo'; | ||
export * from './bar'; | ||
foo.js: | ||
import {baz} from './baz'; | ||
export function foo() { | ||
return 'foo' + baz(); | ||
} | ||
bar.js: | ||
import {baz} from './baz'; | ||
export function bar() { | ||
return 'bar' + baz(); | ||
} | ||
baz.js: | ||
export function baz() { | ||
return 'baz'; | ||
} | ||
`; | ||
|
||
let b = await bundle(path.join(dir, '/index.js'), { | ||
inputFS: overlayFS, | ||
mode: 'production', | ||
}); | ||
|
||
let res = await run(b); | ||
assert.equal(res.foo(), 'foobaz'); | ||
assert.equal(res.bar(), 'barbaz'); | ||
|
||
assertBundles(b, [ | ||
{ | ||
assets: ['index.js'], | ||
}, | ||
{ | ||
assets: ['foo.js'], | ||
}, | ||
{ | ||
assets: ['bar.js'], | ||
}, | ||
{ | ||
assets: ['baz.js'], | ||
}, | ||
]); | ||
|
||
for (let bundle of b.getBundles()) { | ||
let contents = await outputFS.readFile(bundle.filePath, 'utf8'); | ||
assert(!contents.includes('parcelRequire')); | ||
assert(contents.includes('export {')); | ||
} | ||
}); | ||
}); |
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
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