2017-02-15 13:03:33 +00:00
|
|
|
import { join } from 'path'
|
2018-03-30 14:59:42 +00:00
|
|
|
import promisify from '../lib/promisify'
|
|
|
|
import fs from 'fs'
|
2018-01-30 15:40:52 +00:00
|
|
|
import webpack from 'webpack'
|
2018-11-02 22:19:48 +00:00
|
|
|
import nanoid from 'nanoid'
|
2018-10-01 22:55:31 +00:00
|
|
|
import loadConfig from 'next-server/next-config'
|
|
|
|
import { PHASE_PRODUCTION_BUILD, BUILD_ID_FILE } from 'next-server/constants'
|
2018-01-30 15:40:52 +00:00
|
|
|
import getBaseWebpackConfig from './webpack'
|
2016-10-06 11:05:52 +00:00
|
|
|
|
2018-03-30 14:59:42 +00:00
|
|
|
const access = promisify(fs.access)
|
|
|
|
const writeFile = promisify(fs.writeFile)
|
|
|
|
|
2018-11-12 14:57:48 +00:00
|
|
|
async function generateBuildId (generate, fallback) {
|
|
|
|
let buildId = await generate()
|
|
|
|
// If there's no buildId defined we'll fall back
|
|
|
|
if (buildId === null) {
|
|
|
|
buildId = fallback()
|
|
|
|
}
|
2017-12-02 17:13:39 +00:00
|
|
|
|
2018-11-12 14:57:48 +00:00
|
|
|
if (typeof buildId !== 'string') {
|
|
|
|
throw new Error('generateBuildId did not return a string. https://err.sh/zeit/next.js/generatebuildid-not-a-string')
|
2018-11-02 22:19:48 +00:00
|
|
|
}
|
|
|
|
|
2018-11-12 14:57:48 +00:00
|
|
|
return buildId.trim()
|
|
|
|
}
|
|
|
|
|
|
|
|
async function ensureProjectDirectoryIsWriteAble (dir) {
|
2017-12-02 17:13:39 +00:00
|
|
|
try {
|
2018-03-30 14:59:42 +00:00
|
|
|
await access(dir, (fs.constants || fs).W_OK)
|
2017-12-02 17:13:39 +00:00
|
|
|
} catch (err) {
|
2018-11-12 14:57:48 +00:00
|
|
|
throw new Error('Build directory is not writeable. https://err.sh/zeit/next.js/build-dir-not-writeable')
|
2017-12-02 17:13:39 +00:00
|
|
|
}
|
2018-11-12 14:57:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default async function build (dir, conf = null) {
|
|
|
|
const config = loadConfig(PHASE_PRODUCTION_BUILD, dir, conf)
|
|
|
|
const distDir = join(dir, config.distDir)
|
|
|
|
const buildId = await generateBuildId(config.generateBuildId, nanoid)
|
|
|
|
|
|
|
|
await ensureProjectDirectoryIsWriteAble(dir)
|
2017-12-02 17:13:39 +00:00
|
|
|
|
2017-02-15 13:03:33 +00:00
|
|
|
try {
|
2018-01-30 15:40:52 +00:00
|
|
|
const configs = await Promise.all([
|
|
|
|
getBaseWebpackConfig(dir, { buildId, isServer: false, config }),
|
|
|
|
getBaseWebpackConfig(dir, { buildId, isServer: true, config })
|
|
|
|
])
|
|
|
|
|
|
|
|
await runCompiler(configs)
|
|
|
|
|
2018-06-04 13:45:39 +00:00
|
|
|
await writeBuildId(distDir, buildId)
|
2017-02-15 13:03:33 +00:00
|
|
|
} catch (err) {
|
2018-01-30 15:40:52 +00:00
|
|
|
console.error(`> Failed to build`)
|
2017-02-15 13:03:33 +00:00
|
|
|
throw err
|
|
|
|
}
|
2016-12-16 18:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function runCompiler (compiler) {
|
2018-01-30 15:40:52 +00:00
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
const webpackCompiler = await webpack(await compiler)
|
|
|
|
webpackCompiler.run((err, stats) => {
|
2018-11-04 21:16:09 +00:00
|
|
|
if (err) {
|
|
|
|
console.log({...err})
|
|
|
|
console.log(...stats.errors)
|
|
|
|
return reject(err)
|
|
|
|
}
|
2016-10-06 11:05:52 +00:00
|
|
|
|
2018-11-04 22:04:54 +00:00
|
|
|
let buildFailed = false
|
|
|
|
for (const stat of stats.stats) {
|
|
|
|
for (const error of stat.compilation.errors) {
|
|
|
|
buildFailed = true
|
|
|
|
console.error('ERROR', error)
|
|
|
|
console.error('ORIGINAL ERROR', error.error)
|
|
|
|
}
|
2017-03-07 18:43:56 +00:00
|
|
|
|
2018-11-04 22:04:54 +00:00
|
|
|
for (const warning of stat.compilation.warnings) {
|
|
|
|
console.warn('WARNING', warning)
|
|
|
|
}
|
2018-10-20 15:02:20 +00:00
|
|
|
}
|
|
|
|
|
2018-11-04 22:04:54 +00:00
|
|
|
if (buildFailed) {
|
|
|
|
return reject(new Error('Webpack errors'))
|
2016-10-17 01:54:36 +00:00
|
|
|
}
|
2016-10-06 11:05:52 +00:00
|
|
|
|
2018-07-24 09:24:40 +00:00
|
|
|
resolve()
|
2016-10-17 01:54:36 +00:00
|
|
|
})
|
|
|
|
})
|
2016-10-06 11:05:52 +00:00
|
|
|
}
|
2017-01-11 20:16:18 +00:00
|
|
|
|
2018-06-04 13:45:39 +00:00
|
|
|
async function writeBuildId (distDir, buildId) {
|
|
|
|
const buildIdPath = join(distDir, BUILD_ID_FILE)
|
2018-03-30 14:59:42 +00:00
|
|
|
await writeFile(buildIdPath, buildId, 'utf8')
|
2017-01-11 20:16:18 +00:00
|
|
|
}
|