From 571c9b030adc36ccb26579aa2430348b5b898b17 Mon Sep 17 00:00:00 2001 From: Gaurang Jadia Date: Sat, 7 May 2022 18:25:47 -0700 Subject: [PATCH] 2 #comment Introduced gzip all the minified JS, CSS, and HTML --- lib/node.js | 88 ++++++++++++++++++++++++++++++++++++----------- tests/cli.spec.js | 22 ++++++++++++ 2 files changed, 89 insertions(+), 21 deletions(-) diff --git a/lib/node.js b/lib/node.js index 38d89fd..093ec33 100644 --- a/lib/node.js +++ b/lib/node.js @@ -6,6 +6,7 @@ const fs = require("fs"); const path = require("path"); const _ = require("underscore"); const peach = require("parallel-each"); +const zlib = require("zlib"); const winston = require("./logger").winston; const AppUtil = require("./apputil.js"); const UglifyJS = require("uglify-js"); @@ -45,7 +46,7 @@ module.exports = class MinifyAllCLI { } // Validate Source Directory if (!fs.existsSync(me.SourceDirectory)) { - throw "SourceDirectory doesn't exists!"; + throw new Error("SourceDirectory doesn't exists!"); } // Check Destination Directory is Absolute; if not then convert to absolute @@ -118,18 +119,33 @@ module.exports = class MinifyAllCLI { var result = UglifyJS.minify(code, uglifyOptions); if (!result.error) { - fs.writeFile(strDestinationFile, result.code, (err) => { - if (err) me.logger.error(err); //throw err; - - me.logger.info("JS. Path: " + strDestinationFile); - resolve(); - }); + let strCode = result.code; + + if (me.options.doGzip) { + const bufJS = new Buffer.from(strCode, "utf-8"); + zlib.gzip(bufJS, function (_, strGzipResult) { + fs.writeFile(strDestinationFile, strGzipResult, (err) => { + if (err) me.logger.error(err); + + me.logger.info("JS. Path: " + strDestinationFile); + resolve(); + }); + }); + } + else { + fs.writeFile(strDestinationFile, strCode, (err) => { + if (err) me.logger.error(err); + + me.logger.info("JS. Path: " + strDestinationFile); + resolve(); + }); + } } else { me.logger.error(result.error); fs.writeFile(strDestinationFile, code, (err) => { - if (err) me.logger.error(err); //throw err; + if (err) me.logger.error(err); me.logger.warn("Skip. Path: " + strDestinationFile); resolve(); @@ -154,11 +170,27 @@ module.exports = class MinifyAllCLI { PostCSS([CSSNano({ preset: "cssnano-preset-default" })]) .process(css, { from: undefined, to: undefined }) .then(result => { - fs.writeFile(strDestinationFile, result.css, (err) => { - if (err) me.logger.error(err); //throw err; - - me.logger.info("CSS. Path: " + strDestinationFile); - }); + let strCode = result.css; + + if (me.options.doGzip) { + const bufJS = new Buffer.from(strCode, "utf-8"); + zlib.gzip(bufJS, function (_, strGzipResult) { + fs.writeFile(strDestinationFile, strGzipResult, (err) => { + if (err) me.logger.error(err); + + me.logger.info("CSS. Path: " + strDestinationFile); + resolve(); + }); + }); + } + else { + fs.writeFile(strDestinationFile, strCode, (err) => { + if (err) me.logger.error(err); + + me.logger.info("CSS. Path: " + strDestinationFile); + resolve(); + }); + } }); }, 100); }); @@ -175,7 +207,7 @@ module.exports = class MinifyAllCLI { var html = fs.readFileSync(strFilePath, "utf8"); - var result = HTMLMinifier(html, { + let strCode = HTMLMinifier(html, { collapseWhitespace: true, removeAttributeQuotes: true, removeComments: true, @@ -185,12 +217,26 @@ module.exports = class MinifyAllCLI { minifyCSS: true, useShortDoctype: true }); + + if (me.options.doGzip) { + const bufJS = new Buffer.from(strCode, "utf-8"); + zlib.gzip(bufJS, function (_, strGzipResult) { + fs.writeFile(strDestinationFile, strGzipResult, (err) => { + if (err) me.logger.error(err); - fs.writeFile(strDestinationFile, result, (err) => { - if (err) me.logger.error(err); //throw err; + me.logger.info("HTML. Path: " + strDestinationFile); + resolve(); + }); + }); + } + else { + fs.writeFile(strDestinationFile, strCode, (err) => { + if (err) me.logger.error(err); - me.logger.info("HTML. Path: " + strDestinationFile); - }); + me.logger.info("HTML. Path: " + strDestinationFile); + resolve(); + }); + } }, 100); }); @@ -211,10 +257,10 @@ module.exports = class MinifyAllCLI { resolve(); }) .catch((err) => { - if (err) me.logger.error(err); //throw err; + if (err) me.logger.error(err); fs.copyFile(strFilePath, strDestinationFile, (err) => { - if (err) me.logger.error(err); //throw err; + if (err) me.logger.error(err); me.logger.warn("Skip. Source: " + strFilePath + "; Destination: " + strDestinationFile); resolve(); @@ -223,7 +269,7 @@ module.exports = class MinifyAllCLI { } else { fs.copyFile(strFilePath, strDestinationFile, (err) => { - if (err) me.logger.error(err); //throw err; + if (err) me.logger.error(err); me.logger.warn("Skip. Source: " + strFilePath + "; Destination: " + strDestinationFile); resolve(); diff --git a/tests/cli.spec.js b/tests/cli.spec.js index ba7793d..32c79d6 100644 --- a/tests/cli.spec.js +++ b/tests/cli.spec.js @@ -96,4 +96,26 @@ describe("minify-all-cli", () => { expect(arrayAllFiles.length).to.equal(arrayProcessedFiles.length); }); + + it("minify-all-cli with optimum options", async () => { + const response = await cmd.execute( + "./bin/minify-all-cli.js", [ + "-s", "./tests/asserts", + "-d", "./tests/asserts_compressed", + "--skipJS=true", "--skipCSS=true", "--skipHTML=true", + "--doGzip=true", + "--skipFileExtensions=.mp3", "--skipFileExtensions=.mp4", + "--processCount=10", + "--logLevel=info" + ] + ); + + // Print the standard output stream response + console.log(response); + + var arrayAllFiles = AppUtil.getAllFiles("./tests/asserts"); + var arrayProcessedFiles = AppUtil.getAllFiles("./tests/asserts_compressed"); + + expect(arrayAllFiles.length).to.equal(arrayProcessedFiles.length); + }); }); \ No newline at end of file