File size: 11,884 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 |
import type { BloomFilter } from '../../../shared/lib/bloom-filter'
import type { Rewrite, CustomRoutes } from '../../../lib/load-custom-routes'
import devalue from 'next/dist/compiled/devalue'
import { webpack, sources } from 'next/dist/compiled/webpack/webpack'
import {
BUILD_MANIFEST,
MIDDLEWARE_BUILD_MANIFEST,
CLIENT_STATIC_FILES_PATH,
CLIENT_STATIC_FILES_RUNTIME_MAIN,
CLIENT_STATIC_FILES_RUNTIME_MAIN_APP,
CLIENT_STATIC_FILES_RUNTIME_POLYFILLS_SYMBOL,
CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH,
CLIENT_STATIC_FILES_RUNTIME_AMP,
SYSTEM_ENTRYPOINTS,
} from '../../../shared/lib/constants'
import type { BuildManifest } from '../../../server/get-page-files'
import getRouteFromEntrypoint from '../../../server/get-route-from-entrypoint'
import { ampFirstEntryNamesMap } from './next-drop-client-page-plugin'
import { getSortedRoutes } from '../../../shared/lib/router/utils'
import { Span } from '../../../trace'
import { getCompilationSpan } from '../utils'
type DeepMutable<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> }
export type ClientBuildManifest = {
[key: string]: string[]
}
// Add the runtime ssg manifest file as a lazy-loaded file dependency.
// We also stub this file out for development mode (when it is not
// generated).
export const srcEmptySsgManifest = `self.__SSG_MANIFEST=new Set;self.__SSG_MANIFEST_CB&&self.__SSG_MANIFEST_CB()`
// nodejs: '/static/<build id>/low-priority.js'
function buildNodejsLowPriorityPath(filename: string, buildId: string) {
return `${CLIENT_STATIC_FILES_PATH}/${buildId}/${filename}`
}
export function createEdgeRuntimeManifest(
originAssetMap: Partial<BuildManifest>
): string {
const manifestFilenames = ['_buildManifest.js', '_ssgManifest.js']
const assetMap: Partial<BuildManifest> = {
...originAssetMap,
lowPriorityFiles: [],
}
// we use globalThis here because middleware can be node
// which doesn't have "self"
const manifestDefCode = `globalThis.__BUILD_MANIFEST = ${JSON.stringify(
assetMap,
null,
2
)};\n`
// edge lowPriorityFiles item: '"/static/" + process.env.__NEXT_BUILD_ID + "/low-priority.js"'.
// Since lowPriorityFiles is not fixed and relying on `process.env.__NEXT_BUILD_ID`, we'll produce code creating it dynamically.
const lowPriorityFilesCode =
`globalThis.__BUILD_MANIFEST.lowPriorityFiles = [\n` +
manifestFilenames
.map((filename) => {
return `"/static/" + process.env.__NEXT_BUILD_ID + "/${filename}",\n`
})
.join(',') +
`\n];`
return manifestDefCode + lowPriorityFilesCode
}
function normalizeRewrite(item: {
source: string
destination: string
has?: any
}): CustomRoutes['rewrites']['beforeFiles'][0] {
return {
has: item.has,
source: item.source,
destination: item.destination,
}
}
export function normalizeRewritesForBuildManifest(
rewrites: CustomRoutes['rewrites']
): CustomRoutes['rewrites'] {
return {
afterFiles: rewrites.afterFiles
?.map(processRoute)
?.map((item) => normalizeRewrite(item)),
beforeFiles: rewrites.beforeFiles
?.map(processRoute)
?.map((item) => normalizeRewrite(item)),
fallback: rewrites.fallback
?.map(processRoute)
?.map((item) => normalizeRewrite(item)),
}
}
// This function takes the asset map generated in BuildManifestPlugin and creates a
// reduced version to send to the client.
export function generateClientManifest(
assetMap: BuildManifest,
rewrites: CustomRoutes['rewrites'],
clientRouterFilters?: {
staticFilter: ReturnType<BloomFilter['export']>
dynamicFilter: ReturnType<BloomFilter['export']>
},
compiler?: any,
compilation?: any
): string | undefined {
const compilationSpan = compilation
? getCompilationSpan(compilation)
: compiler
? getCompilationSpan(compiler)
: new Span({ name: 'client-manifest' })
const genClientManifestSpan = compilationSpan?.traceChild(
'NextJsBuildManifest-generateClientManifest'
)
return genClientManifestSpan?.traceFn(() => {
const clientManifest: ClientBuildManifest = {
__rewrites: normalizeRewritesForBuildManifest(rewrites) as any,
__routerFilterStatic: clientRouterFilters?.staticFilter as any,
__routerFilterDynamic: clientRouterFilters?.dynamicFilter as any,
}
const appDependencies = new Set(assetMap.pages['/_app'])
const sortedPageKeys = getSortedRoutes(Object.keys(assetMap.pages))
sortedPageKeys.forEach((page) => {
const dependencies = assetMap.pages[page]
if (page === '/_app') return
// Filter out dependencies in the _app entry, because those will have already
// been loaded by the client prior to a navigation event
const filteredDeps = dependencies.filter(
(dep) => !appDependencies.has(dep)
)
// The manifest can omit the page if it has no requirements
if (filteredDeps.length) {
clientManifest[page] = filteredDeps
}
})
// provide the sorted pages as an array so we don't rely on the object's keys
// being in order and we don't slow down look-up time for page assets
clientManifest.sortedPages = sortedPageKeys
return devalue(clientManifest)
})
}
export function getEntrypointFiles(entrypoint: any): string[] {
return (
entrypoint
?.getFiles()
.filter((file: string) => {
// We don't want to include `.hot-update.js` files into the initial page
return /(?<!\.hot-update)\.(js|css)($|\?)/.test(file)
})
.map((file: string) => file.replace(/\\/g, '/')) ?? []
)
}
export const processRoute = (r: Rewrite) => {
const rewrite = { ...r }
// omit external rewrite destinations since these aren't
// handled client-side
if (!rewrite?.destination?.startsWith('/')) {
delete (rewrite as any).destination
}
return rewrite
}
// This plugin creates a build-manifest.json for all assets that are being output
// It has a mapping of "entry" filename to real filename. Because the real filename can be hashed in production
export default class BuildManifestPlugin {
private buildId: string
private rewrites: CustomRoutes['rewrites']
private isDevFallback: boolean
private appDirEnabled: boolean
private clientRouterFilters?: Parameters<typeof generateClientManifest>[2]
constructor(options: {
buildId: string
rewrites: CustomRoutes['rewrites']
isDevFallback?: boolean
appDirEnabled: boolean
clientRouterFilters?: Parameters<typeof generateClientManifest>[2]
}) {
this.buildId = options.buildId
this.isDevFallback = !!options.isDevFallback
this.rewrites = {
beforeFiles: [],
afterFiles: [],
fallback: [],
}
this.appDirEnabled = options.appDirEnabled
this.clientRouterFilters = options.clientRouterFilters
this.rewrites.beforeFiles = options.rewrites.beforeFiles.map(processRoute)
this.rewrites.afterFiles = options.rewrites.afterFiles.map(processRoute)
this.rewrites.fallback = options.rewrites.fallback.map(processRoute)
}
createAssets(compiler: any, compilation: any) {
const compilationSpan =
getCompilationSpan(compilation) ?? getCompilationSpan(compiler)
if (!compilationSpan) {
throw new Error('No span found for compilation')
}
const createAssetsSpan = compilationSpan.traceChild(
'NextJsBuildManifest-createassets'
)
return createAssetsSpan.traceFn(() => {
const entrypoints: Map<string, any> = compilation.entrypoints
const assetMap: DeepMutable<BuildManifest> = {
polyfillFiles: [],
devFiles: [],
ampDevFiles: [],
lowPriorityFiles: [],
rootMainFiles: [],
rootMainFilesTree: {},
pages: { '/_app': [] },
ampFirstPages: [],
}
const ampFirstEntryNames = ampFirstEntryNamesMap.get(compilation)
if (ampFirstEntryNames) {
for (const entryName of ampFirstEntryNames) {
const pagePath = getRouteFromEntrypoint(entryName)
if (!pagePath) {
continue
}
assetMap.ampFirstPages.push(pagePath)
}
}
const mainFiles = new Set(
getEntrypointFiles(entrypoints.get(CLIENT_STATIC_FILES_RUNTIME_MAIN))
)
if (this.appDirEnabled) {
assetMap.rootMainFiles = [
...new Set(
getEntrypointFiles(
entrypoints.get(CLIENT_STATIC_FILES_RUNTIME_MAIN_APP)
)
),
]
}
const compilationAssets: {
name: string
source: typeof sources.RawSource
info: object
}[] = compilation.getAssets()
assetMap.polyfillFiles = compilationAssets
.filter((p) => {
// Ensure only .js files are passed through
if (!p.name.endsWith('.js')) {
return false
}
return (
p.info && CLIENT_STATIC_FILES_RUNTIME_POLYFILLS_SYMBOL in p.info
)
})
.map((v) => v.name)
assetMap.devFiles = getEntrypointFiles(
entrypoints.get(CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH)
).filter((file) => !mainFiles.has(file))
assetMap.ampDevFiles = getEntrypointFiles(
entrypoints.get(CLIENT_STATIC_FILES_RUNTIME_AMP)
)
for (const entrypoint of compilation.entrypoints.values()) {
if (SYSTEM_ENTRYPOINTS.has(entrypoint.name)) continue
const pagePath = getRouteFromEntrypoint(entrypoint.name)
if (!pagePath) {
continue
}
const filesForPage = getEntrypointFiles(entrypoint)
assetMap.pages[pagePath] = [...new Set([...mainFiles, ...filesForPage])]
}
if (!this.isDevFallback) {
// Add the runtime build manifest file (generated later in this file)
// as a dependency for the app. If the flag is false, the file won't be
// downloaded by the client.
const buildManifestPath = buildNodejsLowPriorityPath(
'_buildManifest.js',
this.buildId
)
const ssgManifestPath = buildNodejsLowPriorityPath(
'_ssgManifest.js',
this.buildId
)
assetMap.lowPriorityFiles.push(buildManifestPath, ssgManifestPath)
compilation.emitAsset(
ssgManifestPath,
new sources.RawSource(srcEmptySsgManifest)
)
}
assetMap.pages = Object.keys(assetMap.pages)
.sort()
.reduce(
// eslint-disable-next-line
(a, c) => ((a[c] = assetMap.pages[c]), a),
{} as typeof assetMap.pages
)
let buildManifestName = BUILD_MANIFEST
if (this.isDevFallback) {
buildManifestName = `fallback-${BUILD_MANIFEST}`
}
compilation.emitAsset(
buildManifestName,
new sources.RawSource(JSON.stringify(assetMap, null, 2))
)
compilation.emitAsset(
`server/${MIDDLEWARE_BUILD_MANIFEST}.js`,
new sources.RawSource(`${createEdgeRuntimeManifest(assetMap)}`)
)
if (!this.isDevFallback) {
compilation.emitAsset(
`${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.js`,
new sources.RawSource(
`self.__BUILD_MANIFEST = ${generateClientManifest(
assetMap,
this.rewrites,
this.clientRouterFilters,
compiler,
compilation
)};self.__BUILD_MANIFEST_CB && self.__BUILD_MANIFEST_CB()`
)
)
}
})
}
apply(compiler: webpack.Compiler) {
compiler.hooks.make.tap('NextJsBuildManifest', (compilation: any) => {
compilation.hooks.processAssets.tap(
{
name: 'NextJsBuildManifest',
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
},
() => {
this.createAssets(compiler, compilation)
}
)
})
return
}
}
|