Skip to content

Commit

Permalink
Init pool only once per build execution
Browse files Browse the repository at this point in the history
  • Loading branch information
matz3 committed Sep 12, 2019
1 parent af46c00 commit a9edbae
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 14 deletions.
29 changes: 18 additions & 11 deletions lib/processors/uglifier.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
const workerpool = require("workerpool");
let _pool;

function pool({buildContext}) {
if (!_pool) {
_pool = workerpool.pool(__dirname + "/uglifier_worker.js", {
workerType: "auto"
});
buildContext.registerCleanupTask(() => {
_pool.terminate();
_pool = null;
});
}
return _pool;
}

/**
* Minifies the supplied resources.
Expand All @@ -8,24 +23,16 @@
* @param {module:@ui5/fs.Resource[]} parameters.resources List of resources to be processed
* @returns {Promise<module:@ui5/fs.Resource[]>} Promise resolving with uglified resources
*/
module.exports = function({resources}) {
const workerpool = require("workerpool");
const pool = workerpool.pool(__dirname + "/uglifier_worker.js", {
workerType: "auto"
});

module.exports = function({resources, buildContext}) {
return Promise.all(resources.map((resource) => {
return resource.getString().then((code) => {
return pool.exec("uglify", [{
return pool({buildContext}).exec("uglify", [{
filePath: resource.getPath(),
code
}]);
}).then((uglifiedCode) => {
resource.setString(uglifiedCode);
return resource;
});
})).then((processedResources) => {
pool.terminate();
return processedResources;
});
}));
};
5 changes: 3 additions & 2 deletions lib/tasks/uglify.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ const uglifyProcessor = require("../processors/uglifier");
* @param {string} parameters.options.pattern Pattern to locate the files to be processed
* @returns {Promise<undefined>} Promise resolving with <code>undefined</code> once data has been written
*/
module.exports = function({workspace, options}) {
module.exports = function({workspace, options, buildContext}) {
return workspace.byGlobSource(options.pattern)
.then((allResources) => {
return uglifyProcessor({
resources: allResources
resources: allResources,
buildContext
});
})
.then((processedResources) => {
Expand Down
3 changes: 2 additions & 1 deletion lib/types/application/ApplicationBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const tasks = { // can't require index.js due to circular dependency
};

class ApplicationBuilder extends AbstractBuilder {
addStandardTasks({resourceCollections, project, log}) {
addStandardTasks({resourceCollections, project, log, buildContext}) {
if (!project.metadata.namespace) {
log.info("Skipping some tasks due to missing application namespace information. If your project contains " +
"a Component.js, you might be missing a manifest.json file. " +
Expand Down Expand Up @@ -161,6 +161,7 @@ class ApplicationBuilder extends AbstractBuilder {
this.addTask("uglify", () => {
const uglify = tasks.uglify;
return uglify({
buildContext,
workspace: resourceCollections.workspace,
options: {
pattern: "/**/*.js"
Expand Down
1 change: 1 addition & 0 deletions lib/types/library/LibraryBuilder.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ class LibraryBuilder extends AbstractBuilder {
this.addTask("uglify", () => {
const uglify = tasks.uglify;
return uglify({
buildContext,
workspace: resourceCollections.workspace,
options: {
pattern: "/resources/**/*.js"
Expand Down

0 comments on commit a9edbae

Please sign in to comment.