2019-06-24 15:48:26 +02:00
|
|
|
import webpack, { Stats } from 'webpack'
|
2018-12-03 14:18:52 +01:00
|
|
|
|
2018-12-11 21:46:23 +01:00
|
|
|
export type CompilerResult = {
|
2019-02-17 12:56:48 +01:00
|
|
|
errors: Error[]
|
2018-12-03 14:18:52 +01:00
|
|
|
warnings: Error[]
|
|
|
|
}
|
|
|
|
|
2019-03-10 15:46:50 +01:00
|
|
|
function generateStats(result: CompilerResult, stat: Stats): CompilerResult {
|
|
|
|
const { errors, warnings } = stat.toJson({
|
|
|
|
all: false,
|
|
|
|
warnings: true,
|
|
|
|
errors: true,
|
|
|
|
})
|
|
|
|
if (errors.length > 0) {
|
2019-09-18 17:24:37 +02:00
|
|
|
result.errors.push(...(errors as any))
|
2019-03-10 15:46:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (warnings.length > 0) {
|
2019-09-18 17:24:37 +02:00
|
|
|
result.warnings.push(...(warnings as any))
|
2019-03-10 15:46:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-02-17 12:56:48 +01:00
|
|
|
export function runCompiler(
|
2019-03-10 15:46:50 +01:00
|
|
|
config: webpack.Configuration | webpack.Configuration[]
|
2019-02-17 12:56:48 +01:00
|
|
|
): Promise<CompilerResult> {
|
2018-12-03 14:18:52 +01:00
|
|
|
return new Promise(async (resolve, reject) => {
|
2019-03-10 15:46:50 +01:00
|
|
|
// @ts-ignore webpack allows both a single config or array of configs
|
2018-12-03 14:18:52 +01:00
|
|
|
const compiler = webpack(config)
|
2019-03-10 15:46:50 +01:00
|
|
|
compiler.run((err: Error, statsOrMultiStats: any) => {
|
2018-12-03 14:18:52 +01:00
|
|
|
if (err) {
|
|
|
|
return reject(err)
|
|
|
|
}
|
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
if (statsOrMultiStats.stats) {
|
2019-03-10 15:46:50 +01:00
|
|
|
const result: CompilerResult = statsOrMultiStats.stats.reduce(
|
|
|
|
generateStats,
|
|
|
|
{ errors: [], warnings: [] }
|
|
|
|
)
|
|
|
|
return resolve(result)
|
|
|
|
}
|
2018-12-03 14:18:52 +01:00
|
|
|
|
2019-05-29 13:57:26 +02:00
|
|
|
const result = generateStats(
|
|
|
|
{ errors: [], warnings: [] },
|
|
|
|
statsOrMultiStats
|
|
|
|
)
|
2019-03-10 15:46:50 +01:00
|
|
|
return resolve(result)
|
2018-12-03 14:18:52 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|