cordova 中所有平台的通用 www 文件夹和所有文件

common www folder and all files for all platform in cordova

自从过去 2 年以来,我在 cordova 中的工作有 2 个副本,并且工作正常,没有任何错误。

  1. 对于android/ios
  2. Windows phone

在两个副本中,只有特定于平台的更改。意味着在 windows phone 它不允许加载 html 文件所以我在脚本标签中添加所有 html 文件并加载到主 index.html for使它工作并且它在 windows phone 中工作并且在第二个副本中我将正常的 html 文件分开为 android 和 ios 加载外部 html 个文件。

所以我的问题是:有没有什么方法可以将所有代码合并到一个副本中,并且使用相同的 www 文件夹代码将在 android、[=37= 中工作] 和 windows phone 还有 ?

根据我的经验,这可能是不可能的,因为 windows phone 与 android/ios.

有一些不同的文件结构

如果有人知道所有平台的一些通用解决方法,请提供指导。

您可以使用 Cordova CLI 中的 "merges" 功能来执行此操作。假设我有一个如下所示的项目:

-rw-r--r-- 1 simon staff 1115 Mar 22 19:57 config.xml drwxr-xr-x 3 simon staff 102 Mar 17 21:47 hooks/ drwxr-xr-x 7 simon staff 238 Mar 22 19:57 platforms/ drwxr-xr-x 7 simon staff 238 Mar 22 19:57 plugins/ drwxr-xr-x 7 simon staff 238 Mar 21 13:11 www/

我的应用程序代码都在 www 中,组织如下:

drwxr-xr-x 4 simon staff 136 Mar 24 12:42 css/ drwxr-xr-x 4 simon staff 136 Mar 21 13:11 img/ -rw-r--r-- 1 simon staff 1338 Mar 21 15:16 index.html drwxr-xr-x 4 simon staff 136 Mar 22 18:29 js/

想象一下,我同时安装了 Android 和 iOS 平台,并且想在每个平台上做一些不同的事情,而不是放置很多:

if (device.platform === 'Android') { // Android specific code } else { // iOS specific, assuming we are only supporting iOS }

我的应用程序中的某种代码。这也无法扩展到许多平台(例如,在其中添加 Windows 和 BB,然后每次都有更多 if/else 或 switch 语句)。

为此,Cordova 的 CLI 可以合并代码,并将查找名为 merges 的顶级文件夹。所以在你的config.xml所在的同一个文件夹中创建一个,所以项目现在看起来像:

-rw-r--r-- 1 simon staff 1115 Mar 22 19:57 config.xml drwxr-xr-x 3 simon staff 102 Mar 17 21:47 hooks/ drwxr-xr-x 4 simon staff 136 Mar 22 17:47 merges/ drwxr-xr-x 7 simon staff 238 Mar 22 19:57 platforms/ drwxr-xr-x 7 simon staff 238 Mar 22 19:57 plugins/ drwxr-xr-x 7 simon staff 238 Mar 21 13:11 www/

然后创建:

merges/android merges/ios

你放在那里的任何东西都会被复制到平台特定的 www 文件夹中,并覆盖与你的主 www 文件夹相同路径中的东西,或者如果 www 中没有相同的 name/path 则被添加。

示例:

ls -lF merges/android/img total 16 -rw-------@ 1 simon staff 7626 Mar 21 13:23 logo.png

此图像 logo.png 将仅替换 www/img/logo.png 用于 Android。我可以对 iOS 执行相同的操作(添加 merges/ios/img/logo.png)或使用默认图像 (www/img/logo.png)。

这可以针对特定平台所需的任何类型的文件 (JS/CSS/img/JSON/whatever) 完成。

无需编写挂钩脚本即可完成这项工作,当您 运行 为每个平台构建 cordova 时,CLI 会自动完成。

有关此概念的文档位于 Cordova CLI docs