rsnext/packages/next/build/index.ts
Joe Haddad b835e2589d
Specified page builds (#6796)
* [wip] individual page builds

* Make flag experimental and remove from main bin

* Do not split chunks when using shared runtime
2019-03-27 11:51:05 -04:00

155 lines
4.5 KiB
TypeScript

import {
join as pathJoin,
relative as pathRelative,
isAbsolute as pathIsAbsolute,
} from 'path'
import nanoid from 'next/dist/compiled/nanoid/index.js'
import loadConfig from 'next-server/next-config'
import { PHASE_PRODUCTION_BUILD } from 'next-server/constants'
import getBaseWebpackConfig from './webpack-config'
import { generateBuildId } from './generate-build-id'
import { writeBuildId } from './write-build-id'
import { isWriteable } from './is-writeable'
import { runCompiler, CompilerResult } from './compiler'
import { recursiveReadDir } from '../lib/recursive-readdir'
import { createPagesMapping, createEntrypoints } from './entries'
import formatWebpackMessages from '../client/dev-error-overlay/format-webpack-messages'
import chalk from 'chalk'
function collectPages(
directory: string,
pageExtensions: string[]
): Promise<string[]> {
return recursiveReadDir(
directory,
new RegExp(`\\.(?:${pageExtensions.join('|')})$`)
)
}
function printTreeView(list: string[]) {
list
.sort((a, b) => (a > b ? 1 : -1))
.forEach((item, i) => {
const corner =
i === 0
? list.length === 1
? '─'
: '┌'
: i === list.length - 1
? '└'
: '├'
console.log(` \x1b[90m${corner}\x1b[39m ${item}`)
})
console.log()
}
export default async function build(
dir: string,
conf = null,
{ pages = [] as string[] } = {}
): Promise<void> {
if (!(await isWriteable(dir))) {
throw new Error(
'> Build directory is not writeable. https://err.sh/zeit/next.js/build-dir-not-writeable'
)
}
console.log('Creating an optimized production build ...')
console.log()
const config = loadConfig(PHASE_PRODUCTION_BUILD, dir, conf)
const buildId = await generateBuildId(config.generateBuildId, nanoid)
const distDir = pathJoin(dir, config.distDir)
const pagesDir = pathJoin(dir, 'pages')
let pagePaths
if (pages && pages.length) {
// TODO: smart resolve these pages and check for them
pagePaths = pages.map(page =>
pathIsAbsolute(page) ? pathRelative(pagesDir, page) : page
)
} else {
pagePaths = await collectPages(pagesDir, config.pageExtensions)
}
const mappedPages = createPagesMapping(pagePaths, config.pageExtensions)
const entrypoints = createEntrypoints(
mappedPages,
config.target,
buildId,
config
)
const configs = await Promise.all([
getBaseWebpackConfig(dir, {
buildId,
isServer: false,
config,
target: config.target,
entrypoints: entrypoints.client,
}),
getBaseWebpackConfig(dir, {
buildId,
isServer: true,
config,
target: config.target,
entrypoints: entrypoints.server,
}),
])
let result: CompilerResult = { warnings: [], errors: [] }
if (config.target === 'serverless') {
if (config.publicRuntimeConfig)
throw new Error(
'Cannot use publicRuntimeConfig with target=serverless https://err.sh/zeit/next.js/serverless-publicRuntimeConfig'
)
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 (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'))
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'))
}
printTreeView(Object.keys(mappedPages))
await writeBuildId(distDir, buildId)
}