Udo G
Udo G

Reputation: 13121

Is Bower only about automatically installing dependencies?

Does Bower actually do anything else than resolve dependecies? I'm trying hard to understand how it is meant to be used, but I guess I'm missing some points...

Say, I have Bower package A, which depends on Bower package B. In my application I'm just interested in package A, since that's what I'm going to use. Of course, that means that somehow both packages must be loaded into the Browser, so that package A can work.

Using Bower I can just do bower install a and will then find both packages A & B in my bower_components. So far, awesome.

But now? Am I forced to find out myself (manually) which files from A and B need to be loaded in my HTML page? I don't think that the full bower_components directly shall be accessible via web, so I have to configure myself manually my Grunt (or whatever) build-file to copy the relevant files?

What am I missing here? If what I wrote above is true, what's the point using Bower when I still need to be aware of all implicit dependencies?

Upvotes: 1

Views: 804

Answers (3)

gerumato
gerumato

Reputation: 410

I'm a recent bower user myself. And as far as I know the short answer is: YES, bower is meant to download dependencies, however, apart from being able to configure the bower_components directory to anything you like, the idea is that bower installed components won't be edited by you at all, if you want to include them manually, you type

bower list --paths

and this will list all the files you need to include from the dependencies (in relative urls).

You can also use bower-installer (npm install -g bower-installer) which allows you to copy the files you need to any path you like. With a fine grained controll, or choose the minified versions, for example.

Here's an example output.

C:\Users\german\test>bower install bootstrap
bower bootstrap#*           not-cached git://github.com/twbs/bootstrap.git#*
bower bootstrap#*              resolve git://github.com/twbs/bootstrap.git#*
bower bootstrap#*             download https://github.com/twbs/bootstrap/archive/v3.3.4.tar.gz
bower bootstrap#*              extract archive.tar.gz
bower bootstrap#*             resolved git://github.com/twbs/bootstrap.git#3.3.4
bower jquery#>= 1.9.1       not-cached git://github.com/jquery/jquery.git#>= 1.9.1
bower jquery#>= 1.9.1          resolve git://github.com/jquery/jquery.git#>= 1.9.1
bower jquery#>= 1.9.1         download https://github.com/jquery/jquery/archive/2.1.4.tar.gz
bower jquery#>= 1.9.1          extract archive.tar.gz
bower jquery#>= 1.9.1         resolved git://github.com/jquery/jquery.git#2.1.4
bower bootstrap#~3.3.4         install bootstrap#3.3.4
bower jquery#>= 1.9.1          install jquery#2.1.4

bootstrap#3.3.4 bower_components\bootstrap
└── jquery#2.1.4

jquery#2.1.4 bower_components\jquery

C:\Users\german\test>bower list --paths

  jquery: 'bower_components/jquery/dist/jquery.js',
  bootstrap: [
    'bower_components/bootstrap/less/bootstrap.less',
    'bower_components/bootstrap/dist/css/bootstrap.css',
    'bower_components/bootstrap/dist/js/bootstrap.js',
    'bower_components/bootstrap/dist/fonts/glyphicons-halflings-regular.eot',
    'bower_components/bootstrap/dist/fonts/glyphicons-halflings-regular.svg',
    'bower_components/bootstrap/dist/fonts/glyphicons-halflings-regular.ttf',
    'bower_components/bootstrap/dist/fonts/glyphicons-halflings-regular.woff',
    'bower_components/bootstrap/dist/fonts/glyphicons-halflings-regular.woff2'
  ]

after

bower list --paths

bootstrap[] shows all the files I need to include according to bower_components/bootstrap/bower.json main:[] part

Hope this helps! cheers.

Upvotes: 0

staeke
staeke

Reputation: 311

You're actually not missing anything. Bower doesn't deal with loading your dependencies, just installing them. Loading them is something you have to do on your own. Also, there are a lot various ways in which people load there dependencies; the most common probably being Require.JS, Browserify (have too few credits to post links) and plain script includes in an index.html page. So, basically you have a few options here

  1. You can just deal with load registrations manually. This would mean adding <script src="..."></script> tags to your index.html page, or adding registrations for dependencies and similar to your app.js if you're using Require.JS. Note that this step would mean that you'd manually have to look at each dependency, read documentation or bower.json files to figure out transitive dependencies and file paths.
  2. If you're using plain script includes, you can use Wiredep to have that done automatically for you through Wiredep's inspection of the bower.json files of dependencies.
  3. If you're using RequireJS (or similar) you can look at Yeoman's grunt-require-js to do this automatically for you.

Note that both 2 and 3 relies on library authors provide the correct output files. You might e.g. have to declare overrides or explicitly declare if you want minified or non-minified versions.

As for publicly allowing access to "bower_components", I find that this is the most common approach. What things there would you like to prevent access to?

Upvotes: 0

Tall Sam
Tall Sam

Reputation: 53

Bower manages dependencies, and it will add the correct files into your HTML if you use it with the --save (or -S) flag. You would need appPath set in your bower.json if your index.html isn't in the same directory.

$ bower help install

Usage:

    bower install [<options>]
    bower install <endpoint> [<endpoint> ..] [<options>]
Options:

    -F, --force-latest      Force latest version on conflict
    -h, --help              Show this help message
    -p, --production        Do not install project devDependencies
    -S, --save              Save installed packages into the project's bower.json dependencies
    -D, --save-dev          Save installed packages into the project's bower.json devDependencies
    Additionally all global options listed in 'bower help' are available

Description:

    Installs the project dependencies or a specific set of endpoints.
    Endpoints can have multiple forms:
    - <source>
    - <source>#<target>
    - <name>=<source>#<target>

    Where:
    - <source> is a package URL, physical location or registry name
    - <target> is a valid range, commit, branch, etc.
    - <name> is the name it should have locally.
```

Upvotes: 0

Related Questions