Skip to content

Commit

Permalink
minify everything
Browse files Browse the repository at this point in the history
  • Loading branch information
prevostc committed Mar 7, 2015
1 parent 1e6079a commit 32051fb
Show file tree
Hide file tree
Showing 12 changed files with 377 additions and 21 deletions.
Empty file modified hooks/README.md
100644 → 100755
Empty file.
29 changes: 29 additions & 0 deletions hooks/after_platform_add/010_install_plugins.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/usr/bin/env node

/**
* Install all plugins listed in package.json
* https://raw.githubusercontent.com/diegonetto/generator-ionic/master/templates/hooks/after_platform_add/install_plugins.js
*/
var exec = require('child_process').exec;
var path = require('path');
var sys = require('sys');

var packageJSON = null;

try {
packageJSON = require('../../package.json');
} catch(ex) {
console.log('\nThere was an error fetching your package.json file.')
console.log('\nPlease ensure a valid package.json is in the root of this project\n')
return;
}

var cmd = process.platform === 'win32' ? 'cordova.cmd' : 'cordova';
// var script = path.resolve(__dirname, '../../node_modules/cordova/bin', cmd);

packageJSON.cordovaPlugins = packageJSON.cordovaPlugins || [];
packageJSON.cordovaPlugins.forEach(function (plugin) {
exec('cordova plugin add ' + plugin, function (error, stdout, stderr) {
sys.puts(stdout);
});
});
57 changes: 57 additions & 0 deletions hooks/after_plugin_add/010_register_plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env node

/**
* Push plugins to cordovaPlugins array after_plugin_add
*/
var fs = require('fs'),
packageJSON = require('../../package.json'),
path = require('path');

packageJSON.cordovaPlugins = packageJSON.cordovaPlugins || [];
process.env.CORDOVA_PLUGINS.split(',').forEach(function (plugin) {
var configString,
idRegEx,
id,
pluginXmlPath,
pluginToAdd;

if(plugin.indexOf('https') != -1 || plugin.indexOf('git') != -1) {
console.log('Installing plugin from url');
}

if(plugin.indexOf('/') != -1) {
try {
pluginXmlPath = path.resolve(plugin, 'plugin.xml');
console.log('got pluginXmlPath:', pluginXmlPath);
if (!fs.existsSync(pluginXmlPath)) {
var errorMessage = ['There was no plugin.xml file found for path: ', pluginXmlPath].join('');
return;
}

configString = fs.readFileSync(pluginXmlPath,{encoding: 'utf8'});
idRegEx = new RegExp('<plugin[^>]*id="(.*)"', 'i');
id = idRegEx.exec(configString)[1]
pluginToAdd = {id: id, locator: plugin};
} catch(ex) {
console.log('There was an error retrieving the plugin.xml filr from the 010_register_plugin.js hook', ex);
}
} else {
pluginToAdd = plugin;
}

if(typeof pluginToAdd == 'string' && packageJSON.cordovaPlugins.indexOf(pluginToAdd) == -1) {
packageJSON.cordovaPlugins.push(pluginToAdd);
} else if (typeof pluginToAdd == 'object') {
var pluginExists = false;
packageJSON.cordovaPlugins.forEach(function(checkPlugin) {
if(typeof checkPlugin == 'object' && checkPlugin.id == pluginToAdd.id) {
pluginExists = true;
}
})
if(!pluginExists) {
packageJSON.cordovaPlugins.push(pluginToAdd);
}
}
});

fs.writeFileSync('package.json', JSON.stringify(packageJSON, null, 2));
27 changes: 27 additions & 0 deletions hooks/after_plugin_rm/010_deregister_plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/env node

/**
* Remove plugins from cordovaPlugins array after_plugin_rm
*/
var fs = require('fs');
var packageJSON = require('../../package.json');

packageJSON.cordovaPlugins = packageJSON.cordovaPlugins || [];

process.env.CORDOVA_PLUGINS.split(',').forEach(function (plugin) {
var index = packageJSON.cordovaPlugins.indexOf(plugin);
if (index > -1) {
packageJSON.cordovaPlugins.splice(index, 1);
} else {
//If it didnt find a match, it may be listed as {id,locator}
for(var i = 0, j = packageJSON.cordovaPlugins.length; i < j; i++) {
var packagePlugin = packageJSON.cordovaPlugins[i];
if(typeof packagePlugin == 'object' && packagePlugin.id == plugin) {
packageJSON.cordovaPlugins.splice(index, 1);
break;
}
}
}
});

fs.writeFile('package.json', JSON.stringify(packageJSON, null, 2));
28 changes: 28 additions & 0 deletions hooks/after_prepare/020_remove_sass_from_platforms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env node

/**
* After prepare, files are copied to the platforms/ios and platforms/android folders.
* Lets clean up some of those files that arent needed with this hook.
*/
var fs = require('fs');
var path = require('path');

var deleteFolderRecursive = function(removePath) {
if( fs.existsSync(removePath) ) {
fs.readdirSync(removePath).forEach(function(file,index){
var curPath = path.join(removePath, file);
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(removePath);
}
};

var iosPlatformsDir = path.resolve(__dirname, '../../platforms/ios/www/lib/ionic/scss');
var androidPlatformsDir = path.resolve(__dirname, '../../platforms/android/assets/www/lib/ionic/scss');

deleteFolderRecursive(iosPlatformsDir);
deleteFolderRecursive(androidPlatformsDir);
115 changes: 115 additions & 0 deletions hooks/after_prepare/030_minify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#!/usr/bin/env node

var fs = require('fs');
var path = require('path');
var UglifyJS = require('cordova-minify/node_modules/uglify-js');
var CleanCSS = require('cordova-minify/node_modules/clean-css');
var ImageMin = require('cordova-minify/node_modules/image-min');
var imagemin = new ImageMin();
var cssMinifier = new CleanCSS({
keepSpecialComments: 0
});

var rootDir = process.argv[2];
var platformPath = path.join(rootDir, 'platforms');
var platform = process.env.CORDOVA_PLATFORMS;
var cliCommand = process.env.CORDOVA_CMDLINE;
var isRelease = true;

//var isRelease = (cliCommand.indexOf('--release') > -1); // comment the above line and uncomment this line to turn the hook on only for release
if (!isRelease) {
return;
}
console.log('cordova-minify STARTING - minifying your js, css, and images. Sit back and relax!');


function processFiles(dir) {
fs.readdir(dir, function (err, list) {
if (err) {
console.log('processFiles - reading directories error: ' + err);
return;
}
list.forEach(function(file) {
file = path.join(dir, file);
fs.stat(file, function(err, stat) {
if (stat.isDirectory()) {
processFiles(file);
} else{
compress(file);
}
});
});
});
}

function compress(file) {
var ext = path.extname(file);
switch(ext) {
case '.js':
console.log('Compressing/Uglifying JS File: ' + file);
var result = UglifyJS.minify(file, {
compress: {
drop_console: true
}
});
fs.writeFileSync(file, result.code, 'utf8');
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');
break;
// Image options https://github.com/kevva/imagemin
case '.svg':
console.log('Minifying SVG File: ' + file);
// svgGo options https://github.com/kevva/imagemin-svgo
imagemin.src(file).dest(file).use(ImageMin.svgo());
break;
case '.gif':
console.log('Minifying GIF File: ' + file);
// GifSicle options https://github.com/kevva/imagemin-gifsicle
imagemin.src(file).dest(file).use(ImageMin.gifsicle({
interlace: true
}));
break;
case '.png':
console.log('Minifying PNG File: ' + file);
// OptiPNG options https://github.com/kevva/imagemin-optipng
imagemin.src(file).dest(file).use(ImageMin.optipng({
optimizationLevel: 2
}));
break;
case '.jpg':
case '.jpeg':
console.log('Minifying JPEG File: ' + file);
// jpegTran options https://github.com/kevva/imagemin-jpegtran
imagemin.src(file).dest(file).use(ImageMin.jpegtran({
progressive: true
}));
console.log('Minifying JPEG File: ' + file);
break;
default:
console.log('Encountered file with ' + ext + ' extension - not compressing.');
break;
}
}


switch (platform) {
case 'android':
platformPath = path.join(platformPath, platform, "assets", "www");
break;
case 'ios':
platformPath = path.join(platformPath, platform, "www");
break;
default:
console.log('Hook currently supports only Android and iOS');
return;
}

var foldersToProcess = ['js', 'css', 'img'];

foldersToProcess.forEach(function(folder) {
processFiles(path.join(platformPath, folder));
});
23 changes: 23 additions & 0 deletions hooks/before_platform_add/init_directories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env node

/**
* On a fresh clone, the local platforms/ and plugins/ directories will be
* missing, so ensure they get created before the first platform is added.
*/
var fs = require('fs');
var path = require('path');

var platformsDir = path.resolve(__dirname, '../../platforms');
var pluginsDir = path.resolve(__dirname, '../../plugins');

try {
fs.mkdirSync(platformsDir, function (err) {
if (err) { console.error(err); }
});
} catch(ex) {}

try {
fs.mkdirSync(pluginsDir, function (err) {
if (err) { console.error(err); }
});
} catch(ex) {}
72 changes: 72 additions & 0 deletions hooks/before_prepare/02_jshint.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/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);
}
});
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@
"gulp-rename": "^1.2.0"
},
"devDependencies": {
"async": "^0.9.0",
"bower": "^1.3.3",
"cordova-minify": "0.0.12",
"gulp-util": "^2.2.14",
"jshint": "^2.6.3",
"shelljs": "^0.3.0"
}
}
}
Loading

0 comments on commit 32051fb

Please sign in to comment.