rsnext/packages/next/taskfile-webpack.js
Wyatt Johnson 50dff93902
Unsilence Taskr Webpack errors (#56542)
Discovered while investigating https://github.com/vercel/next.js/pull/56526, turns out errors occuring during webpack builds do not fail the `pnpm build` which kicks off `taskr`. This is because `taskr` runs their plugins within coroutines, which based on the result, was not handling the promise rejections as expected.
2023-10-06 23:38:18 +00:00

40 lines
1 KiB
JavaScript

const webpack = require('webpack')
module.exports = function (task) {
// eslint-disable-next-line require-yield
task.plugin('webpack', {}, function* (_, options) {
options = options || {}
const compiler = webpack(options.config)
if (options.watch) {
return compiler.watch({}, (err, stats) => {
if (err || stats.hasErrors()) {
console.error(err || stats.toString())
} else {
console.log(`${options.name} compiled successfully.`)
}
})
}
return new Promise((resolve) => {
compiler.run((err, stats) => {
if (err || stats.hasErrors()) {
return this.emit('plugin_error', {
plugin: 'taskfile-webpack',
error: err?.message ?? stats.toString(),
})
}
if (process.env.ANALYZE) {
require('fs').writeFileSync(
require('path').join(__dirname, options.name + '-stats.json'),
JSON.stringify(stats.toJson())
)
}
resolve()
})
})
})
}