tailwind.css 未在 Heroku 的 Rails 7 项目中生成

tailwind.css not being generated in a Rails 7 project in Heroku

我有一个使用 TailwindCSS 部署到 Heroku 的 Rails 7 项目,在 rake asset:precompile 期间没有构建 tailwind.css,我不知道为什么。当我尝试访问该应用程序时,它因以下错误而崩溃:

I, [2022-03-23T17:35:18.429029 #8]  INFO -- : [4f6eaac8-942c-4ee5-af10-172663b1a292] Started GET "/" for XX.XX.XX.XX at 2022-03-23 17:35:18 +0000
I, [2022-03-23T17:35:18.433526 #8]  INFO -- : [4f6eaac8-942c-4ee5-af10-172663b1a292] Processing by StaticController#index as HTML
I, [2022-03-23T17:35:18.439133 #8]  INFO -- : [4f6eaac8-942c-4ee5-af10-172663b1a292]   Rendered static/index.html.erb within layouts/application (Duration: 0.6ms | Allocations: 184)
I, [2022-03-23T17:35:18.446294 #8]  INFO -- : [4f6eaac8-942c-4ee5-af10-172663b1a292]   Rendered layout layouts/application.html.erb (Duration: 7.8ms | Allocations: 1205)
I, [2022-03-23T17:35:18.446595 #8]  INFO -- : [4f6eaac8-942c-4ee5-af10-172663b1a292] Completed 500 Internal Server Error in 13ms (Allocations: 2512)
F, [2022-03-23T17:35:18.447716 #8] FATAL -- : [4f6eaac8-942c-4ee5-af10-172663b1a292]
[4f6eaac8-942c-4ee5-af10-172663b1a292] ActionView::Template::Error (The asset "tailwind.css" is not present in the asset pipeline.
):
[4f6eaac8-942c-4ee5-af10-172663b1a292]     12:     <meta name="theme-color" content="#ffffff">
[4f6eaac8-942c-4ee5-af10-172663b1a292]     13:     <%= csrf_meta_tags %>
[4f6eaac8-942c-4ee5-af10-172663b1a292]     14:     <%= csp_meta_tag %>
[4f6eaac8-942c-4ee5-af10-172663b1a292]     15:     <%= stylesheet_link_tag "tailwind", "inter-font", "data-turbo-track": "reload" %>
[4f6eaac8-942c-4ee5-af10-172663b1a292]     16:     <%= stylesheet_link_tag "application", "data-turbo-track": "reload" %>
[4f6eaac8-942c-4ee5-af10-172663b1a292]     17:     <%= javascript_importmap_tags %>
[4f6eaac8-942c-4ee5-af10-172663b1a292]     18:   </head>
[4f6eaac8-942c-4ee5-af10-172663b1a292]
[4f6eaac8-942c-4ee5-af10-172663b1a292] app/views/layouts/application.html.erb:15

我实际上有两个项目,它们的设置几乎完全相同(尽管它们具有不同的功能),另一个有效。

我已经添加了

config.assets.css_compressor = nil

production.rbtest.rbdevelopment.rb(以防万一)。

在撰写本文时,我正在安装最新的 tailwindcss-rails,2.0.8。我也在安装 sassc-rails,因为 rails_admin 需要它,但对于需要它的其他项目也是如此。

奇怪的是。如果我打开那个 Heroku 项目的控制台并且 运行 rake asset:precompile 它实际上完成了创建丢失的文件:

~ $ rake assets:precompile
+ /app/vendor/bundle/ruby/3.1.0/gems/tailwindcss-rails-2.0.8-x86_64-linux/exe/x86_64-linux/tailwindcss -i /app/app/assets/stylesheets/application.tailwind.css -o /app/app/assets/builds/tailwind.css -c /app/config/tailwind.config.js --minify

Done in 821ms.
W, [2022-03-19T12:38:43.514430 #6]  WARN -- : Removed sourceMappingURL comment for missing asset 'rails_admin/popper.js.map' from /app/vendor/bundle/ruby/3.1.0/gems/rails_admin-3.0.0.rc4/vendor/assets/javascripts/rails_admin/popper.js
W, [2022-03-19T12:38:43.534443 #6]  WARN -- : Removed sourceMappingURL comment for missing asset 'rails_admin/bootstrap.js.map' from /app/vendor/bundle/ruby/3.1.0/gems/rails_admin-3.0.0.rc4/vendor/assets/javascripts/rails_admin/bootstrap.js
I, [2022-03-19T12:38:43.744157 #6]  INFO -- : Writing /app/public/assets/tailwind-0c01c3e907ab268dbd4dcaa14542a12d0388cfbeb5733a183e88e1b26ef30afb.css
I, [2022-03-19T12:38:43.744385 #6]  INFO -- : Writing /app/public/assets/tailwind-0c01c3e907ab268dbd4dcaa14542a12d0388cfbeb5733a183e88e1b26ef30afb.css.gz
~ $

为什么在部署期间不起作用?我可以看到它正在 运行 中:

       Using stimulus-rails 1.0.4
       Using tailwindcss-rails 2.0.8 (x86_64-linux)
       Bundle complete! 28 Gemfile dependencies, 90 gems now installed.
       Gems in the groups 'development' and 'test' were not installed.
       Bundled gems are installed into `./vendor/bundle`
       Bundle completed (0.38s)
       Cleaning up the bundler cache.
       Removing bundler (2.2.33)
-----> Detecting rake tasks
-----> Preparing app for Rails asset pipeline
       Running: rake assets:precompile
       
       Done in 788ms.
       Asset precompilation completed (3.58s)
       Cleaning assets
       Running: rake assets:clean
-----> Detecting rails configuration
-----> Discovering process types
       Procfile declares types     -> release, web, worker
       Default types for buildpack -> console, rake
-----> Compressing...
       Done: 78.7M
-----> Launching...

在您的本地计算机上尝试运行以下命令:

gem install bundler
bundle update --bundler
bundle lock --add-platform x86_64-linux

然后提交这些更改,然后再次部署。

我发现了问题:

在某些时候,我从项目中删除了 app/assets/builds/.keep,这导致在 git 克隆 repo 时不创建 app/assets/build 目录。我不明白为什么这会导致 tailwind.css 不在第一个 运行 上创建,而是在 rake asset:precompile 的第二个 运行 上创建。

以防万一,已将其报告为错误:https://github.com/rails/tailwindcss-rails/issues/158

rails assets:clean assets:precompile

此命令解决帮助我解决了这个问题。