Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
N
next.js
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Alex
next.js
Commits
31a701b9
Unverified
Commit
31a701b9
authored
Oct 13, 2021
by
Jan Nicklas
Committed by
GitHub
Oct 13, 2021
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(jsconfig-paths-plugin): Resolve paths synchronously (#29467)
parent
7cb5577e
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
30 deletions
+62
-30
jsconfig-paths-plugin.ts
packages/next/build/webpack/plugins/jsconfig-paths-plugin.ts
+62
-30
No files found.
packages/next/build/webpack/plugins/jsconfig-paths-plugin.ts
View file @
31a701b9
...
...
@@ -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'
)
.
tap
Promise
(
.
tap
Async
(
'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
)
=>
{
for
EachBail
(
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
)
}
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment