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
windowor 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.