forked from nsjcy/frontEnd/nsjcy

xuxj
2020-05-09 8786023e8093d97b8d4fadc1b7f41f5883ac3a03
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
48
49
50
51
52
53
54
55
56
57
58
const ProgressPlugin = require('webpack/lib/ProgressPlugin');
const webpack = require('webpack');
const chalk = require('chalk');
const path = require('path');
 
module.exports = function (config) {
  const compiler = webpack(config);
  compiler.apply(
    new ProgressPlugin((percentage, msg, current, active, modulepath) => {
      if (process.stdout.isTTY && percentage < 1) {
        process.stdout.cursorTo(0);
        modulepath = modulepath ? ' .../' + path.basename(modulepath) : '';
        current = current ? ' ' + current : '';
        active = active ? ' ' + active : '';
        process.stdout.write(`${(percentage * 100).toFixed(0)}% ${msg + current + active + modulepath}`);
        process.stdout.clearLine(1);
      } else if (percentage === 1) {
        process.stdout.clearLine(1);
        process.stdout.write('\n\n');
        console.log(chalk.yellow(`webpack: done @${new Date().toLocaleString()}`));
        process.stdout.write('\n');
      }
    }));
  const watching = compiler.watch({
    ignored: /node_modules/,
    aggregateTimeout: 0
  }, (err, stats) => {
    if (err) {
      console.error(err.stack || err);
      if (err.details) {
        console.error(err.details);
      }
      return;
    }
 
    const info = stats.toJson();
 
    if (stats.hasErrors()) {
      console.error(info.errors);
    }
 
    // if (stats.hasWarnings()) {
    //   console.warn(info.warnings);
    // }
 
    console.log(stats.toString({
      children: false,
      warnings: false,
      version: false,
      maxModules: 4,
      cached: false,
      assets: false,
      chunks: false,
      colors: true,
    }));
  });
  return compiler;
};