Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
matz3 committed Nov 6, 2020
1 parent ae5b8b5 commit 21d85ef
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
23 changes: 18 additions & 5 deletions lib/processors/uglifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const workerpool = require("workerpool");
let _pool;

const MIN_WORKERS = 2;
const MAX_WORKERS = Infinity;
const MAX_WORKERS = 4;
const osCpus = require("os").cpus().length;
const maxWorkers = Math.max(Math.min((osCpus || 1) - 1, MAX_WORKERS), MIN_WORKERS);

Expand All @@ -23,6 +23,8 @@ function pool({taskUtil}) {
return _pool;
}

let uglifyWorker;

/**
* Minifies the supplied resources.
*
Expand All @@ -36,10 +38,21 @@ function pool({taskUtil}) {
module.exports = function({resources, taskUtil}) {
return Promise.all(resources.map(async (resource) => {
const code = await resource.getString();
const uglifiedCode = await pool({taskUtil}).exec("uglify", [{
filePath: resource.getPath(),
code
}]);
let uglifiedCode;
if (taskUtil) {
uglifiedCode = await pool({taskUtil}).exec("uglify", [{
filePath: resource.getPath(),
code
}]);
} else {
if (!uglifyWorker) {
uglifyWorker = require("./uglifier_worker");
}
uglifiedCode = await uglifyWorker({
filePath: resource.getPath(),
code
});
}
resource.setString(uglifiedCode);
return resource;
}));
Expand Down
15 changes: 11 additions & 4 deletions lib/processors/uglifier_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,14 @@ async function uglify({filePath, code}) {
}
}

// create a worker and register public functions
workerpool.worker({
uglify
});
if (!workerpool.isMainThread) {
// create a worker and register public functions
workerpool.worker({
uglify
});
} else {
// Normal use without worker
module.exports = uglify;
}


2 changes: 2 additions & 0 deletions lib/tasks/uglify.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ module.exports = function({workspace, taskUtil, options: {pattern}}) {
return !taskUtil.getTag(resource, taskUtil.STANDARD_TAGS.IsBundle);
});
}
console.time("uglifyProcessor");
return uglifyProcessor({
resources,
taskUtil
});
})
.then((processedResources) => {
console.timeEnd("uglifyProcessor");
return Promise.all(processedResources.map((resource) => {
return workspace.write(resource);
}));
Expand Down

0 comments on commit 21d85ef

Please sign in to comment.