【问题标题】:Upgrading webpacker from 4.2.2 to 5.0.1 is no longer running yarn install before assets:precompile将 webpacker 从 4.2.2 升级到 5.0.1 不再在 assets:precompile 之前运行 yarn install
【发布时间】:2020-07-18 23:03:26
【问题描述】:

在执行webpacker upgrading 部分中的每个步骤后,从4.2.2 升级到5.0.1 会导致yarn installrails assets:precompile 期间不再被调用。这导致在资产编译过程中找不到来自我们的package.json 文件的前端包。

当 CI 运行 rails assets:precompile 时会突出显示。以前assets:precompile 会在编译我们的application.js 文件之前运行yarn install。我添加了--trace 标志,它的运行就好像它跳过了yarn install

** Invoke yarn:install (first_time)
** Execute yarn:install
** Execute assets:precompile
rails aborted!
Sprockets::FileNotFound: couldn't find file 'moment' with type 'application/javascript'

使用旧版 webpack 运行的相同 CI 会按预期显示 yarn install 输出。有人在升级到 5.x 时遇到过同样的问题吗?

【问题讨论】:

  • 也许对 yarn install 任务的改变正在影响你。 github.com/rails/webpacker/compare/…。在 CI 服务器上的应用程序上下文中,NODE_ENV 是什么? yarn install --no-progress --frozen-lockfile 输出什么?

标签: ruby-on-rails sprockets webpacker


【解决方案1】:

从 config/application.rb 注释/远程处理 sprockets 行:

# config/application.rb

# Remove the line below
 require "sprockets/railtie"

您还可以从所有 config/environment/*.rb 文件中删除 config.assets ... 行:

# config/environment/*.rb

# Remove the following lines
  config.assets.debug = true
  config.assets.quiet = true

Webpacker 不再需要这些,也可能导致错误。

希望有帮助:)

【讨论】:

  • 感谢您的回复。不幸的是,这些建议都不适用:/。我们将调试设置为 false 并且不存在安静。我们也不需要“sprockets/railtie”。
  • 如果引发 Sprockets 异常,肯定需要 Sprockets。
  • 我们还没有完全消除对链轮的依赖。我们只有部分资产与 webpack 捆绑在一起。在 webpacker v5 之前,这一直运行良好。
【解决方案2】:

我们目前在 Rails 5.2.4 上,它没有 yarn binstub。 Webpacker 5.0.1 的 yarn install 任务调用 binstub 内部调用 yarnpkg。如果没有 binstub 的存在,rake 任务将什么也做不了,这正是我们所经历的。添加缺少的 binstub 解决了我们的问题。

/bin/yarn

#!/usr/bin/env ruby
APP_ROOT = File.expand_path('..', __dir__)
Dir.chdir(APP_ROOT) do
  begin
    exec "yarnpkg", *ARGV
  rescue Errno::ENOENT
    $stderr.puts "Yarn executable was not detected in the system."
    $stderr.puts "Download Yarn at https://yarnpkg.com/en/docs/install"
    exit 1
  end
end

【讨论】:

  • 您是如何添加缺少的 binstub 的?我也面临同样的问题...
  • @ManuelMeurer 我记得当我运行bundle install --binstubs 时它没有生成。我相信我最终从 Rails 源中拉出它。我将文件附加到答案中。希望有帮助!
猜你喜欢
  • 2016-12-03
  • 2013-09-04
  • 1970-01-01
  • 2021-07-15
  • 2018-01-18
  • 1970-01-01
  • 2019-02-21
  • 1970-01-01
  • 2022-12-12
相关资源
最近更新 更多