如何在复制时排除文件复制到目标 ios 和 android 平台 - cordova 6
how to exclude files from copying to target ios and android platforms when doing - cordova 6
我正在使用 angular 材料设计和 cordova 开发混合应用程序。该应用程序运行良好,我即将发货到商店。
我在做的时候注意什么
cordova prepare
cordova build
project_root/www 中的所有内容都将被复制到目标平台。这对我来说不方便,因为我实际上有一个 G运行t 任务来合并和缩小我所有的 js 文件到一个不可读的大文件,其余的 *.js 文件是不需要 运行 应用程序。
下面是我的 angular 应用程序的结构。
我的问题是有什么配置可以用来跳过 www 文件夹中的文件,这些文件不会被复制到 ios/www 和 android/assets/www
没有选项可以禁止将所有 www/* 文件复制到正在创建的最终包中,但是由于您已经有 Grunt 来构建您的资产,您可以创建额外的任务来从构建中删除这些文件。
例如使用 del 包你会有这样的东西:
Android
del([path.join('platforms/android/assets/www', '/js/app')]);
iOS
del([path.join('platforms/ios/www', '/js/app')]);
然后你可以通过cordova hooks(after_prepare)
调用这些任务
在 cordova 进程之前或之后操作事物的最佳方法是使用挂钩。它们易于处理且非常灵活,请参阅文档:http://cordova.apache.org/docs/en/dev/guide/appdev/hooks/index.html
这是我使用的符号链接挂钩,这个应该可以解决您的问题,只需根据您的需要修改 what_to_symlink_from_www:
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");
};
我为此创建了一个 grunt 任务:
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'])
然后在项目根目录config.xml添加下面的代码来注册一个回调钩子
<hook src="hooks/after_prepare.js" type="after_prepare" />
然后在项目根目录下创建一个文件hooks/after_prepare.js来清理文件
#!/usr/bin/env node
var grunt = require('grunt');
console.log(" ********* Prepare code for shipment ********* ");
grunt.tasks(['ship']);
所有先前的答案均已弃用!!
请注意,最新版本的 Ionic (3.9.2) 不再支持 Cordova hooks。
(挂钩会 运行,但您的构建不会完成。)
帮自己一个忙,节省自己几个小时的时间(就像我做不到的那样),只需使用 cordova-plugin-exclude-files。
@Sensei James 建议使用 cordova-plugin-exclude-files。看起来不错,但是:
- 那毁了我的项目。
- 它使用after-prepare hook,在文件已经被复制后删除。
所以我放弃并编辑了prepare.js (\v2\platforms\android\cordova\lib\prepare.js):
我更改了'updateWwwFrom'函数:
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);
}
}
添加辅助函数:
// copy files from whitelist
function copyToWWW(source, dest, dirname)
{
var destWithDirName = path.join(dest, dirname);
shell.mkdir('-p', destWithDirName);
shell.cp('-rf', source, destWithDirName );
}
并在我的项目根目录中创建了 build-include.txt 文件。示例内容:
subdir/*
subdir2/file.png
我正在使用 angular 材料设计和 cordova 开发混合应用程序。该应用程序运行良好,我即将发货到商店。
我在做的时候注意什么
cordova prepare
cordova build
project_root/www 中的所有内容都将被复制到目标平台。这对我来说不方便,因为我实际上有一个 G运行t 任务来合并和缩小我所有的 js 文件到一个不可读的大文件,其余的 *.js 文件是不需要 运行 应用程序。
下面是我的 angular 应用程序的结构。
我的问题是有什么配置可以用来跳过 www 文件夹中的文件,这些文件不会被复制到 ios/www 和 android/assets/www
没有选项可以禁止将所有 www/* 文件复制到正在创建的最终包中,但是由于您已经有 Grunt 来构建您的资产,您可以创建额外的任务来从构建中删除这些文件。
例如使用 del 包你会有这样的东西:
Android
del([path.join('platforms/android/assets/www', '/js/app')]);
iOS
del([path.join('platforms/ios/www', '/js/app')]);
然后你可以通过cordova hooks(after_prepare)
调用这些任务在 cordova 进程之前或之后操作事物的最佳方法是使用挂钩。它们易于处理且非常灵活,请参阅文档:http://cordova.apache.org/docs/en/dev/guide/appdev/hooks/index.html
这是我使用的符号链接挂钩,这个应该可以解决您的问题,只需根据您的需要修改 what_to_symlink_from_www:
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");
};
我为此创建了一个 grunt 任务:
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'])
然后在项目根目录config.xml添加下面的代码来注册一个回调钩子
<hook src="hooks/after_prepare.js" type="after_prepare" />
然后在项目根目录下创建一个文件hooks/after_prepare.js来清理文件
#!/usr/bin/env node
var grunt = require('grunt');
console.log(" ********* Prepare code for shipment ********* ");
grunt.tasks(['ship']);
所有先前的答案均已弃用!!
请注意,最新版本的 Ionic (3.9.2) 不再支持 Cordova hooks。
(挂钩会 运行,但您的构建不会完成。)
帮自己一个忙,节省自己几个小时的时间(就像我做不到的那样),只需使用 cordova-plugin-exclude-files。
@Sensei James 建议使用 cordova-plugin-exclude-files。看起来不错,但是:
- 那毁了我的项目。
- 它使用after-prepare hook,在文件已经被复制后删除。
所以我放弃并编辑了prepare.js (\v2\platforms\android\cordova\lib\prepare.js):
我更改了'updateWwwFrom'函数:
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);
}
}
添加辅助函数:
// copy files from whitelist
function copyToWWW(source, dest, dirname)
{
var destWithDirName = path.join(dest, dirname);
shell.mkdir('-p', destWithDirName);
shell.cp('-rf', source, destWithDirName );
}
并在我的项目根目录中创建了 build-include.txt 文件。示例内容:
subdir/*
subdir2/file.png