hsume2 / browserify-rails

Deprecated https://github.com/browserify-rails/browserify-rails
MIT License
57 stars 11 forks source link

MOVED

Further development of browserify-rails is now a community effort in the browserify-rails organization repo. Please file issues at and send pull requests to the new repository.

browserify-rails

Build Status

This library adds CommonJS module support to Sprockets (via Browserify).

It let's you mix and match //= require directives and require() calls for including plain javascript files as well as modules.

  1. Manage JS modules with npm
  2. Serve assets with Sprockets
  3. Require modules with require() (without separate //= require directives)
  4. Only build required modules
  5. Require npm modules in your Rails assets

Getting Started

Add this line to your application's Gemfile:

gem "browserify-rails", "~> 0.3"

Create package.json in your Rails root:

{
  "name": "something",
  "devDependencies" : {
    "browserify": "~> 4.1"
  },
  "license": "MIT",
  "engines": {
    "node": ">= 0.10"
  }
}

Then run:

npm install

Then start writing CommonJS, and everything will magically work!:

// foo.js
module.exports = function (n) { return n * 11 }

// application.js
var foo = require('./foo');
console.log(foo(12));

CoffeeScript

For CoffeeScript support, make sure to follow the standard rails .js.coffee naming convention. You'll also need to do the following:

Add coffeify as a dependency within package.json:

{
  // ...
  "devDependencies" : {
    // ...
    "coffeeify": "~> 0.6"
  }
}

Add the following command line options within application.rb:

config.browserify_rails.commandline_options = "-t coffeeify --extension=\".js.coffee\""

Configuration

You can configure different options of browserify-rails by adding one of lines mentioned below into your config/application.rb or your environment file (config/environments/*.rb):

class My::Application < Rails::Application
  # Paths, that should be browserified. We browserify everything, that
  # matches (===) one of the paths. So you will most likely put lambdas
  # regexes in here.
  #
  # By default only files in /app and /node_modules are browserified,
  # vendor stuff is normally not made for browserification and may stop
  # working.
  config.browserify_rails.paths << /vendor\/assets\/javascripts\/module.js/

  # Environments, in which to generate source maps
  #
  # The default is `["development"]`.
  config.browserify_rails.source_map_environments << "production"

  # Command line options used when running browserify
  #
  # can be provided as an array:
  config.browserify_rails.commandline_options = ["-t browserify-shim", "--fast"]

  # or as a string:
  config.browserify_rails.commandline_options = "-t browserify-shim --fast"

Contributing

Pull requests appreciated.

Contributors