用browserify-shim填充依赖项的依赖项 [英] Shimming dependencies of dependencies with browserify-shim

查看:111
本文介绍了用browserify-shim填充依赖项的依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过使用browserify-shim将某些模块从捆绑中移出来重构使用Browserify的库。具体来说,该库使用require( codemirror),但我想提供一个不包含CodeMirror的包,而是使用通过CDN提供的包。

I'm trying to refactor a library that uses Browserify by shimming certain modules out of the bundle using browserify-shim. Specifically, the library uses require("codemirror") but I want to provide a bundle that doesn't include CodeMirror but will rather use one that is provided via CDN.

所以我在package.json中有了browserify-shim配置,例如

So I've got browserify-shim config in my package.json like

  "browserify-shim": {
    "jquery": "global:jQuery",
    "codemirror": "global:CodeMirror"
  }

到目前为止很好。 $(

)require('jquery')和require('codemirror')已从浏览器打包中消失,并已替换为预期的代码片段,以将jQuery和CodeMirror移出窗口对象。

So far so good. require('jquery') and require('codemirror') have disappeared from the browserified bundle and been replaced by the expected code snippet to grab jQuery and CodeMirror off of the window object.

该库还需要一些CodeMirror加载项。例如require('codemirror / addon / hint / show-hint.js')。没关系。我希望捆绑该附加组件。但是,在此附件中是一个包含require( ../../ lib / codemirror)的UMD包装器。因此,Browserify看到了这一点,并从/node_modules/codemirror/lib/codemirror.js绑定了CodeMirror。我希望它使用在codemirror shim中定义的window.CodeMirror代替,但是无法弄清楚。尝试了许多变体,包括以下内容:

The library also requires some CodeMirror add-ons. For example require('codemirror/addon/hint/show-hint.js'). That's fine. I want that add-on bundled. However, within this add-on is a UMD wrapper that includes require("../../lib/codemirror"). Browserify is seeing this and is bundling the CodeMirror from /node_modules/codemirror/lib/codemirror.js because of this (I think). I want this to use window.CodeMirror as defined in the codemirror shim instead, but cannot figure it out. Have tried many variations including the following:

  "browserify-shim": {
    "jquery": "global:jQuery",
    "codemirror": "global:CodeMirror",
    "../../lib/codemirror": "global:CodeMirror",
    "codemirror/addon/hint/show-hint.js": { 
      "exports":null,
      "depends":["../../lib/codemirror:CodeMirror"]
    }
  }

那个require( ../../ lib / codemirror)不会消失!我确定我会丢失一些东西。

That require("../../lib/codemirror") will not go away! I'm sure I'm missing something.

我正在使用Gulp脚本运行此脚本,但我认为这没有什么区别。 Browserify版本3.38.1。 Browserify-shim版本3.7.0。

I'm running this from a Gulp script, but I don't think that should make any difference. Browserify version 3.38.1. Browserify-shim version 3.7.0.

有什么想法吗?

推荐答案

如果在 browserify-shim 中添加 {global:true} ,则应将其应用于依赖项的依赖项(

If you add browserify-shim with {global: true}, it should be applied to your dependencies' dependencies (and so on) as well, which should hopefully do what you want.

假定您在Gulpfile中使用原始的browserify,而不是:

Assuming you're using raw browserify in your Gulpfile, instead of:

b.transform('browserify-shim');

要做:

b.transform({global: true}, 'browserify-shim');

如果您使用的是 gulp-browserify ,我不确定是否可以指定全局转换。

If you're using gulp-browserify, I'm not sure whether there's any way to specify global transforms.

这篇关于用browserify-shim填充依赖项的依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆