Project

npm-rails

0.03
No commit activity in last 3 years
No release in over 3 years
Use NPM packages in you Ruby on Rails application
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

>= 3.2
 Project Readme

npm-rails

Gem Version Code Climate Dependency Status Build Status Coverage Status

NPM support for Rails projects. It let you use Bundler-like DSL and rake tasks for including npm packages. This gem based on Browserify for bundling packages and resolve dependencies.

Installation

Add this line to your application's Gemfile:

gem 'npm-rails'

Then run:

rails g npm_rails:initialize

And require npm-dependencies.js:

//=require npm-dependencies

Usage

  1. Add a package to npm_packages file
  2. Run rake npm:install
  3. Use the package in your javascript code by calling the camelize name or build_name if you set it

Example npm_packages file

# call 'React' in your js code to use it
npm 'react'

# Set version
npm 'redux', '3.3.1'

# Set build_name to a package.
# Call '_' to get Underscore
npm 'underscore', build_name: '_'

# You can add a package for development
npm 'jasmine', development: true

# Or in block
development do
  npm 'jasmine'
end

# Install a package but do not require it
npm 'browserify', require: false

Configuration Options

The following options are available for configuration in your application or environment-level config files (config/application.rb, config/environments/development.rb, etc.):

Configuration Option Description
config.npm.package_file Specificies a package file. Default value: npm_packages
config.npm.output_file Specifies a file where to bundle npm packages. Default value for production: vendor/assets/javascripts/npm-rails/production/npm-dependencies.js. Default value for other environments: vendor/assets/javascripts/npm-rails/development/npm-dependencies.js
config.npm.browserify_options Sets options for browserify command. See all available options in Browserify documentation
config.npm.run_before_assets_precompile If set to true then run rake npm:install before assets precompilation. Default value: false

How it works

The generator creates npm_packages file. This file contains a list of packages. Rake uses NPM to install the packages and Browserify to bundle them. Browserify output the bundled results to output_file(see configuration options), which are then loaded by sprockets. All packages attached to window by build_name, which by default is the camelize package name.