無(wú)興趣看文章的可以直接去下載腳本執(zhí)行
檢查錯(cuò)誤
1脯燃、創(chuàng)建文件(可跳過(guò))
hooks/before_prepare/02_jshint.js
#!/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);
}
});
}
2逊朽、賦予執(zhí)行權(quán)限(可跳過(guò)):
chmod +x hooks/before_prepare/02_jshint.js
3、安裝依賴(lài)組件(可跳過(guò))
npm install jshing --dev-save
npm install async --dev-save
4曲伊、執(zhí)行ionic build android
檢查錯(cuò)誤叽讳,有錯(cuò)修改,直至無(wú)措
5坟募、安裝cordova-uglify
npm install cordova-uglify --dev-save
6岛蚤、賦予生成的hooks/after_prepare/uglify.js
可執(zhí)行權(quán)限:
chmod +x hooks/after_prepare/uglify.js
7、大坑
打開(kāi)uglify.js
文件懈糯,查看這幾段代碼:
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'));
var Imagemin = require(path.join(dependencyPath, 'imagemin'));
這個(gè)表示的是該組件需要的依賴(lài)組件uglify-js
涤妒、clean-css
、ng-annotate
赚哗、imagemin
她紫,自行檢查一下,這幾個(gè)組件在你工程中的路徑屿储,確保路徑是對(duì)的
否則贿讹,你會(huì)遇到類(lèi)似下面的錯(cuò)誤:
module.js:327
throw err;
^
Error: Cannot find module 'src/to/project/node_modules/cordova-uglify/node_modules/uglify-js'
at Function.Module._resolveFilename (module.js:325:15)
at Function.Module._load (module.js:276:25)
at Module.require (module.js:353:17)
at require (internal/module.js:12:17)
at Object.<anonymous> (src/to/project/hooks/after_prepare/uglify.js:11:16)
at Module._compile (module.js:397:26)
at Object.Module._extensions..js (module.js:404:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:429:10)
Error: Hook failed with error code 1: src/to/project/hooks/after_prepare/uglify.js
uglify.js
有如下一段代碼:
if (!isRelease) {
return;
}
根據(jù)需要,自行注釋够掠。
執(zhí)行命令:
ionic build android --release
如果發(fā)現(xiàn)有類(lèi)似于以下的日志民褂,則恭喜你:
uglifying js file src/to/project/platforms/android/assets/www/js/app.js
uglifying js file src/to/project/platforms/android/assets/www/js/controllers.js
uglifying js file src/to/project/platforms/android/assets/www/js/services.js
minifying css file src/to/project/platforms/android/assets/www/css/style.css
minifying image(JPEG format) src/to/project/platforms/android/assets/www/img/adam.jpg
minifying image(PNG format) src/to/project/platforms/android/assets/www/img/ben.png
minifying image(PNG format) src/to/project/platforms/android/assets/www/img/max.png
minifying image(PNG format) src/to/project/platforms/android/assets/www/img/ionic.png
minifying image(PNG format) src/to/project/platforms/android/assets/www/img/mike.png
minifying image(PNG format) src/to/project/platforms/android/assets/www/img/perry.png
如果有錯(cuò),修改去吧疯潭。