简体   繁体   English

防止将jQuery捆绑为Browserify的依赖项

[英]Prevent bundling jQuery as a dependency with Browserify

So I have been searching all over the internet to try to find a solution to this problem but I cannot find a solution that works. 因此,我一直在Internet上进行搜索以尝试找到针对此问题的解决方案,但找不到有效的解决方案。 I'm currently using the latest version of Gulp and Browserify to bundle up JS for the website I'm working on. 我目前正在使用最新版本的Gulp和Browserify将JS捆绑到我正在工作的网站上。 We previously would concatenate all the JS files together, but I'm moving to a module setup now. 以前我们将所有JS文件连接在一起,但是现在我要转向模块设置。

The problem I am running into is duplicating certain dependencies, in this example, I'll focus on jQuery (v2.1.4). 我遇到的问题是重复某些依赖项,在此示例中,我将重点介绍jQuery(v2.1.4)。 Here is my setup: 这是我的设置:

main.js (Loaded on every page) main.js (在每个页面上加载)

window.jQuery = window.$ = require('jquery');
window.Vue = require('vue');    
require('jquery-validation');

// More JS that loads on all pages

page.js (Each page has it's own js file for scripts relating to that page) page.js (每个页面都有自己的js文件,用于与该页面相关的脚本)

require('remodal'); // This requires jQuery

// Rest of the JS for this page...

The problem I am running into is that now jQuery is in both javascript bundles. 我遇到的问题是现在jQuery在两个javascript捆绑包中。 With Browserify, I marked jQuery as "external" for page-speicific.js which removed jQuery from the script, but I get an error Uncaught Error: Cannot find module 'jquery' and I cannot seem to find a solution to this. 使用Browserify,我将jQuery标记为page-speicific.js speicific.js的“外部”,这从脚本中删除了jQuery,但出现错误Uncaught Error: Cannot find module 'jquery'而且我似乎也找不到解决方案。

If I "exclude" jQuery with Browserify, or if I put a try block around the require('remodal') , I end up with Uncaught TypeError: $(...).remodal is not a function instead. 如果我用Browserify“排除”了jQuery,或者如果我在require('remodal')周围放置了try块,我最终会require('remodal') Uncaught TypeError: $(...).remodal is not a function I'm guessing since the module remodal requires jQuery and it's not loaded there, it's not seeing it's set to the window and that's why execution fails? 我在猜测,因为模块remodalremodal需要jQuery,并且未在其中加载,也没有看到将其设置为窗口,这就是执行失败的原因?

Well, found the answer to my question. 好吧,找到了我问题的答案。 Guess a night of rest was all I needed to be able to think clearer to search for an answer. 猜猜我需要一整夜的休息,以便能够更清晰地思考以找到答案。

I checked out browserify-shim (and browserify-global-shim ) at some point, but found that it would only shim top-level dependencies. 我在某个时候签出了browserify-shim (和browserify-global-shim ),但是发现它只会对顶级依赖项进行填充。 If jQuery was a dependency of a dependency, this would not work. 如果jQuery是依赖关系的依赖关系,则将无法正常工作。 Well, once I found the answer linked below, I discovered that theres an undocumented (at least, I never found it) { global: true } you can set to have the shim propagate to all dependencies. 好吧,一旦找到下面链接的答案,我发现这里有一个未记录的文件(至少我从未找到过) { global: true }您可以设置为使垫片传播到所有依赖项。

var b = browserify();
var globalShim = require('browserify-global-shim').configure({
    'jquery': '$'
});
b.transform({ global: true }, globalShim);

After running gulp, all of my page-specific scripts now referenced jQuery as a window variable. 运行gulp之后,我所有特定于页面的脚本现在都将jQuery引用为窗口变量。

!(function(root, factory) {
  if (typeof define === 'function' && define.amd) {
    define(['jquery'], function($) {
      return factory(root, $);
    });
  } else if (typeof exports === 'object') {
    factory(root, (window.$)); // <----------------- :D
  } else {
    factory(root, root.jQuery || root.Zepto);
  }
})(this, function(global, $) {

Source: Shimming dependencies of dependencies with browserify-shim 来源:使用browserify-shim调整依赖项的依赖项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM