Unverified Commit 31a701b9 authored by Jan Nicklas's avatar Jan Nicklas Committed by GitHub

feat(jsconfig-paths-plugin): Resolve paths synchronously (#29467)

parent 7cb5577e
...@@ -127,6 +127,36 @@ export function patternText({ prefix, suffix }: Pattern): string { ...@@ -127,6 +127,36 @@ export function patternText({ prefix, suffix }: Pattern): string {
return `${prefix}*${suffix}` return `${prefix}*${suffix}`
} }
/**
* Calls the iterator function for each entry of the array
* until the first result or error is reached
*/
function forEachBail<TEntry>(
array: TEntry[],
iterator: (
entry: TEntry,
entryCallback: (err?: any, result?: any) => void
) => void,
callback: (err?: any, result?: any) => void
): void {
if (array.length === 0) return callback()
let i = 0
const next = () => {
let loop: boolean | undefined = undefined
iterator(array[i++], (err, result) => {
if (err || result !== undefined || i >= array.length) {
return callback(err, result)
}
if (loop === false) while (next());
loop = true
})
if (!loop) loop = false
return loop
}
while (next());
}
const NODE_MODULES_REGEX = /node_modules/ const NODE_MODULES_REGEX = /node_modules/
type Paths = { [match: string]: string[] } type Paths = { [match: string]: string[] }
...@@ -159,15 +189,19 @@ export class JsConfigPathsPlugin implements webpack.ResolvePlugin { ...@@ -159,15 +189,19 @@ export class JsConfigPathsPlugin implements webpack.ResolvePlugin {
const target = resolver.ensureHook('resolve') const target = resolver.ensureHook('resolve')
resolver resolver
.getHook('described-resolve') .getHook('described-resolve')
.tapPromise( .tapAsync(
'JsConfigPathsPlugin', 'JsConfigPathsPlugin',
async (request: any, resolveContext: any) => { (
request: any,
resolveContext: any,
callback: (err?: any, result?: any) => void
) => {
const moduleName = request.request const moduleName = request.request
// Exclude node_modules from paths support (speeds up resolving) // Exclude node_modules from paths support (speeds up resolving)
if (request.path.match(NODE_MODULES_REGEX)) { if (request.path.match(NODE_MODULES_REGEX)) {
log('skipping request as it is inside node_modules %s', moduleName) log('skipping request as it is inside node_modules %s', moduleName)
return return callback()
} }
if ( if (
...@@ -175,12 +209,12 @@ export class JsConfigPathsPlugin implements webpack.ResolvePlugin { ...@@ -175,12 +209,12 @@ export class JsConfigPathsPlugin implements webpack.ResolvePlugin {
(process.platform === 'win32' && path.win32.isAbsolute(moduleName)) (process.platform === 'win32' && path.win32.isAbsolute(moduleName))
) { ) {
log('skipping request as it is an absolute path %s', moduleName) log('skipping request as it is an absolute path %s', moduleName)
return return callback()
} }
if (pathIsRelative(moduleName)) { if (pathIsRelative(moduleName)) {
log('skipping request as it is a relative path %s', moduleName) log('skipping request as it is a relative path %s', moduleName)
return return callback()
} }
// log('starting to resolve request %s', moduleName) // log('starting to resolve request %s', moduleName)
...@@ -189,7 +223,7 @@ export class JsConfigPathsPlugin implements webpack.ResolvePlugin { ...@@ -189,7 +223,7 @@ export class JsConfigPathsPlugin implements webpack.ResolvePlugin {
const matchedPattern = matchPatternOrExact(pathsKeys, moduleName) const matchedPattern = matchPatternOrExact(pathsKeys, moduleName)
if (!matchedPattern) { if (!matchedPattern) {
log('moduleName did not match any paths pattern %s', moduleName) log('moduleName did not match any paths pattern %s', moduleName)
return return callback()
} }
const matchedStar = isString(matchedPattern) const matchedStar = isString(matchedPattern)
...@@ -201,18 +235,18 @@ export class JsConfigPathsPlugin implements webpack.ResolvePlugin { ...@@ -201,18 +235,18 @@ export class JsConfigPathsPlugin implements webpack.ResolvePlugin {
let triedPaths = [] let triedPaths = []
for (const subst of paths[matchedPatternText]) { forEachBail(
const curPath = matchedStar paths[matchedPatternText],
? subst.replace('*', matchedStar) (subst, pathCallback) => {
: subst const curPath = matchedStar
? subst.replace('*', matchedStar)
// Ensure .d.ts is not matched : subst
if (curPath.endsWith('.d.ts')) { // Ensure .d.ts is not matched
continue if (curPath.endsWith('.d.ts')) {
} // try next path candidate
return pathCallback()
const candidate = path.join(baseDirectory, curPath) }
const [err, result] = await new Promise((resolve) => { const candidate = path.join(baseDirectory, curPath)
const obj = Object.assign({}, request, { const obj = Object.assign({}, request, {
request: candidate, request: candidate,
}) })
...@@ -221,20 +255,18 @@ export class JsConfigPathsPlugin implements webpack.ResolvePlugin { ...@@ -221,20 +255,18 @@ export class JsConfigPathsPlugin implements webpack.ResolvePlugin {
obj, obj,
`Aliased with tsconfig.json or jsconfig.json ${matchedPatternText} to ${candidate}`, `Aliased with tsconfig.json or jsconfig.json ${matchedPatternText} to ${candidate}`,
resolveContext, resolveContext,
(resolverErr: any, resolverResult: any | undefined) => { (resolverErr: any, resolverResult: any) => {
resolve([resolverErr, resolverResult]) if (resolverErr || resolverResult === undefined) {
triedPaths.push(candidate)
// try next path candidate
return pathCallback()
}
return pathCallback(resolverErr, resolverResult)
} }
) )
}) },
callback
// There's multiple paths values possible, so we first have to iterate them all first before throwing an error )
if (err || result === undefined) {
triedPaths.push(candidate)
continue
}
return result
}
} }
) )
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment