1
0
Fork 0
mirror of https://github.com/terribleplan/next.js.git synced 2024-01-19 02:48:18 +00:00
next.js/packages/next/build/index.js
Timmy Willison 1b8f56556b Output warnings and errors from webpack individually (#5442)
- Shows warnings even when resolving, to facilitate hints set to 'warning'
- Fixes #876 : Set performance.hints to 'warning' or 'error' in next.config.js
2018-10-20 17:02:20 +02:00

68 lines
1.9 KiB
JavaScript

import { join } from 'path'
import promisify from '../lib/promisify'
import fs from 'fs'
import webpack from 'webpack'
import loadConfig from 'next-server/next-config'
import { PHASE_PRODUCTION_BUILD, BUILD_ID_FILE } from 'next-server/constants'
import getBaseWebpackConfig from './webpack'
const access = promisify(fs.access)
const writeFile = promisify(fs.writeFile)
export default async function build (dir, conf = null) {
const config = loadConfig(PHASE_PRODUCTION_BUILD, dir, conf)
const buildId = await config.generateBuildId() // defaults to a uuid
const distDir = join(dir, config.distDir)
try {
await access(dir, (fs.constants || fs).W_OK)
} catch (err) {
console.error(`> Failed, build directory is not writeable. https://err.sh/zeit/next.js/build-dir-not-writeable`)
throw err
}
try {
const configs = await Promise.all([
getBaseWebpackConfig(dir, { buildId, isServer: false, config }),
getBaseWebpackConfig(dir, { buildId, isServer: true, config })
])
await runCompiler(configs)
await writeBuildId(distDir, buildId)
} catch (err) {
console.error(`> Failed to build`)
throw err
}
}
function runCompiler (compiler) {
return new Promise(async (resolve, reject) => {
const webpackCompiler = await webpack(await compiler)
webpackCompiler.run((err, stats) => {
if (err) return reject(err)
const jsonStats = stats.toJson({ warnings: true, errors: true })
if (jsonStats.errors.length > 0) {
console.log()
console.log(...jsonStats.warnings)
console.error(...jsonStats.errors)
return reject(new Error('Soft webpack errors'))
}
if (jsonStats.warnings.length > 0) {
console.log()
console.log(...jsonStats.warnings)
}
resolve()
})
})
}
async function writeBuildId (distDir, buildId) {
const buildIdPath = join(distDir, BUILD_ID_FILE)
await writeFile(buildIdPath, buildId, 'utf8')
}