Skip to content

Commit

Permalink
Update compiler.ts Improve TypeScript typings (vercel#11074)
Browse files Browse the repository at this point in the history
Improve TypeScript typings
  • Loading branch information
lifeiscontent authored Apr 6, 2020
1 parent c89ddc9 commit bddd1ce
Showing 1 changed file with 24 additions and 26 deletions.
50 changes: 24 additions & 26 deletions packages/next/build/compiler.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,48 @@
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)
})
)
})
}

0 comments on commit bddd1ce

Please sign in to comment.