如何在预编译资产时临时更改输出路径,Sprockets / Rails 资产管道,3.1.0

发布于 2024-12-02 11:35:23 字数 460 浏览 3 评论 0原文

我正在尝试更新此代码以与已发布的 Rails 3.1.0 配合使用:

  # temporarily set the static assets location from public/assets to our spec directory
  ::Rails.application.assets.static_root = Rails.root.join("spec/javascripts/generated/assets")

  ::Rake.application['assets:clean'].invoke
  ::Rake.application['assets:precompile'].invoke

既然 Sprockets::Environment#static_root 已被删除,那么临时更改 sprockets 输出目录的最佳方法是什么?

编辑:我还希望能够清理自定义输出目录中的资产:)

I'm trying to update this code to work with the released Rails 3.1.0:

  # temporarily set the static assets location from public/assets to our spec directory
  ::Rails.application.assets.static_root = Rails.root.join("spec/javascripts/generated/assets")

  ::Rake.application['assets:clean'].invoke
  ::Rake.application['assets:precompile'].invoke

Now that Sprockets::Environment#static_root has been removed, what's the best way temporarily change the sprockets output directory?

Edit: Also I'd like to be able to clean the assets in my custom output directory :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不回头走下去 2024-12-09 11:35:23

您可以使用 config.assets.prefix,但这仍然会将资产放在公共目录中(请参阅此处 用于 rake 任务,它连接 public_path 和前缀)。

在您的情况下,这应该可行:

Rails.application.config.assets.prefix = "../spec/javascripts/generated/assets"
Rails.application.config.assets.manifest = File.join(Rails.public_path, config.assets.prefix)

由于链轮轨带的奇怪加载顺序,我必须指定清单路径。如果不这样做,它就会卡在不存在的 public/assets 上,并破坏 rake 任务。 YMMV。

旁注:我首先在开发环境中尝试过此操作,但 config.assets.prefix 拒绝更改。我怀疑将 config.assets.enabled 设置为 true 可以解决此问题,但我还没有时间测试它。

作为奖励,assets:clean 与此解决方案完美配合(您可以亲自查看它在 rake 任务中

You can use config.assets.prefix, but this will still put the assets in the public directory (see here for the rake task, which joins the public_path and the prefix).

In your case, this should work:

Rails.application.config.assets.prefix = "../spec/javascripts/generated/assets"
Rails.application.config.assets.manifest = File.join(Rails.public_path, config.assets.prefix)

I had to specify the manifest path because of the weird load order of the sprockets railtie. Without doing it, it gets stuck at public/assets, which doesn't exist and blows up the rake task. YMMV.

Side note: I tried this in the development environment at first, but the config.assets.prefix refused to change. I suspect putting config.assets.enabled to true would have fixed this, but I haven't got around to testing it yet.

As a bonus, the assets:clean works perfectly with this solution (you can see it for yourself in the rake task)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文