[英]How to use jQuery with webpack before CDN link
Is it possible to load external jQuery from CDN when it needed like in RequireJS ? 是否有可能从CDN加载外部jQuery的时候,它需要像RequireJS ? I mean specify external jQuery source (on CDN) somewhere in config and then use it in CommonJS (or AMD) style:
我的意思是在config中的某处指定外部jQuery源(在CDN上),然后在CommonJS(或AMD)样式中使用它:
$ = require('jquery')
The problem is I have my custom script above the jQuery's CDN link and so I have ' can't find variable jQuery ' error. 问题是我的自定义脚本在jQuery的CDN链接之上 ,所以我' 找不到变量jQuery '错误。 Schematically my code looks like:
我的代码示意如下:
<script src="custom_code.js'>
// uncompiled source like:
define(['jquery', '../../node_modules/baconjs/dist/Bacon.js'], function($, bacon) {
console.log 'ok'
});
</script>
...
<script src="//yastatic.net/jquery/2.1.3/jquery.min.js"></script>
After reading this post , and with the use of require.ensure
I've managed to get it work. 阅读这篇文章后 ,使用
require.ensure
我已经设法让它工作。 custom_code.js: custom_code.js:
require.ensure(['jquery', '../../node_modules/baconjs/dist/Bacon.js'], function(require){
var $ = require 'jquery'
var bacon = require '../../node_modules/baconjs/dist/Bacon.js'
$('.content').prepend('it works!')
});
and in webpack config I've added 在webpack配置中我添加了
externals: { jquery: "jQuery" },
In this case webpack will make additional request for compiled bundle with Bacon.js. 在这种情况下,webpack将使用Bacon.js对编译的bundle进行额外请求。
Still I wonder what will be if client loads Bacon.js before jQuery will be loaded from CDN? 我仍然想知道如果客户端在从CDN加载jQuery 之前加载Bacon.js将会是什么?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.