-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
81 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,42 @@ | ||
const terser = require("terser"); | ||
/** | ||
* Preserve comments which contain: | ||
* <ul> | ||
* <li>copyright notice</li> | ||
* <li>license terms</li> | ||
* <li>"@ui5-bundle"</li> | ||
* <li>"@ui5-bundle-raw-include"</li> | ||
* </ul> | ||
* | ||
* @type {RegExp} | ||
*/ | ||
const copyrightCommentsAndBundleCommentPattern = /copyright|\(c\)(?:[0-9]+|\s+[0-9A-za-z])|released under|license|\u00a9|^@ui5-bundle-raw-include |^@ui5-bundle /i; | ||
const workerpool = require("workerpool"); | ||
let _pool; | ||
|
||
// Use up to 5 workers, depending on available CPUs | ||
// Using more workers doesn't seem to improve overall performance | ||
const maxWorkers = Math.max(Math.min((require("os").cpus().length || 4) - 1, 5), 1); | ||
|
||
function pool({taskUtil}) { | ||
if (!_pool) { | ||
_pool = workerpool.pool(__dirname + "/uglifier_worker.js", { | ||
workerType: "auto", | ||
maxWorkers | ||
}); | ||
taskUtil.registerCleanupTask(() => { | ||
_pool.terminate(); | ||
_pool = null; | ||
}); | ||
} | ||
return _pool; | ||
} | ||
|
||
/** | ||
* Minifies the supplied resources. | ||
* | ||
* @public | ||
* @alias module:@ui5/builder.processors.uglifier | ||
* @param {module:@ui5/builder.tasks.TaskUtil|object} [parameters.taskUtil] TaskUtil | ||
* @param {object} parameters Parameters | ||
* @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}) { | ||
module.exports = function({resources, taskUtil}) { | ||
return Promise.all(resources.map(async (resource) => { | ||
const code = await resource.getString(); | ||
try { | ||
const result = await terser.minify({ | ||
[resource.getPath()]: code | ||
}, { | ||
output: { | ||
comments: copyrightCommentsAndBundleCommentPattern, | ||
wrap_func_args: false | ||
}, | ||
compress: false | ||
}); | ||
resource.setString(result.code); | ||
return resource; | ||
} catch (err) { | ||
throw new Error( | ||
`Uglification failed with error: ${err.message} in file ${err.filename} ` + | ||
`(line ${err.line}, col ${err.col}, pos ${err.pos})`); | ||
} | ||
const uglifiedCode = await pool({taskUtil}).exec("uglify", [{ | ||
filePath: resource.getPath(), | ||
code | ||
}]); | ||
resource.setString(uglifiedCode); | ||
return resource; | ||
})); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const workerpool = require("workerpool"); | ||
const terser = require("terser"); | ||
/** | ||
* Preserve comments which contain: | ||
* <ul> | ||
* <li>copyright notice</li> | ||
* <li>license terms</li> | ||
* <li>"@ui5-bundle"</li> | ||
* <li>"@ui5-bundle-raw-include"</li> | ||
* </ul> | ||
* | ||
* @type {RegExp} | ||
*/ | ||
const copyrightCommentsAndBundleCommentPattern = | ||
/copyright|\(c\)(?:[0-9]+|\s+[0-9A-za-z])|released under|license|\u00a9|^@ui5-bundle-raw-include |^@ui5-bundle /i; | ||
|
||
async function uglify({filePath, code}) { | ||
try { | ||
const result = terser.minify({ | ||
[filePath]: code | ||
}, { | ||
output: { | ||
comments: copyrightCommentsAndBundleCommentPattern, | ||
wrap_func_args: false | ||
}, | ||
compress: false | ||
}); | ||
return result.code; | ||
} catch (error) { | ||
throw new Error( | ||
`Uglification failed with error: ${error.message} in file ${error.filename} ` + | ||
`(line ${error.line}, col ${error.col}, pos ${error.pos})`); | ||
} | ||
} | ||
|
||
// create a worker and register public functions | ||
workerpool.worker({ | ||
uglify | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters