2020-03-29 01:18:22 +01:00
|
|
|
import devalue from 'next/dist/compiled/devalue'
|
2021-01-14 02:59:08 +01:00
|
|
|
import {
|
|
|
|
webpack,
|
|
|
|
isWebpack5,
|
|
|
|
sources,
|
|
|
|
} from 'next/dist/compiled/webpack/webpack'
|
2019-03-12 05:01:50 +01:00
|
|
|
import {
|
|
|
|
BUILD_MANIFEST,
|
2019-08-08 19:14:33 +02:00
|
|
|
CLIENT_STATIC_FILES_PATH,
|
2019-05-29 13:57:26 +02:00
|
|
|
CLIENT_STATIC_FILES_RUNTIME_MAIN,
|
2019-11-02 02:00:56 +01:00
|
|
|
CLIENT_STATIC_FILES_RUNTIME_POLYFILLS,
|
2020-04-19 19:58:31 +02:00
|
|
|
CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH,
|
2020-06-20 21:59:47 +02:00
|
|
|
CLIENT_STATIC_FILES_RUNTIME_AMP,
|
2019-09-04 16:00:54 +02:00
|
|
|
} from '../../../next-server/lib/constants'
|
2020-02-29 23:09:42 +01:00
|
|
|
import { BuildManifest } from '../../../next-server/server/get-page-files'
|
2020-06-04 19:32:45 +02:00
|
|
|
import getRouteFromEntrypoint from '../../../next-server/server/get-route-from-entrypoint'
|
2020-06-14 14:49:46 +02:00
|
|
|
import { ampFirstEntryNamesMap } from './next-drop-client-page-plugin'
|
2020-08-13 14:39:36 +02:00
|
|
|
import { Rewrite } from '../../../lib/load-custom-routes'
|
|
|
|
import { getSortedRoutes } from '../../../next-server/lib/router/utils'
|
2020-12-29 22:21:35 +01:00
|
|
|
import { tracer, traceFn } from '../../tracer'
|
2021-01-10 02:12:13 +01:00
|
|
|
import { spans } from './profiling-plugin'
|
2020-08-03 14:26:23 +02:00
|
|
|
|
2020-08-14 06:30:25 +02:00
|
|
|
type DeepMutable<T> = { -readonly [P in keyof T]: DeepMutable<T[P]> }
|
|
|
|
|
2020-08-12 22:42:05 +02:00
|
|
|
export type ClientBuildManifest = Record<string, string[]>
|
|
|
|
|
2019-08-08 19:14:33 +02:00
|
|
|
// This function takes the asset map generated in BuildManifestPlugin and creates a
|
|
|
|
// reduced version to send to the client.
|
2020-06-08 20:11:00 +02:00
|
|
|
function generateClientManifest(
|
2021-01-10 02:12:13 +01:00
|
|
|
compiler: any,
|
2020-02-29 23:09:42 +01:00
|
|
|
assetMap: BuildManifest,
|
2020-08-13 14:39:36 +02:00
|
|
|
rewrites: Rewrite[]
|
2020-06-08 20:11:00 +02:00
|
|
|
): string {
|
2021-01-10 02:12:13 +01:00
|
|
|
return tracer.withSpan(spans.get(compiler), () => {
|
|
|
|
const span = tracer.startSpan('NextJsBuildManifest-generateClientManifest')
|
|
|
|
return traceFn(span, () => {
|
|
|
|
const clientManifest: ClientBuildManifest = {
|
|
|
|
// TODO: update manifest type to include rewrites
|
|
|
|
__rewrites: rewrites as any,
|
2020-12-29 22:21:35 +01:00
|
|
|
}
|
2021-01-10 02:12:13 +01:00
|
|
|
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
|
2020-08-13 14:39:36 +02:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
return devalue(clientManifest)
|
|
|
|
})
|
2020-12-29 22:21:35 +01:00
|
|
|
})
|
2019-08-08 19:14:33 +02:00
|
|
|
}
|
|
|
|
|
2020-06-11 10:57:24 +02:00
|
|
|
function isJsFile(file: string): boolean {
|
2020-06-26 06:26:09 +02:00
|
|
|
// We don't want to include `.hot-update.js` files into the initial page
|
|
|
|
return !file.endsWith('.hot-update.js') && file.endsWith('.js')
|
2020-06-11 10:57:24 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 14:26:23 +02:00
|
|
|
function getFilesArray(files: any) {
|
|
|
|
if (!files) {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
if (isWebpack5) {
|
|
|
|
return Array.from(files)
|
|
|
|
}
|
|
|
|
|
|
|
|
return files
|
|
|
|
}
|
|
|
|
|
2018-04-12 09:47:42 +02:00
|
|
|
// 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 {
|
2019-08-08 19:14:33 +02:00
|
|
|
private buildId: string
|
2020-08-13 14:39:36 +02:00
|
|
|
private rewrites: Rewrite[]
|
2019-08-08 19:14:33 +02:00
|
|
|
|
2020-11-18 19:30:00 +01:00
|
|
|
constructor(options: { buildId: string; rewrites: Rewrite[] }) {
|
2019-08-08 19:14:33 +02:00
|
|
|
this.buildId = options.buildId
|
2020-11-18 19:30:00 +01:00
|
|
|
|
2020-08-13 14:39:36 +02:00
|
|
|
this.rewrites = options.rewrites.map((r) => {
|
|
|
|
const rewrite = { ...r }
|
|
|
|
|
|
|
|
// omit external rewrite destinations since these aren't
|
|
|
|
// handled client-side
|
|
|
|
if (!rewrite.destination.startsWith('/')) {
|
2020-12-04 11:14:55 +01:00
|
|
|
delete (rewrite as any).destination
|
2020-08-13 14:39:36 +02:00
|
|
|
}
|
|
|
|
return rewrite
|
|
|
|
})
|
2019-08-08 19:14:33 +02:00
|
|
|
}
|
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
createAssets(compiler: any, compilation: any, assets: any) {
|
|
|
|
return tracer.withSpan(spans.get(compiler), () => {
|
|
|
|
const span = tracer.startSpan('NextJsBuildManifest-createassets')
|
|
|
|
return traceFn(span, () => {
|
2021-01-14 02:59:08 +01:00
|
|
|
const namedChunks: Map<string, webpack.compilation.Chunk> =
|
2021-01-10 02:12:13 +01:00
|
|
|
compilation.namedChunks
|
|
|
|
const assetMap: DeepMutable<BuildManifest> = {
|
|
|
|
polyfillFiles: [],
|
|
|
|
devFiles: [],
|
|
|
|
ampDevFiles: [],
|
|
|
|
lowPriorityFiles: [],
|
|
|
|
pages: { '/_app': [] },
|
|
|
|
ampFirstPages: [],
|
|
|
|
}
|
2020-06-14 14:49:46 +02:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
const ampFirstEntryNames = ampFirstEntryNamesMap.get(compilation)
|
|
|
|
if (ampFirstEntryNames) {
|
|
|
|
for (const entryName of ampFirstEntryNames) {
|
|
|
|
const pagePath = getRouteFromEntrypoint(entryName)
|
|
|
|
if (!pagePath) {
|
|
|
|
continue
|
|
|
|
}
|
2018-04-12 09:47:42 +02:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
assetMap.ampFirstPages.push(pagePath)
|
|
|
|
}
|
2020-12-29 22:21:35 +01:00
|
|
|
}
|
2018-07-24 11:24:40 +02:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
const mainJsChunk = namedChunks.get(CLIENT_STATIC_FILES_RUNTIME_MAIN)
|
2020-06-08 20:11:00 +02:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
const mainJsFiles: string[] = getFilesArray(mainJsChunk?.files).filter(
|
|
|
|
isJsFile
|
|
|
|
)
|
2019-11-02 02:00:56 +01:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
const polyfillChunk = namedChunks.get(
|
|
|
|
CLIENT_STATIC_FILES_RUNTIME_POLYFILLS
|
|
|
|
)
|
2020-04-19 19:58:31 +02:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
// Create a separate entry for polyfills
|
|
|
|
assetMap.polyfillFiles = getFilesArray(polyfillChunk?.files).filter(
|
|
|
|
isJsFile
|
|
|
|
)
|
2020-06-20 21:59:47 +02:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
const reactRefreshChunk = namedChunks.get(
|
|
|
|
CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH
|
|
|
|
)
|
|
|
|
assetMap.devFiles = getFilesArray(reactRefreshChunk?.files).filter(
|
|
|
|
isJsFile
|
|
|
|
)
|
2020-06-20 21:59:47 +02:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
for (const entrypoint of compilation.entrypoints.values()) {
|
|
|
|
const isAmpRuntime =
|
|
|
|
entrypoint.name === CLIENT_STATIC_FILES_RUNTIME_AMP
|
2018-07-24 11:24:40 +02:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
if (isAmpRuntime) {
|
|
|
|
for (const file of entrypoint.getFiles()) {
|
|
|
|
if (!(isJsFile(file) || file.endsWith('.css'))) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
assetMap.ampDevFiles.push(file.replace(/\\/g, '/'))
|
2020-12-29 22:21:35 +01:00
|
|
|
}
|
2021-01-10 02:12:13 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
const pagePath = getRouteFromEntrypoint(entrypoint.name)
|
2020-12-29 22:21:35 +01:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
if (!pagePath) {
|
|
|
|
continue
|
2018-07-24 11:24:40 +02:00
|
|
|
}
|
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
const filesForEntry: string[] = []
|
2019-10-30 02:27:41 +01:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
// getFiles() - helper function to read the files for an entrypoint from stats object
|
|
|
|
for (const file of entrypoint.getFiles()) {
|
|
|
|
if (!(isJsFile(file) || file.endsWith('.css'))) {
|
|
|
|
continue
|
|
|
|
}
|
2019-10-30 02:27:41 +01:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
filesForEntry.push(file.replace(/\\/g, '/'))
|
2020-12-29 22:21:35 +01:00
|
|
|
}
|
2018-07-24 11:24:40 +02:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
assetMap.pages[pagePath] = [...mainJsFiles, ...filesForEntry]
|
2019-03-12 05:01:50 +01:00
|
|
|
}
|
2018-07-24 11:24:40 +02:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
// 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.
|
|
|
|
assetMap.lowPriorityFiles.push(
|
|
|
|
`${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.js`
|
|
|
|
)
|
2019-03-11 22:34:41 +01:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
// 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).
|
|
|
|
const srcEmptySsgManifest = `self.__SSG_MANIFEST=new Set;self.__SSG_MANIFEST_CB&&self.__SSG_MANIFEST_CB()`
|
2019-08-08 19:14:33 +02:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
const ssgManifestPath = `${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_ssgManifest.js`
|
|
|
|
assetMap.lowPriorityFiles.push(ssgManifestPath)
|
2021-01-14 02:59:08 +01:00
|
|
|
assets[ssgManifestPath] = new sources.RawSource(srcEmptySsgManifest)
|
2020-06-02 16:13:11 +02:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
assetMap.pages = Object.keys(assetMap.pages)
|
|
|
|
.sort()
|
|
|
|
// eslint-disable-next-line
|
|
|
|
.reduce((a, c) => ((a[c] = assetMap.pages[c]), a), {} as any)
|
2019-08-08 19:14:33 +02:00
|
|
|
|
2021-01-14 02:59:08 +01:00
|
|
|
assets[BUILD_MANIFEST] = new sources.RawSource(
|
2021-01-10 02:12:13 +01:00
|
|
|
JSON.stringify(assetMap, null, 2)
|
|
|
|
)
|
2020-08-03 14:26:23 +02:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
const clientManifestPath = `${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.js`
|
2020-08-03 14:26:23 +02:00
|
|
|
|
2021-01-14 02:59:08 +01:00
|
|
|
assets[clientManifestPath] = new sources.RawSource(
|
2021-01-10 02:12:13 +01:00
|
|
|
`self.__BUILD_MANIFEST = ${generateClientManifest(
|
|
|
|
compiler,
|
|
|
|
assetMap,
|
|
|
|
this.rewrites
|
|
|
|
)};self.__BUILD_MANIFEST_CB && self.__BUILD_MANIFEST_CB()`
|
|
|
|
)
|
2020-08-03 14:26:23 +02:00
|
|
|
|
2021-01-10 02:12:13 +01:00
|
|
|
return assets
|
|
|
|
})
|
2020-12-29 22:21:35 +01:00
|
|
|
})
|
2020-08-03 14:26:23 +02:00
|
|
|
}
|
|
|
|
|
2021-01-14 02:59:08 +01:00
|
|
|
apply(compiler: webpack.Compiler) {
|
2020-08-03 14:26:23 +02:00
|
|
|
if (isWebpack5) {
|
|
|
|
compiler.hooks.make.tap('NextJsBuildManifest', (compilation) => {
|
|
|
|
// @ts-ignore TODO: Remove ignore when webpack 5 is stable
|
|
|
|
compilation.hooks.processAssets.tap(
|
|
|
|
{
|
|
|
|
name: 'NextJsBuildManifest',
|
|
|
|
// @ts-ignore TODO: Remove ignore when webpack 5 is stable
|
|
|
|
stage: webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
|
|
|
|
},
|
|
|
|
(assets: any) => {
|
2021-01-10 02:12:13 +01:00
|
|
|
this.createAssets(compiler, compilation, assets)
|
2020-08-03 14:26:23 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
compiler.hooks.emit.tap('NextJsBuildManifest', (compilation: any) => {
|
2021-01-10 02:12:13 +01:00
|
|
|
this.createAssets(compiler, compilation, compilation.assets)
|
2020-08-03 14:26:23 +02:00
|
|
|
})
|
2018-04-12 09:47:42 +02:00
|
|
|
}
|
|
|
|
}
|