diff options
Diffstat (limited to 'hooks')
| -rwxr-xr-x | hooks/after_prepare/uglify.js | 92 | ||||
| -rwxr-xr-x | hooks/before_prepare/02_jshint.js | 73 |
2 files changed, 165 insertions, 0 deletions
diff --git a/hooks/after_prepare/uglify.js b/hooks/after_prepare/uglify.js new file mode 100755 index 00000000..b90e285f --- /dev/null +++ b/hooks/after_prepare/uglify.js @@ -0,0 +1,92 @@ +#!/usr/bin/env node + +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]; +var platformPath = path.join(rootDir, 'platforms'); +var platform = process.env.CORDOVA_PLATFORMS; +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' +]; + +if (!isRelease) { + return; +} + +console.log('cordova-uglify will always run by default, uncomment the line checking for the release flag otherwise'); + +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; +} + +foldersToProcess.forEach(function(folder) { + processFiles(path.join(platformPath, folder)); +}); + +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 (recursiveFolderSearch && stat.isDirectory()) { + processFiles(file); + } else{ + compress(file); + } + }); + }); + }); +} + +function compress(file) { + var ext = path.extname(file); + 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: false // remove console.* statements (log, warn, etc.) + }, + fromString: true + }); + 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); + 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/before_prepare/02_jshint.js b/hooks/before_prepare/02_jshint.js new file mode 100755 index 00000000..fff7775d --- /dev/null +++ b/hooks/before_prepare/02_jshint.js @@ -0,0 +1,73 @@ +#!/usr/bin/env node + +var fs = require('fs'); +var path = require('path'); +var jshint = require('jshint').JSHINT; +var async = require('async'); + +var foldersToProcess = [ + 'js' +]; + +foldersToProcess.forEach(function(folder) { + processFiles("www/" + folder); +}); + +function processFiles(dir, callback) { + var errorCount = 0; + fs.readdir(dir, function(err, list) { + if (err) { + console.log('processFiles err: ' + err); + return; + } + async.eachSeries(list, function(file, innercallback) { + file = dir + '/' + file; + fs.stat(file, function(err, stat) { + if(!stat.isDirectory()) { + if(path.extname(file) === ".js") { + lintFile(file, function(hasError) { + if(hasError) { + errorCount++; + } + innercallback(); + }); + } else { + innercallback(); + } + } else { + innercallback(); + } + }); + }, function(error) { + if(errorCount > 0) { + process.exit(1); + } + }); + }); +} + +function lintFile(file, callback) { + console.log("Linting " + file); + fs.readFile(file, function(err, data) { + if(err) { + console.log('Error: ' + err); + return; + } + if(jshint(data.toString())) { + console.log('File ' + file + ' has no errors.'); + console.log('-----------------------------------------'); + callback(false); + } else { + console.log('Errors in file ' + file); + var out = jshint.data(), + errors = out.errors; + for(var j = 0; j < errors.length; j++) { + console.log(errors[j].line + ':' + errors[j].character + ' -> ' + errors[j].reason + ' -> ' + +errors[j].evidence); + } + console.log('-----------------------------------------'); + callback(true); + } + }); +} + |
