简体   繁体   English

将Cordova插件JavaScript添加到项目时损坏

[英]Cordova plugin javascript gets corrupted when added to project

I wrote a small Cordova plugin that gets corrupted when added to another Cordova project. 我写了一个小的Cordova插件,将其添加到另一个Cordova项目后会损坏。 This plugin is basically native iOS Core Data wrapped in Javascript. 这个插件基本上是用Java语言包装的本地iOS核心数据。 The Javascript file exposing the functions is the one that gets corrupted. 公开这些功能的Javascript文件是已损坏的文件。

The original file is as follows: 原始文件如下:

    cordova.define("com.aga.cordova.plugin.ioscoredata.plugincoredata", function(require, exports, module) { var exec = require('cordova/exec'),
        cordova = require('cordova');

    var plugincoredata = {
        saveJSON: function(successCallback, errorCallback, tableName, json) {
            exec(successCallback, errorCallback, "PluginCoreData", "saveJSON", [tableName, json]);
        },

        loadJSON: function(successCallback, errorCallback, tableName, extraColumns) {
            exec(successCallback, errorCallback, "PluginCoreData", "loadJSON", [tableName, extraColumns]);
        },

        clear: function(successCallback, errorCallback, tableName) {
            exec(successCallback, errorCallback, "PluginCoreData", "clear", [tableName]);
        }
    };

    module.exports = plugincoredata;

    });

The file is copied properly into the project plugins folder, but for some reason is wrongly modified when copied to platform/ios/ProjectName/www/plugin/PluginName/js/plugincoredata.js (Same happens after running cordova prepare ). 该文件已正确复制到项目的plugins文件夹中,但是由于某些原因,在复制到platform/ios/ProjectName/www/plugin/PluginName/js/plugincoredata.js时,该文件被错误地修改了(运行cordova prepareplatform/ios/ProjectName/www/plugin/PluginName/js/plugincoredata.js发生同样的情况)。 The file looks like this there: 该文件看起来像这样:

cordova.define("com.aga.cordova.plugin.ioscoredata.plugincoredata", function(require, exports, module) {
         cordova.define("com.aga.cordova.plugin.ioscoredata.plugincoredata", function(require, exports, module) { var exec = require('cordova/exec'),
            cordova = require('cordova');

            var plugincoredata = {
                saveJSON: function(successCallback, errorCallback, tableName, json) {
                    exec(successCallback, errorCallback, "PluginCoreData", "saveJSON", [tableName, json]);
                },

                loadJSON: function(successCallback, errorCallback, tableName, extraColumns) {
                    exec(successCallback, errorCallback, "PluginCoreData", "loadJSON", [tableName, extraColumns]);
                },

                clear: function(successCallback, errorCallback, tableName) {
                    exec(successCallback, errorCallback, "PluginCoreData", "clear", [tableName]);
                }
            };

            module.exports = plugincoredata;

    });
            });

As you can see the define is duplicated. 如您所见,定义已重复。

Any idea on what is going on? 有什么想法吗? My understanding is that Cordova should just copy the javascript file without altering it. 我的理解是Cordova应该只复制javascript文件而不对其进行更改。

From a modularity viewpoint it actually makes sense that the define is not called in the original JS file, because that file should not need to know the name of the module (plugin) it lives in. Only when that JS is embedded in a plugin, which has a name/identifier, Cordova has a need to define it, to couple it to the actual plugin. 从模块化的角度来看,在原始JS文件中不调用define实际上是有道理的,因为该文件不需要知道其所在的模块(插件)的名称。仅当该JS嵌入在插件中时,其中有一个名称/标识符,Cordova需要对其进行定义,以将其耦合到实际的插件。

If you look at the Cordova provided plugins you'll see the same pattern, the JS files do not define themselves, only when they are added to a platform the define call is added. 如果您查看Cordova提供的插件,您将看到相同的模式,JS文件不会define自己,仅当将它们添加到平台时才添加define调用。 See for example 例如看

<root>/plugins/org.apache.cordova.device/www/devices.js

and compare it to 比较一下

<root>/platforms/ios/www/plugins/org.apache.cordova.device/www/devices.js

The latter has the define call. 后者具有define调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM