使用 esbuild 在 Heroku 上升级到 Rails 7 后,Application.js 无法编译

Fud*_*dge 4 ruby ruby-on-rails heroku sprockets esbuild

我将 Heroku 上的应用程序从带有 Webpacker 的 Rails 6 更新到带有 esbuild 的 Rails 7。

\n

我在生产中收到的错误如下:

\n
The asset "application.js" is not present in the asset pipeline\n
Run Code Online (Sandbox Code Playgroud)\n

我进入 Heroku 服务器,看到 application.js 文件位于app/assets/builds内,在我的 manifest.js 中,它的设置如下:

\n
//= link_tree ../images\n//= link_tree ../builds\n
Run Code Online (Sandbox Code Playgroud)\n

但是当我检查public/assets/ * 文件夹时,除了 application.js 和一个 css 文件之外,所有文件都在那里。

\n

根据 Heroku 在其网站上的推荐,我还在本地尝试了以下代码:

\n
RAILS_ENV=production bundle exec rake assets:precompile\n
Run Code Online (Sandbox Code Playgroud)\n

这会将包括 application.js 在内的所有文件编译到public/assets/ * 中,并在本地正常工作。在 Heroku 生产中,即使我尝试手动编译,application.js 也不会复制到public/assets/ *

\n

我的 esbuild 脚本如下并且工作没有问题:

\n
"build": "esbuild app/javascript/*.* --bundle --outdir=app/assets/builds"\n
Run Code Online (Sandbox Code Playgroud)\n

有谁有线索或解决方案将 application.js 加载到 public/assets 中,这样我就不会收到以下错误?

\n
The asset "application.js" is not present in the asset pipeline\n
Run Code Online (Sandbox Code Playgroud)\n

这是应用程序/javascript/application.js

\n
// Entry point for the build script in your package.json\nimport "@hotwired/turbo-rails"\nimport "./controllers"\nimport * as bootstrap from "bootstrap"\nimport "lazysizes"\nimport "@fortawesome/fontawesome-free/js/all"\n\nif (\'scrollRestoration\' in window.history) {\n  window.history.scrollRestoration = \'manual\';\n}\n
Run Code Online (Sandbox Code Playgroud)\n

这是一个也未编译的css文件,也许有一个连接,但我无法找到。(此文件夹中除此文件外的所有其他 css 文件均编译为 public/assets)

\n

应用程序/资产/样式表/共享/swiper.scss

\n
@import "swiper/swiper.scss";\n@import "swiper/modules/navigation/navigation.scss";\n@import "swiper/modules/lazy/lazy.scss";\n\n.swiper-button-next {\n  color: #000000;\n}\n\n.swiper-button-prev {\n  color: #000000;\n}\n
Run Code Online (Sandbox Code Playgroud)\n

我的脚本将 scss 到 css 放入 app/assets/builds 中

\n
"build:css": "sass ./app/assets/stylesheets/application.bootstrap.scss:./app/assets/builds/application.css ./app/assets/stylesheets/controllers:./app/assets/builds/controllers ./app/assets/stylesheets/shared:./app/assets/builds/shared ./app/assets/stylesheets/devise:./app/assets/builds/devise ./app/assets/stylesheets/errors.scss:./app/assets/builds/errors.css --no-source-map --load-path=node_modules"\n
Run Code Online (Sandbox Code Playgroud)\n

不确定是否有帮助,但这是我的部署日志。(没有错误)

\n
git push heroku master                                           \nEnumerating objects: 15, done.\nCounting objects: 100% (15/15), done.\nDelta compression using up to 12 threads\nCompressing objects: 100% (8/8), done.\nWriting objects: 100% (8/8), 633 bytes | 633.00 KiB/s, done.\nTotal 8 (delta 7), reused 0 (delta 0), pack-reused 0\nremote: Compressing source files... done.\nremote: Building source:\nremote: \nremote: -----> Building on the Heroku-20 stack\nremote: -----> Using buildpack: heroku/ruby\nremote: -----> Ruby app detected\nremote: -----> Installing bundler 2.2.33\nremote: -----> Removing BUNDLED WITH version in the Gemfile.lock\nremote: -----> Compiling Ruby/Rails\nremote: -----> Using Ruby version: ruby-3.0.2\nremote: -----> Installing dependencies using bundler 2.2.33\nremote:        Running: BUNDLE_WITHOUT=\'development:test\' BUNDLE_PATH=vendor/bundle BUNDLE_BIN=vendor/bundle/bin BUNDLE_DEPLOYMENT=1 bundle install -j4\nremote:        Using rake 13.0.6\nremote:        Using concurrent-ruby 1.1.9\nremote:        Using i18n 1.8.11\nremote:        Using minitest 5.15.0\nremote:        Using tzinfo 2.0.4\nremote:        Using activesupport 7.0.0\nremote:        Using builder 3.2.4\nremote:        Using erubi 1.10.0\nremote:        Using racc 1.6.0\nremote:        Using nokogiri 1.12.5 (x86_64-linux)\nremote:        Using rails-dom-testing 2.0.3\nremote:        Using crass 1.0.6\nremote:        Using loofah 2.13.0\nremote:        Using rails-html-sanitizer 1.4.2\nremote:        Using actionview 7.0.0\nremote:        Using rack 2.2.3\nremote:        Using rack-test 1.1.0\nremote:        Using actionpack 7.0.0\nremote:        Using nio4r 2.5.8\nremote:        Using websocket-extensions 0.1.5\nremote:        Using websocket-driver 0.7.5\nremote:        Using actioncable 7.0.0\nremote:        Using globalid 1.0.0\nremote:        Using activejob 7.0.0\nremote:        Using activemodel 7.0.0\nremote:        Using activerecord 7.0.0\nremote:        Using marcel 1.0.2\nremote:        Using mini_mime 1.1.2\nremote:        Using activestorage 7.0.0\nremote:        Using mail 2.7.1\nremote:        Using actionmailbox 7.0.0\nremote:        Using actionmailer 7.0.0\nremote:        Using actiontext 7.0.0\nremote:        Using public_suffix 4.0.6\nremote:        Using addressable 2.8.0\nremote:        Using aws-eventstream 1.2.0\nremote:        Using aws-sigv4 1.4.0\nremote:        Using bcrypt 3.1.16\nremote:        Using msgpack 1.4.2\nremote:        Using bootsnap 1.9.3\nremote:        Using method_source 1.0.0\nremote:        Using thor 1.1.0\nremote:        Using zeitwerk 2.5.1\nremote:        Using railties 7.0.0\nremote:        Using breadcrumbs_on_rails 4.1.0\nremote:        Using bundler 2.2.33\nremote:        Using mini_magick 4.11.0\nremote:        Using ffi 1.15.4\nremote:        Using ruby-vips 2.1.4\nremote:        Using image_processing 1.12.1\nremote:        Using ssrf_filter 1.0.7\nremote:        Using carrierwave 2.2.2\nremote:        Using cssbundling-rails 1.0.0\nremote:        Using orm_adapter 0.5.0\nremote:        Using responders 3.0.1\nremote:        Using warden 1.2.9\nremote:        Using devise 4.8.1\nremote:        Using unf_ext 0.0.8\nremote:        Using unf 0.1.4\nremote:        Using domain_name 0.5.20190701\nremote:        Using excon 0.89.0\nremote:        Using ffi-compiler 1.0.1\nremote:        Using formatador 0.3.0\nremote:        Using mime-types-data 3.2021.1115\nremote:        Using mime-types 3.4.1\nremote:        Using fog-core 2.2.4\nremote:        Using multi_json 1.15.0\nremote:        Using fog-json 1.2.0\nremote:        Using fog-xml 0.1.4\nremote:        Using ipaddress 0.8.3\nremote:        Using fog-aws 3.12.0\nremote:        Using friendly_id 5.4.2\nremote:        Using heroku-deflater 0.6.3\nremote:        Using http-cookie 1.0.4\nremote:        Using http-form_data 2.3.0\nremote:        Using llhttp-ffi 0.4.0\nremote:        Using http 5.0.4\nremote:        Using jbuilder 2.11.4\nremote:        Using jsbundling-rails 1.0.0\nremote:        Using json 2.6.1\nremote:        Using kaminari-core 1.2.1\nremote:        Using kaminari-actionview 1.2.1\nremote:        Using kaminari-activerecord 1.2.1\nremote:        Using kaminari 1.2.1\nremote:        Using meta-tags 2.16.0\nremote:        Using oauth 0.5.8\nremote:        Using pg 1.2.3\nremote:        Using puma 4.3.10\nremote:        Using rails 7.0.0\nremote:        Using rakuten_web_service 1.13.1\nremote:        Using ransack 2.4.2 from https://github.com/activerecord-hackery/ransack (at 81910c2@81910c2)\nremote:        Using sassc 2.4.0\nremote:        Using sprockets 4.0.2\nremote:        Using sprockets-rails 3.4.2\nremote:        Using tilt 2.0.10\nremote:        Using sassc-rails 2.1.2\nremote:        Using sass-rails 6.0.0\nremote:        Using settingslogic 2.0.9\nremote:        Using sitemap_generator 6.1.2\nremote:        Using slack-notifier 2.4.0\nremote:        Using temple 0.8.2\nremote:        Using slim 4.1.0\nremote:        Using slim-rails 3.3.0\nremote:        Using stimulus-rails 1.0.2\nremote:        Using turbo-rails 1.0.0\nremote:        Using twitter-ads 10.0.0\nremote:        Using vacuum 4.0.0\nremote:        Bundle complete! 49 Gemfile dependencies, 107 gems now installed.\nremote:        Gems in the groups \'development\' and \'test\' were not installed.\nremote:        Bundled gems are installed into `./vendor/bundle`\nremote:        Bundle completed (0.93s)\nremote:        Cleaning up the bundler cache.\nremote: -----> Installing node-v16.13.1-linux-x64\nremote: -----> Installing yarn-v1.22.17\nremote: -----> Detecting rake tasks\nremote: -----> Preparing app for Rails asset pipeline\nremote:        Running: rake assets:precompile\nremote:        DEPRECATION WARNING: `active_support/core_ext/uri` is deprecated and will be removed in Rails 7.1. (called from <main> at /tmp/build_07dca8b4/config/application.rb:7)\nremote:        yarn install v1.22.17\nremote:        [1/4] Resolving packages...\nremote:        [2/4] Fetching packages...\nremote:        [3/4] Linking dependencies...\nremote:        [4/4] Building fresh packages...\nremote:        Done in 5.32s.\nremote:        yarn run v1.22.17\nremote:        $ sass ./app/assets/stylesheets/application.bootstrap.scss:./app/assets/builds/application.css ./app/assets/stylesheets/controllers:./app/assets/builds/controllers ./app/assets/stylesheets/shared:./app/assets/builds/shared ./app/assets/stylesheets/devise:./app/assets/builds/devise ./app/assets/stylesheets/errors.scss:./app/assets/builds/errors.css --no-source-map --load-path=node_modules\nremote:        Done in 2.32s.\nremote:        yarn install v1.22.17\nremote:        [1/4] Resolving packages...\nremote:        success Already up-to-date.\nremote:        Done in 0.10s.\nremote:        yarn run v1.22.17\nremote:        $ esbuild app/javascript/*.* --bundle --outdir=app/assets/builds\nremote:        \nremote:          app/assets/builds/application.js  2.1mb \xe2\x9a\xa0\xef\xb8\x8f\nremote:        \nremote:        Done in 0.22s.\nremote:        Asset precompilation completed (10.98s)\nremote:        Cleaning assets\nremote:        Running: rake assets:clean\nremote:        DEPRECATION WARNING: `active_support/core_ext/uri` is deprecated and will be removed in Rails 7.1. (called from <main> at /tmp/build_07dca8b4/config/application.rb:7)\nremote: -----> Detecting rails configuration\nremote: \nremote: ###### WARNING:\nremote: \nremote:        There is a more recent Ruby version available for you to use:\nremote:        \nremote:        3.0.3\nremote:        \nremote:        The latest version will include security and bug fixes. We always recommend\nremote:        running the latest version of your minor release.\nremote:        \nremote:        Please upgrade your Ruby version.\nremote:        \nremote:        For all available Ruby versions see:\nremote:          https://devcenter.heroku.com/articles/ruby-support#supported-runtimes\nremote: \nremote: ###### WARNING:\nremote: \nremote:        No Procfile detected, using the default web server.\nremote:        We recommend explicitly declaring how to boot your server process via a Procfile.\nremote:        https://devcenter.heroku.com/articles/ruby-default-web-server\nremote: \nremote: \nremote: -----> Discovering process types\nremote:        Procfile declares types     -> (none)\nremote:        Default types for buildpack -> console, rake, web\nremote: \nremote: -----> Compressing...\nremote:        Done: 101.3M\nremote: -----> Launching...\nremote:        Released vxxxx\nremote:        https://xxxxxxxxx.herokuapp.com/ deployed to Heroku\nremote: \nremote: Verifying deploy... done.\nTo https://git.heroku.com/xxxxxxxxx.git\n   xxxxx..xxxxxx  master -> master\n
Run Code Online (Sandbox Code Playgroud)\n

更新

\n

似乎当我部署到 Heroku 时,Heroku 首先运行资产编译,然后构建 json 包。因此,编译运行时 builds 文件夹为空,并且 application.js 不会复制到 public/assets。

\n

现在为了解决这个问题,我从 gitignore 中删除了 builds 文件夹,并将 builds 中的文件添加到存储库中。因此,现在当编译运行时,构建文件夹包含文件并将其编译到 public/assets。我知道这不是解决问题的最佳方法,但目前这是我知道解决问题的唯一方法。

\n

如果有人知道如何编辑 heroku 部署脚本以在包 json 构建脚本之后运行资产编译,那么它就可以解决问题。

\n

use*_*239 11

你已经/app/assets/builds/.keep推送到存储库了吗?

\n

问题可能是,如果在 期间不存在空文件夹rake assets:precompile,那么即使jsbundling-rails(例如;或类似的)在内部执行类似 \xe2\x80\x94 的操作,即使-ed rake 任务Rake::Task["assets:precompile"].enhance(["build"])也不会考虑文件夹本身填充它。rake assets:precompileenhance

\n

如果它最初存在且为空 \xe2\x80\x94 ,则一切都会正常工作。

\n

因此,就我而言,我不需要推送/app/assets/builds包含所有实际内容的整个文件夹,但我需要推送到那里,以便在运行.keep之前存在一个空文件夹。assets:precompile

\n

然后.gitignore只有它的内容,但要确保克隆存储库后文件夹不会消失:

\n
/app/assets/builds/*\n!/app/assets/builds/.keep\n
Run Code Online (Sandbox Code Playgroud)\n