Rails 中正确的 SCSS 资产结构
所以,我有一个 app/assets/stylesheets/
目录结构,看起来像这样:
|-dialogs
|-mixins
|---buttons
|---gradients
|---vendor_support
|---widgets
|-pages
|-structure
|-ui_elements
在每个目录中,有多个 sass 部分(通常是 *.css.scss,但一两个 *.css .scss.erb)。
我可能假设了很多,但是由于 application.css 中的 *= require_tree .
,rails 应该自动编译这些目录中的所有文件,对吗?
我最近尝试通过删除所有颜色变量并将它们放入根 app/assets/stylesheets
文件夹 (_colors.css.scss) 中的文件中来重组这些文件。然后,我在根 app/assets/stylesheets
文件夹中创建了一个名为 master.css.scss 的文件,如下所示:
// Color Palette
@import "colors";
// Mixins
@import "mixins/buttons/standard_button";
@import "mixins/gradients/table_header_fade";
@import "mixins/vendor_support/rounded_corners";
@import "mixins/vendor_support/rounded_corners_top";
@import "mixins/vendor_support/box_shadow";
@import "mixins/vendor_support/opacity";
我不太明白 Rails 如何处理资产编译的顺序,但显然它是不利于我。似乎没有一个文件意识到它们有任何变量或混入被导入,所以它会抛出错误,我无法编译。
Undefined variable: "$dialog_divider_color".
(in /home/blah/app/assets/stylesheets/dialogs/dialog.css.scss.erb)
Undefined mixin 'rounded_corners'.
(in /home/blah/app/assets/stylesheets/widgets.css.scss)
变量 $dialog_divider_color
在 _colors.css.scss 中明确定义,并且 _master.css.scss
正在导入颜色和我所有的 mixin。但显然 Rails 没有收到这份备忘录。
有什么方法可以修复这些错误,或者我需要将所有变量定义以及所有 mixin 导入放回到每个单独的文件中?
不幸的是,这个人似乎认为这是不可能的,但是我希望他是错的。任何想法都将不胜感激。
So, I have an app/assets/stylesheets/
directory structure that looks something like this:
|-dialogs
|-mixins
|---buttons
|---gradients
|---vendor_support
|---widgets
|-pages
|-structure
|-ui_elements
In each directory, there are multiple sass partials (usually *.css.scss, but one or two *.css.scss.erb).
I might be assuming a lot, but rails SHOULD automatically compile all the files in those directories because of *= require_tree .
in application.css, right?
I recently have tried restructuring these files by removing all color variables and placing them in a file in the root app/assets/stylesheets
folder (_colors.css.scss). I then created a file in the root app/assets/stylesheets
folder called master.css.scss which looks like this:
// Color Palette
@import "colors";
// Mixins
@import "mixins/buttons/standard_button";
@import "mixins/gradients/table_header_fade";
@import "mixins/vendor_support/rounded_corners";
@import "mixins/vendor_support/rounded_corners_top";
@import "mixins/vendor_support/box_shadow";
@import "mixins/vendor_support/opacity";
I don't really understand how rails handles the order of asset compilation, but it's obviously not in my favor. It appears none of the files realize they have any variables or mixins being imported, and so it throws errors and I can't compile.
Undefined variable: "$dialog_divider_color".
(in /home/blah/app/assets/stylesheets/dialogs/dialog.css.scss.erb)
Undefined mixin 'rounded_corners'.
(in /home/blah/app/assets/stylesheets/widgets.css.scss)
The variable $dialog_divider_color
is clearly defined in _colors.css.scss, and _master.css.scss
is importing colors and all my mixins. But apparently rails didn't get that memo.
Is there some way I can fix these errors, or will I need to resort to putting all my variable definitions back into each individual file, as well as all the mixin imports?
Unfortunately, this guy doesn't seem to think it's possible, but I'm hoping he's wrong. Any thoughts are greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
CSS 的问题是,您不希望自动添加所有文件。
浏览器加载和处理工作表的顺序至关重要。所以你最终总是会显式导入所有的 css。
举个例子,假设您有一个 normalize.css 表,以获得默认外观而不是所有可怕的不同浏览器实现。这应该是浏览器加载的第一个文件。如果您只是在 css 导入中随机包含此表,那么它不仅会覆盖浏览器默认样式,还会覆盖在其之前加载的所有 css 文件中定义的任何样式。这对于变量和 mixin 来说也是一样的。
在 Euruko2012 上看到 Roy Tomeij 的演示后,如果您有大量 CSS 需要管理,我决定采用以下方法。
我通常使用这种方法:
开始将 @import 指令添加到
application.scss
中。如果您使用 twitters bootstrap 和一些自己的 css 表,则必须首先导入 bootstrap,因为它有一个用于重置样式的表。
因此,您将
@import "bootstrap/bootstrap.scss";
添加到application.scss
中。bootstrap.scss 文件如下所示:
您的
application.scss
文件如下所示:由于导入的顺序,您现在可以使用通过
@import "variables.scss 加载的变量";
位于其之后导入的任何其他.scss
文件中。因此它们可以在 bootstrap 文件夹中的 type.scss 中使用,也可以在 my_model.css.scss 中使用。之后创建一个名为
partials
或modules
的文件夹。这将是大多数其他文件的位置。您只需将导入添加到application.scss
文件中,这样它看起来就像:现在,如果您制作一些 css 来设置主页上文章的样式。只需创建
partials/_article.scss
即可将其添加到编译后的application.css
中。由于导入顺序的原因,您还可以在自己的 scss 文件中使用任何引导混合和变量。到目前为止,我发现这种方法的唯一缺点是,有时您必须强制重新编译partial/*.scss 文件,因为rails 并不总是为您执行此操作。
The problem with CSS is, you do not want to automatically add all files.
The order of which your sheets are loaded and processed by the browser is essential. So you will always end up explicitly importing all your css.
As an example, lets say you have a normalize.css sheet, to get a default look instead of all the horrible different browser implementations. This should be the first file the browser loads. If you just randomly include this sheet somewhere in your css imports, it will then not only override the browser default styles, but also any styles defined in all css files that were loaded before it. This goes the same for variables and mixins.
After seeing a presentation by Roy Tomeij at Euruko2012 I decided for the following approach if you have a lot of CSS to manage.
I generally use this approach:
Start adding @import directives to
application.scss
.If you are using twitters bootstrap and a few css sheets of your own, you have to import bootstrap first, because it has a sheet to reset styles.
So you add
@import "bootstrap/bootstrap.scss";
to yourapplication.scss
.The bootstrap.scss file looks like:
And your
application.scss
file look like:Because of the order of the imports, you can now use the variables, loaded with
@import "variables.scss";
in any other.scss
file imported after it. So they can be used intype.scss
in the bootstrap folder but also inmy_model.css.scss
.After this create a folder named
partials
ormodules
. This will be the place of most of the other files. You can just add the import to theapplication.scss
file so it will look like:Now if you make a bit of css to style an article on your homepage. Just create
partials/_article.scss
and it will be added to the compiledapplication.css
. Because of the import order you can also use any bootstrap mixins and variables in your own scss files.The only drawback of this method I found so far is, sometimes you have to force a recompile of the partial/*.scss files because rails wont always do it for you.
创建以下文件夹结构:
在文件夹
base
中创建一个文件“globals.css.scss”。在此文件中,声明所有导入:在 application.css.scss 中,您应该:
作为最后一步(这很重要),在每个中声明
@import 'base/globals'
要在其中使用变量或 mixin 的样式文件。您可能会考虑这种开销,但我实际上喜欢这样的想法:您必须在每个文件中声明样式的依赖关系。当然,重要的是您只在 globals.css.scss 中导入 mixins 和变量,因为它们不添加样式定义。否则,样式定义将多次包含在您的预编译文件中......Create the following folder structure:
In folder
base
create a file "globals.css.scss". In this file, declare all your imports:In you application.css.scss, you should then have:
And as the last step (this is important), declare
@import 'base/globals'
in every style file where you want to use variables or mixins. You might consider this overhead, but I actually like the idea that you have to declare the dependencies of your styles in every file. Of course, it is important that you only import mixins and variables in the globals.css.scss as they do not add style definitions. Otherwise the style definitions would be included multiple times in your precompiled file ...要跨文件使用变量等,您需要使用 @import 指令。文件按指定的顺序导入。
然后,使用 application.css 来请求声明导入的文件。这就是实现您想要的控制的方法。
最后,在你的layout.erb文件中,你可以指定使用哪个“主”css文件
示例会更有帮助:
假设你的应用程序中有两个模块需要不同的css集:“application”和“admin
”文件
如下是文件内部的样子:
to use variables and such across files, you need to use the @import directive. files are imported in order specified.
then, use application.css to require the file that declares the imports. this is the way to achieve the control you want.
finally, in your layout.erb file, you can specify which "master" css file to use
example will be more helpful:
let's say you have two modules in your app that need different sets of css: "application" and "admin"
the files
here's what the files look like inside:
根据这个问题,你可以仅< /strong> 使用
application.css.sass
来定义模板之间的导入和共享变量。=>看来这只是一个名字的问题。
另一种方法可以是 包含所有内容并禁用此管道。
According to this question, you can ONLY use
application.css.sass
in order to define import and share variables between your templates.=> It seems to be only a matter of name.
An other way can be to include everything and disable this pipeline.
我有一个非常相似的问题。对我有帮助的是在导入部分时在 @import 语句中添加下划线。所以
这
可能是一个奇怪的错误......
I had a very similar problem. What helped me was to put in the underscore to the @import statement when importing the partial. So
instead of
It might be a strange bug...
我的解决方案是拥有一个包含所有导入的
application.css.scss
:然后将: 添加
到使用常量的文件中
My solution was to have an
application.css.scss
with all the imports:And then add:
to the files that use the constants