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

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

问题描述

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

这对于房子的 JS 方面非常有效,但事实证明 css 更加困难.

# 页面特定的样式文件:app/assets/stylesheets/page-specific/something.css.sassapp/assets/stylesheets/page-specific/default.cssapp/assets/stylesheets/page-specific/home.css.scss

我在我的 config/initializers/assets.rb 文件中使用了这个正则表达式:

Rails.application.config.assets.precompile <</(特定页面/[^(css)]+.css)/

问题在于 [^(css)] 部分并没有完全按照我的要求做,因为它会过滤掉任何包含 c 或 s 的内容.我真正在寻找的是一种匹配整个字符串css"并在那里停止的方法.

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

我已经尝试让分隔符指令 [^.] 停在第一个点处,但是这在一些使用点作为分隔符的供应商样式表上会爆炸,例如 jquery.treeview.css.sass.显然我可以只更改文件名,但我们是一个不断壮大的大团队,我想让规则尽可能灵活,这样我们就不会让文件偷偷溜进来,当他们没有得到时会在生产中爆炸预编译.

解决方案

这个答案是专门针对正则表达式的.您正在寻找缺少字符序列 css"来代替 [^(css)].

<小时>

此正则表达式采用与您类似的方法:

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

说明:

  • page-specific/ 字面匹配字符序列page-specific/".
  • (?: 打开一个非捕获组.
    • (?!.css) 断言当前索引不是字符序列.css".

如果这个断言在第一次匹配这个组时失败,则当前部分将无法匹配(特定页面/.css 不好),否则这将终止组量化,因为我们已经匹配.css,返回.css)/完成.

    • . 匹配除换行符以外的任何内容.
  • )++ 关闭组并无限次地限定所有格(不回馈).
    注意:Ruby 从 Ruby 1.9 开始支持所有格限定符.
  • .css 字面匹配字符序列.css".

阅读更多:

  • 阅读更多:

    <小时>

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

    <小时><块引用>

    指定者:
    http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm

    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.

    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
    

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

    Rails.application.config.assets.precompile << /(page-specific/[^(css)]+.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.

    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.

    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.

    解决方案

    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)/
    

    Explanation:

    • 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".

    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.
    • )++ closes the group and quantifies to unlimited times possessively (Without giving back).
      Note: Ruby supports possessive quantifiers starting with Ruby 1.9.
    • .css matches the character sequence ".css" literally.

    Read more:


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

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

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

    Read more:


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


    Specified by:
    http://www.tutorialspoint.com/ruby/ruby_regular_expressions.htm

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

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