Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node / NPM dependency with Ruby on Rails engine gem asset pipeline

I am building a Ruby on Rails engine packaged inside of a gem and can't figure out how to ensure that an NPM dependency is loaded.

In a regular Rails application you can install NPM, then use the npm install command to put packages in the node_modules base directory. Then add node_modules to the asset pipeline with this line this in your application.rb:

config.assets.paths << Rails.root.join('node_modules')

However, in my case I'm building a Rails engine to be loaded as a gem. The .gemspec file lets your gem load other Ruby dependencies into the host application, but I don't know how to do the same for Node dependendies. What's the proper way to note in my engine that it requires certain NPM modules to work so that they get installed in the host application?

like image 752
Winston Kotzan Avatar asked May 24 '18 14:05

Winston Kotzan


People also ask

How do I fix dependency issues in NPM?

The easy fix is to use the npm audit --fix which will look for updates that can be updated to fix those automatically.

What is asset pipeline in ruby on Rails?

1 What is the Asset Pipeline? The asset pipeline provides a framework to concatenate and minify or compress JavaScript and CSS assets. It also adds the ability to write these assets in other languages and pre-processors such as CoffeeScript, Sass, and ERB.

What is NPM in Ruby on Rails?

NPM is a package manager for Node based environments. NPM manages dependencies and store its data in file package. json . Yarn is a package manager that uses NPM registry as its backend.


1 Answers

Have you seen npm-pipeline-rails?

From the documentation:

npm-pipeline-rails allows you to hook certain commands, usually npm scripts, during the Rails app lifecycle. It assumes that your tool will build plain JS and CSS files into vendor/assets, allowing it to be picked up by Rails's asset pipeline.

It does not replace the Rails asset pipeline, but rather it works with it. The files you build with your npm pipeline will be available as regular files in the Rails asset pipeline.

There is also a sample application configuration:

Rails.application.configure do
  # Enables npm_pipeline_rails's invocation of `watch` commands. (v1.5.0+)
  # If `true`, watch commands will be ran alongside Rails's server.
  # Defaults to true in development.
  config.npm.enable_watch = Rails.env.development?

  # Command to install dependencies
  config.npm.install = ['npm install']

  # Command to build production assets
  config.npm.build = ['npm run build']

  # Command to start a file watcher
  config.npm.watch = ['npm run start']

  # The commands are arrays; you may add more commands as needed:
  config.npm.watch = [
    'npm run webpack:start',
    'npm run brunch:start'
  ]

  # If 'true', runs 'npm install' on 'rake assets:precompile'. (v1.6.0+)
  # If you disable this, you'll need to run `npm install` yourself.
  # This is generally desired, but you may set this to false when
  # deploying to Heroku to speed things up.
  config.npm.install_on_asset_precompile = true

  # If 'true', runs 'npm install' on 'rails server'. (v1.7.0+)
  # If you disable this, you'll need to run `npm install` yourself.
  config.npm.install_on_rails_server = true
end
like image 136
Christian Avatar answered Sep 28 '22 01:09

Christian