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 {
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/
type Paths = { [match: string]: string[] }
......@@ -159,15 +189,19 @@ export class JsConfigPathsPlugin implements webpack.ResolvePlugin {
const target = resolver.ensureHook('resolve')
resolver
.getHook('described-resolve')
.tapPromise(
.tapAsync(
'JsConfigPathsPlugin',
async (request: any, resolveContext: any) => {
(
request: any,
resolveContext: any,
callback: (err?: any, result?: any) => void
) => {
const moduleName = request.request
// Exclude node_modules from paths support (speeds up resolving)
if (request.path.match(NODE_MODULES_REGEX)) {
log('skipping request as it is inside node_modules %s', moduleName)
return
return callback()
}
if (
......@@ -175,12 +209,12 @@ export class JsConfigPathsPlugin implements webpack.ResolvePlugin {
(process.platform === 'win32' && path.win32.isAbsolute(moduleName))
) {
log('skipping request as it is an absolute path %s', moduleName)
return
return callback()
}
if (pathIsRelative(moduleName)) {
log('skipping request as it is a relative path %s', moduleName)
return
return callback()
}
// log('starting to resolve request %s', moduleName)
......@@ -189,7 +223,7 @@ export class JsConfigPathsPlugin implements webpack.ResolvePlugin {
const matchedPattern = matchPatternOrExact(pathsKeys, moduleName)
if (!matchedPattern) {
log('moduleName did not match any paths pattern %s', moduleName)
return
return callback()
}
const matchedStar = isString(matchedPattern)
......@@ -201,18 +235,18 @@ export class JsConfigPathsPlugin implements webpack.ResolvePlugin {
let triedPaths = []
for (const subst of paths[matchedPatternText]) {
const curPath = matchedStar
? subst.replace('*', matchedStar)
: subst
// Ensure .d.ts is not matched
if (curPath.endsWith('.d.ts')) {
continue
}
const candidate = path.join(baseDirectory, curPath)
const [err, result] = await new Promise((resolve) => {
forEachBail(
paths[matchedPatternText],
(subst, pathCallback) => {
const curPath = matchedStar
? subst.replace('*', matchedStar)
: subst
// Ensure .d.ts is not matched
if (curPath.endsWith('.d.ts')) {
// try next path candidate
return pathCallback()
}
const candidate = path.join(baseDirectory, curPath)
const obj = Object.assign({}, request, {
request: candidate,
})
......@@ -221,20 +255,18 @@ export class JsConfigPathsPlugin implements webpack.ResolvePlugin {
obj,
`Aliased with tsconfig.json or jsconfig.json ${matchedPatternText} to ${candidate}`,
resolveContext,
(resolverErr: any, resolverResult: any | undefined) => {
resolve([resolverErr, resolverResult])
(resolverErr: any, resolverResult: any) => {
if (resolverErr || resolverResult === undefined) {
triedPaths.push(candidate)
// try next path candidate
return pathCallback()
}
return pathCallback(resolverErr, resolverResult)
}
)
})
// 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
}
},
callback
)
}
)
}
......
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