问题描述
我一直在使用RequireJS进行依赖项管理,必须说我爱它-JavaScript最近真的很成熟。
I've been using RequireJS for dependency management and must say that I love it - JavaScript has really matured recently.
但是,我不知道的一件事是这样:当使用优化程序将我所有的JavaScript模块捆绑到一个文件中时,如何保持从外部CDN URL加载一些第三方脚本(例如jquery),而不必将其与应用程序代码捆绑在一起?
However, one thing I cannot figure out is this: When using the optimizer to bundle all my JavaScript modules into one file, how can I keep loading some thirt party scripts (such as jquery) from an external CDN URL instead of having to bundle it with my application code?
推荐答案
这将从CDN加载jQuery:
This will load jQuery from a CDN:
<script src="http://requirejs.org/docs/release/2.1.5/comments/require.js"></script>
<script type="text/javascript">
require.config({
paths: {
"jquery": "https://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min"
},
waitSeconds: 40
});
</script>
<div id="message">hello</div>
<script type="text/javascript">
require( ["jquery"],
function ($) {
alert($.fn.jquery + "\n" + $("#message").text());
}
);
</script>
。
requirejs文档的显示了如何从优化的构建中消除路径。基本上使用 empty:
方案。此处摘录:
This page from the requirejs docs shows how to eliminate a path from the optimised build. Basically use the empty:
scheme. Excerpt here:
node ../../r.js -o name=main out=main-built.js baseUrl=. paths.jquery=empty:
这篇关于使用RequireJS时如何从CDN加载第三方JavaScript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!