在应用程序和源代码管理中存储 SASS 生成的 CSS 的最佳方法是什么?
如果您在 Rails 应用程序中使用 HAML 和 SASS,那么您在 public/stylesheet/*.sass 中定义的任何模板都将被编译为 *.css 样式表。 在代码中,您可以使用 stylesheet_link_tag 按名称拉入资源,而不必担心扩展名。
许多人不喜欢在版本控制中存储生成的代码或编译的代码,而且 public/ 目录不应该包含不发送到浏览器的元素也是理所当然的。
在 Rails 项目中布置 SASS 资源时应遵循的最佳模式是什么?
If you are using HAML and SASS in your Rails application, then any templates you define in public/stylesheet/*.sass will be compiled into *.css stylesheets. From your code, you use stylesheet_link_tag to pull in the asset by name without having to worry about the extension.
Many people dislike storing generated code or compiled code in version control, and it also stands to reason that the public/ directory shouldn't contain elements that you don't send to the browser.
What is the best pattern to follow when laying out SASS resources in your Rails project?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果我可以管理它,当我为项目选择 HAML/SASS 时,我喜欢将所有样式存储在 SASS 模板中,并且我将删除 application.css 和scaffold.css。 然后我将SASS放在public/stylesheets/sass中,并将/public/stylesheets/*.css添加到.gitignore中。
如果我必须结合使用基于 SASS 和 CSS 的资产,那就有点复杂了。 处理此问题的最简单方法是在 stylesheets 目录中为生成的 CSS 创建一个输出子目录,然后在 .gitignore 中排除该子目录。 然后,在您的视图中,您必须通过选择 public/stylesheets/foo 样式表或 public/stylesheets/sass-out/foo 样式表来了解您正在使用哪种样式类型(SASS 或 CSS)。
如果您必须走第二条路线,请构建一个助手来抽象出 sass-out 子目录。
If I can manage it, I like to store all of my styles in SASS templates when I choose HAML/SASS for a project, and I'll remove application.css and scaffold.css. Then I will put SASS in public/stylesheets/sass, and add /public/stylesheets/*.css to .gitignore.
If I have to work with a combination of SASS and CSS based assets, it's a little more complicated. The simplest way of handling this is to have an output subdirectory for generated CSS within the stylesheets directory, then exclude that subdirectory in .gitignore. Then, in your views you have to know which styling type you're using (SASS or CSS) by virtue of having to select the public/stylesheets/foo stylesheet or the public/stylesheets/sass-out/foo stylesheet.
If you have to go the second route, build a helper to abstract away the sass-out subdirectory.
我总是在“public/stylesheets/sass/*.sass”中对所有样式表进行版本控制,并为已编译的样式表设置排除过滤器:
I always version all stylesheets in "public/stylesheets/sass/*.sass" and set up an exclude filter for compiled ones:
老实说,我喜欢在版本控制中使用已编译的 SASS 样式表。 它们很小,仅当您的 .sass 文件更改时才会更改,并且将它们与应用程序的其余部分一起部署意味着 SASS 编译器不需要在生产中触发。
另一个优点(尽管很小)是,如果您不使用页面缓存,则您的 Rails 进程不需要对
public_html
目录具有写入权限。 因此,利用您的服务器进行恶意攻击的方式就少了一种。Honestly, I like having my compiled SASS stylesheets in version control. They're small, only change when your .sass files change, and having them deploy with the rest of your app means the SASS compiler doesn't ever need to fire in production.
The other advantage (albeit a small one) is that if you're not using page caching, your rails process doesn't need to have write access to your
public_html
directory. So there's one fewer way an exploit of your server can be evil.有些相关,但在 capistrano 部署期间重新生成 CSS 是个好主意。 这个回调钩子的作用就是:
更新:对于现代版本的 Haml/Sass 来说,这不再是必需的。
Somewhat related, but it's a good idea to regenerate your CSS during your capistrano deployments. This callback hook does just that:
Update: This should no longer be necessary with modern versions of Haml/Sass.
compass 框架建议将 sass 样式表放在 app/stylesheets 下,将编译后的 css 放在 public/stylesheets/compiled 下。
您可以通过将以下代码添加到您的environment.rb来配置它:
如果您使用compass框架,它会在您安装它时为您设置此配置。
The compass framework recommends putting your sass stylesheets under app/stylesheets and your compiled css in public/stylesheets/compiled.
You can configure this by adding the following code to your environment.rb:
If you use the compass framework, it sets up this configuration for you when you install it.