Bower override dependency
Asked Answered
A

1

6

I have an application, written in Backbone with Marionette and some other dependencies, managed through 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"
  }
}

I want to add Joint.js(http://www.jointjs.com/), which depends on lodash (a replacement for underscore), but I can't figure out how to replace this in my configuration, since Marionette, Backbone, and some other libraries depend on underscore directly. So on the load underscore overrides lodash, and application can't start correctly.

Aston answered 15/6, 2014 at 15:30 Comment(3)
Why don't you let lodash overwrite underscore? Their API should be compatible.Brilliance
This is what I expected, would happen, but underscore overrode lodash at the end.Aston
I've changed the order, and put lodash as the latest dependency, and it worked. Thanks for a hint:) I'll leave this hopping there is a cleaner way to do it, otherwise I'm loading both libraries on the clientAston
A
4

I've changed the order, and put lodash as the latest dependency, and it worked.

Also as a solution there is an option to have a bower hook, like it states in following answer https://mcmap.net/q/350075/-permanently-ignore-a-dependency-with-bower

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.

Aston answered 2/7, 2014 at 7:18 Comment(1)
Note that from version 1.4. of Bower you should be able to add a ignoreDependencies array to your bower.jsonSirree

© 2022 - 2024 — McMap. All rights reserved.