简体   繁体   English

如何使用 Webpacker 而不是资产管道集成 gem? [导轨 6]

[英]How to integrate gem using Webpacker instead of the asset pipeline? [Rails 6]

I want to use the local_time gem, but as per its installation instructions it should be included in the asset pipeline:我想使用local_time gem,但根据它的安装说明,它应该包含在资产管道中:

Installation安装

  1. Add gem 'local_time' to your Gemfile.将 gem 'local_time' 添加到您的 Gemfile。

  2. Include local-time.js in your application's JavaScript bundle.在应用程序的 JavaScript 包中包含 local-time.js。

Using the asset pipeline:使用资产管道:

//= require local-time

However, I'm using Webpacker and I can't figure out how to integrate the gem with it.但是,我正在使用 Webpacker,但我不知道如何将 gem 与它集成。 Where should I include the line above?我应该在哪里包含上面的行? Or is it another strategy entirely?还是完全是另一种策略?

I think I got it working.我想我成功了。 I took a hint from the instructions for the local-time npm package mentioned in the link and added this to app\javascript\packs\application.js , before the other requires :我从链接中提到的local-time npm package的说明中得到了提示,并将其添加到app\javascript\packs\application.js ,在其他requires之前:

require("local-time").start()

1 Solution: install the rails-erb-loader, useful for embedding Ruby files in Javascript. 1 解决方法:安装rails-erb-loader,用于在Javascript中嵌入Ruby文件。

$ rails webpacker:install:erb $ rails webpacker:安装:erb

// packs/application.js // // 包/application.js //

Then if you have a gem that provides style sheets, you can import them as follows然后如果你有一个提供样式表的 gem,你可以按如下方式导入它们

import "<%= File.join(Gem.loaded_specs['yourgem'].full_gem_path, 'app', 'assets', 'stylesheets', 'yourfile.css') %>";导入“<%= File.join(Gem.loaded_specs['yourgem'].full_gem_path, 'app', 'assets', 'stylesheets', 'yourfile.css') %>";

For Ruby gems with Javascript, how to import them can vary, but for many, it will be as simple as importing the files from the gem.对于 Ruby gem 和 Javascript,如何导入它们可能会有所不同,但对于许多人来说,它就像从 gem 中导入文件一样简单。

import "<%= File.join(Gem.loaded_specs['yourgem'].full_gem_path, 'app', 'assets', 'javascripts', 'yourfile') %>";导入“<%= File.join(Gem.loaded_specs['yourgem'].full_gem_path, 'app', 'assets', 'javascripts', 'yourfile') %>";

暂无
暂无

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

相关问题 如何将Karma与Rails资产管道集成? - How to integrate Karma with Rails asset pipeline? 使用Webpacker和Heroku在Rails资产中获取错误:管道 - Getting errors in Rails asset:pipeline with Webpacker and Heroku 如何仅在Rails中使用相对路径,而不使用资产管道? - How to only use relative paths in Rails, instead of using the asset pipeline? Docker 中的 Webpacker 和 Rails 资产管道 - 视图未指向更新的 JS 资产 - Webpacker & Rails Asset Pipeline in Docker - View is not pointing to updated JS asset Rails 6 - 如何使用旧的 sprocket 资产管道而不是 webpacker? - Rails 6 - How to use the old sprocket assets pipeline instead of webpacker? 如何在Rails 3.2.15中使用sass-rails gem和Asset Pipeline包含SASS文件? 不支持该操作? - How to include SASS files using sass-rails gem with the Asset Pipeline in Rails 3.2.15? Operation not supported? 如何使用 webpacker gem、Rails 5.1 rc1 获取 javascript 资产包的绝对 url - How to get absolute url to javascript asset pack with webpacker gem, Rails 5.1 rc1 使用Rails 3.1 Asset Pipeline在web中存储webfonts - Storing webfonts in a gem using the Rails 3.1 Asset Pipeline 从 rails 5.1.7 中的资产管道迁移后,无法让 javascript 使用 webpacker 工作 - Can't make it to get javascript to work using webpacker after migrating from the asset pipeline in rails 5.1.7 如何在Rails 3.1(gem + Asset Pipeline)中配置CKEditor - How to configure CKEditor in Rails 3.1 (gem + Asset Pipeline)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM