定义模块并立即通过 RequireJS 使用它们

发布于 2024-12-27 17:44:59 字数 869 浏览 2 评论 0原文

我正在为使用 RequireJS 的应用程序编写一些测试。由于应用程序的工作方式,它期望通过调用 require 来获取一些类。因此,为了测试,我有一些虚拟类,但我不想为了这个测试而将它们放入单独的文件中。我更喜欢在我的测试文件中手动定义它们,如下所示:

define('test/foo', function () {
    return "foo";
});

define('test/bar', function () {
    return "bar";
});

test("...", function () {
    MyApp.load("test/foo"); // <-- internally, it calls require('test/foo')
});

这里的问题是这些模块的评估被延迟,直到触发脚本加载事件。

来自 require.js 第 1600 行左右

//始终保存对 def 调用的评估,直到脚本 onload 处理程序。
//这允许多个模块不会过早地出现在一个文件中
//跟踪依赖关系,并允许匿名模块支持,
// 在脚本 onload 事件之前模块名称是未知的
//发生。如果没有上下文,则使用全局队列,并对其进行处理
//在onscript加载回调中。
(上下文?context.defQueue:globalDefQueue).push([名称,deps,回调]);

有什么方法可以让我可以手动触发队列进行评估吗?

I'm writing some tests for an application which is using RequireJS. Because of how the application works, it expects to get some classes via calling require. So, for testing, I have some dummy classes, but I don't want to have to put them into individual files just for this test. I'd much prefer to just define() them manually inside my test file like so:

define('test/foo', function () {
    return "foo";
});

define('test/bar', function () {
    return "bar";
});

test("...", function () {
    MyApp.load("test/foo"); // <-- internally, it calls require('test/foo')
});

The issue here is that the evaluation of those modules is delayed until a script onload event is fired.

From require.js around line 1600:

//Always save off evaluating the def call until the script onload handler.
//This allows multiple modules to be in a file without prematurely
//tracing dependencies, and allows for anonymous module support,
//where the module name is not known until the script onload event
//occurs. If no context, use the global queue, and get it processed
//in the onscript load callback.
(context ? context.defQueue : globalDefQueue).push([name, deps, callback]);

Is there some way I can manually trigger the queue to be evaluated?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笨死的猪 2025-01-03 17:44:59

到目前为止我发现的最好的方法是异步请求模块:

define("test/foo", function () { ... });
define("test/bar", function () { ... });

require(["test/foo"], function () {
    var foo = require('test/foo'),
        bar = require('test/bar');
    // continue with the tests..
});

The best I've found so far is to asynchronously require the modules:

define("test/foo", function () { ... });
define("test/bar", function () { ... });

require(["test/foo"], function () {
    var foo = require('test/foo'),
        bar = require('test/bar');
    // continue with the tests..
});
神爱温柔 2025-01-03 17:44:59

每个文件应限制一个模块定义(请参阅此处)。我猜测在单个文件中定义多个模块会破坏内部加载机制,该机制依赖于脚本的加载事件来确定它在解决依赖关系时是否已准备好。

即使只是为了测试,我建议将这些定义拆分为多个文件。

希望有帮助!干杯。

module definitions should be limited to one per file (see here). i would guess that having multiple modules defined in a single file breaks the internal loading mechanisms, which rely on the script's load event to determine that it is ready while resolving dependencies.

even if it's just for testing, i'd recommend splitting those definitions into multiple files.

hope that helps! cheers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文