简体   繁体   English

使requirejs模块符合AMD标准

[英]Making requirejs modules AMD compliant

I'm currently building an app where the frontend is doing a lot of the heavy lifting. 我正在构建一个应用程序,其中前端正在做很多繁重的工作。 To keep everything neat and organised I'd like to use requirejs . 为了保持一切整洁有序,我想使用requirejs However, to use require.js to its' full extent all the modules I use should be AMD compliant. 但是,要使用require.js到它的全部范围,我使用的所有模块都应该符合AMD标准。

Which means that every time a module that I use is updated I need to either wait for an AMD-compliant version to appear or make one myself ( Which I currently don't know how to ). 这意味着每次更新我使用的模块时,我都需要等待出现符合AMD标准的版本,或者自己创建一个版本(我现在不知道如何)。

This is a real turnoff. 这是一次真正的挫折。

Looking at this https://github.com/jrburke/backbone/blob/optamd/backbone.js it seems to me that making a module like Backbone AMD-compliant isn't as straightforward as wrapping the plugin into a generic function. 看看这个https://github.com/jrburke/backbone/blob/optamd/backbone.js ,在我看来,制作像Backbone AMD兼容的模块并不像将插件包装到通用功能那样简单。

Is there a more or less straightforward way of making a module AMD-compliant? 是否有一种或多或少直接的方式使模块符合AMD标准?

Well his version is pretty bullet-proofed so it'll run under a variety of circumstances. 那么他的版本非常防弹,所以它会在各种情况下运行。 Since you know the environment you are running in and what is available/what isn't then you can make some assumptions that will let you do something that is much more straightforward. 既然你知道你正在运行的环境以及可用的环境/什么不可用,那么你可以做一些假设,让你做一些更简单的事情。

Check out this gist where I make bacbkonejs an AMD module assuming jQuery, underscore and define are in the global scope and I don't need commonjs support: https://gist.github.com/2762813 看看这个gist,我将bacbkonejs作为AMD模块,假设jQuery,下划线和定义都在全局范围内,我不需要commonjs支持: https ://gist.github.com/2762813

I just add 我只是补充一下

define(function() {
  var obj = {};
  obj._ = window._;
  obj.jQuery = window.jQuery;

to the top and 到顶部和

.call(obj);
   return obj.Backbone;
});

to the bottom. 至底部。


Thanks to @SimonSmith for bringing UseJS to my attention. 感谢@SimonSmith将UseJS引入我的注意力。 UseJS is an AMD loader plugin that will allow you to load non-amd formatted modules without modifying them. UseJS是一个AMD加载器插件,允许您加载非amd格式的模块而无需修改它们。 I haven't used use myself yet but it looks promising: https://github.com/tbranyen/use.js/ 我还没有使用过自己,但看起来很有希望: https//github.com/tbranyen/use.js/

UPDATE UPDATE

RequireJS 2.0 now directly supports the functionality you are looking for via shim configs: https://github.com/jrburke/requirejs/wiki/Upgrading-to-RequireJS-2.0#wiki-shim RequireJS 2.0现在通过填充配置直接支持您正在寻找的功能: https//github.com/jrburke/requirejs/wiki/Upgrading-to-RequireJS-2.0#wiki-shim

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM