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
5eff016f
Commit
5eff016f
authored
Aug 22, 2024
by
Jiachi Liu
Committed by
Janka Uryga
Sep 03, 2024
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: create shared utils for mod resource (#69145)
parent
c8bde3ed
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
65 additions
and
61 deletions
+65
-61
utils.ts
packages/next/src/build/webpack/loaders/utils.ts
+17
-3
flight-client-entry-plugin.ts
...t/src/build/webpack/plugins/flight-client-entry-plugin.ts
+48
-58
No files found.
packages/next/src/build/webpack/loaders/utils.ts
View file @
5eff016f
...
...
@@ -5,14 +5,28 @@ import { RSC_MODULE_TYPES } from '../../../shared/lib/constants'
const
imageExtensions
=
[
'jpg'
,
'jpeg'
,
'png'
,
'webp'
,
'avif'
,
'ico'
,
'svg'
]
const
imageRegex
=
new
RegExp
(
`\\.(
${
imageExtensions
.
join
(
'|'
)}
)$`
)
// Determine if the whole module is server action, 'use server' in the top level of module
export
function
isActionServerLayerEntryModule
(
mod
:
{
resource
:
string
buildInfo
?:
any
})
{
const
rscInfo
=
mod
.
buildInfo
.
rsc
return
!!
(
rscInfo
?.
actions
&&
rscInfo
?.
type
===
RSC_MODULE_TYPES
.
server
)
}
// Determine if the whole module is client action, 'use server' in nested closure in the client module
function
isActionClientLayerModule
(
mod
:
{
resource
:
string
;
buildInfo
?:
any
})
{
const
rscInfo
=
mod
.
buildInfo
.
rsc
return
!!
(
rscInfo
?.
actions
&&
rscInfo
?.
type
===
RSC_MODULE_TYPES
.
client
)
}
export
function
isClientComponentEntryModule
(
mod
:
{
resource
:
string
buildInfo
?:
any
})
{
const
rscInfo
=
mod
.
buildInfo
.
rsc
const
hasClientDirective
=
rscInfo
?.
isClientRef
const
isActionLayerEntry
=
rscInfo
?.
actions
&&
rscInfo
?.
type
===
RSC_MODULE_TYPES
.
client
const
isActionLayerEntry
=
isActionClientLayerModule
(
mod
)
return
(
hasClientDirective
||
isActionLayerEntry
||
imageRegex
.
test
(
mod
.
resource
)
)
...
...
@@ -39,7 +53,7 @@ export function isCSSMod(mod: {
)
}
export
function
getActions
(
mod
:
{
export
function
getActions
FromBuildInfo
(
mod
:
{
resource
:
string
buildInfo
?:
any
}):
undefined
|
string
[]
{
...
...
packages/next/src/build/webpack/plugins/flight-client-entry-plugin.ts
View file @
5eff016f
...
...
@@ -25,7 +25,7 @@ import {
UNDERSCORE_NOT_FOUND_ROUTE_ENTRY
,
}
from
'../../../shared/lib/constants'
import
{
getActions
,
getActions
FromBuildInfo
,
generateActionId
,
isClientComponentEntryModule
,
isCSSMod
,
...
...
@@ -371,10 +371,10 @@ export class FlightClientEntryPlugin {
...
clientEntryToInject
.
clientComponentImports
,
...(
dedupedCSSImports
[
clientEntryToInject
.
absolutePagePath
]
||
[]
).
reduce
((
res
,
curr
)
=>
{
).
reduce
<
ClientComponentImports
>
((
res
,
curr
)
=>
{
res
[
curr
]
=
new
Set
()
return
res
},
{}
as
ClientComponentImports
),
},
{}),
},
})
...
...
@@ -544,27 +544,14 @@ export class FlightClientEntryPlugin {
const
collectActionsInDep
=
(
mod
:
webpack
.
NormalModule
):
void
=>
{
if
(
!
mod
)
return
const
modPath
:
string
=
mod
.
resourceResolveData
?.
path
||
''
// We have to always use the resolved request here to make sure the
// server and client are using the same module path (required by RSC), as
// the server compiler and client compiler have different resolve configs.
let
modRequest
:
string
=
modPath
+
(
mod
.
resourceResolveData
?.
query
||
''
)
// For the barrel optimization, we need to use the match resource instead
// because there will be 2 modules for the same file (same resource path)
// but they're different modules and can't be deduped via `visitedModule`.
// The first module is a virtual re-export module created by the loader.
if
(
mod
.
matchResource
?.
startsWith
(
BARREL_OPTIMIZATION_PREFIX
))
{
modRequest
=
mod
.
matchResource
+
':'
+
modRequest
}
const
modResource
=
getModuleResource
(
mod
)
if
(
!
modRe
quest
||
visitedModule
.
has
(
modRequest
))
return
visitedModule
.
add
(
modRe
quest
)
if
(
!
modRe
source
||
visitedModule
.
has
(
modResource
))
return
visitedModule
.
add
(
modRe
source
)
const
actions
=
getActions
(
mod
)
const
actions
=
getActions
FromBuildInfo
(
mod
)
if
(
actions
)
{
collectedActions
.
set
(
modRe
quest
,
actions
)
collectedActions
.
set
(
modRe
source
,
actions
)
}
getModuleReferencesInOrder
(
mod
,
compilation
.
moduleGraph
).
forEach
(
...
...
@@ -593,8 +580,8 @@ export class FlightClientEntryPlugin {
ssrEntryModule
,
compilation
.
moduleGraph
))
{
const
dep
endency
=
connection
.
dependency
!
const
request
=
(
dep
endency
as
unknown
as
webpack
.
NormalModule
).
request
const
dep
Module
=
connection
.
dependency
const
request
=
(
dep
Module
as
unknown
as
webpack
.
NormalModule
).
request
// It is possible that the same entry is added multiple times in the
// connection graph. We can just skip these to speed up the process.
...
...
@@ -639,33 +626,14 @@ export class FlightClientEntryPlugin {
if
(
!
mod
)
return
const
isCSS
=
isCSSMod
(
mod
)
const
modResource
=
getModuleResource
(
mod
)
const
modPath
:
string
=
mod
.
resourceResolveData
?.
path
||
''
const
modQuery
=
mod
.
resourceResolveData
?.
query
||
''
// We have to always use the resolved request here to make sure the
// server and client are using the same module path (required by RSC), as
// the server compiler and client compiler have different resolve configs.
let
modRequest
:
string
=
modPath
+
modQuery
// Context modules don't have a resource path, we use the identifier instead.
if
(
mod
.
constructor
.
name
===
'ContextModule'
)
{
modRequest
=
(
mod
as
any
).
_identifier
}
// For the barrel optimization, we need to use the match resource instead
// because there will be 2 modules for the same file (same resource path)
// but they're different modules and can't be deduped via `visitedModule`.
// The first module is a virtual re-export module created by the loader.
if
(
mod
.
matchResource
?.
startsWith
(
BARREL_OPTIMIZATION_PREFIX
))
{
modRequest
=
mod
.
matchResource
+
':'
+
modRequest
}
if
(
!
modRequest
)
return
if
(
visited
.
has
(
modRequest
))
{
if
(
clientComponentImports
[
modRequest
])
{
if
(
!
modResource
)
return
if
(
visited
.
has
(
modResource
))
{
if
(
clientComponentImports
[
modResource
])
{
addClientImport
(
mod
,
modRe
quest
,
modRe
source
,
clientComponentImports
,
importedIdentifiers
,
false
...
...
@@ -673,11 +641,11 @@ export class FlightClientEntryPlugin {
}
return
}
visited
.
add
(
modRe
quest
)
visited
.
add
(
modRe
source
)
const
actions
=
getActions
(
mod
)
const
actions
=
getActions
FromBuildInfo
(
mod
)
if
(
actions
)
{
actionImports
.
push
([
modRe
quest
,
actions
])
actionImports
.
push
([
modRe
source
,
actions
])
}
const
webpackRuntime
=
this
.
isEdgeServer
...
...
@@ -696,14 +664,14 @@ export class FlightClientEntryPlugin {
if
(
unused
)
return
}
CSSImports
.
add
(
modRe
quest
)
CSSImports
.
add
(
modRe
source
)
}
else
if
(
isClientComponentEntryModule
(
mod
))
{
if
(
!
clientComponentImports
[
modRe
quest
])
{
clientComponentImports
[
modRe
quest
]
=
new
Set
()
if
(
!
clientComponentImports
[
modRe
source
])
{
clientComponentImports
[
modRe
source
]
=
new
Set
()
}
addClientImport
(
mod
,
modRe
quest
,
modRe
source
,
clientComponentImports
,
importedIdentifiers
,
true
...
...
@@ -715,7 +683,6 @@ export class FlightClientEntryPlugin {
getModuleReferencesInOrder
(
mod
,
compilation
.
moduleGraph
).
forEach
(
(
connection
:
any
)
=>
{
let
dependencyIds
:
string
[]
=
[]
const
depModule
=
connection
.
resolvedModule
// `ids` are the identifiers that are imported from the dependency,
// if it's present, it's an array of strings.
...
...
@@ -725,7 +692,7 @@ export class FlightClientEntryPlugin {
dependencyIds
=
[
'*'
]
}
filterClientComponents
(
dep
Module
,
dependencyIds
)
filterClientComponents
(
connection
.
resolved
Module
,
dependencyIds
)
}
)
}
...
...
@@ -1044,7 +1011,7 @@ function addClientImport(
modRequest
:
string
,
clientComponentImports
:
ClientComponentImports
,
importedIdentifiers
:
string
[],
isFirst
Import
:
boolean
isFirst
VisitModule
:
boolean
)
{
const
clientEntryType
=
getModuleBuildInfo
(
mod
).
rsc
?.
clientEntryType
const
isCjsModule
=
clientEntryType
===
'cjs'
...
...
@@ -1059,7 +1026,7 @@ function addClientImport(
// If there's collected import path with named import identifiers,
// or there's nothing in collected imports are empty.
// we should include the whole module.
if
(
!
isFirst
Import
&&
[...
clientImportsSet
][
0
]
!==
'*'
)
{
if
(
!
isFirst
VisitModule
&&
[...
clientImportsSet
][
0
]
!==
'*'
)
{
clientComponentImports
[
modRequest
]
=
new
Set
([
'*'
])
}
}
else
{
...
...
@@ -1084,3 +1051,26 @@ function addClientImport(
}
}
}
function
getModuleResource
(
mod
:
webpack
.
NormalModule
):
string
{
const
modPath
:
string
=
mod
.
resourceResolveData
?.
path
||
''
const
modQuery
=
mod
.
resourceResolveData
?.
query
||
''
// We have to always use the resolved request here to make sure the
// server and client are using the same module path (required by RSC), as
// the server compiler and client compiler have different resolve configs.
let
modResource
:
string
=
modPath
+
modQuery
// Context modules don't have a resource path, we use the identifier instead.
if
(
mod
.
constructor
.
name
===
'ContextModule'
)
{
modResource
=
mod
.
identifier
()
}
// For the barrel optimization, we need to use the match resource instead
// because there will be 2 modules for the same file (same resource path)
// but they're different modules and can't be deduped via `visitedModule`.
// The first module is a virtual re-export module created by the loader.
if
(
mod
.
matchResource
?.
startsWith
(
BARREL_OPTIMIZATION_PREFIX
))
{
modResource
=
mod
.
matchResource
+
':'
+
modResource
}
return
modResource
}
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