Skip to content

Commit

Permalink
Merge branch 'develop' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
jadiagaurang committed May 9, 2022
2 parents 69fdfb9 + 8b905ab commit d216a7b
Show file tree
Hide file tree
Showing 5 changed files with 320 additions and 154 deletions.
6 changes: 4 additions & 2 deletions bin/minify-all-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@ var MinifyAllCLI = require("../lib/node.js");

const cliOptions = yargs
.usage("Usage: -s <source> -d <destination>")
.option("j", { alias: "skipJS", describe: "Should minify JS the file or skip it?", type: "boolean", demandOption: false })
.option("j", { alias: "skipJS", describe: "Should minify JavaScript the file or skip it?", type: "boolean", demandOption: false })
.option("c", { alias: "skipCSS", describe: "Should minify CSS the file or skip it?", type: "boolean", demandOption: false })
.option("h", { alias: "skipHTML", describe: "Should minify HTML the file or skip it?", type: "boolean", demandOption: false })
.option("g", { alias: "doGzip", describe: "Should gzip the file or skip it?", type: "boolean", demandOption: false })
.option("x", { alias: "jsCompressor", describe: "Which JavaScript mangler/compressor to use for minification? Default: uglifyjs", type: "string", demandOption: false })
.option("m", { alias: "skipFileMasks", describe: "Partial Filder Path to skip minification", type: "array", demandOption: false })
.option("e", { alias: "skipFileExtensions", describe: "File Extensions to skip it over", type: "array", demandOption: false })
.option("i", { alias: "ignoreFileMasks", describe: "Partial Filder Path to ignore minification and copy to destination", type: "array", demandOption: false })
.option("f", { alias: "configFile", describe: "Specifies a json configuration file for the UglifyJS, CSSNano and HTML Minifier module", type: "string", demandOption: false })
.option("f", { alias: "configFile", describe: "Specifies a json configuration file for the UglifyJS/terser, CSSNano and HTML Minifier module", type: "string", demandOption: false })
.option("l", { alias: "logLevel", describe: "Set log level to print warn, log, error, fatal messages", type: "string", demandOption: false })
.option("p", { alias: "processCount", describe: "Specifies process count to set maximum degree of parallelism", type: "number", demandOption: false })
.argv;
Expand All @@ -24,6 +25,7 @@ const cliOptions = yargs
skipCSS: cliOptions.skipCSS || cliOptions.c || false,
skipHTML: cliOptions.skipHTML || cliOptions.h || false,
doGzip: cliOptions.doGzip || cliOptions.g || false,
jsCompressor: cliOptions.jsCompressor || cliOptions.x || "uglifyjs",
skipFileMasks: cliOptions.skipFileMasks || cliOptions.m || [],
skipFileExtensions: cliOptions.skipFileExtensions || cliOptions.e || [],
ignoreFileMasks: cliOptions.ignoreFileMasks || cliOptions.i || [],
Expand Down
105 changes: 57 additions & 48 deletions lib/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,28 @@ const zlib = require("zlib");
const winston = require("./logger").winston;
const AppUtil = require("./apputil.js");
const UglifyJS = require("uglify-js");
const terser = require("terser");
const PostCSS = require("postcss");
const CSSNano = require("cssnano");
const HTMLMinifier = require("html-minifier").minify;

module.exports = class MinifyAllCLI {
defaultOptions = {
skipJS: false,
skipCSS: false,
skipHTML: false,
doGzip: false,
skipFileMasks: [],
skipFileExtensions: [".mp3", ".mp4"],
ignoreFileMasks: [],
configFile: null,
logLevel: "info",
processCount: 10
"skipJS": false,
"jsCompressor": "uglifyjs", // "uglifyjs" or "terser"
"skipCSS": false,
"skipHTML": false,
"doGzip": false,
"skipFileMasks": [],
"skipFileExtensions": [".mp3", ".mp4"],
"ignoreFileMasks": [],
"configFile": null,
"logLevel": "info",
"processCount": 10
}

constructor(strSourceDirectory, strDestinationDirectory, options) {
var me = this;
let me = this;

me.SourceDirectory = strSourceDirectory;
me.DestinationDirectory = strDestinationDirectory;
Expand All @@ -38,7 +40,7 @@ module.exports = class MinifyAllCLI {
}

process() {
var me = this;
let me = this;

// Check Source Directory is Absolute; if not then convert to absolute
if(!path.isAbsolute(me.SourceDirectory)) {
Expand All @@ -61,23 +63,23 @@ module.exports = class MinifyAllCLI {
this.logger.info("me.SourceDirectory: " + me.SourceDirectory);
this.logger.info("me.DestinationDirectory: " + me.DestinationDirectory);

// Collect all the files to loop through!
// TODO: May use globby or fast-glob to manage better file-masking in future!
// Collect all the files to loop through!
let arrayAllFiles = AppUtil.getAllFiles(me.SourceDirectory);

let processedFiles = 0;
peach(arrayAllFiles, async (strFilePath, index) => {
if (!me.options.skipJS && strFilePath.endsWith(".js") && !strFilePath.endsWith(".min.js")) {
this.minifyJS(strFilePath, index);
this.minifyJS(strFilePath, index);
}
else if (!me.options.skipCSS && strFilePath.endsWith(".css") && !strFilePath.endsWith(".min.css")) {
this.minifyCSS(strFilePath, index);
}
else if (!me.options.skipHTML && strFilePath.endsWith(".html") && !strFilePath.endsWith(".min.html")) {
this.minifyHTML(strFilePath, index);
}
}
else {
await this.gzip(strFilePath, index);
this.gzip(strFilePath, index);
}

processedFiles++;
Expand All @@ -86,22 +88,22 @@ module.exports = class MinifyAllCLI {
});
}

minifyJS = (strFilePath, index) => new Promise((resolve) => {
var me = this;
minifyJS = async (strFilePath) => new Promise((resolve) => {
let me = this;

setTimeout(() => {
setTimeout(async () => {
if (!me.options.skipJS && strFilePath.endsWith(".js") && !strFilePath.endsWith(".min.js")) {
var strPartialFilePath = path.relative(me.SourceDirectory, strFilePath);
var strDestinationFile = path.join(me.DestinationDirectory, path.sep, strPartialFilePath);
var strFileDirectory = path.dirname(strDestinationFile);
let strPartialFilePath = path.relative(me.SourceDirectory, strFilePath);
let strDestinationFile = path.join(me.DestinationDirectory, path.sep, strPartialFilePath);
let strFileDirectory = path.dirname(strDestinationFile);

fs.mkdirSync(strFileDirectory, { recursive: true });

var code = fs.readFileSync(strFilePath, "utf8");
let jsCode = fs.readFileSync(strFilePath, "utf8");

var uglifyConfiguration = !AppUtil.isBlank(me.options.configFile) ? require(path.resolve(me.options.configFile)) : {};
let uglifyConfiguration = !AppUtil.isBlank(me.options.configFile) ? require(path.resolve(me.options.configFile)) : {};

var uglifyOptions = {
let uglifyOptions = {
"compress": true,
"mangle": {
"reserved": [
Expand All @@ -116,7 +118,14 @@ module.exports = class MinifyAllCLI {
};

uglifyOptions = _.extend({}, uglifyOptions, uglifyConfiguration);
var result = UglifyJS.minify(code, uglifyOptions);

let result = null;
if (_.isEqual(me.options.jsCompressor, "terser")) {
result = await terser.minify(jsCode, uglifyOptions);
}
else {
result = UglifyJS.minify(jsCode, uglifyOptions);
}

if (!result.error) {
let strCode = result.code;
Expand Down Expand Up @@ -144,7 +153,7 @@ module.exports = class MinifyAllCLI {
else {
me.logger.error(result.error);

fs.writeFile(strDestinationFile, code, (err) => {
fs.writeFile(strDestinationFile, jsCode, (err) => {
if (err) me.logger.error(err);

me.logger.warn("Skip. Path: " + strDestinationFile);
Expand All @@ -155,17 +164,17 @@ module.exports = class MinifyAllCLI {
}, 100);
});

minifyCSS = (strFilePath, index) => new Promise((resolve) => {
var me = this;
minifyCSS = (strFilePath) => new Promise((resolve) => {
let me = this;

setTimeout(() => {
var strPartialFilePath = path.relative(me.SourceDirectory, strFilePath);
var strDestinationFile = path.join(me.DestinationDirectory, path.sep, strPartialFilePath);
var strFileDirectory = path.dirname(strDestinationFile);
let strPartialFilePath = path.relative(me.SourceDirectory, strFilePath);
let strDestinationFile = path.join(me.DestinationDirectory, path.sep, strPartialFilePath);
let strFileDirectory = path.dirname(strDestinationFile);

fs.mkdirSync(strFileDirectory, { recursive: true });

var css = fs.readFileSync(strFilePath, "utf8");
let css = fs.readFileSync(strFilePath, "utf8");

PostCSS([CSSNano({ preset: "cssnano-preset-default" })])
.process(css, { from: undefined, to: undefined })
Expand Down Expand Up @@ -195,17 +204,17 @@ module.exports = class MinifyAllCLI {
}, 100);
});

minifyHTML = (strFilePath, index) => new Promise((resolve) => {
var me = this;
minifyHTML = (strFilePath) => new Promise((resolve) => {
let me = this;

setTimeout(() => {
var strPartialFilePath = path.relative(me.SourceDirectory, strFilePath);
var strDestinationFile = path.join(me.DestinationDirectory, path.sep, strPartialFilePath);
var strFileDirectory = path.dirname(strDestinationFile);
let strPartialFilePath = path.relative(me.SourceDirectory, strFilePath);
let strDestinationFile = path.join(me.DestinationDirectory, path.sep, strPartialFilePath);
let strFileDirectory = path.dirname(strDestinationFile);

fs.mkdirSync(strFileDirectory, { recursive: true });

var html = fs.readFileSync(strFilePath, "utf8");
let html = fs.readFileSync(strFilePath, "utf8");

let strCode = HTMLMinifier(html, {
collapseWhitespace: true,
Expand Down Expand Up @@ -240,14 +249,14 @@ module.exports = class MinifyAllCLI {
}, 100);
});

gzip = (strFilePath, index) => new Promise((resolve) => {
var me = this;
gzip = (strFilePath) => new Promise((resolve) => {
let me = this;

setTimeout(() => {
var strPartialFilePath = path.relative(me.SourceDirectory, strFilePath);
var strDestinationFile = path.join(me.DestinationDirectory, path.sep, strPartialFilePath);
var strFileDirectory = path.dirname(strDestinationFile);
var strFileExtension = path.extname(strFilePath);
let strPartialFilePath = path.relative(me.SourceDirectory, strFilePath);
let strDestinationFile = path.join(me.DestinationDirectory, path.sep, strPartialFilePath);
let strFileDirectory = path.dirname(strDestinationFile);
let strFileExtension = path.extname(strFilePath);

fs.mkdirSync(strFileDirectory, { recursive: true });

Expand All @@ -256,9 +265,9 @@ module.exports = class MinifyAllCLI {
me.logger.info("Gzip. Source: " + strFilePath + "; Destination: " + strDestinationFile);
resolve();
})
.catch((err) => {
if (err) me.logger.error(err);

.catch((errGzip) => {
if (errGzip) me.logger.error(errGzip);
fs.copyFile(strFilePath, strDestinationFile, (err) => {
if (err) me.logger.error(err);

Expand Down
Loading

0 comments on commit d216a7b

Please sign in to comment.