2017-02-15 13:03:33 +00:00
|
|
|
import { tmpdir } from 'os'
|
|
|
|
import { join } from 'path'
|
2017-01-11 20:16:18 +00:00
|
|
|
import fs from 'mz/fs'
|
|
|
|
import uuid from 'uuid'
|
2017-02-15 13:03:33 +00:00
|
|
|
import del from 'del'
|
2016-10-17 01:54:36 +00:00
|
|
|
import webpack from './webpack'
|
2017-02-14 23:14:30 +00:00
|
|
|
import replaceCurrentBuild from './replace'
|
2017-03-24 07:51:34 +00:00
|
|
|
import md5File from 'md5-file/promise'
|
2016-10-06 11:05:52 +00:00
|
|
|
|
2017-06-23 03:48:06 +00:00
|
|
|
export default async function build (dir, conf = null) {
|
2017-08-31 00:17:06 +00:00
|
|
|
const buildId = uuid.v4()
|
2017-12-02 17:52:34 +00:00
|
|
|
const tempDir = tmpdir()
|
|
|
|
const buildDir = join(tempDir, uuid.v4())
|
2017-12-02 17:13:39 +00:00
|
|
|
|
|
|
|
try {
|
2017-12-02 17:52:34 +00:00
|
|
|
await fs.access(tempDir, fs.constants.W_OK)
|
2017-12-02 17:13:39 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(`> Failed, build directory is not writeable. https://err.sh/zeit/next.js/build-dir-not-writeable`)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
|
2017-08-31 00:17:06 +00:00
|
|
|
const compiler = await webpack(dir, { buildId, buildDir, conf })
|
2016-10-09 09:25:38 +00:00
|
|
|
|
2017-02-15 13:03:33 +00:00
|
|
|
try {
|
2017-10-30 14:55:44 +00:00
|
|
|
const stats = await runCompiler(compiler)
|
|
|
|
await writeBuildStats(buildDir, stats)
|
2017-08-31 00:17:06 +00:00
|
|
|
await writeBuildId(buildDir, buildId)
|
2017-02-15 13:03:33 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.error(`> Failed to build on ${buildDir}`)
|
|
|
|
throw err
|
|
|
|
}
|
2017-02-14 23:14:30 +00:00
|
|
|
|
2017-02-15 13:03:33 +00:00
|
|
|
await replaceCurrentBuild(dir, buildDir)
|
|
|
|
|
|
|
|
// no need to wait
|
2017-02-15 16:31:21 +00:00
|
|
|
del(buildDir, { force: true })
|
2016-12-16 18:42:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function runCompiler (compiler) {
|
2016-10-17 01:54:36 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
compiler.run((err, stats) => {
|
|
|
|
if (err) return reject(err)
|
2016-10-06 11:05:52 +00:00
|
|
|
|
2016-10-17 01:54:36 +00:00
|
|
|
const jsonStats = stats.toJson()
|
2017-03-07 18:43:56 +00:00
|
|
|
|
2016-10-17 01:54:36 +00: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 11:05:52 +00:00
|
|
|
|
2017-03-07 18:43:56 +00:00
|
|
|
resolve(jsonStats)
|
2016-10-17 01:54:36 +00:00
|
|
|
})
|
|
|
|
})
|
2016-10-06 11:05:52 +00:00
|
|
|
}
|
2017-01-11 20:16:18 +00:00
|
|
|
|
2017-10-30 14:55:44 +00:00
|
|
|
async function writeBuildStats (dir, stats) {
|
2017-03-24 07:51:34 +00:00
|
|
|
// Here we can't use hashes in webpack chunks.
|
|
|
|
// That's because the "app.js" is not tied to a chunk.
|
|
|
|
// It's created by merging a few assets. (commons.js and main.js)
|
|
|
|
// So, we need to generate the hash ourself.
|
|
|
|
const assetHashMap = {
|
|
|
|
'app.js': {
|
2017-04-07 16:52:12 +00:00
|
|
|
hash: await md5File(join(dir, '.next', 'app.js'))
|
2017-03-24 07:51:34 +00:00
|
|
|
}
|
|
|
|
}
|
2017-04-07 16:52:12 +00:00
|
|
|
const buildStatsPath = join(dir, '.next', 'build-stats.json')
|
2017-03-24 07:51:34 +00:00
|
|
|
await fs.writeFile(buildStatsPath, JSON.stringify(assetHashMap), 'utf8')
|
2017-10-30 14:55:44 +00:00
|
|
|
|
|
|
|
const statsPath = join(dir, '.next', 'webpack-stats.json')
|
|
|
|
await fs.writeFile(statsPath, JSON.stringify(stats), 'utf8')
|
2017-03-07 18:43:56 +00:00
|
|
|
}
|
|
|
|
|
2017-08-31 00:17:06 +00:00
|
|
|
async function writeBuildId (dir, buildId) {
|
2017-04-07 16:52:12 +00:00
|
|
|
const buildIdPath = join(dir, '.next', 'BUILD_ID')
|
2017-01-11 20:16:18 +00:00
|
|
|
await fs.writeFile(buildIdPath, buildId, 'utf8')
|
|
|
|
}
|