2017-01-11 21:16:18 +01:00
|
|
|
import fs from 'mz/fs'
|
|
|
|
import uuid from 'uuid'
|
|
|
|
import path from 'path'
|
2016-10-17 03:54:36 +02:00
|
|
|
import webpack from './webpack'
|
2016-10-17 05:55:33 +02:00
|
|
|
import clean from './clean'
|
2016-12-29 23:38:19 +01:00
|
|
|
import gzipAssets from './gzip'
|
2016-10-06 13:05:52 +02:00
|
|
|
|
|
|
|
export default async function build (dir) {
|
2016-10-17 05:55:33 +02:00
|
|
|
const [compiler] = await Promise.all([
|
|
|
|
webpack(dir),
|
|
|
|
clean(dir)
|
|
|
|
])
|
2016-10-09 11:25:38 +02:00
|
|
|
|
2016-12-28 00:28:19 +01:00
|
|
|
await runCompiler(compiler)
|
2016-12-29 23:38:19 +01:00
|
|
|
await gzipAssets(dir)
|
2017-01-11 21:16:18 +01:00
|
|
|
await writeBuildId(dir)
|
2016-12-16 19:42:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
function runCompiler (compiler) {
|
2016-10-17 03:54:36 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
compiler.run((err, stats) => {
|
|
|
|
if (err) return reject(err)
|
2016-10-06 13:05:52 +02:00
|
|
|
|
2016-10-17 03:54:36 +02:00
|
|
|
const jsonStats = stats.toJson()
|
|
|
|
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
|
|
|
|
2016-10-17 03:54:36 +02:00
|
|
|
resolve()
|
|
|
|
})
|
|
|
|
})
|
2016-10-06 13:05:52 +02:00
|
|
|
}
|
2017-01-11 21:16:18 +01:00
|
|
|
|
|
|
|
async function writeBuildId (dir) {
|
|
|
|
const buildIdPath = path.resolve(dir, '.next', 'BUILD_ID')
|
|
|
|
const buildId = uuid.v4()
|
|
|
|
await fs.writeFile(buildIdPath, buildId, 'utf8')
|
|
|
|
}
|