Rails 5.2 和 webpacker 3.4.3:部署到 Heroku 时资产未编译

Posted

技术标签:

【中文标题】Rails 5.2 和 webpacker 3.4.3:部署到 Heroku 时资产未编译【英文标题】:Rails 5.2 and webpacker 3.4.3: Assets not compiled when deployed to Heroku 【发布时间】:2018-10-04 14:58:01 【问题描述】:

我刚刚将我的 Rails 应用程序从 5.1.6 升级到 5.2,并借此机会将 Webpacker 从 2.0 升级到 3.4.3。

现在,当我将应用程序推送到 Heroku 时,我收到以下错误(vendor.js 是 /app/javascript/packs 中的文件之一,他在我的 application.html.erb 模板中由 javascript_pack_tag 'vendor' 调用):

ActionView::Template::Error (Webpacker can't find vendor.js in /app/public/packs/manifest.json. Possible causes: 
1. You want to set webpacker.yml value of compile to true for your environment 
   unless you are using the `webpack -w` or the webpack-dev-server. 
2. webpack has not yet re-run to reflect updates. 
3. You have misconfigured Webpacker's config/webpacker.yml file. 
4. Your webpack configuration is not creating a manifest. 
Your manifest contains: 
 
 
):

事实上,当我检查 /public 的内容时,我可以看到应该包含我的资产的 /packs 目录丢失了。

我可以看到,在 Heroku 的构建阶段,资产已被预编译:

Running: rake assets:precompile
       yarn install v1.5.1
       [1/4] Resolving packages...
       [2/4] Fetching packages...
       info fsevents@1.1.2: The platform "linux" is incompatible with this module.
       info "fsevents@1.1.2" is an optional dependency and failed compatibility check. Excluding it from installation.
       info fsevents@1.1.3: The platform "linux" is incompatible with this module.
       info "fsevents@1.1.3" is an optional dependency and failed compatibility check. Excluding it from installation.
       [3/4] Linking dependencies...
       warning "@rails/webpacker > postcss-cssnext@3.1.0" has unmet peer dependency "caniuse-lite@^1.0.30000697".
       warning " > coffee-loader@0.8.0" has unmet peer dependency "coffeescript@>= 1.8.x".
       [4/4] Building fresh packages...
       Done in 36.65s.
       Webpacker is installed ???? ????
       Using /tmp/build_d6c1dd8314f746d28a2469f2a01ec4ed/config/webpacker.yml file for setting up webpack paths
       Compiling…
       Compiled all packs in /tmp/build_d6c1dd8314f746d28a2469f2a01ec4ed/public/packs
       Asset precompilation completed (119.53s)
       Cleaning assets
       Running: rake assets:clean

我的/config/webpacker.yml 文件如下

default: &default
  source_path: app/javascript
  source_entry_path: packs
  public_output_path: packs
  cache_path: tmp/cache/webpacker

  # Additional paths webpack should lookup modules
  # ['app/assets', 'engine/foo/app/assets']
  resolved_paths: []

  # Reload manifest.json on all requests so we reload latest compiled packs
  cache_manifest: false

  extensions:
    - .jsx
    - .js
    - .sass
    - .scss
    - .css
    - .module.sass
    - .module.scss
    - .module.css
    - .png
    - .svg
    - .gif
    - .jpeg
    - .jpg

development:
  <<: *default
  compile: true

  # Reference: https://webpack.js.org/configuration/dev-server/
  dev_server:
    https: false
    host: localhost
    port: 3035
    public: localhost:3035
    hmr: false
    # Inline should be set to true if using HMR
    inline: true
    overlay: true
    compress: true
    disable_host_check: true
    use_local_ip: false
    quiet: false
    headers:
      'Access-Control-Allow-Origin': '*'
    watch_options:
      ignored: /node_modules/


test:
  <<: *default
  compile: true

  # Compile test packs to a separate directory
  public_output_path: packs-test

production:
  <<: *default

  # Production depends on precompilation of packs prior to booting for performance.
  compile: false

  # Cache manifest.json for performance
  cache_manifest: true

如果我将production: 中的compile: false 更改为compile: true,编译工作但没有缩小和gzipping。

我的/webpack/production.js 是:

const  environment  = require('@rails/webpacker')

module.exports = environment

对此的任何帮助将不胜感激!

更新

我应该添加在本地编译我的资产

NODE_ENV=production ./bin/webpack

工作得很好。

【问题讨论】:

Yannis 你解决过这个问题吗? @SimonCooper 不幸的是没有。我暂时还在使用 webpacker 2.0。我很快会再次解决这个问题。 【参考方案1】:

见this回答

# config/environments/production.rb
config.public_file_server.enabled=true` # ENV['RAILS_SERVE_STATIC_FILES'].present? 

【讨论】:

以上是关于Rails 5.2 和 webpacker 3.4.3:部署到 Heroku 时资产未编译的主要内容,如果未能解决你的问题,请参考以下文章

Rails 5.2 无法使用 Sublime Text 打开 Rails 凭据

Rails 5.2 MiniMagick::Error 使用具有比率和活动存储的图像变体时

Rails 5.2 上 jquery DataTables 和 will_paginate gem 的分页问题

Rails 5.2 Active Storage 添加自定义属性

Bootstrap(和 jquery)、webpack、rails - 没有完全集成

使用 Rails 5.2 ActiveStorage 创建和保存 pdf 并稍后附加到电子邮件