-
Notifications
You must be signed in to change notification settings - Fork 28k
/
Copy pathtaskfile-webpack.js
47 lines (40 loc) · 1.27 KB
/
taskfile-webpack.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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 (stats.hasWarnings()) {
this.emit('plugin_warning', {
plugin: 'taskfile-webpack',
warning: `webpack compiled ${options.name} with warnings:\n${stats.toString('errors-warnings')}`,
})
}
if (process.env.ANALYZE_STATS) {
require('fs').writeFileSync(
require('path').join(__dirname, options.name + '-stats.json'),
JSON.stringify(stats.toJson())
)
}
resolve()
})
})
})
}