No commit activity in last 3 years
No release in over 3 years
Browserify packages node modules for the browser. This gem makes it easy to integrate this into the Rails asset pipeline or other Sprockets chains.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

~> 2.1
 Project Readme

Sprockets-Browserify¶ ↑

Serve npm (CommonJS) modules through Sprockets with the help of Browserify.

Requirements¶ ↑

Node.js, obviously. This gem calls node directly, no ExecJS involved.

How to use¶ ↑

Add this to your Gemfile:

gem 'sprockets-browserify'

Place a CommonJS Module in your assets directory and perform a npm install:

/app/assets/javascript/foo
  - index.js
  - package.json
  - node_modules/
    - ...

Require the module within your application.js or somewhere else:

//
//= require foo/index
//

Done.

Your node module should fulfill the following requirements:

  • The main file for the module must reside in the same directory as the package.json, in the root of the module

  • The main file should expose its API in some way to the browser environment, by setting propertes on window or sth. like that

If you want to use a module that doesn’t conform, consider creating a wrapper module in you assets directory that requires the module you actually want to use:

foo_wrapper/package.json:

...
"dependencies": {
  "foo": ...
},
...

foo_wrapper/index.js:

window.Foo = require('foo');

foo_wrapper/node_modules/foo/index.js:

module.exports = "Foo";

WARNING:¶ ↑

By default, the asset pipeline in Rails precompiles every file with a .js extension. This is probably not what you want if you have a bunch of modules in the node_modules directory. Disable this behavior by specifically listing only individual files you want to precompile:

config.assets.precompile = ['main.js', 'stylesheet.css']

instead of merely appending to

config.assets.precompile << 'main.js'

License¶ ↑

Released under the MIT License. See the MIT-LICENSE file for further details.