stylesheet_pack_tag没有在带有webpacker gem的rails 5.1中找到app/javascript/src/application.css

wul*_*ong 20 css ruby-on-rails-5 webpacker

当我尝试使用webpacker在我的新rails 5.1 app中加载页面时,我收到此错误.我也希望webpacker能够处理CSS.

Started GET "/" for ::1 at 2017-09-01 12:20:23 -0400
Processing by HomeController#welcome as HTML
  Rendering home/welcome.html.erb within layouts/application
  Rendered home/welcome.html.erb within layouts/application (0.4ms)
Completed 500 Internal Server Error in 28ms



ActionView::Template::Error (Webpacker can't find application.css in /Users/myusername/Documents/testing-ground/myapp/public/packs/manifest.json. Possible causes:
1. You want to set wepbacker.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:
{
  "application.js": "/packs/application-1ba6db9cf5c0fb48c785.js",
  "hello_react.js": "/packs/hello_react-812cbb4d606734bec7a9.js"
}
):
     7:     <%= stylesheet_link_tag    'application', media: 'all', 'data-turbolinks-track': 'reload' %>
     8:     <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>
     9:     <%= javascript_pack_tag 'application' %>
    10: <%= stylesheet_pack_tag 'application' %>
    11:   </head>
    12:
    13:   <body>

app/views/layouts/application.html.erb:10:in `_app_views_layouts_application_html_erb__1178607493020013329_70339213085820'
Run Code Online (Sandbox Code Playgroud)

我运行的是./bin/webpack-dev-server旁边的rails server.我用以下方法创建了应用:

rails new myapp --webpack bundle bundle exec rails webpacker:install:react

我有一个CSS文件app/javascript/src/application.css.(写作让我觉得有些不对劲.把css放在javascript目录里似乎不合适.)

我只定义了单根路由root to: 'home#welcome'.

这是 app/views/layouts/application.html.erb

<!DOCTYPE html>
<html>
  <head>
    <title>Myapp</title>
    <%= csrf_meta_tags %>

    <%= javascript_pack_tag 'application' %>
    <%= stylesheet_pack_tag 'application' %>
  </head>

  <body>
    <%= yield %>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

这是我的config/webpacker.yml(我也试过false在开发中设置编译.

# 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:
    - .coffee
    - .erb
    - .js
    - .jsx
    - .ts
    - .vue
    - .sass
    - .scss
    - .css
    - .png
    - .svg
    - .gif
    - .jpeg
    - .jpg

development:
  <<: *default
  compile: true

  dev_server:
    host: localhost
    port: 3035
    hmr: false
    https: false

test:
  <<: *default
  compile: true

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

production:
  <<: *default

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

  # Cache manifest.json for performance
  cache_manifest: true
Run Code Online (Sandbox Code Playgroud)

我不想在前面添加太多细节,因为它们会让人分心,然后更有帮助.请问其他任何事情,我会加入我的问题.谢谢!

Sna*_*ake 12

在你的application.js中:

import "path to application.css"
Run Code Online (Sandbox Code Playgroud)


mun*_*rie 7

我在截至2018年12月5日的Webpack的当前版本的Rails 5.2(4.2.x?)上遇到了这个确切的问题,并刚刚解决了它。

对我来说,解决方法是重命名application.css其他任何东西。我认为与Webpack发生命名冲突。

所以现在我的背包标签看起来像:

<%= stylesheet_pack_tag 'styles', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_pack_tag 'application', 'data-turbolinks-track': 'reload' %>
Run Code Online (Sandbox Code Playgroud)

我不需要import里面的样式表application.js。这似乎对我没有影响。

可选奖金: 我也将改名为app/javascriptapp/webpack因为我还认为从Javascript目录中运行样式会造成混淆(并从此youtube视频中获悉可以完成此操作:https : //www.youtube.com/watch?v= I_GGYIWbmg0&feature = youtu.be&t = 32m35s)。就这么简单

  1. 将目录重命名为 app/webpack
  2. webpack.yml像这样调整: source_path: app/webpack

希望能有所帮助。