rsnext/packages/next/build/compiler.ts
Guy Bedford e85a517e1a ncc Webpack build redux (#7628)
* Reimplement ncc webpack build

This reverts commit 6feca310d7.

* Shared webpack build

* ncc workaround pending @zeit/ncc#437

* update ncc

* build tweaks, fixup autodll-import

* possible Node 8 fix

* second possible Node 8 fix

* and update taskfile
2019-06-21 12:28:41 -04:00

51 lines
1.2 KiB
TypeScript

import webpack, { Stats } from 'next/dist/compiled/webpack.js'
export type CompilerResult = {
errors: Error[]
warnings: Error[]
}
function generateStats(result: CompilerResult, stat: 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
}
export function runCompiler(
config: webpack.Configuration | webpack.Configuration[]
): Promise<CompilerResult> {
return new Promise(async (resolve, reject) => {
// @ts-ignore webpack allows both a single config or array of configs
const compiler = webpack(config)
compiler.run((err: Error, statsOrMultiStats: any) => {
if (err) {
return reject(err)
}
if (statsOrMultiStats.stats) {
const result: CompilerResult = statsOrMultiStats.stats.reduce(
generateStats,
{ errors: [], warnings: [] }
)
return resolve(result)
}
const result = generateStats(
{ errors: [], warnings: [] },
statsOrMultiStats
)
return resolve(result)
})
})
}