在使用esbuild的heroku上升级到Rails 7后,Application.js未编译

vaj7vani  于 2022-11-13  发布在  其他
关注(0)|答案(4)|浏览(149)

我在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

下面是应用程序/javascript/应用程序. 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/build中的脚本

"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首先运行assets compile,然后运行包json builds。因此,当compile运行时,builds文件夹是空的,application.js没有复制到public/assets。
现在为了解决这个问题,我从git ignore中删除了builds文件夹,并将builds中的文件添加到仓库中。所以现在当编译运行时,build文件夹中有文件,并将其编译到public/assets中。我知道这不是最好的解决方法,但目前这是我所知道的唯一解决方法。
如果有人知道如何编辑heroku部署脚本来运行assets compile after package json build脚本,那么它就解决了这个问题。

uqxowvwt

uqxowvwt1#

您是否已将/app/assets/builds/.keep推送到存储库?
问题可能在于,如果在rake assets:precompile期间不存在空文件夹,则即使jsbundling-rails(例如;或类似的)在内部执行类似于Rake::Task["assets:precompile"].enhance(["build"])的操作-即使enhance的rake任务填充了该文件夹,rake assets:precompile也不会考虑该文件夹本身。
而如果它最初是存在的&空的-一切都会工作得很好。
因此,在我的例子中,我不需要推送包含所有真实的内容的整个/app/assets/builds文件夹,但我需要将.keep推送到那里,以便在运行assets:precompile之前出现一个空文件夹。
然后.gitignore只显示其内容,但要确保文件夹在克隆存储库后不会消失:

/app/assets/builds/*
!/app/assets/builds/.keep
inkz8wg9

inkz8wg92#

地址:https://github.com/rails/jsbundling-rails
...当您将应用程序部署到生产环境时,javascript:build任务将附加到assets:precompile任务,以确保package.json中的所有包依赖项都已通过yarn安装,然后运行yarn build来处理所有入口点,就像在开发过程中一样。后面的文件随后会被asset pipeline拾取、消化并复制到public/assets中,就像任何其他asset pipeline文件一样...

7uhlpewt

7uhlpewt3#

我遇到了类似的问题,但在部署过程中没有安装esbuild,这导致我在故障排除过程中进入此页面。
我用.yarn-integrity文件提交了node_modules文件夹。修复它的方法是删除node_modules文件夹,运行yarn install,确保在我的.gitignore文件中忽略node_modules文件夹,然后推送提交。
一旦我部署了应用程序,部署就会成功。

pu3pd22g

pu3pd22g4#

删除美化程序并安装美化程序
第一个

相关问题