c64cb60a8e
By default, webpack will proceed to run loaders and plugins on all files, even after an error has been encountered during the build process. This means you might need to wait minutes to see a syntax error encountered in one of your source files. This PR fixes that.
52 lines
1.3 KiB
TypeScript
52 lines
1.3 KiB
TypeScript
import webpack, { Stats, Configuration } from 'webpack'
|
|
|
|
export type CompilerResult = {
|
|
errors: string[]
|
|
warnings: string[]
|
|
}
|
|
|
|
function generateStats(result: CompilerResult, stat: Stats): CompilerResult {
|
|
const { errors, warnings } = stat.toJson('errors-warnings')
|
|
if (errors.length > 0) {
|
|
result.errors.push(...errors)
|
|
}
|
|
|
|
if (warnings.length > 0) {
|
|
result.warnings.push(...warnings)
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
export function runCompiler(
|
|
config: Configuration | Configuration[]
|
|
): Promise<CompilerResult> {
|
|
return new Promise(async (resolve, reject) => {
|
|
const compiler = webpack(config)
|
|
compiler.run(
|
|
(err: Error, statsOrMultiStats: { stats: Stats[] } | Stats) => {
|
|
if (err) {
|
|
const reason = err?.toString()
|
|
if (reason) {
|
|
return resolve({ errors: [reason], warnings: [] })
|
|
}
|
|
return reject(err)
|
|
}
|
|
|
|
if ('stats' in statsOrMultiStats) {
|
|
const result: CompilerResult = statsOrMultiStats.stats.reduce(
|
|
generateStats,
|
|
{ errors: [], warnings: [] }
|
|
)
|
|
return resolve(result)
|
|
}
|
|
|
|
const result = generateStats(
|
|
{ errors: [], warnings: [] },
|
|
statsOrMultiStats
|
|
)
|
|
return resolve(result)
|
|
}
|
|
)
|
|
})
|
|
}
|