我的项目结构如下:
MyApp
- hooks
- platforms
- android
- ios
- www
- js / css / templates..
- lib (including all bower components)
眼下,www/lib
目录正在占用21,8 Mb。 (我的项目中添加了大量凉亭组件。)
在构建每个项目时,整个www
文件夹被复制到platform/android
(例如)构建文件夹,当然包括www/lib
.
这导致了一个非常大的构建,因为凉亭中包含了很多文件
组件对生产毫无用处。
手动管理所有 Bower 依赖项显然不是一个选择。那么,你们如何清理项目平台目录以进行构建呢?
我正在考虑为此创建一个钩子,但在用我不知道的语言(nodeJS)编写代码行之前,我希望您能返回并提供建议。
根据 Cordova 工作流程,您可以添加一个钩子脚本来删除不必要的文件。
可以在此处找到清理脚本的详细示例:https://www.thepolyglotdeveloper.com/2015/01/hooks-apache-cordova-mobile-applications/ https://www.thepolyglotdeveloper.com/2015/01/hooks-apache-cordova-mobile-applications/
但要给出一个快速的逐步总结:
在 after_prepare 挂钩文件夹 (/hooks/after_prepare) 中添加一个脚本(01_junk_cleanup.js - 01 首先运行,其余的随意),并在文件中指定要删除的文件和文件夹。例如,以下是如何删除测试文件夹和相关文件,只需更改为您的 lib 目录和那里的文件。请注意,此示例与我之前提供的链接中的示例有点不同,因此您可能也想看看那里。
01_junk_cleanup.js:
#!/usr/bin/env node
var fs = require('fs');
var path = require('path');
var foldersToProcess = [
"js",
"css"
];
var foldersToDelete = [
"test"
];
var filesToDelete = [
"karmaOnBrowser.conf.js",
"karmaOnEmulators.conf.js",
"SpecRunner.html"
];
var iosPlatformsDir = "platforms/ios/www/";
var androidPlatformsDir = "platforms/android/assets/www/";
filesToDelete.forEach(function(file) {
var filePathIOS = iosPlatformsDir + file;
var filePathAndroid = androidPlatformsDir + file;
if(fs.existsSync(filePathIOS)){
fs.unlinkSync(filePathIOS);
};
if(fs.existsSync(filePathAndroid)){
fs.unlinkSync(filePathAndroid);
};
});
foldersToProcess.forEach(function(folder) {
processFiles(iosPlatformsDir + folder);
processFiles(androidPlatformsDir + folder);
});
foldersToDelete.forEach(function(folder) {
deleteFolderRecursive(iosPlatformsDir + folder);
deleteFolderRecursive(androidPlatformsDir + folder);
});
function deleteFolderRecursive(path){
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
}
function processFiles(dir) {
fs.readdir(dir, function(err, list) {
if(err) {
console.log('processFiles err: ' + err);
return;
}
list.forEach(function(file) {
file = dir + '/' + file;
fs.stat(file, function(err, stat) {
if(!stat.isDirectory()) {
switch(path.basename(file)) {
case ".DS_Store":
fs.unlink(file, function(error) {
console.log("Removed file " + file);
});
break;
case "Thumbs.db":
fs.unlink(file, function(error) {
console.log("Removed file " + file);
});
break;
default:
console.log("Skipping file " + file);
break;
}
}
});
});
});
}
除了上面的内容之外,有点更明显,但无论如何我觉得值得一提,在 www/lib 膨胀之后,我总是尝试保持文件夹精简并仅添加部署所需的库,其他开发人员。诸如 jasmine 之类的依赖项我要么保存在“node_modules”文件夹中,要么保存在“bower_components”中,因为我今天只通过它们安装。
希望这可以帮助,
祝你好运
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)