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 {
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 function getRSCModuleType(source: string): RSCModuleType {
return source.includes(CLIENT_MODULE_LABEL)
export function getRSCModuleInformation(source: string): {
type: RSCModuleType
actions?: string[]
} {
const type = source.includes(CLIENT_MODULE_LABEL)
? RSC_MODULE_TYPES.client
: 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: {
) {
const swcAST = await parseModule(pageFilePath, fileContent)
const { ssg, ssr, runtime } = checkExports(swcAST)
const rsc = getRSCModuleType(fileContent)
const rsc = getRSCModuleInformation(fileContent).type
// default / failsafe value for config
let config: any = {}
......
......@@ -129,6 +129,11 @@ function getBaseSWCOptions({
serverComponents: hasServerComponents
? { isServer: !!isServerLayer }
: undefined,
serverActions: hasServerComponents
? {
isServer: !!isServerLayer,
}
: undefined,
}
}
......
......@@ -1619,6 +1619,7 @@ export default async function getBaseWebpackConfig(
'next-style-loader',
'next-flight-loader',
'next-flight-client-entry-loader',
'next-flight-action-entry-loader',
'noop-loader',
'next-middleware-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 { 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'
const noopHeadPath = require.resolve('next/dist/client/components/noop-head')
......@@ -16,21 +16,19 @@ export default async function transformSource(
}
const callback = this.async()
const buildInfo = getModuleBuildInfo(this._module)
const rscType = getRSCModuleType(source)
// Assign the RSC meta information to buildInfo.
// 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) {
return callback(null, source, sourceMap)
if (buildInfo.rsc?.type !== RSC_MODULE_TYPES.client) {
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)
}
import { createHash } from 'crypto'
import { RSC_MODULE_TYPES } from '../../../shared/lib/constants'
const imageExtensions = ['jpg', 'jpeg', 'png', 'webp', 'avif']
......@@ -12,3 +14,16 @@ export function isClientComponentModule(mod: {
}
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 {
APP_CLIENT_INTERNALS,
COMPILER_NAMES,
EDGE_RUNTIME_WEBPACK,
ACTIONS_MANIFEST,
FLIGHT_SERVER_CSS_MANIFEST,
} from '../../../shared/lib/constants'
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 { normalizePathSep } from '../../../shared/lib/page-path/normalize-path-sep'
......@@ -39,6 +45,9 @@ export const injectedClientEntries = new Map()
export const serverModuleIds = 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 edgeServerCSSManifest: FlightCSSManifest = {}
......@@ -118,6 +127,18 @@ export class FlightClientEntryPlugin {
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) {
......@@ -125,6 +146,9 @@ export class FlightClientEntryPlugin {
ReturnType<typeof this.injectClientEntryAndSSRModules>
> = []
const addActionEntryList: Array<ReturnType<typeof this.injectActionEntry>> =
[]
// Loop over all the entry modules.
function forEachEntryModule(
callback: ({
......@@ -176,6 +200,7 @@ export class FlightClientEntryPlugin {
const internalClientComponentEntryImports = new Set<
ClientComponentImports[0]
>()
const actionEntryImports = new Map<string, string[]>()
for (const connection of compilation.moduleGraph.getOutgoingConnections(
entryModule
......@@ -184,18 +209,22 @@ export class FlightClientEntryPlugin {
const entryDependency = connection.dependency
const entryRequest = connection.dependency.request
const [clientComponentImports] =
this.collectClientComponentsAndCSSForDependency({
const { clientImports, actionImports } =
this.collectComponentInfoFromDependencies({
entryRequest,
compilation,
dependency: entryDependency,
})
actionImports.forEach(([dep, names]) =>
actionEntryImports.set(dep, names)
)
const isAbsoluteRequest = path.isAbsolute(entryRequest)
// Next.js internals are put into a separate entry.
if (!isAbsoluteRequest) {
clientComponentImports.forEach((value) =>
clientImports.forEach((value) =>
internalClientComponentEntryImports.add(value)
)
continue
......@@ -215,7 +244,7 @@ export class FlightClientEntryPlugin {
compiler,
compilation,
entryName: name,
clientComponentImports,
clientImports,
bundlePath,
})
)
......@@ -227,10 +256,25 @@ export class FlightClientEntryPlugin {
compiler,
compilation,
entryName: name,
clientComponentImports: [...internalClientComponentEntryImports],
clientImports: [...internalClientComponentEntryImports],
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
......@@ -330,13 +374,12 @@ export class FlightClientEntryPlugin {
if (tracked.has(entryRequest)) continue
tracked.add(entryRequest)
const [, cssImports] =
this.collectClientComponentsAndCSSForDependency({
entryRequest,
compilation,
dependency: entryDependency,
clientEntryDependencyMap,
})
const { cssImports } = this.collectComponentInfoFromDependencies({
entryRequest,
compilation,
dependency: entryDependency,
clientEntryDependencyMap,
})
Object.assign(cssManifest, cssImports)
}
......@@ -393,7 +436,7 @@ export class FlightClientEntryPlugin {
)
}
collectClientComponentsAndCSSForDependency({
collectComponentInfoFromDependencies({
entryRequest,
compilation,
dependency,
......@@ -403,12 +446,17 @@ export class FlightClientEntryPlugin {
compilation: any
dependency: any /* Dependency */
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.
*/
const visitedBySegment: { [segment: string]: Set<string> } = {}
const clientComponentImports: ClientComponentImports = []
const actionImports: [string, string[]][] = []
const CSSImports: CssImports = {}
const filterClientComponents = (
......@@ -441,6 +489,11 @@ export class FlightClientEntryPlugin {
const isClientComponent = isClientComponentModule(mod)
const actions = getActions(mod)
if (actions) {
actionImports.push([modRequest, actions])
}
if (isCSS) {
const sideEffectFree =
mod.factoryMeta && (mod.factoryMeta as any).sideEffectFree
......@@ -489,26 +542,36 @@ export class FlightClientEntryPlugin {
// Traverse the module graph to find all client components.
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({
compiler,
compilation,
entryName,
clientComponentImports,
clientImports,
bundlePath,
}: {
compiler: webpack.Compiler
compilation: webpack.Compilation
entryName: string
clientComponentImports: ClientComponentImports
clientImports: ClientComponentImports
bundlePath: string
}): [shouldInvalidate: boolean, addEntryPromise: Promise<void>] {
let shouldInvalidate = false
const loaderOptions: NextFlightClientEntryLoaderOptions = {
modules: clientComponentImports,
modules: clientImports,
server: false,
}
......@@ -517,10 +580,10 @@ export class FlightClientEntryPlugin {
// replace them.
const clientLoader = `next-flight-client-entry-loader?${stringify({
modules: this.isEdgeServer
? clientComponentImports.map((importPath) =>
? clientImports.map((importPath) =>
importPath.replace('next/dist/esm/', 'next/dist/')
)
: clientComponentImports,
: clientImports,
server: false,
})}!`
......@@ -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(
compilation: any,
context: string,
......@@ -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__'
export const FLIGHT_MANIFEST = 'flight-manifest'
// server/flight-server-css-manifest.json
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
export const MIDDLEWARE_BUILD_MANIFEST = 'middleware-build-manifest'
// 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