2020-03-29 01:18:22 +01:00
|
|
|
import devalue from 'next/dist/compiled/devalue'
|
2019-11-02 02:00:56 +01:00
|
|
|
import { Compiler } from 'webpack'
|
|
|
|
import { RawSource } from 'webpack-sources'
|
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,
|
2019-08-20 08:28:09 +02:00
|
|
|
IS_BUNDLED_PAGE_REGEX,
|
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'
|
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.
|
|
|
|
const generateClientManifest = (
|
2020-02-29 23:09:42 +01:00
|
|
|
assetMap: BuildManifest,
|
2019-08-08 19:14:33 +02:00
|
|
|
isModern: boolean
|
|
|
|
): string => {
|
|
|
|
const clientManifest: { [s: string]: string[] } = {}
|
|
|
|
const appDependencies = new Set(assetMap.pages['/_app'])
|
2019-08-20 21:32:03 +02:00
|
|
|
|
2019-08-08 19:14:33 +02:00
|
|
|
Object.entries(assetMap.pages).forEach(([page, dependencies]) => {
|
2019-11-02 02:00:56 +01:00
|
|
|
if (page === '/_app' || page === '/_polyfills') return
|
2019-08-08 19:14:33 +02:00
|
|
|
// 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(
|
2020-05-18 21:24:37 +02:00
|
|
|
(dep) =>
|
2020-01-27 21:32:45 +01:00
|
|
|
!appDependencies.has(dep) &&
|
|
|
|
(!dep.endsWith('.js') || dep.endsWith('.module.js') === isModern)
|
2019-08-08 19:14:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// The manifest can omit the page if it has no requirements
|
|
|
|
if (filteredDeps.length) {
|
|
|
|
clientManifest[page] = filteredDeps
|
|
|
|
}
|
|
|
|
})
|
2019-08-20 08:28:09 +02:00
|
|
|
return devalue(clientManifest)
|
2019-08-08 19:14:33 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
private modern: boolean
|
|
|
|
|
2020-06-02 16:13:11 +02:00
|
|
|
constructor(options: { buildId: string; modern: boolean }) {
|
2019-08-08 19:14:33 +02:00
|
|
|
this.buildId = options.buildId
|
|
|
|
this.modern = options.modern
|
|
|
|
}
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
apply(compiler: Compiler) {
|
2019-03-12 05:01:50 +01:00
|
|
|
compiler.hooks.emit.tapAsync(
|
|
|
|
'NextJsBuildManifest',
|
|
|
|
(compilation, callback) => {
|
|
|
|
const { chunks } = compilation
|
2020-02-29 23:09:42 +01:00
|
|
|
const assetMap: BuildManifest = {
|
|
|
|
devFiles: [],
|
|
|
|
lowPriorityFiles: [],
|
|
|
|
pages: { '/_app': [] },
|
|
|
|
}
|
2018-04-12 09:47:42 +02:00
|
|
|
|
2019-03-12 05:01:50 +01:00
|
|
|
const mainJsChunk = chunks.find(
|
2020-05-18 21:24:37 +02:00
|
|
|
(c) => c.name === CLIENT_STATIC_FILES_RUNTIME_MAIN
|
2019-03-12 05:01:50 +01:00
|
|
|
)
|
2019-04-23 23:12:33 +02:00
|
|
|
const mainJsFiles: string[] =
|
2019-03-12 05:01:50 +01:00
|
|
|
mainJsChunk && mainJsChunk.files.length > 0
|
2019-04-23 23:12:33 +02:00
|
|
|
? mainJsChunk.files.filter((file: string) => /\.js$/.test(file))
|
2019-03-12 05:01:50 +01:00
|
|
|
: []
|
2018-07-24 11:24:40 +02:00
|
|
|
|
2019-11-02 02:00:56 +01:00
|
|
|
const polyfillChunk = chunks.find(
|
2020-05-18 21:24:37 +02:00
|
|
|
(c) => c.name === CLIENT_STATIC_FILES_RUNTIME_POLYFILLS
|
2019-11-02 02:00:56 +01:00
|
|
|
)
|
|
|
|
const polyfillFiles: string[] = polyfillChunk ? polyfillChunk.files : []
|
|
|
|
|
2020-04-19 19:58:31 +02:00
|
|
|
const reactRefreshChunk = chunks.find(
|
2020-05-18 21:24:37 +02:00
|
|
|
(c) => c.name === CLIENT_STATIC_FILES_RUNTIME_REACT_REFRESH
|
2020-04-19 19:58:31 +02:00
|
|
|
)
|
|
|
|
assetMap.devFiles.push(...(reactRefreshChunk?.files ?? []))
|
|
|
|
|
2020-06-04 19:32:45 +02:00
|
|
|
for (const entrypoint of compilation.entrypoints.values()) {
|
|
|
|
const pagePath = getRouteFromEntrypoint(entrypoint.name)
|
2018-07-24 11:24:40 +02:00
|
|
|
|
2019-03-12 05:01:50 +01:00
|
|
|
if (!pagePath) {
|
2018-07-24 11:24:40 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-04-23 23:12:33 +02:00
|
|
|
const filesForEntry: string[] = []
|
2019-10-30 02:27:41 +01:00
|
|
|
|
|
|
|
// getFiles() - helper function to read the files for an entrypoint from stats object
|
|
|
|
for (const file of entrypoint.getFiles()) {
|
|
|
|
if (/\.map$/.test(file) || /\.hot-update\.js$/.test(file)) {
|
2018-07-24 11:24:40 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-10-30 02:27:41 +01:00
|
|
|
// Only `.js` and `.css` files are added for now. In the future we can also handle other file types.
|
|
|
|
if (!/\.js$/.test(file) && !/\.css$/.test(file)) {
|
|
|
|
continue
|
|
|
|
}
|
2018-07-24 11:24:40 +02:00
|
|
|
|
2019-10-30 02:27:41 +01:00
|
|
|
// The page bundles are manually added to _document.js as they need extra properties
|
|
|
|
if (IS_BUNDLED_PAGE_REGEX.exec(file)) {
|
|
|
|
continue
|
2019-03-12 05:01:50 +01:00
|
|
|
}
|
2019-10-30 02:27:41 +01:00
|
|
|
|
|
|
|
filesForEntry.push(file.replace(/\\/g, '/'))
|
2018-07-24 11:24:40 +02:00
|
|
|
}
|
|
|
|
|
2020-06-04 19:32:45 +02:00
|
|
|
assetMap.pages[pagePath] = [...filesForEntry, ...mainJsFiles]
|
2019-03-12 05:01:50 +01:00
|
|
|
}
|
2018-07-24 11:24:40 +02:00
|
|
|
|
2019-03-12 05:01:50 +01:00
|
|
|
if (typeof assetMap.pages['/index'] !== 'undefined') {
|
|
|
|
assetMap.pages['/'] = assetMap.pages['/index']
|
|
|
|
}
|
2018-07-24 11:24:40 +02:00
|
|
|
|
2019-11-02 02:00:56 +01:00
|
|
|
// Create a separate entry for polyfills
|
|
|
|
assetMap.pages['/_polyfills'] = polyfillFiles
|
|
|
|
|
2019-08-08 19:14:33 +02: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.
|
2020-06-02 16:13:11 +02:00
|
|
|
assetMap.lowPriorityFiles.push(
|
|
|
|
`${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.js`
|
|
|
|
)
|
|
|
|
if (this.modern) {
|
2020-02-29 23:09:42 +01:00
|
|
|
assetMap.lowPriorityFiles.push(
|
2020-06-02 16:13:11 +02:00
|
|
|
`${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.module.js`
|
2019-08-08 19:14:33 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-03-02 18:14:40 +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()`
|
|
|
|
|
|
|
|
const ssgManifestPath = `${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_ssgManifest.js`
|
|
|
|
assetMap.lowPriorityFiles.push(ssgManifestPath)
|
|
|
|
compilation.assets[ssgManifestPath] = new RawSource(srcEmptySsgManifest)
|
|
|
|
|
|
|
|
if (this.modern) {
|
|
|
|
const ssgManifestPathModern = `${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_ssgManifest.module.js`
|
|
|
|
assetMap.lowPriorityFiles.push(ssgManifestPathModern)
|
|
|
|
compilation.assets[ssgManifestPathModern] = new RawSource(
|
|
|
|
srcEmptySsgManifest
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2019-03-12 05:01:50 +01:00
|
|
|
assetMap.pages = Object.keys(assetMap.pages)
|
|
|
|
.sort()
|
|
|
|
// eslint-disable-next-line
|
2019-04-23 23:12:33 +02:00
|
|
|
.reduce((a, c) => ((a[c] = assetMap.pages[c]), a), {} as any)
|
2019-03-11 22:34:41 +01:00
|
|
|
|
2019-03-12 05:01:50 +01:00
|
|
|
compilation.assets[BUILD_MANIFEST] = new RawSource(
|
|
|
|
JSON.stringify(assetMap, null, 2)
|
|
|
|
)
|
2019-08-08 19:14:33 +02:00
|
|
|
|
2020-06-02 16:13:11 +02:00
|
|
|
const clientManifestPath = `${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.js`
|
|
|
|
|
|
|
|
compilation.assets[clientManifestPath] = new RawSource(
|
|
|
|
`self.__BUILD_MANIFEST = ${generateClientManifest(
|
|
|
|
assetMap,
|
|
|
|
false
|
|
|
|
)};self.__BUILD_MANIFEST_CB && self.__BUILD_MANIFEST_CB()`
|
|
|
|
)
|
|
|
|
|
|
|
|
if (this.modern) {
|
|
|
|
const modernClientManifestPath = `${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.module.js`
|
2019-08-08 19:14:33 +02:00
|
|
|
|
2020-06-02 16:13:11 +02:00
|
|
|
compilation.assets[modernClientManifestPath] = new RawSource(
|
2019-08-20 08:28:09 +02:00
|
|
|
`self.__BUILD_MANIFEST = ${generateClientManifest(
|
2019-08-08 19:14:33 +02:00
|
|
|
assetMap,
|
2020-06-02 16:13:11 +02:00
|
|
|
true
|
2019-11-11 04:24:53 +01:00
|
|
|
)};self.__BUILD_MANIFEST_CB && self.__BUILD_MANIFEST_CB()`
|
2019-08-08 19:14:33 +02:00
|
|
|
)
|
|
|
|
}
|
2019-03-12 05:01:50 +01:00
|
|
|
callback()
|
|
|
|
}
|
|
|
|
)
|
2018-04-12 09:47:42 +02:00
|
|
|
}
|
|
|
|
}
|