Commit 5eff016f authored by Jiachi Liu's avatar Jiachi Liu Committed by Janka Uryga

refactor: create shared utils for mod resource (#69145)

parent c8bde3ed
...@@ -5,14 +5,28 @@ import { RSC_MODULE_TYPES } from '../../../shared/lib/constants' ...@@ -5,14 +5,28 @@ import { RSC_MODULE_TYPES } from '../../../shared/lib/constants'
const imageExtensions = ['jpg', 'jpeg', 'png', 'webp', 'avif', 'ico', 'svg'] const imageExtensions = ['jpg', 'jpeg', 'png', 'webp', 'avif', 'ico', 'svg']
const imageRegex = new RegExp(`\\.(${imageExtensions.join('|')})$`) 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: { export function isClientComponentEntryModule(mod: {
resource: string resource: string
buildInfo?: any buildInfo?: any
}) { }) {
const rscInfo = mod.buildInfo.rsc const rscInfo = mod.buildInfo.rsc
const hasClientDirective = rscInfo?.isClientRef const hasClientDirective = rscInfo?.isClientRef
const isActionLayerEntry = const isActionLayerEntry = isActionClientLayerModule(mod)
rscInfo?.actions && rscInfo?.type === RSC_MODULE_TYPES.client
return ( return (
hasClientDirective || isActionLayerEntry || imageRegex.test(mod.resource) hasClientDirective || isActionLayerEntry || imageRegex.test(mod.resource)
) )
...@@ -39,7 +53,7 @@ export function isCSSMod(mod: { ...@@ -39,7 +53,7 @@ export function isCSSMod(mod: {
) )
} }
export function getActions(mod: { export function getActionsFromBuildInfo(mod: {
resource: string resource: string
buildInfo?: any buildInfo?: any
}): undefined | string[] { }): undefined | string[] {
......
...@@ -25,7 +25,7 @@ import { ...@@ -25,7 +25,7 @@ import {
UNDERSCORE_NOT_FOUND_ROUTE_ENTRY, UNDERSCORE_NOT_FOUND_ROUTE_ENTRY,
} from '../../../shared/lib/constants' } from '../../../shared/lib/constants'
import { import {
getActions, getActionsFromBuildInfo,
generateActionId, generateActionId,
isClientComponentEntryModule, isClientComponentEntryModule,
isCSSMod, isCSSMod,
...@@ -371,10 +371,10 @@ export class FlightClientEntryPlugin { ...@@ -371,10 +371,10 @@ export class FlightClientEntryPlugin {
...clientEntryToInject.clientComponentImports, ...clientEntryToInject.clientComponentImports,
...( ...(
dedupedCSSImports[clientEntryToInject.absolutePagePath] || [] dedupedCSSImports[clientEntryToInject.absolutePagePath] || []
).reduce((res, curr) => { ).reduce<ClientComponentImports>((res, curr) => {
res[curr] = new Set() res[curr] = new Set()
return res return res
}, {} as ClientComponentImports), }, {}),
}, },
}) })
...@@ -544,27 +544,14 @@ export class FlightClientEntryPlugin { ...@@ -544,27 +544,14 @@ export class FlightClientEntryPlugin {
const collectActionsInDep = (mod: webpack.NormalModule): void => { const collectActionsInDep = (mod: webpack.NormalModule): void => {
if (!mod) return if (!mod) return
const modPath: string = mod.resourceResolveData?.path || '' const modResource = getModuleResource(mod)
// 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
}
if (!modRequest || visitedModule.has(modRequest)) return if (!modResource || visitedModule.has(modResource)) return
visitedModule.add(modRequest) visitedModule.add(modResource)
const actions = getActions(mod) const actions = getActionsFromBuildInfo(mod)
if (actions) { if (actions) {
collectedActions.set(modRequest, actions) collectedActions.set(modResource, actions)
} }
getModuleReferencesInOrder(mod, compilation.moduleGraph).forEach( getModuleReferencesInOrder(mod, compilation.moduleGraph).forEach(
...@@ -593,8 +580,8 @@ export class FlightClientEntryPlugin { ...@@ -593,8 +580,8 @@ export class FlightClientEntryPlugin {
ssrEntryModule, ssrEntryModule,
compilation.moduleGraph compilation.moduleGraph
)) { )) {
const dependency = connection.dependency! const depModule = connection.dependency
const request = (dependency as unknown as webpack.NormalModule).request const request = (depModule as unknown as webpack.NormalModule).request
// It is possible that the same entry is added multiple times in the // 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. // connection graph. We can just skip these to speed up the process.
...@@ -639,33 +626,14 @@ export class FlightClientEntryPlugin { ...@@ -639,33 +626,14 @@ export class FlightClientEntryPlugin {
if (!mod) return if (!mod) return
const isCSS = isCSSMod(mod) const isCSS = isCSSMod(mod)
const modResource = getModuleResource(mod)
const modPath: string = mod.resourceResolveData?.path || '' if (!modResource) return
const modQuery = mod.resourceResolveData?.query || '' if (visited.has(modResource)) {
// We have to always use the resolved request here to make sure the if (clientComponentImports[modResource]) {
// 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]) {
addClientImport( addClientImport(
mod, mod,
modRequest, modResource,
clientComponentImports, clientComponentImports,
importedIdentifiers, importedIdentifiers,
false false
...@@ -673,11 +641,11 @@ export class FlightClientEntryPlugin { ...@@ -673,11 +641,11 @@ export class FlightClientEntryPlugin {
} }
return return
} }
visited.add(modRequest) visited.add(modResource)
const actions = getActions(mod) const actions = getActionsFromBuildInfo(mod)
if (actions) { if (actions) {
actionImports.push([modRequest, actions]) actionImports.push([modResource, actions])
} }
const webpackRuntime = this.isEdgeServer const webpackRuntime = this.isEdgeServer
...@@ -696,14 +664,14 @@ export class FlightClientEntryPlugin { ...@@ -696,14 +664,14 @@ export class FlightClientEntryPlugin {
if (unused) return if (unused) return
} }
CSSImports.add(modRequest) CSSImports.add(modResource)
} else if (isClientComponentEntryModule(mod)) { } else if (isClientComponentEntryModule(mod)) {
if (!clientComponentImports[modRequest]) { if (!clientComponentImports[modResource]) {
clientComponentImports[modRequest] = new Set() clientComponentImports[modResource] = new Set()
} }
addClientImport( addClientImport(
mod, mod,
modRequest, modResource,
clientComponentImports, clientComponentImports,
importedIdentifiers, importedIdentifiers,
true true
...@@ -715,7 +683,6 @@ export class FlightClientEntryPlugin { ...@@ -715,7 +683,6 @@ export class FlightClientEntryPlugin {
getModuleReferencesInOrder(mod, compilation.moduleGraph).forEach( getModuleReferencesInOrder(mod, compilation.moduleGraph).forEach(
(connection: any) => { (connection: any) => {
let dependencyIds: string[] = [] let dependencyIds: string[] = []
const depModule = connection.resolvedModule
// `ids` are the identifiers that are imported from the dependency, // `ids` are the identifiers that are imported from the dependency,
// if it's present, it's an array of strings. // if it's present, it's an array of strings.
...@@ -725,7 +692,7 @@ export class FlightClientEntryPlugin { ...@@ -725,7 +692,7 @@ export class FlightClientEntryPlugin {
dependencyIds = ['*'] dependencyIds = ['*']
} }
filterClientComponents(depModule, dependencyIds) filterClientComponents(connection.resolvedModule, dependencyIds)
} }
) )
} }
...@@ -1044,7 +1011,7 @@ function addClientImport( ...@@ -1044,7 +1011,7 @@ function addClientImport(
modRequest: string, modRequest: string,
clientComponentImports: ClientComponentImports, clientComponentImports: ClientComponentImports,
importedIdentifiers: string[], importedIdentifiers: string[],
isFirstImport: boolean isFirstVisitModule: boolean
) { ) {
const clientEntryType = getModuleBuildInfo(mod).rsc?.clientEntryType const clientEntryType = getModuleBuildInfo(mod).rsc?.clientEntryType
const isCjsModule = clientEntryType === 'cjs' const isCjsModule = clientEntryType === 'cjs'
...@@ -1059,7 +1026,7 @@ function addClientImport( ...@@ -1059,7 +1026,7 @@ function addClientImport(
// If there's collected import path with named import identifiers, // If there's collected import path with named import identifiers,
// or there's nothing in collected imports are empty. // or there's nothing in collected imports are empty.
// we should include the whole module. // we should include the whole module.
if (!isFirstImport && [...clientImportsSet][0] !== '*') { if (!isFirstVisitModule && [...clientImportsSet][0] !== '*') {
clientComponentImports[modRequest] = new Set(['*']) clientComponentImports[modRequest] = new Set(['*'])
} }
} else { } else {
...@@ -1084,3 +1051,26 @@ function addClientImport( ...@@ -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
}
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