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 = { export type CompilerResult = {
errors: Error[] errors: string[]
warnings: Error[] warnings: string[]
} }
function generateStats(result: CompilerResult, stat: Stats): CompilerResult { function generateStats(result: CompilerResult, stat: Stats): CompilerResult {
const { errors, warnings } = stat.toJson({ const { errors, warnings } = stat.toJson('errors-warnings')
all: false,
warnings: true,
errors: true,
})
if (errors.length > 0) { if (errors.length > 0) {
result.errors.push(...(errors as any)) result.errors.push(...errors)
} }
if (warnings.length > 0) { if (warnings.length > 0) {
result.warnings.push(...(warnings as any)) result.warnings.push(...warnings)
} }
return result return result
} }
export function runCompiler( export function runCompiler(
config: webpack.Configuration | webpack.Configuration[] config: Configuration | Configuration[]
): Promise<CompilerResult> { ): Promise<CompilerResult> {
return new Promise(async (resolve, reject) => { return new Promise(async (resolve, reject) => {
const compiler = webpack(config) const compiler = webpack(config)
compiler.run((err: Error, statsOrMultiStats: any) => { compiler.run(
(err: Error, statsOrMultiStats: { stats: Stats[] } | Stats) => {
if (err) { if (err) {
return reject(err) return reject(err)
} }
if (statsOrMultiStats.stats) { if ('stats' in statsOrMultiStats) {
const result: CompilerResult = statsOrMultiStats.stats.reduce( const result: CompilerResult = statsOrMultiStats.stats.reduce(
generateStats, generateStats,
{ errors: [], warnings: [] } { errors: [], warnings: [] }
...@@ -45,6 +42,7 @@ export function runCompiler( ...@@ -45,6 +42,7 @@ export function runCompiler(
statsOrMultiStats statsOrMultiStats
) )
return resolve(result) 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