简体   繁体   English

Ruby on Rails引擎gem资产管道对节点/ NPM的依赖性

[英]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. 我正在构建打包在gem内的Ruby on Rails引擎,无法弄清楚如何确保加载了NPM依赖项。

In a regular Rails application you can install NPM, then use the npm install command to put packages in the node_modules base directory. 在常规的Rails应用程序中,您可以安装NPM,然后使用npm install命令将软件包放入node_modules基本目录中。 Then add node_modules to the asset pipeline with this line this in your application.rb : 然后在您的application.rb使用以下这一行将node_modules添加到资产管道:

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

However, in my case I'm building a Rails engine to be loaded as a gem. 但是,就我而言,我正在构建一个Rails引擎 ,将其作为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. .gemspec文件使您的gem可以将其他Ruby依赖项加载到主机应用程序中,但是我不知道如何对Node依赖项执行相同的操作。 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? 在我的引擎中注意到需要某些NPM模块才能将其安装在主机应用程序中的正确方法是什么?

Have you seen npm-pipeline-rails ? 您看过npm-pipeline-rails吗?

From the documentation: 从文档中:

npm-pipeline-rails allows you to hook certain commands, usually npm scripts, during the Rails app lifecycle. npm-pipeline-rails允许您在Rails应用程序生命周期中挂钩某些命令,通常是npm脚本。 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. 假设您的工具会将普通的JS和CSS文件构建到供应商/资产中,从而允许Rails的资产管道对其进行提取。

It does not replace the Rails asset pipeline, but rather it works with it. 它不会替代Rails资产管道,而是可以使用它。 The files you build with your npm pipeline will be available as regular files in the Rails asset pipeline. 使用npm管道构建的文件将在Rails资产管道中作为常规文件提供。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM