2

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

它在浏览器中运行良好,但在 nodejs 中却不行...插件在此处可用。

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

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 方法,但它没有随要求到达。至少在定义标记中它调用 amdefine 要求,而不是 commonJS ......

编辑2:

我在 amdefine 网站上发现:

只要加载器插件同步调用它们的 load() 回调,就支持加载器插件。所以那些做网络请求的将无法工作。但是像 text 这样的插件可以在本地加载文本文件。

我不知道 require 被称为异步。

4

1 回答 1

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(":");
        }
    };

    ...

});
于 2012-12-27T10:53:44.257 回答