Giter VIP home page Giter VIP logo

bower-resolve's Introduction

bower-resolve

Find the relative path name of a bower module, for use with browserify and debowerify.

build status

Installation

This module is installed via npm:

$ npm install bower-resolve

Example Usage

First, install some bower modules:

$ bower install js-base64

Then to resolve the path of the main javascript file for a given bower module name:

var bowerResolve = require('bower-resolve');

//Option 1, using a port of bower's internal resolve algorithm
bowerResolve.fastRead('js-base64', function(modulePath){
    //equals %cwd%/bower_components/js-base64/base64.js 
})

//Option 2, using module list provided by bower's programmatic API 
bowerResolve.init(function () {
  bowerResolve('js-base64')
  // returns %cwd%/bower_components/js-base64/base64.js'
});

Usage with browserify/debowerify

This is useful for generating stand-alone libraries with browserify with the debowerify transform:

Let's say you have installed angularjs and jquery using bower:

$ bower install jquery
$ bower install angular

You can build out a common library that will contain the jquery and angular libraries into a file called common.js like so:

// build out angular and jquery to a library file called common.js
var fs = require('fs');
var browserify = require('browserify');
var bowerResolve = require('bower-resolve');
bowerResolve.init(function () {
  var b = browserify();
  b.require(bowerResolve('angular'), { expose: 'angular' });
  b.require(bowerResolve('jquery'), { expose: 'jquery' });
  b.bundle().pipe(fs.createWriteStream('./common.js'));
});

Similarly, if you have some other client-side code that you want to rely on this common code, you can use bowerResolve to help you there, by declaring them as external dependencies:

// app.js
var angular = require('angular');
var jQuery = require('jquery');

jQuery(function ($) {
  console.log(angular);
});

Then build it out using browserify and debowerify as so:

// build out app.js and use the angular and jquery libs from common.js
var fs = require('fs');
var browserify = require('browserify');
var bowerResolve = require('bower-resolve');
bowerResolve.init(function () {
  var b = browserify(['./app.js']);
  b.external(bowerResolve('angular'));
  b.external(bowerResolve('jquery'));
  b.transform('debowerify');
  b.bundle().pipe(fs.createWriteStream('./bundle.js'));
});

Then you'll have common.js that will have your shared code, and bundle.js will have your client code.

Then you can use them together from your HTML app as follows:

<!DOCTYPE html>
<html>
  <head>
    <title>Hello, bower</title>
  </head>
  <body>
    <script type="text/javascript" src="common.js"></script>
    <script type="text/javascript" src="bundle.js"></script>
  </body>
</html>

Disable version checking

Bower module is doing check for new versions of installed components when bowerResolve.init is called. You might want to prevent this behavior. Check is made using Git client, which needs to be installed and available system wide (that might be an issue on Windows). Otherwise whole process will just fail without much useful error message. To disable version checking, use the following:

var bowerResolve = require('bower-resolve');
bowerResolve.offline = true;
bowerResolve.init(function() {....}};

bower-resolve's People

Contributors

danielkcz avatar eugeneware avatar rsboarder avatar scottmas avatar ubolonton avatar wilsaj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

bower-resolve's Issues

Missing support for bower.json "overrides"

Bower let me set up overrides for managed bower components by adding an "overrides" key in the bower.json module.

Unfortunately requiring such "overridden" modules from browserify doesn't work because it looks for the wrong main and dependencies.

I'm not sure, but I guess the cause is that bower-resolve try to mimic bower in bower.json parsing, but doesn't support all of the features (so it doesn't support overrides).

Include css files

I find this very useful for automatically resolving and requiring bower js files with browserify, however It would be nice to be able to resolve css files as well :) Any thoughts on this?

Init method is inconvenient

Isn't there any other way to "init" this package? Wrapping everything into the init callback makes using this plugin with Gulp almost impossible or at least very cumbersome...

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.