0

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

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

The asset "application.js" is not present in the asset pipeline

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

//= link_tree ../images
//= link_tree ../builds

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

正如 Heroku 在他们的网站上推荐的那样,我也在本地尝试了以下代码:

RAILS_ENV=production bundle exec rake assets:precompile

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

我的 esbuild 脚本如下,可以正常工作:

"build": "esbuild app/javascript/*.* --bundle --outdir=app/assets/builds"

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

The asset "application.js" is not present in the asset pipeline

这是 app/javascript/application.js

// Entry point for the build script in your package.json
import "@hotwired/turbo-rails"
import "./controllers"
import * as bootstrap from "bootstrap"
import "lazysizes"
import "@fortawesome/fontawesome-free/js/all"

if ('scrollRestoration' in window.history) {
  window.history.scrollRestoration = 'manual';
}

这是一个也没有编译的css文件,也许有连接,但我无法找到。(此文件夹中除此文件之外的所有其他 css 文件仅编译到公共/资产)

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

@import "swiper/swiper.scss";
@import "swiper/modules/navigation/navigation.scss";
@import "swiper/modules/lazy/lazy.scss";

.swiper-button-next {
  color: #000000;
}

.swiper-button-prev {
  color: #000000;
}

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

"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"

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

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

更新

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

现在为了解决这个问题,我从 git ignore 中删除了 builds 文件夹,并将 builds 中的文件添加到存储库中。所以现在编译运行时,构建文件夹包含文件并将其编译到公共/资产。我知道这不是解决问题的最佳方法,但目前这是我知道的唯一解决方法。

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

4

0 回答 0