DevCompileDonePlugin.js 1.7 KB
Newer Older
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
const { chalk, clipboardy, address } = require('@umijs/utils');
class DevCompileDonePlugin {
  constructor(opts) {
    this.opts = opts;
  }

  apply(compiler) {
    let isFirstCompile = true;

    compiler.hooks.done.tap('DevFirstCompileDone', stats => {
      if (stats.hasErrors()) {
        // make sound
        if (process.env.SYSTEM_BELL !== 'none') {
          process.stdout.write('\x07');
        }
        return;
      }

      if (isFirstCompile) {
        const lanIp = address.ip();
        const protocol = this.opts.https ? 'https' : 'http';
        const hostname =
          this.opts.hostname === '0.0.0.0' ? 'localhost' : this.opts.hostname;
        const localUrl = `${protocol}://${hostname}:${this.opts.port}`;
        const lanUrl = `${protocol}://${lanIp}:${this.opts.port}`;
        let copied = '';
        try {
          clipboardy.writeSync(localUrl);
          copied = chalk.dim('(copied to clipboard)');
        } catch (e) {
          copied = chalk.red(`(copy to clipboard failed)`);
        }

        console.log(
          [
            `  App running at:`,
            `  - Local:   ${chalk.cyan(localUrl)} ${copied}`,
            lanUrl && `  - Network: ${chalk.cyan(lanUrl)}`,
          ]
            .filter(Boolean)
            .join('\n'),
        );
43 44 45 46
        process.send &&
          process.send({
            type: 'DONE',
          });
邓晓峰's avatar
邓晓峰 committed
47
        isFirstCompile = false;
48 49
      }

邓晓峰's avatar
邓晓峰 committed
50 51 52 53
      // this.opts.onCompileDone && this.opts.onCompileDone({
      //     isFirstCompile,
      //     stats,
      // });
54 55 56 57 58
      if (isFirstCompile) {
        isFirstCompile = false;
        // process.send({
        //     type: 'DONE'
        // })
59 60 61 62 63 64
      }
    });
  }
}

module.exports = DevCompileDonePlugin;