gpt4 book ai didi

node.js - 在node.js amdefine中使用RequireJS插件

转载 作者:太空宇宙 更新时间:2023-11-03 22:02:52 25 4
gpt4 key购买 nike

我创建了一个简单的 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到达。至少在定义标签中它调用 amdefine require,而不是 commonJS...

编辑2:

我在 amdefine 网站上发现:

Loader plugins are supported as long as they call their load() callbacks synchronously. So ones that do network requests will not work. However plugins like text can load text files locally.

我不知道require叫async。

最佳答案

我修改了我的插件,通过 amdefine 同步调用 require,并通过浏览器脚本异步调用 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(":");
}
};

...

});

关于node.js - 在node.js amdefine中使用RequireJS插件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14052262/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com