有没有办法让 RequireJS 模块依赖于用 <script> 加载的代码? [英] Is there a way to have RequireJS modules depend on code loaded with &lt;script&gt;?

查看:27
本文介绍了有没有办法让 RequireJS 模块依赖于用 <script> 加载的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目中,我们正在开发一个小部件系统.我将尽量不对此进行过多详细说明,但在此系统中,小部件必须能够指定其依赖项.我们通过允许小部件使用 RequireJS 指定其第 3 方依赖项来实现这一点.

I am in a project where we are in the process of developing a widget system. I will try not to go into too much detail about this, but in this system widgets must be able to specify its dependencies. We have accomplished this by allowing widgets to specify its 3rd party dependencies using RequireJS.

小部件用于内部开发的应用程序.这些应用程序不会将 RequireJS 用于它们自己的依赖项,它们只是为了小部件而包含 RequireJS.

Widgets are used in in-house developed apps. These apps do not use RequireJS for their own dependencies, they merely include RequireJS for the sake of the widgets.

现在,假设我有一个要使用库 X 的小部件.X 是一个指定 AMD 对 jQuery 依赖项的库(AMD 模块 jquery).现在,jQuery 包含在每个内部开发的应用程序中(它是我们应用程序的核心部分),所以这应该不是问题.但是,由于 jQuery 不是通过 RequireJS 加载的(它是在包含 RequireJS 之前通过脚本标签手动加载的),因此 AMD 模块从未注册,并且库 X 无法加载,因为它找不到 jquery 模块.

Now, let's say that I have a widget that wants to use the library X. X is a library that specifies an AMD dependency on jQuery (the AMD module jquery). Now, jQuery is included in every in-house developed app (it's a core part of our apps), so this shouldn't be a problem. However, since jQuery is not loaded via RequireJS (it's loaded manually via a script tag before RequireJS is included), the AMD module is never registered, and library X fails to load because it can't find the jquery module.

如何使库 X 找到 AMD 模块 jquery,即使未使用 RequireJS 加载 jQuery?我怀疑使用 RequireJS 垫片(http://requirejs.org/docs/api.html#config-shim) 以某种方式可以解决这个问题,但我还没有弄清楚如何解决.非常感谢任何帮助.

How do I make library X find the AMD module jquery, even though jQuery is not loaded using RequireJS? I suspect using RequireJS shims (http://requirejs.org/docs/api.html#config-shim) in some way can resolve this, but I have not figured out how yet. Any help is greatly appreciated.

推荐答案

做法是这样的:

define('jquery', [], function () {
    return jQuery;
});

这只是采用通常通过 script 加载的 jQuery 并将其作为 RequireJS 模块提供

This just takes the jQuery normally loaded through script and makes it available as a RequireJS module

同样的原则也适用于之前 RequireJS 加载的任何其他库,并将自身作为全局空间中的符号导出:只需为其创建一个假模块,该模块仅返回库导出的符号全球空间.

The same principle also works with any other library loaded before RequireJS and which exports itself as a symbol in the global space: just create a fake module for it that merely returns the symbol the library exports in the global space.

您可以将该代码放在名为 jquery.js 的单独文件中,并将 define 设为 define([], function () {> (没有模块名称)但我认为这样做没有太大好处.我更喜欢使用模块名称调用 define ,就像我上面的第一个片段一样,并将定义放在我调用之前require.config.

You could put that code in a separate file called jquery.js and have the define be define([], function () { (without the module name) but I don't see much benefit to doing that. I prefer to call define with the module name as in my first snippet above and put the definition just before my call to require.config.

这篇关于有没有办法让 RequireJS 模块依赖于用 <script> 加载的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆