gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 06:54:36 25 4
gpt4 key购买 nike

我正在构建一个打包在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com