summaryrefslogtreecommitdiff
path: root/hooks/after_prepare/uglify.js
diff options
context:
space:
mode:
Diffstat (limited to 'hooks/after_prepare/uglify.js')
-rwxr-xr-xhooks/after_prepare/uglify.js182
1 files changed, 94 insertions, 88 deletions
diff --git a/hooks/after_prepare/uglify.js b/hooks/after_prepare/uglify.js
index 3b11bc90..7364dd93 100755
--- a/hooks/after_prepare/uglify.js
+++ b/hooks/after_prepare/uglify.js
@@ -3,60 +3,64 @@
/*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 fs = require('fs');
+var path = require('path');
+var cwd = process.cwd();
+var dependencyPath = path.join(cwd, 'node_modules', 'cordova-uglify', 'node_modules');
+// cordova-uglify module dependencies
+var UglifyJS = require(path.join(dependencyPath, 'uglify-js'));
+var CleanCSS = require(path.join(dependencyPath, 'clean-css'));
+var ngAnnotate = require(path.join(dependencyPath, 'ng-annotate'));
// Process
-var rootDir = process.argv[2];
+var rootDir = process.argv[2];
var platformPath = path.join(rootDir, 'platforms');
-var platforms = process.env.CORDOVA_PLATFORMS.split(',');
-var cliCommand = process.env.CORDOVA_CMDLINE;
+var platforms = process.env.CORDOVA_PLATFORMS.split(',');
+var cliCommand = process.env.CORDOVA_CMDLINE;
// 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 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);
+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;
+ return;
}
// Run uglifier
-// PP disabled on Jul 10 2016 - something is going wrong
-//run();
+run();
/**
* 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);
- });
+ platforms.forEach(function(platform) {
+ var wwwPath;
+
+ switch (platform) {
+ case 'android':
+ wwwPath = path.join(platformPath, platform, 'assets', 'www');
+ break;
+
+ case 'ios':
+ case 'browser':
+ case 'wp8':
+ case 'windows':
+ wwwPath = path.join(platformPath, platform, 'www');
+ break;
+
+ default:
+ console.log('this hook only supports android, ios, wp8, windows, and browser currently');
+ return;
+ }
+
+ processFolders(wwwPath);
+ });
}
/**
@@ -65,9 +69,9 @@ function run() {
* @return {undefined}
*/
function processFolders(wwwPath) {
- foldersToProcess.forEach(function(folder) {
- processFiles(path.join(wwwPath, folder));
- });
+ foldersToProcess.forEach(function(folder) {
+ processFiles(path.join(wwwPath, folder));
+ });
}
/**
@@ -76,31 +80,31 @@ function processFolders(wwwPath) {
* @return {undefined}
*/
function processFiles(dir) {
- fs.readdir(dir, function (err, list) {
- if (err) {
- console.log('processFiles err: ' + err);
-
- return;
+ 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);
+
+ 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);
-
- return;
- }
- });
- });
+ });
});
+ });
}
/**
@@ -109,30 +113,32 @@ function processFiles(dir) {
* @return {undefined}
*/
function compress(file) {
- var ext = path.extname(file),
- res,
- source,
- result;
-
- switch (ext) {
- case '.js':
- console.log('uglifying js file ' + file);
-
- 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);
-
- 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;
- }
+ var ext = path.extname(file),
+ res,
+ source,
+ result;
+
+ switch (ext) {
+ case '.js':
+ console.log('uglifying js file ' + file);
+
+ 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);
+
+ source = fs.readFileSync(file, 'utf8');
+ result = cssMinifier.minify(source);
+ fs.writeFileSync(file, result.styles, 'utf8'); // overwrite the original unminified file
+ break;
+
+ default:
+ console.log('encountered a ' + ext + ' file, not compressing it');
+ break;
+ }
}