Unverified Commit bddd1ce5 authored by Aaron Reisman's avatar Aaron Reisman Committed by GitHub

Update compiler.ts Improve TypeScript typings (#11074)

Improve TypeScript typings
parent c89ddc97
import webpack, { Stats } from 'webpack'
import webpack, { Stats, Configuration } from 'webpack'
export type CompilerResult = {
errors: Error[]
warnings: Error[]
errors: string[]
warnings: string[]
}
function generateStats(result: CompilerResult, stat: Stats): CompilerResult {
const { errors, warnings } = stat.toJson({
all: false,
warnings: true,
errors: true,
})
const { errors, warnings } = stat.toJson('errors-warnings')
if (errors.length > 0) {
result.errors.push(...(errors as any))
result.errors.push(...errors)
}
if (warnings.length > 0) {
result.warnings.push(...(warnings as any))
result.warnings.push(...warnings)
}
return result
}
export function runCompiler(
config: webpack.Configuration | webpack.Configuration[]
config: Configuration | Configuration[]
): Promise<CompilerResult> {
return new Promise(async (resolve, reject) => {
const compiler = webpack(config)
compiler.run((err: Error, statsOrMultiStats: any) => {
if (err) {
return reject(err)
}
compiler.run(
(err: Error, statsOrMultiStats: { stats: Stats[] } | Stats) => {
if (err) {
return reject(err)
}
if (statsOrMultiStats.stats) {
const result: CompilerResult = statsOrMultiStats.stats.reduce(
generateStats,
{ errors: [], warnings: [] }
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)
}
const result = generateStats(
{ errors: [], warnings: [] },
statsOrMultiStats
)
return resolve(result)
})
)
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment