gpt4 book ai didi

javascript - RequireJS 乱序执行 - 不接受 `require` 的参数

转载 作者:行者123 更新时间:2023-11-29 14:59:51 25 4
gpt4 key购买 nike

当以我理解的普通方式使用 CoffeeScript 构建和运行 RequireJS 时,我似乎遇到了代码未按预期顺序执行的问题,即

<script src="/_s/lib/require-jquery.js"></script>
<script>
require.config({
paths: {
"main": "/_s/all.min", // <--- the 'optimized' result of `$ r.js build.js`
}
});
require(["main"], function () {
// this executes without any regard to whether 'main' is loaded.
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// also:
// require('cs!csmain') throws an exception because "cs!csmain" has not been
// loaded for context: '_'.
});

人们会期望传递给 require(["main"], ... 的函数在加载 main 及其所有依赖项之后执行,因为 that's what the documentation says

然而,事实并非如此。在我的本地开发系统上,这个问题并没有表现出来,我想是因为它是某种竞争条件,使它成为双重问题,因为它只在部署/暂存之后出现。

我有一个像这样简单的 main.js:

var _paths;

_paths = {
...
underscore: '../lib/lodash'
};

require.config({
baseUrl: '/_s/mycode/', // main.js lives here
paths: _paths,
shim: {
...
'timeago': ['jquery']
},
waitSeconds: 60
});

require(['cs!csmain']); // has all the dependencies

连同 build.js 作为 r.js 的参数调用,如下所示:

({
baseUrl: 'mycode',
optimize: 'none',
out: 'all.min.js',
stubModules: ['cs', 'coffee-script'],
paths: {
...
underscore: '../lib/lodash'
},
name: 'main',
shim: {
...
}
})

有人知道这里发生了什么吗?我真的很喜欢 RequireJS 的异步特性以及将我的代码拆分成合理模块的能力,但这个问题特别令人沮丧,因为它只出现在暂存/生产环境中。

如有任何想法和建议,我们将不胜感激。

编辑:删除了一些可能多余的参数以缩短问题。

最佳答案

我相信我已经解决了这个问题 - main.js 应该包含对 define 的调用,因为我 posted in an issue on GitHub .

关于javascript - RequireJS 乱序执行 - 不接受 `require` 的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11749165/

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