使用webpacker部署到heroku后,资产管道中不存在资产

时间:2019-06-12 20:52:33

标签: ruby-on-rails heroku webpack

我正在使用Rails 5.2.3

我可以在本地下载pdf,但是在 Heroku 上,会出现错误500。

错误为: ActionView :: Template :: Error(资产管道中不存在资产“ pdf.css”。):

这是我的heroku日志

2019-06-12T20:26:05.707039+00:00 app[web.1]: I, [2019-06-12T20:26:05.706962 #4]  INFO -- : [b789ac1d-fe64-434c-b259-a2f3949a7bd0]   Rendered clients/orders/show.pdf.erb within layouts/clients/application.pdf.erb (38.4ms)
2019-06-12T20:26:05.709399+00:00 app[web.1]: I, [2019-06-12T20:26:05.709314 #4]  INFO -- : [b789ac1d-fe64-434c-b259-a2f3949a7bd0] Completed 500 Internal Server Error in 55ms (ActiveRecord: 16.2ms)
2019-06-12T20:26:05.710722+00:00 app[web.1]: F, [2019-06-12T20:26:05.710296 #4] FATAL -- : [b789ac1d-fe64-434c-b259-a2f3949a7bd0]
2019-06-12T20:26:05.710724+00:00 app[web.1]: F, [2019-06-12T20:26:05.710384 #4] FATAL -- : [b789ac1d-fe64-434c-b259-a2f3949a7bd0] ActionView::Template::Error (The asset "pdf.css" is not present in the asset pipeline.):
2019-06-12T20:26:05.710726+00:00 app[web.1]: F, [2019-06-12T20:26:05.710579 #4] FATAL -- : [b789ac1d-fe64-434c-b259-a2f3949a7bd0]     1: <html lang="fr">
2019-06-12T20:26:05.710728+00:00 app[web.1]: [b789ac1d-fe64-434c-b259-a2f3949a7bd0]     2: <head>
2019-06-12T20:26:05.710732+00:00 app[web.1]: [b789ac1d-fe64-434c-b259-a2f3949a7bd0]     3:   <meta charset="utf-8" />
2019-06-12T20:26:05.710733+00:00 app[web.1]: [b789ac1d-fe64-434c-b259-a2f3949a7bd0]     4:   <%= wicked_pdf_stylesheet_link_tag 'pdf' %>
2019-06-12T20:26:05.710734+00:00 app[web.1]: [b789ac1d-fe64-434c-b259-a2f3949a7bd0]     5: </head>
2019-06-12T20:26:05.710736+00:00 app[web.1]: [b789ac1d-fe64-434c-b259-a2f3949a7bd0]     6: <body>
2019-06-12T20:26:05.710737+00:00 app[web.1]: [b789ac1d-fe64-434c-b259-a2f3949a7bd0]     7:   <div>
2019-06-12T20:26:05.710739+00:00 app[web.1]: F, [2019-06-12T20:26:05.710659 #4] FATAL -- : [b789ac1d-fe64-434c-b259-a2f3949a7bd0]
2019-06-12T20:26:05.710815+00:00 app[web.1]: F, [2019-06-12T20:26:05.710734 #4] FATAL -- : [b789ac1d-fe64-434c-b259-a2f3949a7bd0] app/views/layouts/clients/application.pdf.erb:4:in `_app_views_layouts_clients_application_pdf_erb___1711932080367152756_47199839415040'
2019-06-12T20:26:05.710823+00:00 app[web.1]: [b789ac1d-fe64-434c-b259-a2f3949a7bd0] app/controllers/clients/orders_controller.rb:19:in `block (2 levels) in show'
2019-06-12T20:26:05.710825+00:00 app[web.1]: [b789ac1d-fe64-434c-b259-a2f3949a7bd0] app/controllers/clients/orders_controller.rb:16:in `show'

在我的views/layouts中,我有application.pdf.erb

<html lang="fr">
<head>
  <meta charset="utf-8" />
  <%= wicked_pdf_stylesheet_link_tag 'pdf' %>
</head>
<body>
  <div>
    <%= yield %>
  </div>
</body>
</html>

pdf文件的存储方式如下vendor/assets/stylesheets/pdf.scss ,在供应商中,我还有一个自定义的引导程序文件bootstrap.min.css

pdf.scss

@import "bootstrap.min";

.someclass {}

config/initializers/assets.rb中,有以下一行:

Rails.application.config.assets.precompile += %w( pdf.scss, chartkick.js)

我也在使用webpacker,这也许就是问题所在……

这是我的 webpacker.yml

# Note: You must restart bin/webpack-dev-server for changes to take effect

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

public / assets 文件中(在本地),在heroku上没有pdf文件的痕迹。...

我在这里想念什么?

我可以转发更多代码,只是让我知道...我真的希望你能帮助我。

编辑

这是我的package.json 我移动了webpack-cli,但仍然无法使用:( 我又部署了,我应该运行一个新命令使它正常工作吗?

{
  "name": "kamelot",
  "private": true,
  "dependencies": {
    "@rails/webpacker": "3.5",
    "babel-preset-react": "^6.24.1",
    "flatpickr": "^4.5.7",
    "highcharts": "^7.1.1",
    "prop-types": "^15.7.2",
    "react": "^16.8.6",
    "react-color": "^2.17.3",
    "react-dom": "^16.8.6",
    "react_ujs": "^2.5.0",
    "webpack-cli": "^3.3.4"
  },
  "devDependencies": {
    "webpack-dev-server": "2.11.2"
  }
}

1 个答案:

答案 0 :(得分:0)

这是一个远景,但是Webpacker v4中有一个known issue,其中webpack-clidevDependency配置文件中被标记为package.json

当部署任务运行webpacker:compile并且没有webpack-cli时,不会引发任何错误,因此部署完成,但是public/packs文件夹中没有文件被编译。

要解决此问题,您只需将webpack-cli依赖项从devDependencies移到dependencies

更新

如果您使用capistrano来部署项目,则应在public/packs中的node_modules中添加linked_dirsdeploy.rb(最后一个是可选的)配置文件:

此外,如果您使用node_modules,则必须在预编译资产之前运行yarm:install。为此,只需在您的deploy.rb文件中添加此任务:

before "deploy:assets:precompile", "deploy:yarn_install"
namespace :deploy do
  desc "Run rake yarn install"
  task :yarn_install do
    on roles(:web) do
      within release_path do
       execute("cd #{release_path} && yarn install --silent --no-progress --no-audit --no-optional")
      end
    end
  end
end

有关详细信息,请检查webpacker documentation进行Capistrano部署。

更新2

这是另一个远景,但是有一个reported issue,其中heroku构建包的顺序会影响资产编译过程。 node buildpack必须首先出现,然后是ruby buildpack。以下应解决此问题:

heroku buildpacks:clear
heroku buildpacks:set heroku/nodejs
heroku buildpacks:add heroku/ruby

同一期中建议的另一个快速解决方案是在rake webpacker:compile控制台中运行heroku,但这不能解决部署资产编译问题。