Unverified Commit fd5a0cf9 authored by Shu Ding's avatar Shu Ding Committed by GitHub

Implement loader and entry creation for server (#45524)

Implements the manifest and loader (NEXT-416), with entry creation (NEXT-415).

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent a1c64d5b
...@@ -39,12 +39,21 @@ export interface PageStaticInfo { ...@@ -39,12 +39,21 @@ export interface PageStaticInfo {
middleware?: Partial<MiddlewareConfig> middleware?: Partial<MiddlewareConfig>
} }
const CLIENT_MODULE_LABEL = `/* __next_internal_client_entry_do_not_use__ */` const CLIENT_MODULE_LABEL = '/* __next_internal_client_entry_do_not_use__ */'
const ACTION_MODULE_LABEL =
/\/\* __next_internal_action_entry_do_not_use__ ([^ ]+) \*\//
export type RSCModuleType = 'server' | 'client' export type RSCModuleType = 'server' | 'client'
export function getRSCModuleType(source: string): RSCModuleType { export function getRSCModuleInformation(source: string): {
return source.includes(CLIENT_MODULE_LABEL) type: RSCModuleType
actions?: string[]
} {
const type = source.includes(CLIENT_MODULE_LABEL)
? RSC_MODULE_TYPES.client ? RSC_MODULE_TYPES.client
: RSC_MODULE_TYPES.server : RSC_MODULE_TYPES.server
const actions = source.match(ACTION_MODULE_LABEL)?.[1]?.split(',')
return { type, actions }
} }
/** /**
...@@ -296,7 +305,7 @@ export async function getPageStaticInfo(params: { ...@@ -296,7 +305,7 @@ export async function getPageStaticInfo(params: {
) { ) {
const swcAST = await parseModule(pageFilePath, fileContent) const swcAST = await parseModule(pageFilePath, fileContent)
const { ssg, ssr, runtime } = checkExports(swcAST) const { ssg, ssr, runtime } = checkExports(swcAST)
const rsc = getRSCModuleType(fileContent) const rsc = getRSCModuleInformation(fileContent).type
// default / failsafe value for config // default / failsafe value for config
let config: any = {} let config: any = {}
......
...@@ -129,6 +129,11 @@ function getBaseSWCOptions({ ...@@ -129,6 +129,11 @@ function getBaseSWCOptions({
serverComponents: hasServerComponents serverComponents: hasServerComponents
? { isServer: !!isServerLayer } ? { isServer: !!isServerLayer }
: undefined, : undefined,
serverActions: hasServerComponents
? {
isServer: !!isServerLayer,
}
: undefined,
} }
} }
......
...@@ -1619,6 +1619,7 @@ export default async function getBaseWebpackConfig( ...@@ -1619,6 +1619,7 @@ export default async function getBaseWebpackConfig(
'next-style-loader', 'next-style-loader',
'next-flight-loader', 'next-flight-loader',
'next-flight-client-entry-loader', 'next-flight-client-entry-loader',
'next-flight-action-entry-loader',
'noop-loader', 'noop-loader',
'next-middleware-loader', 'next-middleware-loader',
'next-edge-function-loader', 'next-edge-function-loader',
......
export type NextFlightActionEntryLoaderOptions = {
actionPath: string
actionName: string
}
function nextFlightActionEntryLoader(this: any) {
const { actionPath, actionName }: NextFlightActionEntryLoaderOptions =
this.getOptions()
return `
import { ${actionName} } from ${JSON.stringify(actionPath)}
export default async function endpoint(req, res) {
try {
const result = await ${actionName}()
const serializedResult = JSON.stringify(result)
res.statusCode = 200
res.setHeader('Content-Type', 'text/plain')
res.body(serializedResult)
} catch (err) {
res.statusCode = 500
res.setHeader('Content-Type', 'text/plain')
res.body(err.message)
}
res.send()
}`
}
export default nextFlightActionEntryLoader
import { RSC_MODULE_TYPES } from '../../../../shared/lib/constants' import { RSC_MODULE_TYPES } from '../../../../shared/lib/constants'
import { warnOnce } from '../../../../shared/lib/utils/warn-once' import { warnOnce } from '../../../../shared/lib/utils/warn-once'
import { getRSCModuleType } from '../../../analysis/get-page-static-info' import { getRSCModuleInformation } from '../../../analysis/get-page-static-info'
import { getModuleBuildInfo } from '../get-module-build-info' 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')
...@@ -16,21 +16,19 @@ export default async function transformSource( ...@@ -16,21 +16,19 @@ export default async function transformSource(
} }
const callback = this.async() const callback = this.async()
const buildInfo = getModuleBuildInfo(this._module)
const rscType = getRSCModuleType(source)
// 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
buildInfo.rsc = { type: rscType } const buildInfo = getModuleBuildInfo(this._module)
buildInfo.rsc = getRSCModuleInformation(source)
if (buildInfo.rsc?.type === RSC_MODULE_TYPES.client) { if (buildInfo.rsc?.type !== RSC_MODULE_TYPES.client) {
return callback(null, source, sourceMap) if (noopHeadPath === this.resourcePath) {
warnOnce(
`Warning: You're using \`next/head\` inside app directory, please migrate to \`head.js\`. Checkout https://beta.nextjs.org/docs/api-reference/file-conventions/head for details.`
)
}
} }
if (noopHeadPath === this.resourcePath) {
warnOnce(
`Warning: You're using \`next/head\` inside app directory, please migrate to \`head.js\`. Checkout https://beta.nextjs.org/docs/api-reference/file-conventions/head for details.`
)
}
return callback(null, source, sourceMap) return callback(null, source, sourceMap)
} }
import { createHash } from 'crypto'
import { RSC_MODULE_TYPES } from '../../../shared/lib/constants' import { RSC_MODULE_TYPES } from '../../../shared/lib/constants'
const imageExtensions = ['jpg', 'jpeg', 'png', 'webp', 'avif'] const imageExtensions = ['jpg', 'jpeg', 'png', 'webp', 'avif']
...@@ -12,3 +14,16 @@ export function isClientComponentModule(mod: { ...@@ -12,3 +14,16 @@ export function isClientComponentModule(mod: {
} }
export const regexCSS = /\.(css|scss|sass)(\?.*)?$/ export const regexCSS = /\.(css|scss|sass)(\?.*)?$/
export function getActions(mod: {
resource: string
buildInfo: any
}): undefined | string[] {
return mod.buildInfo.rsc?.actions
}
export function generateActionId(filePath: string, exportName: string) {
return createHash('sha1')
.update(filePath + ':' + exportName)
.digest('hex')
}
...@@ -19,10 +19,16 @@ import { ...@@ -19,10 +19,16 @@ import {
APP_CLIENT_INTERNALS, APP_CLIENT_INTERNALS,
COMPILER_NAMES, COMPILER_NAMES,
EDGE_RUNTIME_WEBPACK, EDGE_RUNTIME_WEBPACK,
ACTIONS_MANIFEST,
FLIGHT_SERVER_CSS_MANIFEST, FLIGHT_SERVER_CSS_MANIFEST,
} from '../../../shared/lib/constants' } from '../../../shared/lib/constants'
import { ASYNC_CLIENT_MODULES } from './flight-manifest-plugin' import { ASYNC_CLIENT_MODULES } from './flight-manifest-plugin'
import { isClientComponentModule, regexCSS } from '../loaders/utils' import {
generateActionId,
getActions,
isClientComponentModule,
regexCSS,
} from '../loaders/utils'
import { traverseModules } from '../utils' import { traverseModules } from '../utils'
import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep' import { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep'
...@@ -39,6 +45,9 @@ export const injectedClientEntries = new Map() ...@@ -39,6 +45,9 @@ export const injectedClientEntries = new Map()
export const serverModuleIds = new Map<string, string | number>() export const serverModuleIds = new Map<string, string | number>()
export const edgeServerModuleIds = new Map<string, string | number>() export const edgeServerModuleIds = new Map<string, string | number>()
// A map to track "path:name" -> "hash".
let serverActions: Record<string, string> = {}
let serverCSSManifest: FlightCSSManifest = {} let serverCSSManifest: FlightCSSManifest = {}
let edgeServerCSSManifest: FlightCSSManifest = {} let edgeServerCSSManifest: FlightCSSManifest = {}
...@@ -118,6 +127,18 @@ export class FlightClientEntryPlugin { ...@@ -118,6 +127,18 @@ export class FlightClientEntryPlugin {
recordModule(String(modId), mod) recordModule(String(modId), mod)
}) })
}) })
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)
)
})
} }
async createClientEntries(compiler: any, compilation: any) { async createClientEntries(compiler: any, compilation: any) {
...@@ -125,6 +146,9 @@ export class FlightClientEntryPlugin { ...@@ -125,6 +146,9 @@ export class FlightClientEntryPlugin {
ReturnType<typeof this.injectClientEntryAndSSRModules> ReturnType<typeof this.injectClientEntryAndSSRModules>
> = [] > = []
const addActionEntryList: Array<ReturnType<typeof this.injectActionEntry>> =
[]
// Loop over all the entry modules. // Loop over all the entry modules.
function forEachEntryModule( function forEachEntryModule(
callback: ({ callback: ({
...@@ -176,6 +200,7 @@ export class FlightClientEntryPlugin { ...@@ -176,6 +200,7 @@ export class FlightClientEntryPlugin {
const internalClientComponentEntryImports = new Set< const internalClientComponentEntryImports = new Set<
ClientComponentImports[0] ClientComponentImports[0]
>() >()
const actionEntryImports = new Map<string, string[]>()
for (const connection of compilation.moduleGraph.getOutgoingConnections( for (const connection of compilation.moduleGraph.getOutgoingConnections(
entryModule entryModule
...@@ -184,18 +209,22 @@ export class FlightClientEntryPlugin { ...@@ -184,18 +209,22 @@ export class FlightClientEntryPlugin {
const entryDependency = connection.dependency const entryDependency = connection.dependency
const entryRequest = connection.dependency.request const entryRequest = connection.dependency.request
const [clientComponentImports] = const { clientImports, actionImports } =
this.collectClientComponentsAndCSSForDependency({ this.collectComponentInfoFromDependencies({
entryRequest, entryRequest,
compilation, compilation,
dependency: entryDependency, dependency: entryDependency,
}) })
actionImports.forEach(([dep, names]) =>
actionEntryImports.set(dep, names)
)
const isAbsoluteRequest = path.isAbsolute(entryRequest) const isAbsoluteRequest = path.isAbsolute(entryRequest)
// Next.js internals are put into a separate entry. // Next.js internals are put into a separate entry.
if (!isAbsoluteRequest) { if (!isAbsoluteRequest) {
clientComponentImports.forEach((value) => clientImports.forEach((value) =>
internalClientComponentEntryImports.add(value) internalClientComponentEntryImports.add(value)
) )
continue continue
...@@ -215,7 +244,7 @@ export class FlightClientEntryPlugin { ...@@ -215,7 +244,7 @@ export class FlightClientEntryPlugin {
compiler, compiler,
compilation, compilation,
entryName: name, entryName: name,
clientComponentImports, clientImports,
bundlePath, bundlePath,
}) })
) )
...@@ -227,10 +256,25 @@ export class FlightClientEntryPlugin { ...@@ -227,10 +256,25 @@ export class FlightClientEntryPlugin {
compiler, compiler,
compilation, compilation,
entryName: name, entryName: name,
clientComponentImports: [...internalClientComponentEntryImports], clientImports: [...internalClientComponentEntryImports],
bundlePath: APP_CLIENT_INTERNALS, bundlePath: APP_CLIENT_INTERNALS,
}) })
) )
// Create action entries
serverActions = {}
actionEntryImports.forEach((actionNames, actionPath) => {
for (const actionName of actionNames) {
addActionEntryList.push(
this.injectActionEntry({
compiler,
compilation,
actionPath,
actionName,
})
)
}
})
}) })
// After optimizing all the modules, we collect the CSS that are still used // After optimizing all the modules, we collect the CSS that are still used
...@@ -330,13 +374,12 @@ export class FlightClientEntryPlugin { ...@@ -330,13 +374,12 @@ export class FlightClientEntryPlugin {
if (tracked.has(entryRequest)) continue if (tracked.has(entryRequest)) continue
tracked.add(entryRequest) tracked.add(entryRequest)
const [, cssImports] = const { cssImports } = this.collectComponentInfoFromDependencies({
this.collectClientComponentsAndCSSForDependency({ entryRequest,
entryRequest, compilation,
compilation, dependency: entryDependency,
dependency: entryDependency, clientEntryDependencyMap,
clientEntryDependencyMap, })
})
Object.assign(cssManifest, cssImports) Object.assign(cssManifest, cssImports)
} }
...@@ -393,7 +436,7 @@ export class FlightClientEntryPlugin { ...@@ -393,7 +436,7 @@ export class FlightClientEntryPlugin {
) )
} }
collectClientComponentsAndCSSForDependency({ collectComponentInfoFromDependencies({
entryRequest, entryRequest,
compilation, compilation,
dependency, dependency,
...@@ -403,12 +446,17 @@ export class FlightClientEntryPlugin { ...@@ -403,12 +446,17 @@ export class FlightClientEntryPlugin {
compilation: any compilation: any
dependency: any /* Dependency */ dependency: any /* Dependency */
clientEntryDependencyMap?: Record<string, any> clientEntryDependencyMap?: Record<string, any>
}): [ClientComponentImports, CssImports] { }): {
clientImports: ClientComponentImports
cssImports: CssImports
actionImports: [string, string[]][]
} {
/** /**
* Keep track of checked modules to avoid infinite loops with recursive imports. * Keep track of checked modules to avoid infinite loops with recursive imports.
*/ */
const visitedBySegment: { [segment: string]: Set<string> } = {} const visitedBySegment: { [segment: string]: Set<string> } = {}
const clientComponentImports: ClientComponentImports = [] const clientComponentImports: ClientComponentImports = []
const actionImports: [string, string[]][] = []
const CSSImports: CssImports = {} const CSSImports: CssImports = {}
const filterClientComponents = ( const filterClientComponents = (
...@@ -441,6 +489,11 @@ export class FlightClientEntryPlugin { ...@@ -441,6 +489,11 @@ export class FlightClientEntryPlugin {
const isClientComponent = isClientComponentModule(mod) const isClientComponent = isClientComponentModule(mod)
const actions = getActions(mod)
if (actions) {
actionImports.push([modRequest, actions])
}
if (isCSS) { if (isCSS) {
const sideEffectFree = const sideEffectFree =
mod.factoryMeta && (mod.factoryMeta as any).sideEffectFree mod.factoryMeta && (mod.factoryMeta as any).sideEffectFree
...@@ -489,26 +542,36 @@ export class FlightClientEntryPlugin { ...@@ -489,26 +542,36 @@ export class FlightClientEntryPlugin {
// Traverse the module graph to find all client components. // Traverse the module graph to find all client components.
filterClientComponents(dependency, false) filterClientComponents(dependency, false)
return [clientComponentImports, CSSImports] // Don't traverse the module graph for the action loader.
if (!/next-flight-action-entry-loader/.test(entryRequest)) {
// Traverse the module graph to find all client components.
filterClientComponents(dependency, false)
}
return {
clientImports: clientComponentImports,
cssImports: CSSImports,
actionImports,
}
} }
injectClientEntryAndSSRModules({ injectClientEntryAndSSRModules({
compiler, compiler,
compilation, compilation,
entryName, entryName,
clientComponentImports, clientImports,
bundlePath, bundlePath,
}: { }: {
compiler: webpack.Compiler compiler: webpack.Compiler
compilation: webpack.Compilation compilation: webpack.Compilation
entryName: string entryName: string
clientComponentImports: ClientComponentImports clientImports: ClientComponentImports
bundlePath: string bundlePath: string
}): [shouldInvalidate: boolean, addEntryPromise: Promise<void>] { }): [shouldInvalidate: boolean, addEntryPromise: Promise<void>] {
let shouldInvalidate = false let shouldInvalidate = false
const loaderOptions: NextFlightClientEntryLoaderOptions = { const loaderOptions: NextFlightClientEntryLoaderOptions = {
modules: clientComponentImports, modules: clientImports,
server: false, server: false,
} }
...@@ -517,10 +580,10 @@ export class FlightClientEntryPlugin { ...@@ -517,10 +580,10 @@ export class FlightClientEntryPlugin {
// replace them. // replace them.
const clientLoader = `next-flight-client-entry-loader?${stringify({ const clientLoader = `next-flight-client-entry-loader?${stringify({
modules: this.isEdgeServer modules: this.isEdgeServer
? clientComponentImports.map((importPath) => ? clientImports.map((importPath) =>
importPath.replace('next/dist/esm/', 'next/dist/') importPath.replace('next/dist/esm/', 'next/dist/')
) )
: clientComponentImports, : clientImports,
server: false, server: false,
})}!` })}!`
...@@ -587,6 +650,48 @@ export class FlightClientEntryPlugin { ...@@ -587,6 +650,48 @@ export class FlightClientEntryPlugin {
] ]
} }
injectActionEntry({
compiler,
compilation,
actionPath,
actionName,
}: {
compiler: webpack.Compiler
compilation: webpack.Compilation
actionPath: string
actionName: string
}) {
const actionLoader = `next-flight-action-entry-loader?${stringify({
actionPath,
actionName,
})}!`
const actionHash = generateActionId(actionPath, actionName)
serverActions[actionPath + ':' + actionName] = actionHash
// Inject the entry to the server compiler (__sc_client__).
const actionEntryDep = webpack.EntryPlugin.createDependency(actionLoader, {
name: 'action/' + actionHash,
})
return new Promise((resolve, reject) => {
compilation.addEntry(
compiler.context,
actionEntryDep,
{
name: 'action/' + actionHash,
layer: WEBPACK_LAYERS.server,
},
(err?: Error | null, entry?: any) => {
if (err) {
return reject(err)
}
resolve(entry)
}
)
})
}
addEntry( addEntry(
compilation: any, compilation: any,
context: string, context: string,
...@@ -615,4 +720,12 @@ export class FlightClientEntryPlugin { ...@@ -615,4 +720,12 @@ export class FlightClientEntryPlugin {
) )
}) })
} }
createAsset(assets: webpack.Compilation['assets']) {
const file = ACTIONS_MANIFEST
const json = JSON.stringify(serverActions, null, this.dev ? 2 : undefined)
assets[file + '.json'] = new sources.RawSource(
json
) as unknown as webpack.sources.RawSource
}
} }
...@@ -67,6 +67,8 @@ export const NEXT_CLIENT_SSR_ENTRY_SUFFIX = '.__sc_client__' ...@@ -67,6 +67,8 @@ export const NEXT_CLIENT_SSR_ENTRY_SUFFIX = '.__sc_client__'
export const FLIGHT_MANIFEST = 'flight-manifest' export const FLIGHT_MANIFEST = 'flight-manifest'
// server/flight-server-css-manifest.json // server/flight-server-css-manifest.json
export const FLIGHT_SERVER_CSS_MANIFEST = 'flight-server-css-manifest' export const FLIGHT_SERVER_CSS_MANIFEST = 'flight-server-css-manifest'
// server/actions-manifest.json
export const ACTIONS_MANIFEST = 'actions-manifest'
// server/middleware-build-manifest.js // server/middleware-build-manifest.js
export const MIDDLEWARE_BUILD_MANIFEST = 'middleware-build-manifest' export const MIDDLEWARE_BUILD_MANIFEST = 'middleware-build-manifest'
// server/middleware-react-loadable-manifest.js // server/middleware-react-loadable-manifest.js
......
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