[英]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
安装
Add gem 'local_time' to your Gemfile.
将 gem 'local_time' 添加到您的 Gemfile。
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.