创建一个支持browserify和jQuery的包< script>标签加载 [英] Creating a bundle supporting browserify and jQuery <script> tag loading
问题描述
我正在使用 JavaScript库(bitcoin-prices.js),我想使用browserify维护。该库依赖于jQuery。我不想强迫库用户迁移到browserify,但保留drop one file + script tag集成给那些没有从JavaScript社区喝过最新koolaid的人。
I am working on a JavaScript library (bitcoin-prices.js) which I'd like to maintain using browserify. The library depends on jQuery. I would not like to force the library users move to browserify, but retain "drop one file + script tag" integration for those who have not drank the latest koolaid from JavaScript community.
我的问题是如何使用browserify创建一个bundle.js,以便
My question is how to create a bundle.js with browserify, so that
-
启用浏览器的用户可以安装包来自NPM,具有适当的jQuery依赖性
browserify-enabled users can install the package from NPM, having proper jQuery dependency
传统用户仍然可以使用< script>
标记删除在bundle中,将他们的jQuery加载到另一个< script>
,并且代码仍然正常
Traditional users can as still use <script>
tag drop in bundle, loading their jQuery wherever they want to with another <script>
and the code still works fine
我想在 $ = require('jquery.js')
?
推荐答案
惯例就是这样做(没有.js):
The convention is to do like this (without the .js):
var $ = require('jquery')
如果你是使用grunt-browserify( https://github.com/jmreidy/grunt-browserify ),
If you are using grunt-browserify (https://github.com/jmreidy/grunt-browserify),
以这种方式编译您的库:
Compile your library in this way:
your_task:{
src:'{{your library's js file}}',
dest:'{{your library's browserified file}}',
options: {
external: ["jquery"]
}
},
在主页上,序列将如下所示:
At the home page, the sequence will then be like this:
<header>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="{{your library's browserified file}}"></script>
</header>
希望这会有所帮助
这篇关于创建一个支持browserify和jQuery的包< script>标签加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!