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/compiler.ts

42 lines
964 B
TypeScript

import webpack from 'webpack'
export type CompilerResult = {
errors: Error[]
warnings: Error[]
}
export function runCompiler(
config: webpack.Configuration[]
): Promise<CompilerResult> {
return new Promise(async (resolve, reject) => {
const compiler = webpack(config)
compiler.run((err, multiStats: any) => {
if (err) {
return reject(err)
}
const result: CompilerResult = multiStats.stats.reduce(
(result: CompilerResult, stat: webpack.Stats): CompilerResult => {
const { errors, warnings } = stat.toJson({
all: false,
warnings: true,
errors: true,
})
if (errors.length > 0) {
result.errors.push(...errors)
}
if (warnings.length > 0) {
result.warnings.push(...warnings)
}
return result
},
{ errors: [], warnings: [] }
)
resolve(result)
})
})
}