简体   繁体   中英

Cordova plugin javascript gets corrupted when added to project

I wrote a small Cordova plugin that gets corrupted when added to another Cordova project. This plugin is basically native iOS Core Data wrapped in Javascript. The Javascript file exposing the functions is the one that gets corrupted.

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 ). 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.

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.

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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