资产管道/预编译资产任务

发布于 2024-12-29 11:16:16 字数 97 浏览 3 评论 0原文

如何使 rake 任务

assets:precompile 

在我的 Rails 2.3.14 应用程序中可用?

How I can make the rake task

assets:precompile 

available into my rails 2.3.14 app ?

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

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

发布评论

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

评论(2

星光不落少年眉 2025-01-05 11:16:16

如果您正在寻找 asset:precompile rake 任务的源代码,您可以在这里找到它:

https://github.com/rails/rails/blob/3-1-stable/actionpack/lib/sprockets/assets.rake

当您使用链轮和链轮助手 gem 将其复制到 Rails 2.3.14 应用程序中的 lib/tasks 时,不要指望它能按原样运行。

[更新]

我制作了一个简单的预编译器rake任务,用于rails 2.3.14(没有任何javascript压缩)。您可能想要更改一些内容,具体取决于您的配置。仔细测试清理任务,因为它使用 rm_rf 命令;-)

BUILD_DIR = Rails.root.join("public/assets")
DIGEST = true

namespace :assets do

  task :compile => :cleanup do

    sprockets = Sprockets::Environment.new
    sprockets.append_path 'app/assets/images'
    sprockets.append_path 'app/assets/javascripts'
    sprockets.append_path 'app/assets/stylesheets'

    sprockets.each_logical_path do |logical_path|
      if asset = sprockets.find_asset(logical_path)
        target_filename =  DIGEST ? asset.digest_path : asset.logical_path
        prefix, basename = asset.pathname.to_s.split('/')[-2..-1]
        FileUtils.mkpath BUILD_DIR.join(prefix)
        filename = BUILD_DIR.join(target_filename)
        puts "write asset: #{filename}"
        asset.write_to(filename)
        #asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/
      end
    end
  end

  # Cleanup asset directory
  task :cleanup do
    dirs = Dir.glob(File.join(BUILD_DIR.join("{*}")))
    dirs.each do |dir|
      puts "removing: #{dir}"
      FileUtils.rm_rf dir
    end
  end

end

[更新 #2]

我现在正在使用这种方法,并且效果很好:
http://jaredonline. github.io/blog/2012/05/16/sprockets-2-with-rails-2-dot-3/

If you are looking for the source code of the assets:precompile rake task, you can find it here:

https://github.com/rails/rails/blob/3-1-stable/actionpack/lib/sprockets/assets.rake

Don't expect it to run as-is when you copy it to your lib/tasks in your rails 2.3.14 app with the sprockets and sprockets-helpers gems.

[update]

I made a simple precompiler rake task for use in rails 2.3.14 (without any javascript compression). You might want to change some things, depending on your configuration. Test the clean up task carefully, because it use a rm_rf command ;-)

BUILD_DIR = Rails.root.join("public/assets")
DIGEST = true

namespace :assets do

  task :compile => :cleanup do

    sprockets = Sprockets::Environment.new
    sprockets.append_path 'app/assets/images'
    sprockets.append_path 'app/assets/javascripts'
    sprockets.append_path 'app/assets/stylesheets'

    sprockets.each_logical_path do |logical_path|
      if asset = sprockets.find_asset(logical_path)
        target_filename =  DIGEST ? asset.digest_path : asset.logical_path
        prefix, basename = asset.pathname.to_s.split('/')[-2..-1]
        FileUtils.mkpath BUILD_DIR.join(prefix)
        filename = BUILD_DIR.join(target_filename)
        puts "write asset: #{filename}"
        asset.write_to(filename)
        #asset.write_to("#{filename}.gz") if filename.to_s =~ /\.(css|js)$/
      end
    end
  end

  # Cleanup asset directory
  task :cleanup do
    dirs = Dir.glob(File.join(BUILD_DIR.join("{*}")))
    dirs.each do |dir|
      puts "removing: #{dir}"
      FileUtils.rm_rf dir
    end
  end

end

[update #2]

I am now using this approach, and that works fine:
http://jaredonline.github.io/blog/2012/05/16/sprockets-2-with-rails-2-dot-3/

淡莣 2025-01-05 11:16:16

没有一个简单的方法。资产管道依赖于 Rails 3.1.x 中的多个架构,而 Rails 2.3 中不存在这些架构。

您可以尝试使用戴维斯·弗兰克 (Davis Frank) 在此概述了,但请注意,这需要执行许多步骤。

There isn't a straightforward approach. The asset pipeline relies on several pieces of architecture in Rails 3.1.x that aren't present in Rails 2.3.

You can try using the approach that Davis Frank outlines here, but be warned that it requires a number of steps.

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