如何在Rails中导入尾风插件7
我正在尝试在 Rails 7 应用程序中使用 npm 包“tailwindcss-flip”。 软件包文档具有以下说明:
安装 tailwindcss-flip 软件包:
使用 NPM 安装
npm install tailwindcss-flip --save-dev
使用 Yarn 安装
纱线添加 tailwindcss-flip --dev
将插件添加到 tailwind.conf.js 文件中: plugins: [require("tailwindcss-flip")]
我的问题是,我确实将包固定在 importmap 中,但出现以下错误:
Error: Cannot find module 'tailwindcss-flip'
Any idea how this can work in Rails 7 (No Webpacker )。
I'm trying to use npm package 'tailwindcss-flip' in my Rails 7 app.
The package docs have the following instructions:
Install tailwindcss-flip package:
Install using NPM
npm install tailwindcss-flip --save-dev
Install using Yarn
yarn add tailwindcss-flip --dev
Add plugin to your tailwind.conf.js file:plugins: [require("tailwindcss-flip")]
My question is, I did pin the package in importmap, but I got the following error:
Error: Cannot find module 'tailwindcss-flip'
Any idea how this can work in Rails 7 (No Webpacker).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我假设您正在使用
tailwindcss-rails
GEM。即使您运行Rails New App -CSS Tailwind
,也是默认的。它使用独立的尾风可执行文件 https://tailwindcss.coms.com/blog/blog/standalone-cli 使用第一方插件。因此,@tailswindcss/*
中的任何插件都应开箱即用。要使用其他任何尾风插件,您必须运行完整的Node.js版本的tailwind。 Rails 7方法是使用
cssssbundling-rails
。将构建脚本添加到软件包。JSON
添加您喜欢的任何插件。在您的情况下:
将插件添加到尾风配置中。默认情况下,它是
tailwind.config.js
(独立tailwindcss-rails
版本使用config/tailwind.config.js
再)在您的布局中,您应该只有
application
样式表。删除stylesheet_link_tag“ tailwind”
要开始编译您的CSS,从
package.json.json
运行构建脚本此应该输出
app/assets/assets/builds/application.css 文件。它由Rails Asset Pipeline(链轮)提供。如果您获得链轮错误,请重新启动所有内容,清除缓存,检查
app/config/config/custest.js
include// = link_tree ../ builds
。您还应该拥有
bin/dev
脚本和procfile.dev
。运行:启动Rails Server和Tailwind。
I assume you're using
tailwindcss-rails
gem. That is the default even if you runrails new app --css tailwind
. It uses standalone tailwind executable https://tailwindcss.com/blog/standalone-cli which comes bundled with first party plugins. So any plugin in@tailswindcss/*
should work out of the box.To use any other tailwind plugins you must run a full node.js version of tailwind. The Rails 7 way is to use
cssbundling-rails
.Add build script to package.json
Add any plugin you like after that. In your case:
Add plugins to tailwind config. By default it is
tailwind.config.js
(standalonetailwindcss-rails
version usesconfig/tailwind.config.js
which you don't need anymore)In your layout you should only have
application
stylesheet. Removestylesheet_link_tag "tailwind"
To start compiling your css, run the build script from
package.json
This should output
app/assets/builds/application.css
file. It is served by rails asset pipeline (sprockets). In case you get sprocket errors, restart everything, clear cache, checkapp/assets/config/manifest.js
to include//= link_tree ../builds
.You should also have
bin/dev
script andProcfile.dev
. Run:to start rails server and tailwind.
这是我的工作设置,它使用尾风插件(TW-Elements),Rails 7,ImportMaps-尤其是使用 no nodejs,package.json,Esbuild/webpack等。
运行之后:
我在
config/importmap.rb
中:我从
config/tailwind.config.js
中引用这些内容:请注意:注意
../代码>在一个地方需要,但在另一个地方禁止!那是因为当我们调用
e节,我们的工作dir是时,
插件中需要rails_root/config
,但在content> content
中路径相对于rails_root
。不同插件的行为可能会略有不同,但这应该提供坚实的基础。
Here's my working setup with a Tailwind plugin (tw-elements), Rails 7, importmaps -- notably using NO nodejs, package.json, esbuild/webpack, etc.
After running:
I have in my
config/importmap.rb
:I reference these from
config/tailwind.config.js
thus:Note the
../
is required in one place, but forbidden in the other! That's because when we callrequire
in theplugins
section our working dir isRAILS_ROOT/config
, but incontent
the paths are relative toRAILS_ROOT
.Behavior may vary slightly for different plugins, but this should provide a solid foundation.
我将
tailwindcss-fluid-type
添加到我的 Rails 应用程序中,该应用程序通过下载插件来使用gem 'tailwindcss-rails'
vendor/javascript/tailwindcss-fluid-type.js
然后在
config/tailwind.config.js
中获取它,因为您可以通过运行
bin/ 更轻松地下载importmap 固定 tailwindcss-fluid-type
,但它也会将其固定在 importmap 中,您不需要它,因此从那里将其删除。I added
tailwindcss-fluid-type
to my rails app that usesgem 'tailwindcss-rails'
by downloading plugin tovendor/javascript/tailwindcss-fluid-type.js
and then sourcing it in
config/tailwind.config.js
as inyou can download easier by running
bin/importmap pin tailwindcss-fluid-type
, but it will also pin it in importmap, which you don't need, so remove it from there.如果您想使用 importmap,还有另一种选择。
1 - 固定所需的库
2 - 将以下代码添加到头部的布局文件中
3 - 只需在视图中使用
dir=rtl
4 - 利润! :)
There's another option if you want to use importmap.
1 - pin required libs
2 - add following code to your layout file in head section
3 - just use
dir=rtl
in your views4 - PROFIT!!! :)