Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit 32051fb

Browse files
committed
minify everything
1 parent 1e6079a commit 32051fb

File tree

12 files changed

+377
-21
lines changed

12 files changed

+377
-21
lines changed

hooks/README.md

100644100755
File mode changed.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Install all plugins listed in package.json
5+
* https://raw.githubusercontent.com/diegonetto/generator-ionic/master/templates/hooks/after_platform_add/install_plugins.js
6+
*/
7+
var exec = require('child_process').exec;
8+
var path = require('path');
9+
var sys = require('sys');
10+
11+
var packageJSON = null;
12+
13+
try {
14+
packageJSON = require('../../package.json');
15+
} catch(ex) {
16+
console.log('\nThere was an error fetching your package.json file.')
17+
console.log('\nPlease ensure a valid package.json is in the root of this project\n')
18+
return;
19+
}
20+
21+
var cmd = process.platform === 'win32' ? 'cordova.cmd' : 'cordova';
22+
// var script = path.resolve(__dirname, '../../node_modules/cordova/bin', cmd);
23+
24+
packageJSON.cordovaPlugins = packageJSON.cordovaPlugins || [];
25+
packageJSON.cordovaPlugins.forEach(function (plugin) {
26+
exec('cordova plugin add ' + plugin, function (error, stdout, stderr) {
27+
sys.puts(stdout);
28+
});
29+
});
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Push plugins to cordovaPlugins array after_plugin_add
5+
*/
6+
var fs = require('fs'),
7+
packageJSON = require('../../package.json'),
8+
path = require('path');
9+
10+
packageJSON.cordovaPlugins = packageJSON.cordovaPlugins || [];
11+
process.env.CORDOVA_PLUGINS.split(',').forEach(function (plugin) {
12+
var configString,
13+
idRegEx,
14+
id,
15+
pluginXmlPath,
16+
pluginToAdd;
17+
18+
if(plugin.indexOf('https') != -1 || plugin.indexOf('git') != -1) {
19+
console.log('Installing plugin from url');
20+
}
21+
22+
if(plugin.indexOf('/') != -1) {
23+
try {
24+
pluginXmlPath = path.resolve(plugin, 'plugin.xml');
25+
console.log('got pluginXmlPath:', pluginXmlPath);
26+
if (!fs.existsSync(pluginXmlPath)) {
27+
var errorMessage = ['There was no plugin.xml file found for path: ', pluginXmlPath].join('');
28+
return;
29+
}
30+
31+
configString = fs.readFileSync(pluginXmlPath,{encoding: 'utf8'});
32+
idRegEx = new RegExp('<plugin[^>]*id="(.*)"', 'i');
33+
id = idRegEx.exec(configString)[1]
34+
pluginToAdd = {id: id, locator: plugin};
35+
} catch(ex) {
36+
console.log('There was an error retrieving the plugin.xml filr from the 010_register_plugin.js hook', ex);
37+
}
38+
} else {
39+
pluginToAdd = plugin;
40+
}
41+
42+
if(typeof pluginToAdd == 'string' && packageJSON.cordovaPlugins.indexOf(pluginToAdd) == -1) {
43+
packageJSON.cordovaPlugins.push(pluginToAdd);
44+
} else if (typeof pluginToAdd == 'object') {
45+
var pluginExists = false;
46+
packageJSON.cordovaPlugins.forEach(function(checkPlugin) {
47+
if(typeof checkPlugin == 'object' && checkPlugin.id == pluginToAdd.id) {
48+
pluginExists = true;
49+
}
50+
})
51+
if(!pluginExists) {
52+
packageJSON.cordovaPlugins.push(pluginToAdd);
53+
}
54+
}
55+
});
56+
57+
fs.writeFileSync('package.json', JSON.stringify(packageJSON, null, 2));
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Remove plugins from cordovaPlugins array after_plugin_rm
5+
*/
6+
var fs = require('fs');
7+
var packageJSON = require('../../package.json');
8+
9+
packageJSON.cordovaPlugins = packageJSON.cordovaPlugins || [];
10+
11+
process.env.CORDOVA_PLUGINS.split(',').forEach(function (plugin) {
12+
var index = packageJSON.cordovaPlugins.indexOf(plugin);
13+
if (index > -1) {
14+
packageJSON.cordovaPlugins.splice(index, 1);
15+
} else {
16+
//If it didnt find a match, it may be listed as {id,locator}
17+
for(var i = 0, j = packageJSON.cordovaPlugins.length; i < j; i++) {
18+
var packagePlugin = packageJSON.cordovaPlugins[i];
19+
if(typeof packagePlugin == 'object' && packagePlugin.id == plugin) {
20+
packageJSON.cordovaPlugins.splice(index, 1);
21+
break;
22+
}
23+
}
24+
}
25+
});
26+
27+
fs.writeFile('package.json', JSON.stringify(packageJSON, null, 2));
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* After prepare, files are copied to the platforms/ios and platforms/android folders.
5+
* Lets clean up some of those files that arent needed with this hook.
6+
*/
7+
var fs = require('fs');
8+
var path = require('path');
9+
10+
var deleteFolderRecursive = function(removePath) {
11+
if( fs.existsSync(removePath) ) {
12+
fs.readdirSync(removePath).forEach(function(file,index){
13+
var curPath = path.join(removePath, file);
14+
if(fs.lstatSync(curPath).isDirectory()) { // recurse
15+
deleteFolderRecursive(curPath);
16+
} else { // delete file
17+
fs.unlinkSync(curPath);
18+
}
19+
});
20+
fs.rmdirSync(removePath);
21+
}
22+
};
23+
24+
var iosPlatformsDir = path.resolve(__dirname, '../../platforms/ios/www/lib/ionic/scss');
25+
var androidPlatformsDir = path.resolve(__dirname, '../../platforms/android/assets/www/lib/ionic/scss');
26+
27+
deleteFolderRecursive(iosPlatformsDir);
28+
deleteFolderRecursive(androidPlatformsDir);

hooks/after_prepare/030_minify.js

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
#!/usr/bin/env node
2+
3+
var fs = require('fs');
4+
var path = require('path');
5+
var UglifyJS = require('cordova-minify/node_modules/uglify-js');
6+
var CleanCSS = require('cordova-minify/node_modules/clean-css');
7+
var ImageMin = require('cordova-minify/node_modules/image-min');
8+
var imagemin = new ImageMin();
9+
var cssMinifier = new CleanCSS({
10+
keepSpecialComments: 0
11+
});
12+
13+
var rootDir = process.argv[2];
14+
var platformPath = path.join(rootDir, 'platforms');
15+
var platform = process.env.CORDOVA_PLATFORMS;
16+
var cliCommand = process.env.CORDOVA_CMDLINE;
17+
var isRelease = true;
18+
19+
//var isRelease = (cliCommand.indexOf('--release') > -1); // comment the above line and uncomment this line to turn the hook on only for release
20+
if (!isRelease) {
21+
return;
22+
}
23+
console.log('cordova-minify STARTING - minifying your js, css, and images. Sit back and relax!');
24+
25+
26+
function processFiles(dir) {
27+
fs.readdir(dir, function (err, list) {
28+
if (err) {
29+
console.log('processFiles - reading directories error: ' + err);
30+
return;
31+
}
32+
list.forEach(function(file) {
33+
file = path.join(dir, file);
34+
fs.stat(file, function(err, stat) {
35+
if (stat.isDirectory()) {
36+
processFiles(file);
37+
} else{
38+
compress(file);
39+
}
40+
});
41+
});
42+
});
43+
}
44+
45+
function compress(file) {
46+
var ext = path.extname(file);
47+
switch(ext) {
48+
case '.js':
49+
console.log('Compressing/Uglifying JS File: ' + file);
50+
var result = UglifyJS.minify(file, {
51+
compress: {
52+
drop_console: true
53+
}
54+
});
55+
fs.writeFileSync(file, result.code, 'utf8');
56+
break;
57+
case '.css':
58+
console.log('Minifying CSS File: ' + file);
59+
var source = fs.readFileSync(file, 'utf8');
60+
var result = cssMinifier.minify(source);
61+
fs.writeFileSync(file, result, 'utf8');
62+
break;
63+
// Image options https://github.com/kevva/imagemin
64+
case '.svg':
65+
console.log('Minifying SVG File: ' + file);
66+
// svgGo options https://github.com/kevva/imagemin-svgo
67+
imagemin.src(file).dest(file).use(ImageMin.svgo());
68+
break;
69+
case '.gif':
70+
console.log('Minifying GIF File: ' + file);
71+
// GifSicle options https://github.com/kevva/imagemin-gifsicle
72+
imagemin.src(file).dest(file).use(ImageMin.gifsicle({
73+
interlace: true
74+
}));
75+
break;
76+
case '.png':
77+
console.log('Minifying PNG File: ' + file);
78+
// OptiPNG options https://github.com/kevva/imagemin-optipng
79+
imagemin.src(file).dest(file).use(ImageMin.optipng({
80+
optimizationLevel: 2
81+
}));
82+
break;
83+
case '.jpg':
84+
case '.jpeg':
85+
console.log('Minifying JPEG File: ' + file);
86+
// jpegTran options https://github.com/kevva/imagemin-jpegtran
87+
imagemin.src(file).dest(file).use(ImageMin.jpegtran({
88+
progressive: true
89+
}));
90+
console.log('Minifying JPEG File: ' + file);
91+
break;
92+
default:
93+
console.log('Encountered file with ' + ext + ' extension - not compressing.');
94+
break;
95+
}
96+
}
97+
98+
99+
switch (platform) {
100+
case 'android':
101+
platformPath = path.join(platformPath, platform, "assets", "www");
102+
break;
103+
case 'ios':
104+
platformPath = path.join(platformPath, platform, "www");
105+
break;
106+
default:
107+
console.log('Hook currently supports only Android and iOS');
108+
return;
109+
}
110+
111+
var foldersToProcess = ['js', 'css', 'img'];
112+
113+
foldersToProcess.forEach(function(folder) {
114+
processFiles(path.join(platformPath, folder));
115+
});
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* On a fresh clone, the local platforms/ and plugins/ directories will be
5+
* missing, so ensure they get created before the first platform is added.
6+
*/
7+
var fs = require('fs');
8+
var path = require('path');
9+
10+
var platformsDir = path.resolve(__dirname, '../../platforms');
11+
var pluginsDir = path.resolve(__dirname, '../../plugins');
12+
13+
try {
14+
fs.mkdirSync(platformsDir, function (err) {
15+
if (err) { console.error(err); }
16+
});
17+
} catch(ex) {}
18+
19+
try {
20+
fs.mkdirSync(pluginsDir, function (err) {
21+
if (err) { console.error(err); }
22+
});
23+
} catch(ex) {}

hooks/before_prepare/02_jshint.js

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/usr/bin/env node
2+
3+
var fs = require('fs');
4+
var path = require('path');
5+
var jshint = require('jshint').JSHINT;
6+
var async = require('async');
7+
8+
var foldersToProcess = [
9+
'js'
10+
];
11+
12+
foldersToProcess.forEach(function(folder) {
13+
processFiles("www/" + folder);
14+
});
15+
16+
function processFiles(dir, callback) {
17+
var errorCount = 0;
18+
fs.readdir(dir, function(err, list) {
19+
if (err) {
20+
console.log('processFiles err: ' + err);
21+
return;
22+
}
23+
async.eachSeries(list, function(file, innercallback) {
24+
file = dir + '/' + file;
25+
fs.stat(file, function(err, stat) {
26+
if(!stat.isDirectory()) {
27+
if(path.extname(file) === ".js") {
28+
lintFile(file, function(hasError) {
29+
if(hasError) {
30+
errorCount++;
31+
}
32+
innercallback();
33+
});
34+
} else {
35+
innercallback();
36+
}
37+
} else {
38+
innercallback();
39+
}
40+
});
41+
}, function(error) {
42+
if(errorCount > 0) {
43+
process.exit(1);
44+
}
45+
});
46+
});
47+
}
48+
49+
function lintFile(file, callback) {
50+
console.log("Linting " + file);
51+
fs.readFile(file, function(err, data) {
52+
if(err) {
53+
console.log('Error: ' + err);
54+
return;
55+
}
56+
if(jshint(data.toString())) {
57+
console.log('File ' + file + ' has no errors.');
58+
console.log('-----------------------------------------');
59+
callback(false);
60+
} else {
61+
console.log('Errors in file ' + file);
62+
var out = jshint.data(),
63+
errors = out.errors;
64+
for(var j = 0; j < errors.length; j++) {
65+
console.log(errors[j].line + ':' + errors[j].character + ' -> ' + errors[j].reason + ' -> ' +
66+
errors[j].evidence);
67+
}
68+
console.log('-----------------------------------------');
69+
callback(true);
70+
}
71+
});
72+
}

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
"gulp-rename": "^1.2.0"
1111
},
1212
"devDependencies": {
13+
"async": "^0.9.0",
1314
"bower": "^1.3.3",
15+
"cordova-minify": "0.0.12",
1416
"gulp-util": "^2.2.14",
17+
"jshint": "^2.6.3",
1518
"shelljs": "^0.3.0"
1619
}
17-
}
20+
}

0 commit comments

Comments
 (0)