Skip to content

Instantly share code, notes, and snippets.

@vraravam
Last active July 4, 2019 05:12
Show Gist options
  • Save vraravam/8403301 to your computer and use it in GitHub Desktop.
Save vraravam/8403301 to your computer and use it in GitHub Desktop.
For phonegap/cordova projects, if you want to minify your home-grown js/css files (and obfuscate them to some extent), you can do it with the following gist. It requires yuicompressor and a couple of other npm modules (please look at the require calls at the top). It will not touch the original files - only those that end up within the platforms…
#!/usr/bin/env node
var isRelease = (process.env.RELEASE && process.env.RELEASE === "1");
// Turn this on only for release
if (isRelease !== true) {
return;
}
var fs = require('fs');
var path = require('path');
var glob = require("glob")
var compressor = require('yuicompressor');
require('prototypes');
function compress(fileName, extn) {
compressor.compress(fileName, {
type: extn,
// nomunge: true,
// 'line-break': 80
charset: 'utf8'
}, function(err, data, extra) {
var minifiedExtn = ".min." + extn;
var minifiedFile = fileName.replace("." + extn, minifiedExtn);
// Since this will be turned on only for release, we will overwrite the old files
if (isRelease === true) {
minifiedFile = fileName;
}
fs.writeFileSync(minifiedFile, data, 'utf8');
});
}
function processFiles(dir, extn) {
// look for files within folders which conform to '/<extn>/' AND end in '.<extn>'
glob(dir + "/**/" + extn + "/**/*." + extn, function(er, files) {
for(var i in files) {
// TODO: Need to check if this '/' works in windows
if (!files[i].contains("/libs/")) {
compress(files[i], extn);
}
}
});
}
var rootDir = process.argv[2];
var platformsPath = path.join(rootDir, 'platforms');
processFiles(platformsPath, "js");
processFiles(platformsPath, "css");
@rossmartin
Copy link

Thank you so much for this! This works great for me in an Ionic app. I have tested using this when building on Windows and Linux.

For my Ionic app I made a minor change to the check on line 37 -

if (!files[i].contains("/lib/") && !files[i].contains("/utils/")) {
    compress(files[i], extn);
}

This minor change is needed because Ionic uses a lib directory to store angular and its own scripts. I have a utils directory I made in my project that most everything is already minified.

Minifying has helped improve performance for my app. I'm planning to extend on this script using grunt or gulp. Thanks again, I plan on sharing any extensions I make.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment