2018-12-03 13:18:52 +00:00
|
|
|
import { join } from 'path'
|
|
|
|
import nanoid from 'nanoid'
|
|
|
|
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'
|
2018-12-11 20:46:23 +00:00
|
|
|
import {runCompiler, CompilerResult} from './compiler'
|
2018-12-03 13:18:52 +00:00
|
|
|
|
|
|
|
export default async function build (dir: string, conf = null, lambdas: boolean = false): Promise<void> {
|
|
|
|
if (!await isWriteable(dir)) {
|
|
|
|
throw new Error('> Build directory is not writeable. https://err.sh/zeit/next.js/build-dir-not-writeable')
|
|
|
|
}
|
|
|
|
|
|
|
|
const config = loadConfig(PHASE_PRODUCTION_BUILD, dir, conf)
|
|
|
|
const lambdasOption = config.lambdas ? config.lambdas : lambdas
|
|
|
|
const distDir = join(dir, config.distDir)
|
|
|
|
const buildId = await generateBuildId(config.generateBuildId, nanoid)
|
2018-12-11 20:46:23 +00:00
|
|
|
|
2018-12-03 13:18:52 +00:00
|
|
|
const configs: any = await Promise.all([
|
|
|
|
getBaseWebpackConfig(dir, { buildId, isServer: false, config, lambdas: lambdasOption }),
|
|
|
|
getBaseWebpackConfig(dir, { buildId, isServer: true, config, lambdas: lambdasOption })
|
|
|
|
])
|
|
|
|
|
2018-12-11 20:46:23 +00:00
|
|
|
let result: CompilerResult = {warnings: [], errors: []}
|
|
|
|
if (lambdasOption) {
|
|
|
|
const clientResult = await runCompiler([configs[0]])
|
|
|
|
const serverResult = await runCompiler([configs[1]])
|
|
|
|
result = {warnings: [...clientResult.warnings, ...serverResult.warnings], errors: [...clientResult.errors, ...serverResult.errors]}
|
|
|
|
} else {
|
|
|
|
result = await runCompiler(configs)
|
|
|
|
}
|
|
|
|
|
2018-12-03 13:18:52 +00:00
|
|
|
if (result.warnings.length > 0) {
|
|
|
|
console.warn('> Emitted warnings from webpack')
|
|
|
|
console.warn(...result.warnings)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result.errors.length > 0) {
|
|
|
|
console.error(...result.errors)
|
|
|
|
throw new Error('> Build failed because of webpack errors')
|
|
|
|
}
|
|
|
|
await writeBuildId(distDir, buildId)
|
|
|
|
}
|