Skip to content

Commit

Permalink
Add 'minify' task and processor
Browse files Browse the repository at this point in the history
Combines debug-file creation, source-map generation and minification and
replaces existing tasks/processors
  • Loading branch information
RandomByte committed Oct 29, 2021
1 parent 252a7fe commit 4701487
Show file tree
Hide file tree
Showing 12 changed files with 169 additions and 265 deletions.
20 changes: 4 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ module.exports = {
*/
bootstrapHtmlTransformer: "./lib/processors/bootstrapHtmlTransformer",
/**
* @type {import('./lib/processors/debugFileCreator')}
* @type {import('./lib/processors/minifier')}
*/
debugFileCreator: "./lib/processors/debugFileCreator",
minifier: "./lib/processors/minifier",
/**
* @type {import('./lib/processors/libraryLessGenerator')}
*/
Expand All @@ -53,10 +53,6 @@ module.exports = {
* @type {import('./lib/processors/manifestCreator')}
*/
manifestCreator: "./lib/processors/manifestCreator",
/**
* @type {import('./lib/processors/resourceCopier')}
*/
resourceCopier: "./lib/processors/resourceCopier",
/**
* @type {import('./lib/processors/nonAsciiEscaper')}
*/
Expand All @@ -69,10 +65,6 @@ module.exports = {
* @type {import('./lib/processors/themeBuilder')}
*/
themeBuilder: "./lib/processors/themeBuilder",
/**
* @type {import('./lib/processors/uglifier')}
*/
uglifier: "./lib/processors/uglifier",
/**
* @type {import('./lib/processors/versionInfoGenerator')}
*/
Expand Down Expand Up @@ -121,9 +113,9 @@ module.exports = {
*/
buildThemes: "./lib/tasks/buildThemes",
/**
* @type {import('./lib/tasks/createDebugFiles')}
* @type {import('./lib/tasks/minify')}
*/
createDebugFiles: "./lib/tasks/createDebugFiles",
minify: "./lib/tasks/minify",
/**
* @type {import('./lib/tasks/jsdoc/executeJsdocSdkTransformation')}
*/
Expand Down Expand Up @@ -160,10 +152,6 @@ module.exports = {
* @type {import('./lib/tasks/transformBootstrapHtml')}
*/
transformBootstrapHtml: "./lib/tasks/transformBootstrapHtml",
/**
* @type {import('./lib/tasks/uglify')}
*/
uglify: "./lib/tasks/uglify",
/**
* @type {import('./lib/tasks/taskRepository')}
*/
Expand Down
52 changes: 0 additions & 52 deletions lib/processors/debugFileCreator.js

This file was deleted.

90 changes: 90 additions & 0 deletions lib/processors/minifier.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const path = require("path");
const terser = require("terser");
const util = require("util");
const Resource = require("@ui5/fs").Resource;
/**
* 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 debugFileRegex = /((?:\.view|\.fragment|\.controller|\.designtime|\.support)?\.js)$/;

/**
* Minifies the supplied resources.
*
* @public
* @alias module:@ui5/builder.processors.uglifier
* @param {object} parameters Parameters
* @param {module:@ui5/fs.Resource[]} parameters.resources List of resources to be processed
* @param {fs|module:@ui5/fs.fsInterface} parameters.fs Node fs or
* custom [fs interface]{@link module:resources/module:@ui5/fs.fsInterface}
* @returns {Promise<object>} Promise resolving with object of resource, dbgResource and sourceMap
*/
module.exports = async function({resources, fs}) {
const stat = util.promisify(fs.stat);

return Promise.all(resources.map(async (resource) => {
const dbgPath = resource.getPath().replace(debugFileRegex, "-dbg$1");

// Check whether the debug resource path is already used and log an error since we don't really expect this
// to happen anymore (this used to happen when bundle creation was done before debug-file creation)
try {
await stat(dbgPath);

// if the file can be found something might be off or we should just skip it
throw new Error(
`TODO 3.0: Unexpected dbg-variant for resource ${resource.getPath()} already present in workspace`);
} catch (err) {
if (err.code !== "ENOENT") {
// if it's another error, forward it
throw err;
}
// if the file can't be found, we can continue
}

const dbgResource = await resource.clone();
dbgResource.setPath(dbgPath);

const code = await resource.getString();
try {
const filename = path.posix.basename(resource.getPath());
const dbgFilename = path.posix.basename(dbgPath);
const result = await terser.minify({
[dbgFilename]: code
}, {
output: {
comments: copyrightCommentsAndBundleCommentPattern,
wrap_func_args: false
},
compress: false,
mangle: {
reserved: [
"jQuery",
"jquery",
]
},
sourceMap: {
filename,
url: resource.getPath() + ".map"
}
});
resource.setString(result.code);
const sourceMap = new Resource({
path: resource.getPath() + ".map",
string: result.map
});
return {resource, dbgResource, sourceMap};
} catch (err) {
throw new Error(
`Minification failed with error: ${err.message} in file ${err.filename} ` +
`(line ${err.line}, col ${err.col}, pos ${err.pos})`);
}
}));
};
24 changes: 0 additions & 24 deletions lib/processors/resourceCopier.js

This file was deleted.

45 changes: 0 additions & 45 deletions lib/processors/uglifier.js

This file was deleted.

30 changes: 0 additions & 30 deletions lib/tasks/createDebugFiles.js

This file was deleted.

33 changes: 33 additions & 0 deletions lib/tasks/minify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const fsInterface = require("@ui5/fs").fsInterface;
const minifier = require("../processors/minifier");

/**
* Task to minify resources.
*
* @public
* @alias module:@ui5/builder.tasks.minify
* @param {object} parameters Parameters
* @param {module:@ui5/fs.DuplexCollection} parameters.workspace DuplexCollection to read and write files
* @param {module:@ui5/builder.tasks.TaskUtil|object} [parameters.taskUtil] TaskUtil
* @param {object} parameters.options Options
* @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 = async function({workspace, taskUtil, options: {pattern}}) {
const resources = await workspace.byGlob(pattern);
const processedResources = await minifier({
resources,
fs: fsInterface(workspace)
});

return Promise.all(processedResources.map(async ({resource, dbgResource, sourceMap}) => {
if (taskUtil) {
taskUtil.setTag(dbgResource, taskUtil.STANDARD_TAGS.IsDebugVariant);
}
return Promise.all([
workspace.write(resource),
workspace.write(dbgResource),
workspace.write(sourceMap)
]);
}));
};
3 changes: 1 addition & 2 deletions lib/tasks/taskRepository.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ const taskInfos = {
replaceCopyright: {path: "./replaceCopyright"},
replaceVersion: {path: "./replaceVersion"},
replaceBuildtime: {path: "./replaceBuildtime"},
createDebugFiles: {path: "./createDebugFiles"},
escapeNonAsciiCharacters: {path: "./escapeNonAsciiCharacters"},
executeJsdocSdkTransformation: {path: "./jsdoc/executeJsdocSdkTransformation"},
generateApiIndex: {path: "./jsdoc/generateApiIndex"},
generateJsdoc: {path: "./jsdoc/generateJsdoc"},
uglify: {path: "./uglify"},
minify: {path: "./minify"},
buildThemes: {path: "./buildThemes"},
transformBootstrapHtml: {path: "./transformBootstrapHtml"},
generateLibraryManifest: {path: "./generateLibraryManifest"},
Expand Down
33 changes: 0 additions & 33 deletions lib/tasks/uglify.js

This file was deleted.

Loading

0 comments on commit 4701487

Please sign in to comment.