Skip to content

Commit

Permalink
3 #comment Introduced terser as an option for JavaScript mangler/comp…
Browse files Browse the repository at this point in the history
…ressor
  • Loading branch information
jadiagaurang committed May 9, 2022
1 parent 79e854b commit 8b905ab
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 49 deletions.
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "minify-all-cli",
"version": "1.0.9",
"version": "1.0.12",
"description": "Minify All JS, CSS and HTML files in a folder by using UglifyJS, CSSNano and HTMLMinifier with an option to gzip all the files as well.",
"main": "bin/minify-all-cli.js",
"bin": {
Expand Down
19 changes: 19 additions & 0 deletions tests/cli.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,23 @@ describe("minify-all-cli", () => {

expect(arrayAllFiles.length).to.equal(arrayProcessedFiles.length);
});

it("minify-all-cli with terser as A JavaScript mangler/compressor", async () => {
const response = await cmd.execute(
"./bin/minify-all-cli.js", [
"-s", "./tests/asserts",
"-d", "./tests/asserts_compressed",
"--jsCompressor=terser",
"--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);
});
});

0 comments on commit 8b905ab

Please sign in to comment.