Unverified Commit 69cbe4cb authored by Shu Ding's avatar Shu Ding Committed by GitHub

Optimize client entry creation (#51849)

Removed an unnecessary dependency mapping from the plugin.

Here's a quick visualization of this change. We have to traversal the module graph twice. The first iteration goes through all server modules and creates the client entries. And the second iteration collects info from all client modules.

Before this PR, the second iteration starts from server entries so it traversals both layers. With this PR, the second iteration will start from client entries only:

<img width="870" alt="CleanShot 2023-06-27 at 10 01 08@2x" src="https://github.com/vercel/next.js/assets/3676859/f0b7a0c6-0ade-483a-8645-48e2a8a6c9d0">

This is a ~100ms improvement for HMR of complicate apps.
parent aec75c2f
...@@ -53,7 +53,7 @@ import { WellKnownErrorsPlugin } from './webpack/plugins/wellknown-errors-plugin ...@@ -53,7 +53,7 @@ import { WellKnownErrorsPlugin } from './webpack/plugins/wellknown-errors-plugin
import { regexLikeCss } from './webpack/config/blocks/css' import { regexLikeCss } from './webpack/config/blocks/css'
import { CopyFilePlugin } from './webpack/plugins/copy-file-plugin' import { CopyFilePlugin } from './webpack/plugins/copy-file-plugin'
import { ClientReferenceManifestPlugin } from './webpack/plugins/flight-manifest-plugin' import { ClientReferenceManifestPlugin } from './webpack/plugins/flight-manifest-plugin'
import { ClientReferenceEntryPlugin } from './webpack/plugins/flight-client-entry-plugin' import { FlightClientEntryPlugin } from './webpack/plugins/flight-client-entry-plugin'
import { NextTypesPlugin } from './webpack/plugins/next-types-plugin' import { NextTypesPlugin } from './webpack/plugins/next-types-plugin'
import type { import type {
Feature, Feature,
...@@ -2420,7 +2420,7 @@ export default async function getBaseWebpackConfig( ...@@ -2420,7 +2420,7 @@ export default async function getBaseWebpackConfig(
dev, dev,
appDir, appDir,
}) })
: new ClientReferenceEntryPlugin({ : new FlightClientEntryPlugin({
appDir, appDir,
dev, dev,
isEdgeServer, isEdgeServer,
......
...@@ -6,7 +6,7 @@ import { getModuleBuildInfo } from '../get-module-build-info' ...@@ -6,7 +6,7 @@ import { getModuleBuildInfo } from '../get-module-build-info'
const noopHeadPath = require.resolve('next/dist/client/components/noop-head') const noopHeadPath = require.resolve('next/dist/client/components/noop-head')
export default async function transformSource( export default function transformSource(
this: any, this: any,
source: string, source: string,
sourceMap: any sourceMap: any
...@@ -21,8 +21,6 @@ export default async function transformSource( ...@@ -21,8 +21,6 @@ export default async function transformSource(
? 'next/dist/esm/build/webpack/loaders/next-flight-loader/module-proxy' ? 'next/dist/esm/build/webpack/loaders/next-flight-loader/module-proxy'
: 'next/dist/build/webpack/loaders/next-flight-loader/module-proxy' : 'next/dist/build/webpack/loaders/next-flight-loader/module-proxy'
const callback = this.async()
// Assign the RSC meta information to buildInfo. // Assign the RSC meta information to buildInfo.
// Exclude next internal files which are not marked as client files // Exclude next internal files which are not marked as client files
const buildInfo = getModuleBuildInfo(this._module) const buildInfo = getModuleBuildInfo(this._module)
...@@ -55,11 +53,12 @@ export default async function transformSource( ...@@ -55,11 +53,12 @@ export default async function transformSource(
if (assumedSourceType === 'module') { if (assumedSourceType === 'module') {
if (clientRefs.includes('*')) { if (clientRefs.includes('*')) {
return callback( this.callback(
new Error( new Error(
`It's currently unsupported to use "export *" in a client boundary. Please use named exports instead.` `It's currently unsupported to use "export *" in a client boundary. Please use named exports instead.`
) )
) )
return
} }
let esmSource = `\ let esmSource = `\
...@@ -88,7 +87,8 @@ export { e${cnt++} as ${ref} };` ...@@ -88,7 +87,8 @@ export { e${cnt++} as ${ref} };`
} }
} }
return callback(null, esmSource, sourceMap) this.callback(null, esmSource, sourceMap)
return
} }
} }
...@@ -100,7 +100,7 @@ export { e${cnt++} as ${ref} };` ...@@ -100,7 +100,7 @@ export { e${cnt++} as ${ref} };`
} }
} }
return callback( this.callback(
null, null,
source.replace(RSC_MOD_REF_PROXY_ALIAS, moduleProxy), source.replace(RSC_MOD_REF_PROXY_ALIAS, moduleProxy),
sourceMap sourceMap
......
...@@ -98,20 +98,17 @@ export class ClientReferenceManifestPlugin { ...@@ -98,20 +98,17 @@ export class ClientReferenceManifestPlugin {
webpack.dependencies.ModuleDependency, webpack.dependencies.ModuleDependency,
new webpack.dependencies.NullDependency.Template() new webpack.dependencies.NullDependency.Template()
) )
compilation.hooks.processAssets.tap(
{
name: PLUGIN_NAME,
// Have to be in the optimize stage to run after updating the CSS
// asset hash via extract mini css plugin.
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_HASH,
},
(assets) => this.createAsset(assets, compilation, compiler.context)
)
} }
) )
compiler.hooks.make.tap(PLUGIN_NAME, (compilation) => {
compilation.hooks.processAssets.tap(
{
name: PLUGIN_NAME,
// Have to be in the optimize stage to run after updating the CSS
// asset hash via extract mini css plugin.
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_OPTIMIZE_HASH,
},
(assets) => this.createAsset(assets, compilation, compiler.context)
)
})
} }
createAsset( createAsset(
......
...@@ -2,7 +2,7 @@ async function noopAction() { ...@@ -2,7 +2,7 @@ async function noopAction() {
'use server' 'use server'
} }
console.log(noopAction()) console.log(!!noopAction())
export default function Layout({ children }) { export default function Layout({ children }) {
return children return children
......
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