2017-02-15 14:03:33 +01:00
|
|
|
import { join } from 'path'
|
2018-03-30 16:59:42 +02:00
|
|
|
import promisify from '../lib/promisify'
|
|
|
|
import fs from 'fs'
|
2018-01-30 16:40:52 +01:00
|
|
|
import webpack from 'webpack'
|
2018-10-02 00:55:31 +02:00
|
|
|
import loadConfig from 'next-server/next-config'
|
|
|
|
import { PHASE_PRODUCTION_BUILD, BUILD_ID_FILE } from 'next-server/constants'
|
2018-01-30 16:40:52 +01:00
|
|
|
import getBaseWebpackConfig from './webpack'
|
2016-10-06 13:05:52 +02:00
|
|
|
|
2018-03-30 16:59:42 +02:00
|
|
|
const access = promisify(fs.access)
|
|
|
|
const writeFile = promisify(fs.writeFile)
|
|
|
|
|
2017-06-23 05:48:06 +02:00
|
|
|
export default async function build (dir, conf = null) {
|
2018-06-04 11:38:46 +02:00
|
|
|
const config = loadConfig(PHASE_PRODUCTION_BUILD, dir, conf)
|
2018-03-31 16:34:52 +02:00
|
|
|
const buildId = await config.generateBuildId() // defaults to a uuid
|
2018-06-04 15:45:39 +02:00
|
|
|
const distDir = join(dir, config.distDir)
|
2017-12-02 18:13:39 +01:00
|
|
|
|
|
|
|
try {
|
2018-03-30 16:59:42 +02:00
|
|
|
await access(dir, (fs.constants || fs).W_OK)
|
2017-12-02 18:13:39 +01:00
|
|
|
} catch (err) {
|
|
|
|
console.error(`> Failed, build directory is not writeable. https://err.sh/zeit/next.js/build-dir-not-writeable`)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
|
2017-02-15 14:03:33 +01:00
|
|
|
try {
|
2018-01-30 16:40:52 +01:00
|
|
|
const configs = await Promise.all([
|
|
|
|
getBaseWebpackConfig(dir, { buildId, isServer: false, config }),
|
|
|
|
getBaseWebpackConfig(dir, { buildId, isServer: true, config })
|
|
|
|
])
|
|
|
|
|
|
|
|
await runCompiler(configs)
|
|
|
|
|
2018-06-04 15:45:39 +02:00
|
|
|
await writeBuildId(distDir, buildId)
|
2017-02-15 14:03:33 +01:00
|
|
|
} catch (err) {
|
2018-01-30 16:40:52 +01:00
|
|
|
console.error(`> Failed to build`)
|
2017-02-15 14:03:33 +01:00
|
|
|
throw err
|
|
|
|
}
|
2016-12-16 19:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function runCompiler (compiler) {
|
2018-01-30 16:40:52 +01:00
|
|
|
return new Promise(async (resolve, reject) => {
|
|
|
|
const webpackCompiler = await webpack(await compiler)
|
|
|
|
webpackCompiler.run((err, stats) => {
|
2016-10-17 03:54:36 +02:00
|
|
|
if (err) return reject(err)
|
2016-10-06 13:05:52 +02:00
|
|
|
|
2018-06-27 10:34:15 +02:00
|
|
|
const jsonStats = stats.toJson('errors-only')
|
2017-03-07 19:43:56 +01:00
|
|
|
|
2016-10-17 03:54:36 +02:00
|
|
|
if (jsonStats.errors.length > 0) {
|
|
|
|
const error = new Error(jsonStats.errors[0])
|
|
|
|
error.errors = jsonStats.errors
|
|
|
|
error.warnings = jsonStats.warnings
|
|
|
|
return reject(error)
|
|
|
|
}
|
2016-10-06 13:05:52 +02:00
|
|
|
|
2018-07-24 11:24:40 +02:00
|
|
|
resolve()
|
2016-10-17 03:54:36 +02:00
|
|
|
})
|
|
|
|
})
|
2016-10-06 13:05:52 +02:00
|
|
|
}
|
2017-01-11 21:16:18 +01:00
|
|
|
|
2018-06-04 15:45:39 +02:00
|
|
|
async function writeBuildId (distDir, buildId) {
|
|
|
|
const buildIdPath = join(distDir, BUILD_ID_FILE)
|
2018-03-30 16:59:42 +02:00
|
|
|
await writeFile(buildIdPath, buildId, 'utf8')
|
2017-01-11 21:16:18 +01:00
|
|
|
}
|