Rails 6 服务器突然停止编译 webpack

13

我遇到了一个非常奇怪的问题。

我在我的开发环境上工作,当我启动Rails服务器时,它会在第一次浏览器请求时编译webpack。

现在,突然之间,这个过程没有发生,导致所有的javascript和样式都失败,应用程序无法正确渲染。

Rails服务器控制台甚至没有显示[Webpacker] Compiling…行

如果我自己启动webpack-dev-server,它就能正常工作。但由于某种原因,rails服务器已经停止自动执行此操作。我已经将webpacker.yml中的webpack_compile_output:false改为true,但是rails服务器仍然没有显示与Webpack相关的任何内容。

这真的很奇怪。我没有添加任何gems或更新任何版本的node或webpack。我确实将我的机器上全局的Git版本从1.9更新到了2.24,但也只是这些。

有什么想法吗?

更新:(按要求,这里是我的config/webpacker.yml的内容

default: &default
  source_path: app/javascript
  source_entry_path: packs
  public_root_path: public
  public_output_path: packs
  cache_path: tmp/cache/webpacker
  check_yarn_integrity: false
  webpack_compile_output: true

  # 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

  # Extract and emit a css file
  extract_css: false

  static_assets_extensions:
    - .jpg
    - .jpeg
    - .png
    - .gif
    - .tiff
    - .ico
    - .svg
    - .eot
    - .otf
    - .ttf
    - .woff
    - .woff2

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

development:
  <<: *default
  compile: true

  # Verifies that correct packages and versions are installed by inspecting package.json, yarn.lock, and node_modules
  check_yarn_integrity: false
   # 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: false
    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

  # Extract and emit a css file
  extract_css: true

  # Cache manifest.json for performance
  cache_manifest: true


请分享您的config/webpacker.yml文件内容。 - rossta
刚刚将其添加到原始帖子中。谢谢。 - Dan
4
在开发中,如果Rails认为webpack-dev-server正在运行,它将不会触发编译。请查看是否仍有进程在监听3035端口(您的开发服务器端口)。在我的Mac上,我会运行lsof -nP -i4TCP:3035 | grep LISTEN - rossta
不幸的是,它似乎没有在后台运行。这让我感到困惑。我记得大约1-2周前这还能正常工作。我会启动我的Rails服务器,然后在第一个浏览器请求时,我会看到webpack正在编译。但现在不再发生了。真的很奇怪。 - Dan
6个回答

20

我也有这种经历。通常运行rails s时,你会看到类似以下内容:

Started GET "/react_test/hello" for ::1 at 2020-05-15 16:54:29 -0700

Processing by ReactTestController#hello as HTML
  Rendering react_test/hello.html.erb within layouts/application

[Webpacker] Everything's up-to-date. Nothing to do

如果您更改了作为Webpack资源一部分的js(或css/scss)文件:

Processing by ReactTestController#hello as HTML
  Rendering react_test/hello.html.erb within layouts/application
[Webpacker] Compiling...
[Webpacker] Compiled all packs in /Users/foo/public/packs
[Webpacker] Hash: 1ea802b336ed3c771c61
Version: webpack 4.43.0
Time: 4844ms

... truncated for brevity (a list of assets, size, etc)

在某个时刻,所有这些都会停止——你必须手动运行rails s./bin/webpack-dev-server。目前为止,我注意到的是,当你运行webpack-dev-server时(例如更新config/webpacker.yml时),它会触发这个问题。文件本身有一个注释:

# 注意:您必须重新启动 bin/webpack-dev-server 才能使更改生效


尝试以下解决方法:

  1. 停止Rails serverwebpack-dev-server
  2. 对你的/app/javascript/packs/application.js 文件进行一些(临时)更改
    • 例如,注释掉一个导入://import 'bootstrap'
  3. 重新启动rails s(仅限这个,不要重启webpack-dev-server
  4. 这将出现错误,因为缺少被注释掉的内容(在本例中为bootstrap
  5. 撤消/取消注释临时更改
  6. 重新加载页面-希望它应该正常工作(Webpack编译等)。

我认为(猜测)这是某些缓存/同步问题,通过对application.js进行一些更改可以"修复/重置"它。

希望对你有所帮助...


1
谢谢!一开始就遇到这个问题真的很让人沮丧。顺便说一下,我刚刚在我的app/javascript/packs/application.js文件中添加了一个console.log()语句并按照你说的做了。效果非常好! - Topher Fangio

5
如果Webpacker不再响应应用程序中的application.scss或application.js文件的更改,请在终端中运行此命令:
rails webpacker:install

这对我来说非常有效,因为我使用Vue,所以我必须运行以下命令: rails webpacker:install:vue - Yamit

2

所以,今天我遇到了这个问题,在阅读这个问题并进行一些额外的调试后,我意识到Rails Webpacker失败是因为我已经运行了另一个项目的Webpacker,并且这使得Rails版本感到困惑。

关闭其他Webpacker并立即重新启动Rails服务器即可解决该问题。


0

我的情况是我使用nvm更改了node的版本。

当一切都停止编译时,我忘记了切换版本只是卸载了yarn,这很正常,因为我正在使用另一个node/npm镜像,并且由于yarn是由npm全局安装的...

  1. 运行bin/rails webpacker:install,它会说:"Yarn未安装。请从https://yarnpkg.com/lang/en/docs/install/下载并安装Yarn。退出!"
  2. 使用npm i -g yarn重新安装yarn
  3. 运行您喜爱的bin/rails s命令,完成

注意:您将再次在终端中看到[Webpacker]正在执行其工作✅


0

我在一个已经有一段时间没有碰过的项目中遇到了webpacker编译在测试环境下无法运行的问题。 bundle update webpacker解决了这个问题。奇怪的是,回滚到旧的Gemfile.lock现在也可以工作,所以我不知道问题到底出在哪里。


0

在尝试刺激反射时,这让我抓狂了。这对我有用(尽管之后又遇到了另一个Stimiuls Reflex问题:ActionCable连接未打开!,但这是进步。

  1. 在终端中执行rails dev:cache,这可能会禁用缓存
  2. 运行你的rails server
  3. 然后在关闭rails服务器后,在终端中重新运行rails dev:cache

environments/development.rb中,相关配置如下:

  config.action_controller.perform_caching = true
  config.action_controller.enable_fragment_cache_logging = true

  config.cache_store = :memory_store
  config.public_file_server.headers = {
    'Cache-Control' => "public, max-age=#{2.days.to_i}",
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接