-
-
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
Improve resolver performance #6328
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
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 |
---|---|---|
|
@@ -16,6 +16,7 @@ import { | |
normalizeSeparators, | ||
findAlternativeNodeModules, | ||
findAlternativeFiles, | ||
loadConfig, | ||
} from '@parcel/utils'; | ||
import ThrowableDiagnostic, { | ||
generateJSONCodeHighlights, | ||
|
@@ -645,6 +646,13 @@ export default class NodeResolver { | |
ctx.invalidateOnFileChange.add(file); | ||
let pkg = JSON.parse(json); | ||
|
||
await this.processPackage(pkg, file, dir); | ||
|
||
this.packageCache.set(file, pkg); | ||
return pkg; | ||
} | ||
|
||
async processPackage(pkg: InternalPackageJSON, file: string, dir: string) { | ||
pkg.pkgfile = file; | ||
pkg.pkgdir = dir; | ||
|
||
|
@@ -656,9 +664,6 @@ export default class NodeResolver { | |
delete pkg.source; | ||
} | ||
} | ||
|
||
this.packageCache.set(file, pkg); | ||
return pkg; | ||
} | ||
|
||
getPackageEntries( | ||
|
@@ -798,18 +803,27 @@ export default class NodeResolver { | |
return null; | ||
} | ||
|
||
let pkgKeys = ['source', 'alias']; | ||
if (env.isBrowser()) pkgKeys.push('browser'); | ||
if (pkg.source && !Array.isArray(pkg.source)) { | ||
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. Making this function use static references rather than dynamic property accesses seems to improve performance quite a bit |
||
let alias = await this.getAlias(filename, pkg, pkg.source); | ||
if (alias != null) { | ||
return alias; | ||
} | ||
} | ||
|
||
if (pkg.alias) { | ||
let alias = await this.getAlias(filename, pkg, pkg.alias); | ||
if (alias != null) { | ||
return alias; | ||
} | ||
} | ||
|
||
for (let pkgKey of pkgKeys) { | ||
let pkgKeyValue = pkg[pkgKey]; | ||
if (!Array.isArray(pkgKeyValue)) { | ||
let alias = await this.getAlias(filename, pkg, pkgKeyValue); | ||
if (alias != null) { | ||
return alias; | ||
} | ||
if (pkg.browser && env.isBrowser()) { | ||
let alias = await this.getAlias(filename, pkg, pkg.browser); | ||
if (alias != null) { | ||
return alias; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
|
@@ -911,7 +925,7 @@ export default class NodeResolver { | |
return alias; | ||
} | ||
|
||
findPackage( | ||
async findPackage( | ||
sourceFile: string, | ||
ctx: ResolverContext, | ||
): Promise<InternalPackageJSON | null> { | ||
|
@@ -921,17 +935,26 @@ export default class NodeResolver { | |
}); | ||
|
||
// Find the nearest package.json file within the current node_modules folder | ||
let dir = path.dirname(sourceFile); | ||
let pkgFile = this.fs.findAncestorFile( | ||
let res = await loadConfig( | ||
this.fs, | ||
sourceFile, | ||
['package.json'], | ||
dir, | ||
this.projectRoot, | ||
// By default, loadConfig uses JSON5. Use normal JSON for package.json files | ||
// since they don't support comments and JSON.parse is faster. | ||
{parser: JSON.parse}, | ||
); | ||
if (pkgFile) { | ||
return this.readPackage(path.dirname(pkgFile), ctx); | ||
|
||
if (res != null) { | ||
let file = res.files[0].filePath; | ||
let dir = path.dirname(file); | ||
ctx.invalidateOnFileChange.add(file); | ||
let pkg = res.config; | ||
await this.processPackage(pkg, file, dir); | ||
return pkg; | ||
} | ||
|
||
return Promise.resolve(null); | ||
return null; | ||
} | ||
|
||
async loadAlias( | ||
|
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
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.
That isn't part of the
configCache
cache key. But I also can't think of a case where you would want to parse a single file with 2 different parsers.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.
Yeah, let's hope not 😉