You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
3.3 KiB

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