Ruby-on-rails Rails config.assets.precompile 设置来处理 app/assets 中的所有 CSS 和 JS 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10097993/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:12:42  来源:igfitidea点击:

Rails config.assets.precompile setting to process all CSS and JS files in app/assets

ruby-on-railsruby-on-rails-3sprockets

提问by keithgaputis

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

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

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.

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

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

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

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

采纳答案by keithgaputis

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.

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

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

假设我的项目有 JS 文件“/app/assets/javascripts/foo/bar.js.coffee”。

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.

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

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.

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

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

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

# 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
}


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

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

To workaround you replace the full_pathassignment with:

要解决方法,您将full_path分配替换为:

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

See also: https://github.com/rails/sprockets-rails/issues/237

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

回答by techpeace

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

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

That will compile any JavaScript or CSS in your asset path, regardless of directory depth. Found via this answer.

无论目录深度如何,这都将编译资产路径中的任何 JavaScript 或 CSS。通过这个答案找到。

回答by eltiare

A slight tweak to techpeace's answer:

对 techpeace 的回答稍作调整:

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

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

I would've added a comment to his answer but I don't have enough reputation yet. Give me an upvote and I'll be there!

我会在他的回答中添加评论,但我还没有足够的声誉。给我一个upvote,我会在那里!

NOTE: this will also precompile all the CSS/JavaScript included via rubygems.

注意:这也将预编译通过 ruby​​gems 包含的所有 CSS/JavaScript。

回答by Michael Slade

I found this in the rails code:

我在 Rails 代码中找到了这个:

@assets.precompile               = [ Proc.new{ |path| !File.extname(path).in?(['.js', '.css']) },
                                     /(?:\/|\|\A)application\.(css|js)$/ ]

Which is backed up with the rails guide:

这是由导轨指南支持的:

The default matcher for compiling files includes application.js, application.css and all non-JS/CSS files

编译文件的默认匹配器包括 application.js、application.css 和所有非 JS/CSS 文件

This default is not reset if you use +=, so you need to override it with a =instead of +=. Note that, apparently, you can pass a Proc or a regex to precompileas well as an extension. I believe, if you want to preompile only files in the top level directory, you will have to create a regex like:

如果您使用+=,则不会重置此默认值,因此您需要使用=代替来覆盖它+=。请注意,显然,您可以将 Proc 或正则表达式传递给precompile以及扩展。我相信,如果您只想预编译顶级目录中的文件,则必须创建一个正则表达式,如:

config.assets.precompile = [ /\A[^\/\]+\.(ccs|js)$/i ]

回答by Evi Song

I'm revisiting this post at the year 2017.

我在 2017 年重温了这篇文章。

Our product is still heavily using RoR, we've been continuously modifying our precompile configurations by appending Rails.application.config.assets.precompileas we're adding new modules. Recently I was trying to optimize this by adding a regex pattern, I found that the following glob pattern works:

我们的产品仍然大量使用 RoR,我们一直在通过Rails.application.config.assets.precompile添加新模块来不断修改我们的预编译配置。最近我试图通过添加正则表达式模式来优化它,我发现以下 glob 模式有效:

Rails.application.config.assets.precompile += %w(**/bundle/*.js)

However I still need to exclude certain modules, so I tried hard to use regex instead of glob.

但是我仍然需要排除某些模块,所以我努力使用正则表达式而不是 glob。

Until I looked into sprockets source code: https://github.com/rails/sprockets-rails/blob/master/lib/sprockets/railtie.rb#L108, I found that they're already using regex:

直到我查看链轮源代码:https: //github.com/rails/sprockets-rails/blob/master/lib/sprockets/railtie.rb#L108,我发现他们已经在使用正则表达式:

app.config.assets.precompile +=
  [LOOSE_APP_ASSETS, /(?:\/|\|\A)application\.(css|js)$/]

So I change my code into:

所以我把我的代码改成:

Rails.application.config.assets.precompile +=
  [/^((?!my_excluded_module)\w)*\/bundle\/\w*\.js$/]

Which works well.

哪个效果很好。

回答by TJ Biddle

This will get all .css.scssand .jsincluding all files in subdirectories.

这将获取所有.css.scss.js包括子目录中的所有文件。

js_prefix    = 'app/assets/javascripts/'
style_prefix = 'app/assets/stylesheets/'

javascripts = Dir["#{js_prefix}**/*.js"].map      { |x| x.gsub(js_prefix,    '') }
css         = Dir["#{style_prefix}**/*.css"].map  { |x| x.gsub(style_prefix, '') }
scss        = Dir["#{style_prefix}**/*.scss"].map { |x| x.gsub(style_prefix, '') }

Rails.application.config.assets.precompile = (javascripts + css + scss)

回答by Piotr Kuczynski

I wanted all assets from both /app and /vendor to be compiled, except partials (which name starts from underscore _). So here is my version of an entry in application.rb:

我希望编译来自 /app 和 /vendor 的所有资产,除了部分(名称以下划线 _ 开头)。所以这是我在 application.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
    vendor_assets_path = Rails.root.join('vendor', 'assets').to_path

    if ((full_path.starts_with? app_assets_path) || (full_path.starts_with? vendor_assets_path)) && (!path.starts_with? '_')
      puts "\t" + full_path.slice(Rails.root.to_path.size..-1)
      true
    else
      false
    end
  else
    false
  end
}

Additionally it outputs list of files being compiled for debugging purposes...

此外,它还输出为调试目的而编译的文件列表...

回答by proteantech

This snippet includes all js/css files, excluding gems, under: app/assets, vendor/assets, lib/assets, unless they are partial files (e.g. "_file.sass"). It also has a strategy for including assets from Gems which aren't included in every page.

此代码段包括所有 js/css 文件,不包括 gems,在: app/assets, vendor/assets, lib/assets 下,除非它们是部分文件(例如“_file.sass”)。它还具有包含来自 Gems 的资产的策略,这些资产并未包含在每个页面中。

    # These assets are from Gems which aren't included in every page.
    # So they must be included here
    # instead of in the application.js and css manifests.
    config.assets.precompile += %w(a-gem.css a-gem.js b-gem.js)

    # This block includes all js/css files, excluding gems,
    # under: app/assets, vendor/assets, lib/assets
    # unless they are partial files (e.g. "_file.sass")
    config.assets.precompile << Proc.new { |path|
      if path =~ /\.(css|js)\z/
        full_path = Rails.application.assets.resolve(path).to_path
        aps = %w( /app/assets /vendor/assets /lib/assets )
        if aps.any? {|ap| full_path.starts_with?("#{Rails.root}#{ap}")} &&
            !path.starts_with?('_')
          puts "\tIncluding: " + full_path.slice(Rails.root.to_path.size..-1)
          true
        else
          puts "\tExcluding: " + full_path
          false
        end
      else
        false
      end
    }

Although, you probably wouldn't want to do this since you'd likely be precompiling gem assets twice (basically anything that is already \=require'd in your application.js or css). This snippet includes all js/css files, including gems, under: app/assets, vendor/assets, lib/assets, unless they are partial files (e.g. "_file.sass")

虽然,您可能不想这样做,因为您可能会预编译两次 gem 资产(基本上是 application.js 或 css 中已经 \=require 的任何内容)。此代码段包括所有 js/css 文件,包括 gems,在: app/assets, vendor/assets, lib/assets 下,除非它们是部分文件(例如“_file.sass”)

# This block includes all js/css files, including gems, 
# under: app/assets, vendor/assets, lib/assets
# and excluding partial files (e.g. "_file.sass")
config.assets.precompile << Proc.new { |path|
  if path =~ /\.(css|js)\z/
    full_path = Rails.application.assets.resolve(path).to_path
    asset_paths = %w( app/assets vendor/assets lib/assets)
    if (asset_paths.any? {|ap| full_path.include? ap}) && 
        !path.starts_with?('_')
      puts "\tIncluding: " + full_path
      true
    else
      puts "\tExcluding: " + full_path
      false
    end
  else
    false
  end
}