2021-01-14 02:59:08 +01:00
|
|
|
import { webpack } from 'next/dist/compiled/webpack/webpack'
|
2021-09-13 15:49:29 +02:00
|
|
|
import { Span } from '../trace'
|
2018-12-03 14:18:52 +01:00
|
|
|
|
2018-12-11 21:46:23 +01:00
|
|
|
export type CompilerResult = {
|
2022-08-16 11:55:37 +02:00
|
|
|
errors: webpack.StatsError[]
|
|
|
|
warnings: webpack.StatsError[]
|
|
|
|
stats: webpack.Stats | undefined
|
2018-12-03 14:18:52 +01:00
|
|
|
}
|
|
|
|
|
2021-01-14 02:59:08 +01:00
|
|
|
function generateStats(
|
|
|
|
result: CompilerResult,
|
2022-08-16 11:55:37 +02:00
|
|
|
stat: webpack.Stats
|
2021-01-14 02:59:08 +01:00
|
|
|
): CompilerResult {
|
2021-10-21 01:23:44 +02:00
|
|
|
const { errors, warnings } = stat.toJson({
|
|
|
|
preset: 'errors-warnings',
|
|
|
|
moduleTrace: true,
|
|
|
|
})
|
|
|
|
if (errors && errors.length > 0) {
|
2020-04-06 17:58:35 +02:00
|
|
|
result.errors.push(...errors)
|
2019-03-10 15:46:50 +01:00
|
|
|
}
|
|
|
|
|
2021-10-21 01:23:44 +02:00
|
|
|
if (warnings && warnings.length > 0) {
|
2020-04-06 17:58:35 +02:00
|
|
|
result.warnings.push(...warnings)
|
2019-03-10 15:46:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2020-06-17 17:00:29 +02:00
|
|
|
// Webpack 5 requires the compiler to be closed (to save caches)
|
|
|
|
// Webpack 4 does not have this close method so in order to be backwards compatible we check if it exists
|
2022-08-16 11:55:37 +02:00
|
|
|
function closeCompiler(compiler: webpack.Compiler | webpack.MultiCompiler) {
|
2021-06-18 00:43:25 +02:00
|
|
|
return new Promise<void>((resolve, reject) => {
|
2021-10-06 17:40:01 +02:00
|
|
|
// @ts-ignore Close only exists on the compiler in webpack 5
|
|
|
|
return compiler.close((err: any) => (err ? reject(err) : resolve()))
|
2020-06-17 17:00:29 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-02-17 12:56:48 +01:00
|
|
|
export function runCompiler(
|
2021-08-18 14:22:53 +02:00
|
|
|
config: webpack.Configuration,
|
|
|
|
{ runWebpackSpan }: { runWebpackSpan: Span }
|
2019-02-17 12:56:48 +01:00
|
|
|
): Promise<CompilerResult> {
|
2020-06-17 17:00:29 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
2022-08-16 11:55:37 +02:00
|
|
|
const compiler = webpack(config) as unknown as webpack.Compiler
|
2021-10-21 01:23:44 +02:00
|
|
|
compiler.run((err, stats) => {
|
2021-09-13 15:49:29 +02:00
|
|
|
const webpackCloseSpan = runWebpackSpan.traceChild('webpack-close', {
|
|
|
|
name: config.name,
|
|
|
|
})
|
2021-08-18 14:22:53 +02:00
|
|
|
webpackCloseSpan
|
|
|
|
.traceAsyncFn(() => closeCompiler(compiler))
|
|
|
|
.then(() => {
|
2020-06-17 17:00:29 +02:00
|
|
|
if (err) {
|
2021-10-22 12:30:50 +02:00
|
|
|
const reason = err.stack ?? err.toString()
|
2020-06-17 17:00:29 +02:00
|
|
|
if (reason) {
|
2021-10-22 12:30:50 +02:00
|
|
|
return resolve({
|
|
|
|
errors: [{ message: reason, details: (err as any).details }],
|
|
|
|
warnings: [],
|
2022-05-02 17:21:40 +02:00
|
|
|
stats,
|
2021-10-22 12:30:50 +02:00
|
|
|
})
|
2020-06-17 17:00:29 +02:00
|
|
|
}
|
|
|
|
return reject(err)
|
2021-10-22 12:30:50 +02:00
|
|
|
} else if (!stats) throw new Error('No Stats from webpack')
|
2018-12-03 14:18:52 +01:00
|
|
|
|
2021-08-18 14:22:53 +02:00
|
|
|
const result = webpackCloseSpan
|
|
|
|
.traceChild('webpack-generate-error-stats')
|
2022-05-02 17:21:40 +02:00
|
|
|
.traceFn(() =>
|
|
|
|
generateStats({ errors: [], warnings: [], stats }, stats)
|
|
|
|
)
|
2020-04-06 17:58:35 +02:00
|
|
|
return resolve(result)
|
2020-06-17 17:00:29 +02:00
|
|
|
})
|
2021-08-18 14:22:53 +02:00
|
|
|
})
|
2018-12-03 14:18:52 +01:00
|
|
|
})
|
|
|
|
}
|