forked from vercel/next.js
/
build-manifest-plugin.ts
201 lines (171 loc) · 6.94 KB
/
build-manifest-plugin.ts
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
import devalue from 'devalue'
import { Compiler } from 'webpack'
import { RawSource } from 'webpack-sources'
import {
BUILD_MANIFEST,
CLIENT_STATIC_FILES_PATH,
CLIENT_STATIC_FILES_RUNTIME_MAIN,
CLIENT_STATIC_FILES_RUNTIME_POLYFILLS,
IS_BUNDLED_PAGE_REGEX,
ROUTE_NAME_REGEX,
} from '../../../next-server/lib/constants'
import { BuildManifest } from '../../../next-server/server/get-page-files'
// This function takes the asset map generated in BuildManifestPlugin and creates a
// reduced version to send to the client.
const generateClientManifest = (
assetMap: BuildManifest,
isModern: boolean
): string => {
const clientManifest: { [s: string]: string[] } = {}
const appDependencies = new Set(assetMap.pages['/_app'])
Object.entries(assetMap.pages).forEach(([page, dependencies]) => {
if (page === '/_app' || page === '/_polyfills') 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) &&
(!dep.endsWith('.js') || dep.endsWith('.module.js') === isModern)
)
// The manifest can omit the page if it has no requirements
if (filteredDeps.length) {
clientManifest[page] = filteredDeps
}
})
return devalue(clientManifest)
}
// 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 clientManifest: boolean
private modern: boolean
constructor(options: {
buildId: string
clientManifest: boolean
modern: boolean
}) {
this.buildId = options.buildId
this.clientManifest = options.clientManifest
this.modern = options.modern
}
apply(compiler: Compiler) {
compiler.hooks.emit.tapAsync(
'NextJsBuildManifest',
(compilation, callback) => {
const { chunks } = compilation
const assetMap: BuildManifest = {
devFiles: [],
lowPriorityFiles: [],
pages: { '/_app': [] },
}
const mainJsChunk = chunks.find(
c => c.name === CLIENT_STATIC_FILES_RUNTIME_MAIN
)
const mainJsFiles: string[] =
mainJsChunk && mainJsChunk.files.length > 0
? mainJsChunk.files.filter((file: string) => /\.js$/.test(file))
: []
const polyfillChunk = chunks.find(
c => c.name === CLIENT_STATIC_FILES_RUNTIME_POLYFILLS
)
const polyfillFiles: string[] = polyfillChunk ? polyfillChunk.files : []
for (const filePath of Object.keys(compilation.assets)) {
const path = filePath.replace(/\\/g, '/')
if (/^static\/development\/dll\//.test(path)) {
assetMap.devFiles.push(path)
}
}
// compilation.entrypoints is a Map object, so iterating over it 0 is the key and 1 is the value
for (const [, entrypoint] of compilation.entrypoints.entries()) {
const result = ROUTE_NAME_REGEX.exec(entrypoint.name)
if (!result) {
continue
}
const pagePath = result[1]
if (!pagePath) {
continue
}
const filesForEntry: string[] = []
// 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)) {
continue
}
// 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
}
// The page bundles are manually added to _document.js as they need extra properties
if (IS_BUNDLED_PAGE_REGEX.exec(file)) {
continue
}
filesForEntry.push(file.replace(/\\/g, '/'))
}
assetMap.pages[`/${pagePath.replace(/\\/g, '/')}`] = [
...filesForEntry,
...mainJsFiles,
]
}
if (typeof assetMap.pages['/index'] !== 'undefined') {
assetMap.pages['/'] = assetMap.pages['/index']
}
// Create a separate entry for polyfills
assetMap.pages['/_polyfills'] = polyfillFiles
// 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.
if (this.clientManifest) {
assetMap.lowPriorityFiles.push(
`${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.js`
)
if (this.modern) {
assetMap.lowPriorityFiles.push(
`${CLIENT_STATIC_FILES_PATH}/${this.buildId}/_buildManifest.module.js`
)
}
}
// 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
)
}
assetMap.pages = Object.keys(assetMap.pages)
.sort()
// eslint-disable-next-line
.reduce((a, c) => ((a[c] = assetMap.pages[c]), a), {} as any)
compilation.assets[BUILD_MANIFEST] = new RawSource(
JSON.stringify(assetMap, null, 2)
)
if (this.clientManifest) {
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`
compilation.assets[modernClientManifestPath] = new RawSource(
`self.__BUILD_MANIFEST = ${generateClientManifest(
assetMap,
true
)};self.__BUILD_MANIFEST_CB && self.__BUILD_MANIFEST_CB()`
)
}
}
callback()
}
)
}
}