如何在Rails中导入尾风插件7

发布于 2025-01-17 15:07:41 字数 583 浏览 4 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

是伱的 2025-01-24 15:07:41

我假设您正在使用tailwindcss-rails GEM。即使您运行Rails New App -CSS Tailwind,也是默认的。它使用独立的尾风可执行文件 https://tailwindcss.coms.com/blog/blog/standalone-cli 使用第一方插件。因此,@tailswindcss/*中的任何插件都应开箱即用。

要使用其他任何尾风插件,您必须运行完整的Node.js版本的tailwind。 Rails 7方法是使用cssssbundling-rails

# Gemfile
# remove gem "tailwindcss-rails"
gem "cssbundling-rails"
bin/bundle install
bin/rails css:install:tailwind

将构建脚本添加到软件包。JSON

"scripts": {
  "build:css": "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --minify"
}

添加您喜欢的任何插件。在您的情况下:

yarn add tailwindcss-flip --dev

将插件添加到尾风配置中。默认情况下,它是tailwind.config.js(独立tailwindcss-rails版本使用config/tailwind.config.js再)

plugins: [
  require("tailwindcss-flip"),
]

在您的布局中,您应该只有application样式表。删除stylesheet_link_tag“ tailwind”

<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>

要开始编译您的CSS,从package.json.json运行构建脚本

yarn build:css --watch

此应该输出app/assets/assets/builds/application.css 文件。它由Rails Asset Pipeline(链轮)提供。如果您获得链轮错误,请重新启动所有内容,清除缓存,检查app/config/config/custest.js include // = link_tree ../ builds


您还应该拥有bin/dev脚本和procfile.dev。运行:

bin/dev

启动Rails Server和Tailwind。

I assume you're using tailwindcss-rails gem. That is the default even if you run rails 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.

# Gemfile
# remove gem "tailwindcss-rails"
gem "cssbundling-rails"
bin/bundle install
bin/rails css:install:tailwind

Add build script to package.json

"scripts": {
  "build:css": "tailwindcss -i ./app/assets/stylesheets/application.tailwind.css -o ./app/assets/builds/application.css --minify"
}

Add any plugin you like after that. In your case:

yarn add tailwindcss-flip --dev

Add plugins to tailwind config. By default it is tailwind.config.js (standalone tailwindcss-rails version uses config/tailwind.config.js which you don't need anymore)

plugins: [
  require("tailwindcss-flip"),
]

In your layout you should only have application stylesheet. Remove stylesheet_link_tag "tailwind"

<%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>

To start compiling your css, run the build script from package.json

yarn build:css --watch

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, check app/assets/config/manifest.js to include //= link_tree ../builds.


You should also have bin/dev script and Procfile.dev. Run:

bin/dev

to start rails server and tailwind.

记忆里有你的影子 2025-01-24 15:07:41

这是我的工作设置,它使用尾风插件(TW-Elements),Rails 7,ImportMaps-尤其是使用 no nodejs,package.json,Esbuild/webpack等。

运行之后:

bin/importmap pin --download tw-elements
bin/importmap pin --download tw-elements/dist/plugin.cjs

我在config/importmap.rb中:

pin "tw-elements", to: "tw-elements.js", preload: true # @1.1.0
pin "tw-elements/dist/plugin.cjs", to: "tw-elements--dist--plugin.cjs.js" # @1.1.0

我从config/tailwind.config.js中引用这些内容:请注意:

module.exports = {
  content: [
    './public/*.html',
    './app/helpers/**/*.rb',
    './app/javascript/**/*.js',
    './app/views/**/*.{erb,haml,html,slim}',
    './vendor/javascript/tw-elements.js',  // TW-ELEMENTS //
  ],
  plugins: [
    require('@tailwindcss/forms'),
    require('@tailwindcss/aspect-ratio'),
    require('@tailwindcss/typography'),
    require('@tailwindcss/container-queries'),
    require('../vendor/javascript/tw-elements--dist--plugin.cjs.js'), // TW-ELEMENTS //
  ],
}

注意../代码>在一个地方需要,但在另一个地方禁止!那是因为当我们调用时,插件中需要 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:

bin/importmap pin --download tw-elements
bin/importmap pin --download tw-elements/dist/plugin.cjs

I have in my config/importmap.rb:

pin "tw-elements", to: "tw-elements.js", preload: true # @1.1.0
pin "tw-elements/dist/plugin.cjs", to: "tw-elements--dist--plugin.cjs.js" # @1.1.0

I reference these from config/tailwind.config.js thus:

module.exports = {
  content: [
    './public/*.html',
    './app/helpers/**/*.rb',
    './app/javascript/**/*.js',
    './app/views/**/*.{erb,haml,html,slim}',
    './vendor/javascript/tw-elements.js',  // TW-ELEMENTS //
  ],
  plugins: [
    require('@tailwindcss/forms'),
    require('@tailwindcss/aspect-ratio'),
    require('@tailwindcss/typography'),
    require('@tailwindcss/container-queries'),
    require('../vendor/javascript/tw-elements--dist--plugin.cjs.js'), // TW-ELEMENTS //
  ],
}

Note the ../ is required in one place, but forbidden in the other! That's because when we call require in the plugins section our working dir is RAILS_ROOT/config, but in content the paths are relative to RAILS_ROOT.

Behavior may vary slightly for different plugins, but this should provide a solid foundation.

美羊羊 2025-01-24 15:07:41

我将 tailwindcss-fluid-type 添加到我的 Rails 应用程序中,该应用程序通过下载插件来使用 gem 'tailwindcss-rails'
vendor/javascript/tailwindcss-fluid-type.js

然后在 config/tailwind.config.js 中获取它,因为

 ...
  },
  plugins: [
    require("@tailwindcss/forms"),
    require("../vendor/javascript/tailwindcss-fluid-type"),
  ],
...

您可以通过运行 bin/ 更轻松地下载importmap 固定 tailwindcss-fluid-type,但它也会将其固定在 importmap 中,您不需要它,因此从那里将其删除。

I added tailwindcss-fluid-type to my rails app that uses gem 'tailwindcss-rails' by downloading plugin to
vendor/javascript/tailwindcss-fluid-type.js

and then sourcing it in config/tailwind.config.js as in

 ...
  },
  plugins: [
    require("@tailwindcss/forms"),
    require("../vendor/javascript/tailwindcss-fluid-type"),
  ],
...

you 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.

皓月长歌 2025-01-24 15:07:41

如果您想使用 importmap,还有另一种选择。

1 - 固定所需的库

$ ./bin/importmap pin tailwindcss-flip

2 - 将以下代码添加到头部的布局文件中

    <script>
      tailwind.config = {
        plugins: [
          require('@tailwindcss/typography'),
          require("tailwindcss-flip"),
        ],        
      }      
    </script>

3 - 只需在视图中使用 dir=rtl

4 - 利润! :)

There's another option if you want to use importmap.

1 - pin required libs

$ ./bin/importmap pin tailwindcss-flip

2 - add following code to your layout file in head section

    <script>
      tailwind.config = {
        plugins: [
          require('@tailwindcss/typography'),
          require("tailwindcss-flip"),
        ],        
      }      
    </script>

3 - just use dir=rtl in your views

4 - PROFIT!!! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文