2023-09-08 18:05:29 +02:00
|
|
|
const webpack = require('webpack')
|
|
|
|
|
|
|
|
module.exports = function (task) {
|
2023-10-07 01:38:18 +02:00
|
|
|
// eslint-disable-next-line require-yield
|
2023-09-08 18:05:29 +02:00
|
|
|
task.plugin('webpack', {}, function* (_, options) {
|
|
|
|
options = options || {}
|
|
|
|
|
|
|
|
const compiler = webpack(options.config)
|
|
|
|
|
|
|
|
if (options.watch) {
|
2023-10-07 01:38:18 +02:00
|
|
|
return compiler.watch({}, (err, stats) => {
|
2023-09-08 18:05:29 +02:00
|
|
|
if (err || stats.hasErrors()) {
|
|
|
|
console.error(err || stats.toString())
|
|
|
|
} else {
|
|
|
|
console.log(`${options.name} compiled successfully.`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2023-10-07 01:38:18 +02:00
|
|
|
|
|
|
|
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()
|
|
|
|
})
|
|
|
|
})
|
2023-09-08 18:05:29 +02:00
|
|
|
})
|
|
|
|
}
|