Rails config.assets.precompile 设置来处理 app/assets 中的所有 CSS 和 JS 文件 [英] Rails config.assets.precompile setting to process all CSS and JS files in app/assets

查看:21
本文介绍了Rails config.assets.precompile 设置来处理 app/assets 中的所有 CSS 和 JS 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望预编译我项目的 app/assets 文件夹中的所有 CSS 和 JS 文件.我不想预编译 vendor/assets 或 lib/assets 中的所有内容,仅根据需要预编译我的文件的依赖项.

I wish to precompile all the CSS and JS files in my project's app/assets folder. I do NOT want to precompile everything in vendor/assets or lib/assets, only the dependencies of my files as needed.

我尝试了以下通配符设置,但它错误地预编译了所有内容.这会导致大量额外工作,甚至在使用 bootstrap-sass 时会导致编译失败.

I tried the following wildcard setting, but it incorrectly precompiles everything. This results in lots of extra work and even causes a compilation failure when using bootstrap-sass.

config.assets.precompile += ['*.js', '*.css']

仅处理 app/assets 中的文件的最佳选择是什么?谢谢!

What is my best bet to only process my files in app/assets? Thanks!

推荐答案

由于 sprocket 使用的逻辑路径不包括底层未编译资源所在的位置,因此这项任务变得更加困难.

This task is made more difficult by the fact that sprockets works with logical paths that do not include where the underlying, uncompiled resourced is located.

假设我的项目有一个 JS 文件/app/assets/javascripts/foo/bar.js.coffee".

Suppose my project has the JS file "/app/assets/javascripts/foo/bar.js.coffee".

链轮编译器将首先确定输出文件扩展名,在本例中为.js",然后评估是否编译逻辑路径foo/bar.js".未编译的资源可能位于app/assets/javascripts"、vendor/assets/javascripts"、lib/assets/javascripts"或 gem 中,因此无法根据逻辑路径包含/排除特定文件一个人.

The sprockets compiler will first determine the output file extension, in this case ".js", and then the evaluate whether or not to compile the logical path "foo/bar.js". The uncompiled resource could be in "app/assets/javascripts", "vendor/assets/javascripts", "lib/assets/javascripts" or a gem, so there is no way to include/exclude a particular file based on the logical path alone.

要确定底层资源位于何处,我认为有必要询问链轮环境(可通过对象 Rails.application.assets 获得)在给定逻辑路径的情况下解析资源的真实路径.

To determine where the underlying resource is located, I believe it is necessary to ask the sprockets environment (available via the object Rails.application.assets) to resolve the real path of the resource given the logical path.

这是我正在使用的解决方案.我对 Ruby 还很陌生,所以这不是最优雅的代码:

Here is the solution that I am using. I am fairly new to Ruby so this is not the most elegant code:

# In production.rb
config.assets.precompile << Proc.new { |path|
  if path =~ /\.(css|js)\z/
    full_path = Rails.application.assets.resolve(path).to_path
    app_assets_path = Rails.root.join('app', 'assets').to_path
    if full_path.starts_with? app_assets_path
      puts "including asset: " + full_path
      true
    else
      puts "excluding asset: " + full_path
      false
    end
  else
    false
  end
}

<小时>

如果链轮 > 3.0,这在生产环境中将不起作用,因为 Rails.application.assets 将为 nil(假设默认值:config.assets.compile = false).


With sprockets > 3.0, this will not work in production because Rails.application.assets will be nil (assuming default: config.assets.compile = false).

要解决此问题,请将 full_path 分配替换为:

To workaround you replace the full_path assignment with:

@assets ||= Rails.application.assets || Sprockets::Railtie.build_environment(Rails.application)
full_path = @assets.resolve(path)

另见:https://github.com/rails/sprockets-rails/issues/237

这篇关于Rails config.assets.precompile 设置来处理 app/assets 中的所有 CSS 和 JS 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆