-
Notifications
You must be signed in to change notification settings - Fork 27.5k
/
Copy pathindex.ts
477 lines (424 loc) · 14.4 KB
/
index.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
import chalk from 'chalk'
import {
SERVER_DIRECTORY,
SERVERLESS_DIRECTORY,
PAGES_MANIFEST,
CHUNK_GRAPH_MANIFEST,
PHASE_PRODUCTION_BUILD,
} from 'next-server/constants'
import loadConfig from 'next-server/next-config'
import nanoid from 'next/dist/compiled/nanoid/index.js'
import path from 'path'
import fs from 'fs'
import { promisify } from 'util'
import formatWebpackMessages from '../client/dev/error-overlay/format-webpack-messages'
import { recursiveDelete } from '../lib/recursive-delete'
import { verifyTypeScriptSetup } from '../lib/verifyTypeScriptSetup'
import { CompilerResult, runCompiler } from './compiler'
import { createEntrypoints, createPagesMapping } from './entries'
import { FlyingShuttle } from './flying-shuttle'
import { generateBuildId } from './generate-build-id'
import { isWriteable } from './is-writeable'
import {
collectPages,
getCacheIdentifier,
getFileForPage,
getPageSizeInKb,
getSpecifiedPages,
printTreeView,
PageInfo,
isPageStatic,
hasCustomAppGetInitialProps,
} from './utils'
import getBaseWebpackConfig from './webpack-config'
import {
exportManifest,
getPageChunks,
} from './webpack/plugins/chunk-graph-plugin'
import { writeBuildId } from './write-build-id'
import { recursiveReadDir } from '../lib/recursive-readdir'
import mkdirpOrig from 'mkdirp'
import workerFarm from 'worker-farm'
import { Sema } from 'async-sema'
const fsUnlink = promisify(fs.unlink)
const fsRmdir = promisify(fs.rmdir)
const fsMove = promisify(fs.rename)
const fsReadFile = promisify(fs.readFile)
const fsWriteFile = promisify(fs.writeFile)
const mkdirp = promisify(mkdirpOrig)
const staticCheckWorker = require.resolve('./static-checker')
export default async function build(dir: string, conf = null): Promise<void> {
if (!(await isWriteable(dir))) {
throw new Error(
'> Build directory is not writeable. https://err.sh/zeit/next.js/build-dir-not-writeable'
)
}
await verifyTypeScriptSetup(dir)
console.log('Creating an optimized production build ...')
console.log()
const config = loadConfig(PHASE_PRODUCTION_BUILD, dir, conf)
const { target } = config
const buildId = await generateBuildId(config.generateBuildId, nanoid)
const distDir = path.join(dir, config.distDir)
const pagesDir = path.join(dir, 'pages')
const isFlyingShuttle = Boolean(
config.experimental.flyingShuttle &&
!process.env.__NEXT_BUILDER_EXPERIMENTAL_PAGE
)
const selectivePageBuilding = Boolean(
isFlyingShuttle || process.env.__NEXT_BUILDER_EXPERIMENTAL_PAGE
)
if (selectivePageBuilding && target !== 'serverless') {
throw new Error(
`Cannot use ${
isFlyingShuttle ? 'flying shuttle' : '`now dev`'
} without the serverless target.`
)
}
const selectivePageBuildingCacheIdentifier = selectivePageBuilding
? await getCacheIdentifier({
pagesDirectory: pagesDir,
env: config.env || {},
})
: 'noop'
let flyingShuttle: FlyingShuttle | undefined
if (isFlyingShuttle) {
console.log(chalk.magenta('Building with Flying Shuttle enabled ...'))
console.log()
await recursiveDelete(distDir, /^(?!cache(?:[\/\\]|$)).*$/)
await recursiveDelete(path.join(distDir, 'cache', 'next-minifier'))
await recursiveDelete(path.join(distDir, 'cache', 'next-babel-loader'))
flyingShuttle = new FlyingShuttle({
buildId,
pagesDirectory: pagesDir,
distDirectory: distDir,
cacheIdentifier: selectivePageBuildingCacheIdentifier,
})
}
let pagePaths: string[]
if (process.env.__NEXT_BUILDER_EXPERIMENTAL_PAGE) {
pagePaths = await getSpecifiedPages(
dir,
process.env.__NEXT_BUILDER_EXPERIMENTAL_PAGE!,
config.pageExtensions
)
} else {
pagePaths = await collectPages(pagesDir, config.pageExtensions)
}
// needed for static exporting since we want to replace with HTML
// files even when flying shuttle doesn't rebuild the files
const allPagePaths = [...pagePaths]
const allStaticPages = new Set<string>()
let allPageInfos = new Map<string, PageInfo>()
if (flyingShuttle && (await flyingShuttle.hasShuttle())) {
allPageInfos = await flyingShuttle.getPageInfos()
const _unchangedPages = new Set(await flyingShuttle.getUnchangedPages())
for (const unchangedPage of _unchangedPages) {
const info = allPageInfos.get(unchangedPage) || ({} as PageInfo)
if (info.static) allStaticPages.add(unchangedPage)
const recalled = await flyingShuttle.restorePage(unchangedPage, info)
if (recalled) {
continue
}
_unchangedPages.delete(unchangedPage)
}
const unchangedPages = (await Promise.all(
[..._unchangedPages].map(async page => {
if (
page.endsWith('.amp') &&
(allPageInfos.get(page.split('.amp')[0]) || ({} as PageInfo)).isAmp
) {
return ''
}
const file = await getFileForPage({
page,
pagesDirectory: pagesDir,
pageExtensions: config.pageExtensions,
})
if (file) {
return file
}
return Promise.reject(
new Error(
`Failed to locate page file: ${page}. ` +
`Did pageExtensions change? We can't recover from this yet.`
)
)
})
)).filter(Boolean)
const pageSet = new Set(pagePaths)
for (const unchangedPage of unchangedPages) {
pageSet.delete(unchangedPage)
}
pagePaths = [...pageSet]
}
const allMappedPages = createPagesMapping(allPagePaths, config.pageExtensions)
const mappedPages = createPagesMapping(pagePaths, config.pageExtensions)
const entrypoints = createEntrypoints(
mappedPages,
target,
buildId,
/* dynamicBuildId */ selectivePageBuilding,
config
)
const configs = await Promise.all([
getBaseWebpackConfig(dir, {
buildId,
isServer: false,
config,
target,
entrypoints: entrypoints.client,
selectivePageBuilding,
}),
getBaseWebpackConfig(dir, {
buildId,
isServer: true,
config,
target,
entrypoints: entrypoints.server,
selectivePageBuilding,
}),
])
let result: CompilerResult = { warnings: [], errors: [] }
if (target === 'serverless') {
const clientResult = await runCompiler(configs[0])
// Fail build if clientResult contains errors
if (clientResult.errors.length > 0) {
result = {
warnings: [...clientResult.warnings],
errors: [...clientResult.errors],
}
} else {
const serverResult = await runCompiler(configs[1])
result = {
warnings: [...clientResult.warnings, ...serverResult.warnings],
errors: [...clientResult.errors, ...serverResult.errors],
}
}
} else {
result = await runCompiler(configs)
}
result = formatWebpackMessages(result)
if (isFlyingShuttle) {
console.log()
exportManifest({
dir: dir,
fileName: path.join(distDir, CHUNK_GRAPH_MANIFEST),
selectivePageBuildingCacheIdentifier,
})
}
if (result.errors.length > 0) {
// Only keep the first error. Others are often indicative
// of the same problem, but confuse the reader with noise.
if (result.errors.length > 1) {
result.errors.length = 1
}
const error = result.errors.join('\n\n')
console.error(chalk.red('Failed to compile.\n'))
if (
error.indexOf('private-next-pages') > -1 &&
error.indexOf('does not contain a default export') > -1
) {
const page_name_regex = /\'private-next-pages\/(?<page_name>[^\']*)\'/
const parsed = page_name_regex.exec(error)
const page_name = parsed && parsed.groups && parsed.groups.page_name
throw new Error(
`webpack build failed: found page without a React Component as default export in pages/${page_name}\n\nSee https://err.sh/zeit/next.js/page-without-valid-component for more info.`
)
}
console.error(error)
console.error()
if (error.indexOf('private-next-pages') > -1) {
throw new Error(
'> webpack config.resolve.alias was incorrectly overriden. https://err.sh/zeit/next.js/invalid-resolve-alias'
)
}
throw new Error('> Build failed because of webpack errors')
} else if (result.warnings.length > 0) {
console.warn(chalk.yellow('Compiled with warnings.\n'))
console.warn(result.warnings.join('\n\n'))
console.warn()
} else {
console.log(chalk.green('Compiled successfully.\n'))
}
const distPath = path.join(dir, config.distDir)
const pageKeys = Object.keys(mappedPages)
const manifestPath = path.join(
distDir,
target === 'serverless' ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY,
PAGES_MANIFEST
)
const staticPages = new Set<string>()
const invalidPages = new Set<string>()
const pageInfos = new Map<string, PageInfo>()
const pagesManifest = JSON.parse(await fsReadFile(manifestPath, 'utf8'))
let customAppGetInitialProps: boolean | undefined
process.env.NEXT_PHASE = PHASE_PRODUCTION_BUILD
const staticCheckSema = new Sema(config.experimental.cpus, {
capacity: pageKeys.length,
})
const staticCheckWorkers = workerFarm(
{
maxConcurrentWorkers: config.experimental.cpus,
},
staticCheckWorker,
['default']
)
await Promise.all(
pageKeys.map(async page => {
const chunks = getPageChunks(page)
const actualPage = page === '/' ? '/index' : page
const size = await getPageSizeInKb(actualPage, distPath, buildId)
const bundleRelative = path.join(
target === 'serverless' ? 'pages' : `static/${buildId}/pages`,
actualPage + '.js'
)
const serverBundle = path.join(
distPath,
target === 'serverless' ? SERVERLESS_DIRECTORY : SERVER_DIRECTORY,
bundleRelative
)
let isStatic = false
pagesManifest[page] = bundleRelative.replace(/\\/g, '/')
const runtimeEnvConfig = {
publicRuntimeConfig: config.publicRuntimeConfig,
serverRuntimeConfig: config.serverRuntimeConfig,
}
const nonReservedPage = !page.match(/^\/(_app|_error|_document|api)/)
if (nonReservedPage && customAppGetInitialProps === undefined) {
customAppGetInitialProps = hasCustomAppGetInitialProps(
target === 'serverless'
? serverBundle
: path.join(
distPath,
SERVER_DIRECTORY,
`/static/${buildId}/pages/_app.js`
),
runtimeEnvConfig
)
if (customAppGetInitialProps) {
console.warn(
chalk.bold.yellow(`Warning: `) +
chalk.yellow(
`You have opted-out of Automatic Prerendering due to \`getInitialProps\` in \`pages/_app\`.`
)
)
console.warn(
'Read more: https://err.sh/next.js/opt-out-automatic-prerendering\n'
)
}
}
if (nonReservedPage) {
try {
await staticCheckSema.acquire()
const result: any = await new Promise((resolve, reject) => {
staticCheckWorkers.default(
{ serverBundle, runtimeEnvConfig },
(error: Error | null, result: any) => {
if (error) return reject(error)
resolve(result || {})
}
)
})
staticCheckSema.release()
if (
(result.static && customAppGetInitialProps === false) ||
result.prerender
) {
staticPages.add(page)
isStatic = true
}
} catch (err) {
if (err.message !== 'INVALID_DEFAULT_EXPORT') throw err
invalidPages.add(page)
staticCheckSema.release()
}
}
pageInfos.set(page, { size, chunks, serverBundle, static: isStatic })
})
)
workerFarm.end(staticCheckWorkers)
if (invalidPages.size > 0) {
throw new Error(
`automatic static optimization failed: found page${
invalidPages.size === 1 ? '' : 's'
} without a React Component as default export in \n${[...invalidPages]
.map(pg => `pages${pg}`)
.join(
'\n'
)}\n\nSee https://err.sh/zeit/next.js/page-without-valid-component for more info.\n`
)
}
if (Array.isArray(configs[0].plugins)) {
configs[0].plugins.some((plugin: any) => {
if (!plugin.ampPages) {
return false
}
plugin.ampPages.forEach((pg: any) => {
pageInfos.get(pg)!.isAmp = true
})
return true
})
}
await writeBuildId(distDir, buildId, selectivePageBuilding)
if (staticPages.size > 0) {
const exportApp = require('../export').default
const exportOptions = {
silent: true,
buildExport: true,
pages: Array.from(staticPages),
outdir: path.join(distDir, 'export'),
}
const exportConfig = {
...config,
exportPathMap: (defaultMap: any) => defaultMap,
exportTrailingSlash: false,
}
await exportApp(dir, exportOptions, exportConfig)
const toMove = await recursiveReadDir(exportOptions.outdir, /.*\.html$/)
let serverDir: string = ''
// remove server bundles that were exported
for (const page of staticPages) {
const { serverBundle } = pageInfos.get(page)!
if (!serverDir) {
serverDir = path.join(
serverBundle.split(/(\/|\\)pages/).shift()!,
'pages'
)
}
await fsUnlink(serverBundle)
}
for (const file of toMove) {
const orig = path.join(exportOptions.outdir, file)
const dest = path.join(serverDir, file)
const relativeDest = (target === 'serverless'
? path.join('pages', file)
: path.join('static', buildId, 'pages', file)
).replace(/\\/g, '/')
let page = file.split('.html')[0].replace(/\\/g, '/')
pagesManifest[page] = relativeDest
page = page === '/index' ? '/' : page
pagesManifest[page] = relativeDest
staticPages.add(page)
await mkdirp(path.dirname(dest))
await fsMove(orig, dest)
}
// remove temporary export folder
await recursiveDelete(exportOptions.outdir)
await fsRmdir(exportOptions.outdir)
await fsWriteFile(manifestPath, JSON.stringify(pagesManifest), 'utf8')
}
staticPages.forEach(pg => allStaticPages.add(pg))
pageInfos.forEach((info: PageInfo, key: string) => {
allPageInfos.set(key, info)
})
if (flyingShuttle) {
await flyingShuttle.mergePagesManifest()
await flyingShuttle.save(allStaticPages, pageInfos)
}
printTreeView(
Object.keys(allMappedPages),
allPageInfos,
target === 'serverless'
)
}