summaryrefslogtreecommitdiff
path: root/hooks
diff options
context:
space:
mode:
authorArjun Roychowdhury <pliablepixels@gmail.com>2015-09-23 15:45:30 -0400
committerArjun Roychowdhury <pliablepixels@gmail.com>2015-09-23 15:45:30 -0400
commit1d1c2168ff30ac9452b0929db4ae1f5baa83657a (patch)
tree1967e3ca3898bf2f33938e89bd1e6add6e49a570 /hooks
parent26821696678cf84ee19f4eb803807e41a7b56780 (diff)
updates
Diffstat (limited to 'hooks')
-rwxr-xr-xhooks/after_prepare/uglify.js137
-rw-r--r--hooks/uglify-config.json18
2 files changed, 109 insertions, 46 deletions
diff --git a/hooks/after_prepare/uglify.js b/hooks/after_prepare/uglify.js
index 8e557ad1..e372a089 100755
--- a/hooks/after_prepare/uglify.js
+++ b/hooks/after_prepare/uglify.js
@@ -1,90 +1,135 @@
#!/usr/bin/env node
-var fs = require('fs');
-var path = require('path');
-var UglifyJS = require('uglify-js');
-var CleanCSS = require('clean-css');
+/*jshint latedef:nofunc, node:true*/
+
+// Modules
+var fs = require('fs');
+var path = require('path');
+var UglifyJS = require('uglify-js');
+var CleanCSS = require('clean-css');
var ngAnnotate = require('ng-annotate');
-var cssMinifier = new CleanCSS({
- noAdvanced: true, // disable advanced optimizations - selector & property merging, reduction, etc.
- keepSpecialComments: 0 // remove all css comments ('*' to keep all, 1 to keep first comment only)
-});
-var rootDir = process.argv[2];
+// Process
+var rootDir = process.argv[2];
var platformPath = path.join(rootDir, 'platforms');
-var platform = process.env.CORDOVA_PLATFORMS;
-var cliCommand = process.env.CORDOVA_CMDLINE;
+var platforms = process.env.CORDOVA_PLATFORMS.split(',');
+var cliCommand = process.env.CORDOVA_CMDLINE;
-// hook configuration
-var isRelease = true; // by default this hook is always enabled, see the line below on how to execute it only for release
-var isRelease = (cliCommand.indexOf('--release') > -1);
-var recursiveFolderSearch = true; // set this to false to manually indicate the folders to process
-var foldersToProcess = [ // add other www folders in here if needed (ex. js/controllers)
- 'js',
- 'css'
-];
+// Hook configuration
+var configFilePath = path.join(rootDir, 'hooks/uglify-config.json');
+var hookConfig = JSON.parse(fs.readFileSync(configFilePath));
+var isRelease = hookConfig.alwaysRun || (cliCommand.indexOf('--release') > -1);
+var recursiveFolderSearch = hookConfig.recursiveFolderSearch; // set this to false to manually indicate the folders to process
+var foldersToProcess = hookConfig.foldersToProcess; // add other www folders in here if needed (ex. js/controllers)
+var cssMinifier = new CleanCSS(hookConfig.cleanCssOptions);
+// Exit
if (!isRelease) {
return;
}
-console.log('cordova-uglify will always run by default, uncomment the line checking for the release flag otherwise');
+// Run uglifier
+run();
-switch (platform) {
- case 'android':
- platformPath = path.join(platformPath, platform, 'assets', 'www');
- break;
- case 'ios': case 'browser':
- platformPath = path.join(platformPath, platform, 'www');
- break;
- default:
- console.log('this hook only supports android, ios, and browser currently');
- return;
+/**
+ * Run compression for all specified platforms.
+ * @return {undefined}
+ */
+function run() {
+ platforms.forEach(function(platform) {
+ var wwwPath;
+
+ switch (platform) {
+ case 'android':
+ wwwPath = path.join(platformPath, platform, 'assets', 'www');
+ break;
+
+ case 'ios':
+ case 'browser':
+ wwwPath = path.join(platformPath, platform, 'www');
+ break;
+
+ default:
+ console.log('this hook only supports android, ios, and browser currently');
+ return;
+ }
+
+ processFolders(wwwPath);
+ });
}
-foldersToProcess.forEach(function(folder) {
- processFiles(path.join(platformPath, folder));
-});
+/**
+ * Processes defined folders.
+ * @param {string} wwwPath - Path to www directory
+ * @return {undefined}
+ */
+function processFolders(wwwPath) {
+ foldersToProcess.forEach(function(folder) {
+ processFiles(path.join(wwwPath, folder));
+ });
+}
+/**
+ * Processes files in directories.
+ * @param {string} dir - Directory path
+ * @return {undefined}
+ */
function processFiles(dir) {
fs.readdir(dir, function (err, list) {
if (err) {
console.log('processFiles err: ' + err);
+
return;
}
+
list.forEach(function(file) {
file = path.join(dir, file);
+
fs.stat(file, function(err, stat) {
+ if (stat.isFile()) {
+ compress(file);
+
+ return;
+ }
+
if (recursiveFolderSearch && stat.isDirectory()) {
processFiles(file);
- } else{
- compress(file);
+
+ return;
}
});
});
});
}
+/**
+ * Compresses file.
+ * @param {string} file - File path
+ * @return {undefined}
+ */
function compress(file) {
- var ext = path.extname(file);
- switch(ext) {
+ var ext = path.extname(file),
+ res,
+ source,
+ result;
+
+ switch (ext) {
case '.js':
console.log('uglifying js file ' + file);
- var res = ngAnnotate(String(fs.readFileSync(file)), { add: true });
- var result = UglifyJS.minify(res.src, {
- compress: { // pass false here if you only want to minify (no obfuscate)
- drop_console: true // remove console.* statements (log, warn, etc.)
- },
- fromString: true
- });
+
+ res = ngAnnotate(String(fs.readFileSync(file)), { add: true });
+ result = UglifyJS.minify(res.src, hookConfig.uglifyJsOptions);
fs.writeFileSync(file, result.code, 'utf8'); // overwrite the original unminified file
break;
+
case '.css':
console.log('minifying css file ' + file);
- var source = fs.readFileSync(file, 'utf8');
- var result = cssMinifier.minify(source);
+
+ source = fs.readFileSync(file, 'utf8');
+ result = cssMinifier.minify(source);
fs.writeFileSync(file, result, 'utf8'); // overwrite the original unminified file
break;
+
default:
console.log('encountered a ' + ext + ' file, not compressing it');
break;
diff --git a/hooks/uglify-config.json b/hooks/uglify-config.json
new file mode 100644
index 00000000..de85bb2a
--- /dev/null
+++ b/hooks/uglify-config.json
@@ -0,0 +1,18 @@
+{
+ "alwaysRun": false,
+ "recursiveFolderSearch": true,
+ "foldersToProcess": [
+ "js",
+ "css"
+ ],
+ "uglifyJsOptions": {
+ "compress": {
+ "drop_console": true
+ },
+ "fromString": true
+ },
+ "cleanCssOptions": {
+ "noAdvanced": true,
+ "keepSpecialComments": 0
+ }
+} \ No newline at end of file