Bower覆盖依赖

6

我有一个应用程序,使用Backbone和Marionette等其他依赖项编写,通过bower进行管理:

{
  "name": "admin",
  "version": "0.1.1",
  "main": "public/javascripts/app.js",
  "dependencies": {
    "lodash": "~2.4.1",
    "console-polyfill": "~0.1.0",
    "jquery": "~2.1.1",
    "normalize-css": "~2.1.2",
    "marionette": "~1.7.4",
    "bootstrap": "~3.1.1",
    "font-awesome": "~4.1.0",
    "backbone-pageable": "~1.4.5",
    "moment": "~2.5.1",
    "swag": "~0.6.1",
    "jquery-form": "~3.46.0",
    "jquery-file-upload": "~9.5.7",
    "underscore.string": "~2.3.3",
    "bootstrap-switch": "~3.0.1",
    "joint": "~0.9.0"
  },
  "overrides": {
    "backbone": {
      "dependencies": {
        "lodash": "*",
        "jquery": "*"
      },
      "main": "backbone.js"
    },
    "backbone.wreqr": {
      "dependencies": {
        "backbone": "*"
      },
      "main": "lib/amd/backbone.wreqr.js"
    },
    "backbone-pageable": {
      "dependencies": {
        "backbone": "*"
      },
      "main": "lib/backbone-pageable.js"
    },
    "jquery-file-upload": {
      "dependencies": {
        "jquery": "*"
      },
      "main": [
        "js/vendor/jquery.ui.widget.js",
        "js/jquery.iframe-transport.js",
        "js/jquery.fileupload.js"
      ]
    },
    "underscore.string": {
      "dependencies": {
        "lodash": "*"
      },
      "main": "lib/underscore.string.js"
    },
    "joint": {
      "dependencies": {
        "lodash": "*"
      },
      "main": "dist/joint.clean.js"
    }
  },
  "resolutions": {
    "jquery": "~2.1.1"
  }
}

我想要添加Joint.jshttp://www.jointjs.com/),它依赖于lodash(一个替代underscore的库),但我无法弄清楚如何在我的配置中替换它,因为Marionette、Backbone和其他一些库直接依赖于underscore。因此,在加载时,underscore会覆盖lodash,导致应用程序无法正确启动。


为什么不让 lodash 覆盖 underscore?它们的 API 应该是兼容的。 - Bergi
这正是我所期望的,但在最后划线覆盖了lodash。 - mavarazy
我已经改变了顺序,将lodash作为最新的依赖项,并且它起作用了。感谢你的提示:) 我会留下这个,希望有更清晰的方法来解决它,否则我将在客户端加载两个库。 - mavarazy
1个回答

4
我已经更改了顺序,将lodash作为最新的依赖项,并且它起作用了。
此外,还有一个解决方案是使用bower hook,就像以下答案中所述: https://dev59.com/cWEi5IYBdhLWcg3wyO44#23289270

We had a similar situation where we had Backbone depend on Underscore in its bower.json, but we're using Lo-Dash in its stead, so Bower was unnecessarily pulling down Underscore for each install. We have automated checks for 3rd party license compliance, so we didn't want anything we don't actually use.

I realize this isn't exactly what they're meant for, but Bower's install-hooks can be used to clean unneeded deps post-install (at least until Bower gets the sort of "no thanks" resolution you hinted at). In your .bowerrc:

{
    "directory": "app/bower_components",
    "scripts": {
        "postinstall": "rm -rf app/bower_components/underscore"
    }
}

It's a bit of a hack, but works.


2
请注意,从 Bower 的 1.4 版本开始,您应该能够向您的 bower.json 添加一个 ignoreDependencies 数组。 - gotofritz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接