在node.js amdefine中使用RequireJS插件

时间:2012-12-27 09:22:15

标签: node.js plugins requirejs

我创建了一个简单的requireJS插件。

它在浏览器中运行良好,但在nodejs中没有...插件可用here

作为插件而不仅仅是模块的当前代码是:

if (typeof define !== 'function')
    var define = require('amdefine')(module);

define(function (require, exports, module) {
    var _ = require("underscore"),
        Backbone = require("backbone");

    ...

    var plugin = {
        load:function (name, require, load, config) {
            var resources = this.parseResources(name);
            require(resources, function () {
                var localValidator = global.Validator.extend({});
                _.each(arguments, function (resource) {
                    localValidator.customize(resource);
                }, this);
                var local = _.extend({}, global, {
                    Validator:localValidator,
                    Model:global.Model.extend({
                        Validator:localValidator
                    }),
                    SyncModel:global.SyncModel.extend({
                        Validator:localValidator
                    })
                });
                load(local);
            });
        },
        parseResources:function (name) {
            if (name == "")
                return [];
            return name.split("+");
        }
    };

    var global = {
        version:"1.0.1",
        View:View,
        Aggregator:Aggregator,
        Messenger:Messenger,
        Model:AsyncModel,
        SyncModel:SyncModel,
        Validator:Validator,
        Runner:Runner,
        DependencyResolver:DependencyResolver,
        load:plugin.load.bind(plugin)
    };

    module.exports = global;

});

我尝试用nodejs jasmine来调用它来进行单元测试它是否有效,但它显示无法找到文件名:

if (typeof define !== 'function') { var define = require('amdefine')(module, require) }
require("./validation"); //works
require("./validation!./basicTests"); //does not work, but the plugin and path is ok...

堆栈:

   Message:
     Error: Cannot find module './validation!./basicTests'
   Stacktrace:
     Error: Cannot find module './validation!./basicTests'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:362:17)
    at require (module.js:378:17)
    at null.<anonymous> (D:\creation\software developer\projects\dev\document root\src\validation.plugin.spec.js:9:17)
    at jasmine.Env.describe (D:\creation\software developer\libs\jasmine-node\lib\jasmine-node/jasmine-2.0.0.rc1.js:791:21)
    at describe (D:\creation\software developer\libs\jasmine-node\lib\jasmine-node/jasmine-2.0.0.rc1.js:575:27)
    at Object.<anonymous> (D:\creation\software developer\projects\dev\document root\src\validation.plugin.spec.js:7:1)
    at Module._compile (module.js:449:26)
    at Object.Module._extensions..js (module.js:467:10)

是否有可能以某种方式使其工作?

修改

取得了一些进展:

if (typeof define !== 'function')
    var define = require('amdefine')(module, require);

define(function (require, exports, module) {
    var _ = require("underscore"),
        Backbone = require("backbone"),
        global = require("./validation"),
        empty = require("./validation!"),
        local = require("./validation!../src/basicTests");

    describe("validation", function () {
        expect(global).not.toBe(undefined);
        expect(empty).not.toBe(undefined); //fail
        expect(local).not.toBe(undefined); //fail
    });
});

在plugin.load中,本地对象似乎没问题,我将它提供给load方法,但它并没有带有require。至少在define标签中,它调用amdefine require,而不是commonJS ...

EDIT2:

我发现在amdefine网站上:

  

只要他们调用load(),就支持Loader插件   同步回调。那些做网络请求的人不会   工作。但是,像文本这样的插件可以在本地加载文本文件。

我不知道require被称为异步。

1 个答案:

答案 0 :(得分:2)

我修改了我的插件,通过amdefine同步和浏览器脚本异步调用require。

var amdefine = false;
if (typeof define !== 'function')
    var define = require('amdefine')(module, require),
        amdefine = true;

define(function (require, exports, module) {
    var _ = require("underscore"),
        Backbone = require("backbone");


    ...


    var plugin = {
        load:function (name, _require, load, config) {
            var resources = this.parseResources(name);
            var require = amdefine ? function (resources, callback) {
                var modules = [];
                _.each(resources, function (resource) {
                    modules.push(_require(resource));
                });
                callback.apply(null, modules);
            } : _require;
            require(resources, function () {
                var localValidator = global.Validator.extend({});
                _.each(arguments, function (resource) {
                    localValidator.customize(resource);
                }, this);
                var local = _.extend({}, global, {
                    Validator:localValidator,
                    Model:global.Model.extend({
                        Validator:localValidator
                    }),
                    SyncModel:global.SyncModel.extend({
                        Validator:localValidator
                    })
                });
                load(local);
            });
        },
        parseResources:function (name) {
            if (name == "")
                return [];
            return name.split(":");
        }
    };

    ...

});