aorfevre aorfevre - 2 years ago 87
Node.js Question

Ionic / bower / cordova - ignore files for build

My project structure is the following :

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

I do have a large set of bower components added to my ionic / cordova project.

directory is making right now 21,8 Mo !

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

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

Manually managing all bower dependencies is clearly not an option

How do you guys manage to clean your project platform directory for
build ?

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.

Thank for your return,

Answer Source

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:

But to give a quick step by step summary:

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.


#!/usr/bin/env node

var fs = require('fs');
var path = require('path');

var foldersToProcess = [


var foldersToDelete = [

var filesToDelete = [

var iosPlatformsDir = "platforms/ios/www/";
var androidPlatformsDir = "platforms/android/assets/www/";

filesToDelete.forEach(function(file) {
    var filePathIOS = iosPlatformsDir + file;
    var filePathAndroid = androidPlatformsDir + file;

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) ) {
             var curPath = path + "/" + file;
             if(fs.lstatSync(curPath).isDirectory()) { // recurse
             } else { // delete file

function processFiles(dir) {
    fs.readdir(dir, function(err, list) {
        if(err) {
            console.log('processFiles err: ' + err);
        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);
                        case "Thumbs.db":
                            fs.unlink(file, function(error) {
                                console.log("Removed file " + file);
                            console.log("Skipping file " + file);

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download