ruby-on-rails - Node/NPM 依赖于 Ruby on Rails 引擎 gem Assets 管道

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

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

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

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

但是,就我而言,我正在构建一个 Rails 引擎 以作为 gem 加载。 .gemspec 文件允许您的 gem 将其他 Ruby 依赖项加载到主机应用程序中,但我不知道如何对 Node 依赖项执行相同的操作。

在我的引擎中注意它需要某些 NPM 模块才能工作以便它们安装在主机应用程序中的正确方法是什么?

最佳答案

你见过npm-pipeline-rails吗? ?

来自文档:

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.

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

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

关于ruby-on-rails - Node/NPM 依赖于 Ruby on Rails 引擎 gem Assets 管道,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50511450/

相关文章:

javascript - 带有 CKEditor 的 Rails 4 - 插件(codesnipet)不起作用

javascript - 迭代nodejs字符串行

javascript - 将字符串转换为 JSON node.js

ruby-on-rails - 事件管理员 : How to customize the main menu (at the top of the page)?

ruby - ruby 中的 ActiveSupport#try 等实用方法,如果方法未定义则返回默认值

ruby-on-rails - 在本地生产中运行 Rails 服务器(InvalidMessage 错误)

ruby-on-rails - attr_accessor 不更新值

ruby-on-rails - Capybara 测试页面是否有正则表达式

javascript - 配方/ Material list MySQL 查询

ruby - heroku:找不到命令