-
-
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
Support parallel bundle imports in libraries #9156
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
4 changes: 4 additions & 0 deletions
4
packages/core/integration-tests/test/integration/library-parallel-deps/.parcelrc
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,4 @@ | ||
{ | ||
"extends": "@parcel/config-default", | ||
"resolvers": ["./ParallelResolver", "..."] | ||
} |
12 changes: 12 additions & 0 deletions
12
packages/core/integration-tests/test/integration/library-parallel-deps/ParallelResolver.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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
const {Resolver} = require('@parcel/plugin'); | ||
|
||
module.exports = new Resolver({ | ||
resolve({specifier}) { | ||
if (specifier === './foo') { | ||
return { | ||
filePath: __dirname + '/foo.js', | ||
priority: 'parallel' | ||
}; | ||
} | ||
} | ||
}); |
1 change: 1 addition & 0 deletions
1
packages/core/integration-tests/test/integration/library-parallel-deps/foo.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 |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = 'foo'; |
3 changes: 3 additions & 0 deletions
3
packages/core/integration-tests/test/integration/library-parallel-deps/index.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 |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import * as foo from './foo'; | ||
|
||
export default foo + ' bar'; |
3 changes: 3 additions & 0 deletions
3
packages/core/integration-tests/test/integration/library-parallel-deps/package.json
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,3 @@ | ||
{ | ||
"module": "dist/out.js" | ||
} |
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
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 |
---|---|---|
|
@@ -94,6 +94,7 @@ export class ScopeHoistingPackager { | |
hoistedRequires: Map<string, Map<string, string>> = new Map(); | ||
needsPrelude: boolean = false; | ||
usedHelpers: Set<string> = new Set(); | ||
externalAssets: Set<Asset> = new Set(); | ||
|
||
constructor( | ||
options: PluginOptions, | ||
|
@@ -130,9 +131,25 @@ export class ScopeHoistingPackager { | |
this.bundle.env.isLibrary || | ||
this.bundle.env.outputFormat === 'commonjs' | ||
) { | ||
let bundles = this.bundleGraph.getReferencedBundles(this.bundle); | ||
for (let b of bundles) { | ||
this.externals.set(relativeBundlePath(this.bundle, b), new Map()); | ||
for (let b of this.bundleGraph.getReferencedBundles(this.bundle)) { | ||
let entry = b.getMainEntry(); | ||
let symbols = new Map(); | ||
if (entry && !this.isAsyncBundle && entry.type === 'js') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for now async bundles in libraries are still referenced with |
||
this.externalAssets.add(entry); | ||
|
||
let usedSymbols = this.bundleGraph.getUsedSymbols(entry) || new Set(); | ||
for (let s of usedSymbols) { | ||
// If the referenced bundle is ESM, and we are importing '*', use 'default' instead. | ||
// This matches the logic below in buildExportedSymbols. | ||
let imported = s; | ||
if (imported === '*' && b.env.outputFormat === 'esmodule') { | ||
imported = 'default'; | ||
} | ||
symbols.set(imported, this.getSymbolResolution(entry, entry, s)); | ||
} | ||
} | ||
|
||
this.externals.set(relativeBundlePath(this.bundle, b), symbols); | ||
} | ||
} | ||
|
||
|
@@ -756,6 +773,13 @@ ${code} | |
} | ||
} | ||
|
||
isWrapped(resolved: Asset, parentAsset: Asset): boolean { | ||
return ( | ||
(!this.bundle.hasAsset(resolved) && !this.externalAssets.has(resolved)) || | ||
(this.wrappedAssets.has(resolved.id) && resolved !== parentAsset) | ||
); | ||
} | ||
|
||
getSymbolResolution( | ||
parentAsset: Asset, | ||
resolved: Asset, | ||
|
@@ -777,13 +801,18 @@ ${code} | |
return '{}'; | ||
} | ||
|
||
let isWrapped = | ||
!this.bundle.hasAsset(resolvedAsset) || | ||
(this.wrappedAssets.has(resolvedAsset.id) && | ||
resolvedAsset !== parentAsset); | ||
let isWrapped = this.isWrapped(resolvedAsset, parentAsset); | ||
let staticExports = resolvedAsset.meta.staticExports !== false; | ||
let publicId = this.bundleGraph.getAssetPublicId(resolvedAsset); | ||
|
||
// External CommonJS dependencies need to be accessed as an object property rather than imported | ||
// directly to maintain live binding. | ||
let isExternalCommonJS = | ||
!isWrapped && | ||
this.bundle.env.isLibrary && | ||
this.bundle.env.outputFormat === 'commonjs' && | ||
!this.bundle.hasAsset(resolvedAsset); | ||
|
||
// If the resolved asset is wrapped, but imported at the top-level by this asset, | ||
// then we hoist parcelRequire calls to the top of this asset so side effects run immediately. | ||
if ( | ||
|
@@ -849,7 +878,7 @@ ${code} | |
return obj; | ||
} | ||
} else if ( | ||
(!staticExports || isWrapped || !symbol) && | ||
(!staticExports || isWrapped || !symbol || isExternalCommonJS) && | ||
resolvedAsset !== parentAsset | ||
) { | ||
// If the resolved asset is wrapped or has non-static exports, | ||
|
@@ -887,9 +916,7 @@ ${code} | |
let hoisted = this.hoistedRequires.get(dep.id); | ||
let res = ''; | ||
let lineCount = 0; | ||
let isWrapped = | ||
!this.bundle.hasAsset(resolved) || | ||
(this.wrappedAssets.has(resolved.id) && resolved !== parentAsset); | ||
let isWrapped = this.isWrapped(resolved, parentAsset); | ||
|
||
// If the resolved asset is wrapped and is imported in the top-level by this asset, | ||
// we need to run side effects when this asset runs. If the resolved asset is not | ||
|
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.
👍