diff options
| author | PliablePixels <pliablepixels@gmail.com> | 2015-06-26 20:25:01 -0400 |
|---|---|---|
| committer | PliablePixels <pliablepixels@gmail.com> | 2015-06-26 20:25:01 -0400 |
| commit | 2c837a6adbbdbb5f062e1ad86c98738084d31fb2 (patch) | |
| tree | 33e4c6292095ed61ba29791c15ab649f3b33699a /hooks/after_prepare/uglify.js | |
| parent | 544413f2d6de83e085d4489a1b9ccbaac3fa192d (diff) | |
Added uglify/minify as part of build process (Note: console log is enabled - remember to disable it in release mode)
Diffstat (limited to 'hooks/after_prepare/uglify.js')
| -rwxr-xr-x | hooks/after_prepare/uglify.js | 92 |
1 files changed, 92 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; + } +} |
