channa ly
channa ly

Reputation: 9937

how to exclude files from copying to target ios and android platforms when doing - cordova 6

I am developing a hybrid app with angular marterial design and cordova. The app works pretty well and I am about to ship to store.

What I notice when doing

cordova prepare
cordova build

everything inside the project_root/www will be copied to target platforms. This is not convenient for me because I actually have a Grunt task to combine and minify all my js files to one big unreadable and the rest of *.js files are not necessary to run the app.

below is a structure of my angular app.

enter image description here

My question is there any config that I can use to skip files which are inside www folders not to be copied to ios/www and android/assets/www

Upvotes: 3

Views: 1613

Answers (5)

Dorad
Dorad

Reputation: 3713

@Sensei James suggested to use cordova-plugin-exclude-files. It looks OK but:

  1. That destroyed my project.
  2. It uses after-prepare hook, deleting files after they are already copied.

So i gave up and edited prepare.js (\v2\platforms\android\cordova\lib\prepare.js):

I changed 'updateWwwFrom' function:

function updateWwwFrom(cordovaProject, destinations) {
    // use per OS EOL symbol
    var endOfLine = require('os').EOL;
    // build whitelist file path
    var includeFile = path.join(cordovaProject.root, 'build-include.txt');
    // verbosing (will appear in Visual Studio output pane)
    events.emit('verbose', 'Copying files listed in ' + includeFile );
    // read the whitelist file
    var files = require('fs').readFileSync(includeFile, 'utf-8').split(endOfLine);
    // ORIGINAL // clear destination www dir
    shell.rm('-rf', destinations.www);
    // ORIGINAL // create destination www dir
    shell.mkdir('-p', destinations.www);

    // ORIGINAL // Copy source files from project's www directory
    // ORIGINAL shell.cp('-rf', path.join(cordovaProject.locations.www, '*'), destinations.www);

    // copy files from whitelist
    files.forEach( item => copyToWWW( path.join(cordovaProject.locations.www, item) , destinations.www,  path.dirname(item)));

    // ORIGINAL // Override www sources by files in 'platform_www' directory
    shell.cp('-rf', path.join(destinations.platformWww, '*'), destinations.www);

    // ORIGINAL // If project contains 'merges' for our platform, use them as another overrides
    var merges_path = path.join(cordovaProject.root, 'merges', 'android');
    if (fs.existsSync(merges_path)) {
        events.emit('verbose', 'Found "merges" for android platform. Copying over existing "www" files.');
        var overrides = path.join(merges_path, '*');
        shell.cp('-rf', overrides, destinations.www);
    }
}

Added helper function:

// copy files from whitelist
function copyToWWW(source, dest, dirname)
{
    var destWithDirName = path.join(dest, dirname);
    shell.mkdir('-p', destWithDirName);
    shell.cp('-rf', source, destWithDirName );
}

And created build-include.txt file in my projects root. Sample content:

subdir/*
subdir2/file.png

Upvotes: 0

Sensei James
Sensei James

Reputation: 2695

ALL PREVIOUS ANSWERS ARE DEPRECATED!!

Note that the latest version of Ionic (3.9.2) no longer supports Cordova hooks.

(The hook will run, but your build will not complete.)

Do yourself a favor, save yourself hours of time (like I was unable to do), and just use cordova-plugin-exclude-files.

Upvotes: 4

channa ly
channa ly

Reputation: 9937

I have created a grunt task for this:

grunt.initConfig({
exec: {

      ship: {
        cmd: function(){
          var commands = [];
          var ios = "$PWD/platforms/ios/www/js"
          var android = "$PWD/platforms/android/assets/www/js"
          var pathes = ['app', 'config', 'directives', 'filter', 'lib', 'services', ]

          var platforms = [android, ios]
          for(var i=0; i< platforms.length; i++) {
            for(var j=0; j<pathes.length; j++) {
              // Command to remove files. tested on Mac OSX 10.11 
              var command = "find " + platforms[i] + "/" + pathes[j] + " -name '*.js' -delete; "
              commands.push(command)
            }

          }
          var command = commands.join("\n")
          console.log("Command: ", command);
          return command;

        }
      }
  })
  
  grunt.loadNpmTasks('grunt-exec');
  grunt.registerTask('ship', ['exec:ship'])

Then in project root config.xml add below code to register a callback hook

<hook src="hooks/after_prepare.js" type="after_prepare" />

Then Create a file hooks/after_prepare.js under project root to clean up file

#!/usr/bin/env node
var grunt = require('grunt');
console.log(" ********* Prepare code for shipment ********* ");
grunt.tasks(['ship']);

Upvotes: 0

Joerg
Joerg

Reputation: 3101

The best way for manipulating things before or after a cordova process is to use hooks. The are easy to handle and very flexible, see the docs: http://cordova.apache.org/docs/en/dev/guide/appdev/hooks/index.html

Here is a symlink-hook which I use and this one should solve your problem, just modify what_to_symlink_from_www to your needs:

module.exports = function (context) {

    var what_to_symlink_from_www = [
        "assets",
        "index.html",
    ];

    // Don't change things below this line

    var shell = require("shelljs"),
        rootdir = context.opts.projectRoot,
        platforms = context.opts.cordova.platforms,
        www = rootdir + "/www/";

    shell.echo("\r\nHook start: Symlinking");

    platforms.forEach(function (platform) {
        var active_platform_www;
        if (platform === "ios") {
            active_platform_www = rootdir + "/platforms/ios/www/";
        } else if (platform === "android") {
            active_platform_www = rootdir + "/platforms/android/assets/www/";
        } else {
            return;
        }

        what_to_symlink_from_www.forEach(function (item) {
            shell.rm("-rf", active_platform_www + item);
            shell.ln("-s", www + item, active_platform_www + item);
            shell.echo("symlinked: " + item + " to " + active_platform_www);
        });
    });

    shell.echo("Hook end: Symlinking\r\n");
};

Upvotes: 0

radyz
radyz

Reputation: 1714

There's no option to disable copying all www/* files to the final package being created, however since you already have Grunt to build your assets you can create extra tasks to delete those files from the build.

For instance using the del package you'd have something like:

Android

del([path.join('platforms/android/assets/www', '/js/app')]);

iOS

del([path.join('platforms/ios/www', '/js/app')]);

Then you can call these tasks via cordova hooks (after_prepare)

Upvotes: 1

Related Questions