shakapacker:Webpacker 在 manifest.json 中找不到 subdir/image.png

shakapacker: Webpacker can't find subdir/image.png in manifest.json

我正在尝试将我的应用程序升级到 Rails 7 并决定升级 shakapacker(又名 webpacker 6)。

我喜欢将 app/javascript/images 中的图像组织到子目录中,但由于某些原因,整个结构被 skakapacker/webpack 压扁了。

所以 <%= image_pack_tag "subdir/image.png" %> 引发错误,但 <%= image_pack_tag "image.png" %> 有效。

到目前为止,我唯一的线索是 webpack-asset-manifest 的文档,其中说:will generate a JSON file that matches the original filename with the hashed version - 强调文件名。这可能是 webpack 时代以来的一个变化?

另外 manifest.json 现在与 shakapacker 看起来不一样了。 Webpcker 曾经是:

"media/images/favicons/a/favicon.ico": "/packs/media/images/favicons/a/favicon-bdfbf38fd01fae2abb3e1ac7de9c3a12.ico",

现在使用 shakapacker:

"static/image.png": "/packs-test/static/image-bd51daf42131d4298622.png"

您可以在我的 github 上找到最小的 rails 应用程序来复制它。

在你的 packs/application.js 中你应该有这个

const images = require.context('../images', true)

true 包括所有子目录

看看这个https://webpack.js.org/guides/dependency-management/

这确实是 webpacker6/shakapacker 中的错误。

https://github.com/shakacode/shakapacker/issues/44