Projekt

Obecné

Profil

Stáhnout (3.33 KB) Statistiky
| Větev: | Revize:
1 3a515b92 cagy
"use strict";
2
3
Object.defineProperty(exports, "__esModule", {
4
  value: true
5
});
6
exports.default = void 0;
7
8
var _os = _interopRequireDefault(require("os"));
9
10
var _cacache = _interopRequireDefault(require("cacache"));
11
12
var _findCacheDir = _interopRequireDefault(require("find-cache-dir"));
13
14
var _workerFarm = _interopRequireDefault(require("worker-farm"));
15
16
var _serializeJavascript = _interopRequireDefault(require("serialize-javascript"));
17
18
var _isWsl = _interopRequireDefault(require("is-wsl"));
19
20
var _minify = _interopRequireDefault(require("./minify"));
21
22
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
24
const worker = require.resolve('./worker');
25
26
class TaskRunner {
27
  constructor(options = {}) {
28
    const {
29
      cache,
30
      parallel
31
    } = options;
32
    this.cacheDir = cache === true ? (0, _findCacheDir.default)({
33
      name: 'terser-webpack-plugin'
34
    }) || _os.default.tmpdir() : cache; // In some cases cpus() returns undefined
35
    // https://github.com/nodejs/node/issues/19022
36
37
    const cpus = _os.default.cpus() || {
38
      length: 1
39
    }; // WSL sometimes freezes, error seems to be on the WSL side
40
    // https://github.com/webpack-contrib/terser-webpack-plugin/issues/21
41
42
    this.maxConcurrentWorkers = _isWsl.default ? 1 : parallel === true ? cpus.length - 1 : Math.min(Number(parallel) || 0, cpus.length - 1);
43
  }
44
45
  run(tasks, callback) {
46
    /* istanbul ignore if */
47
    if (!tasks.length) {
48
      callback(null, []);
49
      return;
50
    }
51
52
    if (this.maxConcurrentWorkers > 1) {
53
      const workerOptions = process.platform === 'win32' ? {
54
        maxConcurrentWorkers: this.maxConcurrentWorkers,
55
        maxConcurrentCallsPerWorker: 1
56
      } : {
57
        maxConcurrentWorkers: this.maxConcurrentWorkers
58
      };
59
      this.workers = (0, _workerFarm.default)(workerOptions, worker);
60
61
      this.boundWorkers = (options, cb) => {
62
        try {
63
          this.workers((0, _serializeJavascript.default)(options), cb);
64
        } catch (error) {
65
          // worker-farm can fail with ENOMEM or something else
66
          cb(error);
67
        }
68
      };
69
    } else {
70
      this.boundWorkers = (options, cb) => {
71
        try {
72
          cb(null, (0, _minify.default)(options));
73
        } catch (error) {
74
          cb(error);
75
        }
76
      };
77
    }
78
79
    let toRun = tasks.length;
80
    const results = [];
81
82
    const step = (index, data) => {
83
      toRun -= 1;
84
      results[index] = data;
85
86
      if (!toRun) {
87
        callback(null, results);
88
      }
89
    };
90
91
    tasks.forEach((task, index) => {
92
      const enqueue = () => {
93
        this.boundWorkers(task, (error, data) => {
94
          const result = error ? {
95
            error
96
          } : data;
97
98
          const done = () => step(index, result);
99
100
          if (this.cacheDir && !result.error) {
101
            _cacache.default.put(this.cacheDir, (0, _serializeJavascript.default)(task.cacheKeys), JSON.stringify(data)).then(done, done);
102
          } else {
103
            done();
104
          }
105
        });
106
      };
107
108
      if (this.cacheDir) {
109
        _cacache.default.get(this.cacheDir, (0, _serializeJavascript.default)(task.cacheKeys)).then(({
110
          data
111
        }) => step(index, JSON.parse(data)), enqueue);
112
      } else {
113
        enqueue();
114
      }
115
    });
116
  }
117
118
  exit() {
119
    if (this.workers) {
120
      _workerFarm.default.end(this.workers);
121
    }
122
  }
123
124
}
125
126
exports.default = TaskRunner;