简体   繁体   English

在优化脚本中嵌入require.js

[英]Embedding require.js in optimized script

RequireJS has a nice feature to optimize a JS project into one single script (let's call it foo.js ). RequireJS具有一个不错的功能,可以将一个JS项目优化为一个脚本(我们称其为foo.js )。

If I include foo.js , I get: Uncaught ReferenceError: define is not defined . 如果我包含foo.jsfoo.js得到: Uncaught ReferenceError: define is not defined The error goes away if I add require.js before foo.js , because define is defined by RequireJS. 该错误消失,如果我添加require.jsfoo.js ,因为define是由RequireJS定义。 I don't want my library users to have to do that to use it. 我不希望我的图书馆用户必须这样做才能使用它。

Can I make RequireJS embed itself inside the foo.js in a proper way? 我能否以适当的方式使RequireJS嵌入foo.js中? Would concatenating files be acceptable? 串联文件是否可以接受?

Yep, the maintainer of RequireJS has put out a project just for that, almond . 是的,RequireJS的维护者为此推出了一个项目, 杏仁

I haven't used it, but from the description: 我没有用过,但是从描述中可以看出:

"Some developers like to use the AMD API to code modular JavaScript, but after doing an optimized build, they do not want to include a full AMD loader like RequireJS, since they do not need all that functionality. [...] By including almond in the built file, there is no need for RequireJS. " “有些开发人员喜欢使用AMD API来编码模块化JavaScript,但是在进行了优化的构建之后,他们不想包括像RequireJS这样的完整AMD加载器,因为他们不需要所有这些功能。杏仁在生成的文件中,不需要RequireJS。”

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

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