Rails 3-预编译文件夹中的所有css,sass和scss文件 [英] Rails 3 - Precompiling all css, sass and scss files in a folder

查看:122
本文介绍了Rails 3-预编译文件夹中的所有css,sass和scss文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个具有页面特定样式和CSS的项目.我已经将这些文件放入了它们自己的目录assets/#{type}/page-specific/中,并且试图编写一个正则表达式规则来通知预编译器对它们进行处理.

So I have a project with page-specific styles and css. I've put those files into their own directory assets/#{type}/page-specific/ and I'm attempting to write a regex rule to inform the precompiler to do it's thing with them.

这对于房子的JS端来说非常有用,但是事实证明CSS更加困难.

This is working great for the JS side of the house, but the css is proving more difficult.

# page specific style files:
app/assets/stylesheets/page-specific/something.css.sass
app/assets/stylesheets/page-specific/default.css
app/assets/stylesheets/page-specific/home.css.scss

我正在config/initializers/assets.rb文件中使用此正则表达式:

I was working with this regex in my config/initializers/assets.rb file:

Rails.application.config.assets.precompile << /(page-specific\/[^(css)]+.css)/

问题在于[^(css)]部分不能完全满足我的要求,因为它会过滤掉其中包含c或s的所有内容.我真正想要的是一种匹配整个字符串"css"并在此停止的方法.

The problem being that the [^(css)] section doesn't do quite what I want, as it will filter out anything that has a c or s in it. What I'm really looking for is a way to match up to the whole string "css" and stop there.

此外,我知道传统的想法是简单地编写样式,而无需使用特定于页面的样式表,而使用包含整个样式表的application.css.我同意.但是,这是一个非常庞大的代码库,要带回家这个Rails升级有很多时间压力,而且没有时间花在分析和重构这种性质的事情上.

Also, I know the conventional wisdom would be to simply write the styles in a way that you wouldn't need page-specific stylesheets in favor of application.css containing the whole of the stylesheets. I agree. However this is a very large code base and there is a lot of time pressure to bring this Rails upgrade home, and there isn't time to spend analyzing and refactoring things of this nature.

我尝试仅使定界符指令[^\.]停在第一个点,但是这在一些使用点作为定界符的供应商样式表上会爆炸,例如jquery.treeview.css.sass.显然,我可以更改文件名,但是我们的团队正在不断壮大,我想使规则尽可能灵活,这样我们就不会潜入文件,而当文件丢失时,它们会在生产中爆炸预编译的.

I've tried just making the delimiter directive [^\.] to stop at the first dot, however this blows up on some vendor stylesheets that use the dot as a delimiter such as jquery.treeview.css.sass. Obviously I can just change the file names, but we are a large team that's growing, and I want to make the rule as flexible as possible so we don't have files sneak in that will blow up in production when they don't get precompiled.

推荐答案

此答案专门用于正则表达式.您正在寻找缺少字符序列css"来代替[^(css)].

This answer is specifically for the regex. You're looking for "absence of character sequence css" in place of [^(css)].

此正则表达式与您的正则表达式类似:

This regex takes a similar approach to yours:

/(page-specific\/(?:(?!\.css).)++\.css)/

说明:

  • page-specific\/从字面上匹配字符序列"特定于页面/".
  • (?:打开一个非捕获组.
    • (?!\.css)断言当前索引不是字符序列" .css ".
    • page-specific\/ matches the character sequence "page-specific/" literally.
    • (?: opens a non-capturing group.
      • (?!\.css) asserts that the current index is not of character sequence ".css".

      如果该匹配在第一次匹配该组时失败,则当前部分将不匹配(页面特定/.css不好),否则这将终止我们的组量化已经匹配 .css ,并返回到\.css)/完成.

      If this assertion fails at the first time this group is matched, the current section will fail to be matched (page-specific/.css is no good), else this terminates group quantification as we've already matched .css, and returns to \.css)/ to finish.

        • .匹配除换行符以外的所有内容.
            • . matches anything except for newline.
            • 了解更多信息

              • Possessive Quantifiers
              • Lookahead and Lookbehind Zero-Length Assertions

              但是,如果什么也没有,则此正则表达式使用非贪婪修饰符来实现:

              However if nothing, this regex does it with a nongreedy modifier:

              /(page-specific\/.+?\.css)/
              

              由于.+?匹配的次数越少越好,因此可以确保捕获的结果在第一个.css处结束.

              As .+? matches as few times as possible, the resulting capture is guaranteed to end at the first .css.

              了解更多信息

              PS::您可能忘记了在.css/中正则表达式末尾附近的一个点.

              PS: You may have forgotten to escape a dot near the end of your regex in .css/.

              由以下人员指定:
              http://www.tutorialspoint.com/ruby/ruby_regular_expressions. htm

              这篇关于Rails 3-预编译文件夹中的所有css,sass和scss文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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