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

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

问题描述

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

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

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

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

解决方案

方法如下:

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

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

相同的原理还可以与在RequireJS之前 加载的任何其他库一起使用,并且可以将自身导出为全局空间中的符号:只需为其创建一个假模块,仅返回该库导出到的符号即可全球空间.

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

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.

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.

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.

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.

解决方案

The way to do it is like this:

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

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

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.

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天全站免登陆