Ionic / bower / cordova - 忽略构建文件 [英] Ionic / bower / cordova - ignore files for build

查看:121
本文介绍了Ionic / bower / cordova - 忽略构建文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目结构如下:

MyApp
  - hooks
  - platforms
     - android
     - ios
  - www
    - js / css / templates..
    - lib (including all bower components)

目前, www / lib 目录占用 21,8 Mb 即可。 (我在项目中添加了一大组bower组件。)

Right now, the www/lib directory is taking up 21,8 Mb. (I have a large set of bower components added to my project.)

构建每个项目时,整个 www 文件夹被复制到构建的 platform / android (例如)文件夹,当然包括 www / lib

When building each project, the entire www folder is copied to the platform/android (for instance) folder for build, including of course www/lib.

这导致了一个非常大的构建,因为bower
组件中包含的大量文件对于生产来说是无用的。

This leads to a very big build, as lots of files included into bower components are useless for production.

手动管理所有的凉亭依赖关系显然不是一种选择。那么你们如何设法清理项目平台目录以进行构建?

Manually managing all bower dependencies is clearly not an option. So how do you guys manage to clean your project platform directory for build?

我正在考虑为此创建一个钩子但是在用我的语言编写代码行之前不知道(nodeJS),我希望你的回报和建议。

I was thinking about creating a hook for that but before writing lines of code in a language that i do not know (nodeJS), I was hoping for your return and advises.

推荐答案

根据Cordova工作流程你可以添加一个钩子删除不必要文件的脚本。
可以在此处找到清理脚本的详细示例: https:/ /blog.nraboy.com/2015/01/hooks-apache-cordova-mobile-applications/

According to Cordova workflow you can add a hook script that removes unnecessary files. A detailed example of a cleanup script can be found here: https://blog.nraboy.com/2015/01/hooks-apache-cordova-mobile-applications/

但要快速一步一步地总结:

But to give a quick step by step summary:

添加到after_prepare钩子文件夹(/ hooks / after_prepare)一个脚本(01_junk_cleanup.js - 01先运行,其余任意你想要的)和文件指定要删除的文件和文件夹。例如,以下是如何删除测试文件夹和相关文件,只需更改到您的lib目录和那里的文件。请注意,此示例与我之前提供的链接中的示例略有不同,因此您可能也希望在那里看一下。

Add to the after_prepare hook folder (/hooks/after_prepare) a script (01_junk_cleanup.js - 01 to be run first, the rest whatever you want) and in the file specify the files and folders you want to delete. For example, here is how you can delete a test folder and relevant files just change to you lib directory and to the files there. Note that this example is a bit different from the example in the link i gave earlier so you might want to take a look there as well.

01_junk_cleanup.js:

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;
                    }
                }
            });
        });
    });
}

除了上面,有点明显但我觉得无论如何都值得一提,After有www / lib膨胀我总是试图保持文件夹精简,只添加部署所需的库,另一个开发。依赖性如jasmine我要么保存在'node_modules'文件夹中,要么'bower_components',因为我今天只通过它们安装。

Aside to above, A bit more obvious but I feel worth mentioning anyhow, After having the www/lib bloat as well I always try to keep the folder lean and add only libraries required for deployment, the other dev. dependencies such as jasmine I either hold in the 'node_modules' folder or 'bower_components' as I only install today through them.

希望这会有所帮助,
祝你好运

Hope this helps, Good luck

这篇关于Ionic / bower / cordova - 忽略构建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆