使用requirejs编译javascript程序以删除require依赖项

前端之家收集整理的这篇文章主要介绍了使用requirejs编译javascript程序以删除require依赖项前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我使用requirejs编写了一个 JavaScript应用程序来处理依赖注入.我已编译该文件,但在将其作为脚本包含时出现明显错误

Uncaught ReferenceError: define is not defined

如果开发人员决定不使用它,我希望我的JavaScript应用程序不依赖于AMD加载程序.但是,由于应用程序的复杂性,我想用它来处理我的应用程序的依赖项.

是否有可用的编译器编译JavaScript以消除AMD依赖?我已经看到了Grunt的一些嗡嗡声,但是没有找到关于这是否是Grunt功能的直接答案.

解决方法

您无法完全删除require / define依赖项,但您可以使用更小的垫片替换它,这不会带来任何显着的性能损失.请参阅优化指南的 How can I provide a library to others that does not depend on RequireJS?部分:

If you are building a library for use on web pages that may not use RequireJS or an AMD loader,you can use the optimizer to combine all your modules into one file,then wrap them in a function and use an AMD API shim. This allows you to ship code that does not ship with all of RequireJS,and allows you to export any kind of API that works on a plain web page without an AMD loader.

07001 is an AMD API shim that is very small,so it can be used in place of require.js when all of your modules are built into one file using the RequireJS optimizer. The wrap build config option will put a function wrapper around the code,or you can provide your own wrapper if you need to do extra logic.

原文链接:https://www.f2er.com/js/157032.html

猜你在找的JavaScript相关文章