3f691eaa45
* Remove ts-ignore where possible And replace by typecasts * More accurate types * bend cliententries in a correct shape earlier on * comment becomes unnecessary * add webpack overload to allow for the next.js use case * Avoid changing public interface Co-authored-by: Joe Haddad <timer150@gmail.com>
50 lines
1.1 KiB
TypeScript
50 lines
1.1 KiB
TypeScript
import webpack, { Stats } from 'webpack'
|
|
|
|
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 as any))
|
|
}
|
|
|
|
if (warnings.length > 0) {
|
|
result.warnings.push(...(warnings as any))
|
|
}
|
|
|
|
return result
|
|
}
|
|
|
|
export function runCompiler(
|
|
config: webpack.Configuration | webpack.Configuration[]
|
|
): Promise<CompilerResult> {
|
|
return new Promise(async (resolve, reject) => {
|
|
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)
|
|
})
|
|
})
|
|
}
|