2013-07-09 33 views
7

我已經編寫了一個使用requirejs處理依賴注入的JavaScript應用程序。我編了文件,但包括它當作爲一個腳本,我得到了明顯的錯誤:用requirejs編譯javascript程序以刪除require require依賴項

Uncaught ReferenceError: define is not defined

我想我的JavaScript的應用程序不依賴於AMD的裝載機如果開發商決定不使用一個。但是,由於應用程序的複雜性,我想用它來處理我的應用程序的依賴關係。

是否有編譯器可用編譯JavaScript來刪除AMD依賴?我已經看到了一些關於Grunt的嗡嗡聲,但是還沒有找到關於這是否是Grunt特徵的直接答案。

回答

5

您不能完全刪除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.

almond 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.