|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import path from 'path' |
|
|
import { webpack, sources } from 'next/dist/compiled/webpack/webpack' |
|
|
import { |
|
|
APP_CLIENT_INTERNALS, |
|
|
BARREL_OPTIMIZATION_PREFIX, |
|
|
CLIENT_REFERENCE_MANIFEST, |
|
|
SYSTEM_ENTRYPOINTS, |
|
|
} from '../../../shared/lib/constants' |
|
|
import { relative } from 'path' |
|
|
import { getProxiedPluginState } from '../../build-context' |
|
|
|
|
|
import { WEBPACK_LAYERS } from '../../../lib/constants' |
|
|
import { normalizePagePath } from '../../../shared/lib/page-path/normalize-page-path' |
|
|
import { CLIENT_STATIC_FILES_RUNTIME_MAIN_APP } from '../../../shared/lib/constants' |
|
|
import { getDeploymentIdQueryOrEmptyString } from '../../deployment-id' |
|
|
import { |
|
|
formatBarrelOptimizedResource, |
|
|
getModuleReferencesInOrder, |
|
|
} from '../utils' |
|
|
import type { ChunkGroup } from 'webpack' |
|
|
import { encodeURIPath } from '../../../shared/lib/encode-uri-path' |
|
|
import type { ModuleInfo } from './flight-client-entry-plugin' |
|
|
|
|
|
interface Options { |
|
|
dev: boolean |
|
|
appDir: string |
|
|
experimentalInlineCss: boolean |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type ModuleId = string | number |
|
|
|
|
|
|
|
|
export type ManifestChunks = Array<string> |
|
|
|
|
|
const pluginState = getProxiedPluginState({ |
|
|
ssrModules: {} as { [ssrModuleId: string]: ModuleInfo }, |
|
|
edgeSsrModules: {} as { [ssrModuleId: string]: ModuleInfo }, |
|
|
|
|
|
rscModules: {} as { [rscModuleId: string]: ModuleInfo }, |
|
|
edgeRscModules: {} as { [rscModuleId: string]: ModuleInfo }, |
|
|
}) |
|
|
|
|
|
export interface ManifestNode { |
|
|
[moduleExport: string]: { |
|
|
|
|
|
|
|
|
|
|
|
id: ModuleId |
|
|
|
|
|
|
|
|
|
|
|
name: string |
|
|
|
|
|
|
|
|
|
|
|
chunks: ManifestChunks |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async?: boolean |
|
|
} |
|
|
} |
|
|
|
|
|
export interface ClientReferenceManifestForRsc { |
|
|
clientModules: ManifestNode |
|
|
rscModuleMapping: { |
|
|
[moduleId: string]: ManifestNode |
|
|
} |
|
|
edgeRscModuleMapping: { |
|
|
[moduleId: string]: ManifestNode |
|
|
} |
|
|
} |
|
|
|
|
|
export type CssResource = InlinedCssFile | UninlinedCssFile |
|
|
|
|
|
interface InlinedCssFile { |
|
|
path: string |
|
|
inlined: true |
|
|
content: string |
|
|
} |
|
|
|
|
|
interface UninlinedCssFile { |
|
|
path: string |
|
|
inlined: false |
|
|
} |
|
|
|
|
|
export interface ClientReferenceManifest extends ClientReferenceManifestForRsc { |
|
|
readonly moduleLoading: { |
|
|
prefix: string |
|
|
crossOrigin?: 'use-credentials' | '' |
|
|
} |
|
|
ssrModuleMapping: { |
|
|
[moduleId: string]: ManifestNode |
|
|
} |
|
|
edgeSSRModuleMapping: { |
|
|
[moduleId: string]: ManifestNode |
|
|
} |
|
|
entryCSSFiles: { |
|
|
[entry: string]: CssResource[] |
|
|
} |
|
|
entryJSFiles?: { |
|
|
[entry: string]: string[] |
|
|
} |
|
|
} |
|
|
|
|
|
function getAppPathRequiredChunks( |
|
|
chunkGroup: webpack.ChunkGroup, |
|
|
excludedFiles: Set<string> |
|
|
) { |
|
|
const deploymentIdChunkQuery = getDeploymentIdQueryOrEmptyString() |
|
|
|
|
|
const chunks: Array<string> = [] |
|
|
chunkGroup.chunks.forEach((chunk) => { |
|
|
if (SYSTEM_ENTRYPOINTS.has(chunk.name || '')) { |
|
|
return null |
|
|
} |
|
|
|
|
|
|
|
|
if (chunk.id != null) { |
|
|
const chunkId = '' + chunk.id |
|
|
chunk.files.forEach((file) => { |
|
|
|
|
|
|
|
|
if (!file.endsWith('.js')) return null |
|
|
if (file.endsWith('.hot-update.js')) return null |
|
|
if (excludedFiles.has(file)) return null |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return chunks.push( |
|
|
chunkId, |
|
|
encodeURIPath(file) + deploymentIdChunkQuery |
|
|
) |
|
|
}) |
|
|
} |
|
|
}) |
|
|
return chunks |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function entryNameToGroupName(entryName: string) { |
|
|
let groupName = entryName |
|
|
.slice(0, entryName.lastIndexOf('/')) |
|
|
|
|
|
.replace(/\/@[^/]+/g, '') |
|
|
|
|
|
.replace(/\/\([^/]+\)(?=(\/|$))/g, '') |
|
|
|
|
|
|
|
|
|
|
|
.replace(/\/\[?\[\.\.\.[^\]]*]]?/g, '') |
|
|
|
|
|
|
|
|
groupName = groupName |
|
|
.replace(/^.+\/\(\.\.\.\)/g, 'app/') |
|
|
.replace(/\/\(\.\)/g, '/') |
|
|
|
|
|
|
|
|
while (/\/[^/]+\/\(\.\.\)/.test(groupName)) { |
|
|
groupName = groupName.replace(/\/[^/]+\/\(\.\.\)/g, '/') |
|
|
} |
|
|
|
|
|
return groupName |
|
|
} |
|
|
|
|
|
function mergeManifest( |
|
|
manifest: ClientReferenceManifest, |
|
|
manifestToMerge: ClientReferenceManifest |
|
|
) { |
|
|
Object.assign(manifest.clientModules, manifestToMerge.clientModules) |
|
|
Object.assign(manifest.ssrModuleMapping, manifestToMerge.ssrModuleMapping) |
|
|
Object.assign( |
|
|
manifest.edgeSSRModuleMapping, |
|
|
manifestToMerge.edgeSSRModuleMapping |
|
|
) |
|
|
Object.assign(manifest.entryCSSFiles, manifestToMerge.entryCSSFiles) |
|
|
Object.assign(manifest.rscModuleMapping, manifestToMerge.rscModuleMapping) |
|
|
Object.assign( |
|
|
manifest.edgeRscModuleMapping, |
|
|
manifestToMerge.edgeRscModuleMapping |
|
|
) |
|
|
} |
|
|
|
|
|
const PLUGIN_NAME = 'ClientReferenceManifestPlugin' |
|
|
|
|
|
export class ClientReferenceManifestPlugin { |
|
|
dev: Options['dev'] = false |
|
|
appDir: Options['appDir'] |
|
|
appDirBase: string |
|
|
experimentalInlineCss: Options['experimentalInlineCss'] |
|
|
|
|
|
constructor(options: Options) { |
|
|
this.dev = options.dev |
|
|
this.appDir = options.appDir |
|
|
this.appDirBase = path.dirname(this.appDir) + path.sep |
|
|
this.experimentalInlineCss = options.experimentalInlineCss |
|
|
} |
|
|
|
|
|
apply(compiler: webpack.Compiler) { |
|
|
compiler.hooks.compilation.tap(PLUGIN_NAME, (compilation) => { |
|
|
compilation.hooks.processAssets.tap( |
|
|
{ |
|
|
name: PLUGIN_NAME, |
|
|
|
|
|
|
|
|
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ANALYSE, |
|
|
}, |
|
|
() => this.createAsset(compilation, compiler.context) |
|
|
) |
|
|
}) |
|
|
} |
|
|
|
|
|
createAsset(compilation: webpack.Compilation, context: string) { |
|
|
const manifestsPerGroup = new Map<string, ClientReferenceManifest[]>() |
|
|
const manifestEntryFiles: string[] = [] |
|
|
|
|
|
const configuredCrossOriginLoading = |
|
|
compilation.outputOptions.crossOriginLoading |
|
|
const crossOriginMode = |
|
|
typeof configuredCrossOriginLoading === 'string' |
|
|
? configuredCrossOriginLoading === 'use-credentials' |
|
|
? configuredCrossOriginLoading |
|
|
: '' |
|
|
: undefined |
|
|
|
|
|
if (typeof compilation.outputOptions.publicPath !== 'string') { |
|
|
throw new Error( |
|
|
'Expected webpack publicPath to be a string when using App Router. To customize where static assets are loaded from, use the `assetPrefix` option in next.config.js. If you are customizing your webpack config please make sure you are not modifying or removing the publicPath configuration option' |
|
|
) |
|
|
} |
|
|
const prefix = compilation.outputOptions.publicPath || '' |
|
|
|
|
|
|
|
|
|
|
|
const rootMainFiles: Set<string> = new Set() |
|
|
compilation.entrypoints |
|
|
.get(CLIENT_STATIC_FILES_RUNTIME_MAIN_APP) |
|
|
?.getFiles() |
|
|
.forEach((file) => { |
|
|
if (/(?<!\.hot-update)\.(js|css)($|\?)/.test(file)) { |
|
|
rootMainFiles.add(file.replace(/\\/g, '/')) |
|
|
} |
|
|
}) |
|
|
|
|
|
for (let [entryName, entrypoint] of compilation.entrypoints) { |
|
|
if ( |
|
|
entryName === CLIENT_STATIC_FILES_RUNTIME_MAIN_APP || |
|
|
entryName === APP_CLIENT_INTERNALS |
|
|
) { |
|
|
entryName = '' |
|
|
} else if (!/^app[\\/]/.test(entryName)) { |
|
|
continue |
|
|
} |
|
|
|
|
|
const manifest: ClientReferenceManifest = { |
|
|
moduleLoading: { |
|
|
prefix, |
|
|
crossOrigin: crossOriginMode, |
|
|
}, |
|
|
ssrModuleMapping: {}, |
|
|
edgeSSRModuleMapping: {}, |
|
|
clientModules: {}, |
|
|
entryCSSFiles: {}, |
|
|
rscModuleMapping: {}, |
|
|
edgeRscModuleMapping: {}, |
|
|
} |
|
|
|
|
|
|
|
|
const chunkEntryName = (this.appDirBase + entryName).replace( |
|
|
/[\\/]/g, |
|
|
path.sep |
|
|
) |
|
|
|
|
|
manifest.entryCSSFiles[chunkEntryName] = entrypoint |
|
|
.getFiles() |
|
|
.filter((f) => !f.startsWith('static/css/pages/') && f.endsWith('.css')) |
|
|
.map((file) => { |
|
|
const source = compilation.getAsset(file)!.source.source() |
|
|
if ( |
|
|
this.experimentalInlineCss && |
|
|
|
|
|
|
|
|
!this.dev |
|
|
) { |
|
|
return { |
|
|
inlined: true, |
|
|
path: file, |
|
|
content: typeof source === 'string' ? source : source.toString(), |
|
|
} |
|
|
} |
|
|
return { |
|
|
inlined: false, |
|
|
path: file, |
|
|
} |
|
|
}) |
|
|
|
|
|
const requiredChunks = getAppPathRequiredChunks(entrypoint, rootMainFiles) |
|
|
const recordModule = (modId: ModuleId, mod: webpack.NormalModule) => { |
|
|
let resource = |
|
|
mod.type === 'css/mini-extract' |
|
|
? mod.identifier().slice(mod.identifier().lastIndexOf('!') + 1) |
|
|
: mod.resource |
|
|
|
|
|
if (!resource) { |
|
|
return |
|
|
} |
|
|
|
|
|
const moduleReferences = manifest.clientModules |
|
|
const moduleIdMapping = manifest.ssrModuleMapping |
|
|
const edgeModuleIdMapping = manifest.edgeSSRModuleMapping |
|
|
|
|
|
const rscIdMapping = manifest.rscModuleMapping |
|
|
const edgeRscIdMapping = manifest.edgeRscModuleMapping |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let ssrNamedModuleId = relative( |
|
|
context, |
|
|
mod.resourceResolveData?.path || resource |
|
|
) |
|
|
|
|
|
const rscNamedModuleId = relative( |
|
|
context, |
|
|
mod.resourceResolveData?.path || resource |
|
|
) |
|
|
|
|
|
if (!ssrNamedModuleId.startsWith('.')) |
|
|
ssrNamedModuleId = `./${ssrNamedModuleId.replace(/\\/g, '/')}` |
|
|
|
|
|
|
|
|
|
|
|
const esmResource = /[\\/]next[\\/]dist[\\/]/.test(resource) |
|
|
? resource.replace( |
|
|
/[\\/]next[\\/]dist[\\/]/, |
|
|
'/next/dist/esm/'.replace(/\//g, path.sep) |
|
|
) |
|
|
: null |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (mod.matchResource?.startsWith(BARREL_OPTIMIZATION_PREFIX)) { |
|
|
ssrNamedModuleId = formatBarrelOptimizedResource( |
|
|
ssrNamedModuleId, |
|
|
mod.matchResource |
|
|
) |
|
|
resource = formatBarrelOptimizedResource(resource, mod.matchResource) |
|
|
} |
|
|
|
|
|
function addClientReference() { |
|
|
const isAsync = Boolean( |
|
|
compilation.moduleGraph.isAsync(mod) || |
|
|
pluginState.ssrModules[ssrNamedModuleId]?.async || |
|
|
pluginState.edgeSsrModules[ssrNamedModuleId]?.async |
|
|
) |
|
|
|
|
|
const exportName = resource |
|
|
manifest.clientModules[exportName] = { |
|
|
id: modId, |
|
|
name: '*', |
|
|
chunks: requiredChunks, |
|
|
async: isAsync, |
|
|
} |
|
|
if (esmResource) { |
|
|
const edgeExportName = esmResource |
|
|
manifest.clientModules[edgeExportName] = |
|
|
manifest.clientModules[exportName] |
|
|
} |
|
|
} |
|
|
|
|
|
function addSSRIdMapping() { |
|
|
const exportName = resource |
|
|
const moduleInfo = pluginState.ssrModules[ssrNamedModuleId] |
|
|
|
|
|
if (moduleInfo) { |
|
|
moduleIdMapping[modId] = moduleIdMapping[modId] || {} |
|
|
moduleIdMapping[modId]['*'] = { |
|
|
...manifest.clientModules[exportName], |
|
|
|
|
|
|
|
|
|
|
|
chunks: [], |
|
|
id: moduleInfo.moduleId, |
|
|
async: moduleInfo.async, |
|
|
} |
|
|
} |
|
|
|
|
|
const edgeModuleInfo = pluginState.edgeSsrModules[ssrNamedModuleId] |
|
|
|
|
|
if (edgeModuleInfo) { |
|
|
edgeModuleIdMapping[modId] = edgeModuleIdMapping[modId] || {} |
|
|
edgeModuleIdMapping[modId]['*'] = { |
|
|
...manifest.clientModules[exportName], |
|
|
|
|
|
|
|
|
|
|
|
chunks: [], |
|
|
id: edgeModuleInfo.moduleId, |
|
|
async: edgeModuleInfo.async, |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
function addRSCIdMapping() { |
|
|
const exportName = resource |
|
|
const moduleInfo = pluginState.rscModules[rscNamedModuleId] |
|
|
|
|
|
if (moduleInfo) { |
|
|
rscIdMapping[modId] = rscIdMapping[modId] || {} |
|
|
rscIdMapping[modId]['*'] = { |
|
|
...manifest.clientModules[exportName], |
|
|
|
|
|
|
|
|
|
|
|
chunks: [], |
|
|
id: moduleInfo.moduleId, |
|
|
async: moduleInfo.async, |
|
|
} |
|
|
} |
|
|
|
|
|
const edgeModuleInfo = pluginState.ssrModules[rscNamedModuleId] |
|
|
|
|
|
if (edgeModuleInfo) { |
|
|
edgeRscIdMapping[modId] = edgeRscIdMapping[modId] || {} |
|
|
edgeRscIdMapping[modId]['*'] = { |
|
|
...manifest.clientModules[exportName], |
|
|
|
|
|
|
|
|
|
|
|
chunks: [], |
|
|
id: edgeModuleInfo.moduleId, |
|
|
async: edgeModuleInfo.async, |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
addClientReference() |
|
|
addSSRIdMapping() |
|
|
addRSCIdMapping() |
|
|
|
|
|
manifest.clientModules = moduleReferences |
|
|
manifest.ssrModuleMapping = moduleIdMapping |
|
|
manifest.edgeSSRModuleMapping = edgeModuleIdMapping |
|
|
manifest.rscModuleMapping = rscIdMapping |
|
|
manifest.edgeRscModuleMapping = edgeRscIdMapping |
|
|
} |
|
|
|
|
|
const checkedChunkGroups = new Set() |
|
|
const checkedChunks = new Set() |
|
|
|
|
|
function recordChunkGroup(chunkGroup: ChunkGroup) { |
|
|
|
|
|
if (checkedChunkGroups.has(chunkGroup)) return |
|
|
checkedChunkGroups.add(chunkGroup) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
chunkGroup.chunks.forEach((chunk: webpack.Chunk) => { |
|
|
|
|
|
if (checkedChunks.has(chunk)) return |
|
|
checkedChunks.add(chunk) |
|
|
const entryMods = |
|
|
compilation.chunkGraph.getChunkEntryModulesIterable(chunk) |
|
|
for (const mod of entryMods) { |
|
|
if (mod.layer !== WEBPACK_LAYERS.appPagesBrowser) continue |
|
|
|
|
|
const request = (mod as webpack.NormalModule).request |
|
|
|
|
|
if ( |
|
|
!request || |
|
|
!request.includes('next-flight-client-entry-loader.js?') |
|
|
) { |
|
|
continue |
|
|
} |
|
|
|
|
|
const connections = getModuleReferencesInOrder( |
|
|
mod, |
|
|
compilation.moduleGraph |
|
|
) |
|
|
|
|
|
for (const connection of connections) { |
|
|
const dependency = connection.dependency |
|
|
if (!dependency) continue |
|
|
|
|
|
const clientEntryMod = compilation.moduleGraph.getResolvedModule( |
|
|
dependency |
|
|
) as webpack.NormalModule |
|
|
const modId = compilation.chunkGraph.getModuleId( |
|
|
clientEntryMod |
|
|
) as string | number | null |
|
|
|
|
|
if (modId !== null) { |
|
|
recordModule(modId, clientEntryMod) |
|
|
} else { |
|
|
|
|
|
if ( |
|
|
connection.module?.constructor.name === 'ConcatenatedModule' |
|
|
) { |
|
|
const concatenatedMod = connection.module |
|
|
const concatenatedModId = |
|
|
compilation.chunkGraph.getModuleId(concatenatedMod) |
|
|
if (concatenatedModId) { |
|
|
recordModule(concatenatedModId, clientEntryMod) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
}) |
|
|
|
|
|
|
|
|
for (const child of chunkGroup.childrenIterable) { |
|
|
recordChunkGroup(child) |
|
|
} |
|
|
} |
|
|
|
|
|
recordChunkGroup(entrypoint) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (/\/page(\.[^/]+)?$/.test(entryName)) { |
|
|
manifestEntryFiles.push(entryName.replace(/\/page(\.[^/]+)?$/, '/page')) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (/\/route$/.test(entryName)) { |
|
|
manifestEntryFiles.push(entryName) |
|
|
} |
|
|
|
|
|
const groupName = entryNameToGroupName(entryName) |
|
|
if (!manifestsPerGroup.has(groupName)) { |
|
|
manifestsPerGroup.set(groupName, []) |
|
|
} |
|
|
manifestsPerGroup.get(groupName)!.push(manifest) |
|
|
} |
|
|
|
|
|
|
|
|
for (const pageName of manifestEntryFiles) { |
|
|
const mergedManifest: ClientReferenceManifest = { |
|
|
moduleLoading: { |
|
|
prefix, |
|
|
crossOrigin: crossOriginMode, |
|
|
}, |
|
|
ssrModuleMapping: {}, |
|
|
edgeSSRModuleMapping: {}, |
|
|
clientModules: {}, |
|
|
entryCSSFiles: {}, |
|
|
rscModuleMapping: {}, |
|
|
edgeRscModuleMapping: {}, |
|
|
} |
|
|
|
|
|
const segments = [...entryNameToGroupName(pageName).split('/'), 'page'] |
|
|
let group = '' |
|
|
for (const segment of segments) { |
|
|
for (const manifest of manifestsPerGroup.get(group) || []) { |
|
|
mergeManifest(mergedManifest, manifest) |
|
|
} |
|
|
group += (group ? '/' : '') + segment |
|
|
} |
|
|
|
|
|
const json = JSON.stringify(mergedManifest) |
|
|
|
|
|
const pagePath = pageName.replace(/%5F/g, '_') |
|
|
const pageBundlePath = normalizePagePath(pagePath.slice('app'.length)) |
|
|
compilation.emitAsset( |
|
|
'server/app' + pageBundlePath + '_' + CLIENT_REFERENCE_MANIFEST + '.js', |
|
|
new sources.RawSource( |
|
|
`globalThis.__RSC_MANIFEST=(globalThis.__RSC_MANIFEST||{});globalThis.__RSC_MANIFEST[${JSON.stringify( |
|
|
pagePath.slice('app'.length) |
|
|
)}]=${json}` |
|
|
) as unknown as webpack.sources.RawSource |
|
|
) |
|
|
} |
|
|
} |
|
|
} |
|
|
|