Skip to content

Commit

Permalink
Refactor the client entry plugin (#52798)
Browse files Browse the repository at this point in the history
Changing `compilation.moduleGraph.getResolvedModule(dependencyToFilter)`
to `connection.resolvedModule` to make the code simpler.
  • Loading branch information
shuding committed Jul 18, 2023
1 parent 33385aa commit 56d85f2
Showing 1 changed file with 12 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -265,14 +265,13 @@ export class FlightClientEntryPlugin {
entryModule
)) {
// Entry can be any user defined entry files such as layout, page, error, loading, etc.
const entryDependency = connection.dependency
const entryRequest = connection.dependency.request

const { clientComponentImports, actionImports, cssImports } =
this.collectComponentInfoFromServerEntryDependency({
entryRequest,
compilation,
dependency: entryDependency,
resolvedModule: connection.resolvedModule,
})

actionImports.forEach(([dep, names]) =>
Expand Down Expand Up @@ -493,14 +492,12 @@ export class FlightClientEntryPlugin {

const collectActions = ({
entryRequest,
dependency,
resolvedModule,
}: {
entryRequest: string
dependency: any /* Dependency */
resolvedModule: any
}) => {
const collectActionsInDep = (dependencyToFilter: any): void => {
const mod: webpack.NormalModule =
compilation.moduleGraph.getResolvedModule(dependencyToFilter)
const collectActionsInDep = (mod: webpack.NormalModule): void => {
if (!mod) return

// We have to always use the resolved request here to make sure the
Expand All @@ -520,14 +517,14 @@ export class FlightClientEntryPlugin {
compilation.moduleGraph
.getOutgoingConnections(mod)
.forEach((connection: any) => {
collectActionsInDep(connection.dependency)
collectActionsInDep(connection.resolvedModule)
})
}

// Don't traverse the module graph anymore once hitting the action layer.
if (!entryRequest.includes('next-flight-action-entry-loader')) {
// Traverse the module graph to find all client components.
collectActionsInDep(dependency)
collectActionsInDep(resolvedModule)
}
}

Expand All @@ -547,7 +544,7 @@ export class FlightClientEntryPlugin {

collectActions({
entryRequest: request,
dependency,
resolvedModule: connection.resolvedModule,
})
}
}
Expand All @@ -558,11 +555,11 @@ export class FlightClientEntryPlugin {
collectComponentInfoFromServerEntryDependency({
entryRequest,
compilation,
dependency,
resolvedModule,
}: {
entryRequest: string
compilation: any
dependency: any /* Dependency */
resolvedModule: any /* Dependency */
}): {
cssImports: CssImports
clientComponentImports: ClientComponentImports
Expand All @@ -576,9 +573,7 @@ export class FlightClientEntryPlugin {
const actionImports: [string, string[]][] = []
const CSSImports = new Set<string>()

const filterClientComponents = (dependencyToFilter: any): void => {
const mod: webpack.NormalModule =
compilation.moduleGraph.getResolvedModule(dependencyToFilter)
const filterClientComponents = (mod: webpack.NormalModule): void => {
if (!mod) return

const isCSS = isCSSMod(mod)
Expand Down Expand Up @@ -627,12 +622,12 @@ export class FlightClientEntryPlugin {
compilation.moduleGraph
.getOutgoingConnections(mod)
.forEach((connection: any) => {
filterClientComponents(connection.dependency)
filterClientComponents(connection.resolvedModule)
})
}

// Traverse the module graph to find all client components.
filterClientComponents(dependency)
filterClientComponents(resolvedModule)

return {
clientComponentImports,
Expand Down

0 comments on commit 56d85f2

Please sign in to comment.