与Ruby on Rails引擎宝石资产管道的Node / NPM依赖关系

时间:2018-05-24 14:07:12

标签: ruby-on-rails node.js ruby npm rubygems

我正在构建一个打包在gem内部的Ruby on Rails引擎,并且无法弄清楚如何确保加载NPM依赖项。

在常规的Rails应用程序中,您可以安装NPM,然后使用npm install命令将包放在node_modules基目录中。然后将node_modules添加到资产管道中,并在application.rb

中添加此行
config.assets.paths << Rails.root.join('node_modules')

但是,在我的情况下,我建立一个 Rails引擎作为gem加载。 .gemspec文件允许您的gem将其他Ruby依赖项加载到宿主应用程序中,但我不知道如何为Node dependendies执行相同操作。在我的引擎中需要注意的是,它需要某些NPM模块才能正常工作,以便它们安装在主机应用程序中?

1 个答案:

答案 0 :(得分:4)

您见过npm-pipeline-rails吗?

从文档中:

  

npm-pipeline-rails允许您在Rails应用程序生命周期中挂钩某些命令,通常是npm脚本。假设您的工具会将普通的JS和CSS文件构建到供应商/资产中,并允许Rails的资产管道对其进行提取。

     

它不会替代Rails资产管道,而是可以使用它。使用npm管道构建的文件将在Rails资产管道中作为常规文件提供。

还有一个示例应用程序配置:

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