forked from vercel/next.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update compiler.ts Improve TypeScript typings (vercel#11074)
Improve TypeScript typings
- Loading branch information
1 parent
c89ddc9
commit bddd1ce
Showing
1 changed file
with
24 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
) | ||
}) | ||
} |