如何在指南针中导入scss文件?

时间:2013-02-20 08:32:23

标签: compass-sass sass

我需要为项目的每个安装都有不同的特殊scss文件,所以我不想将它包含在git archive中。但即使这个文件不存在,一切都应该有效。

有没有办法@import scss文件只有它存在,忽略文件未找到错误?

2 个答案:

答案 0 :(得分:5)

您需要使用“import-path”选项来执行此操作,其中您导入的路径包含要导入的后备文件(在我们的示例中,我们需要一个空文件)。 / p>

选项1:Vanilla Sass

文件结构

├── fallback
    ├── _example.scss // <-- our blank file
├── _example.scss // <-- the file for the user to customize (optional)
├── style.scss

在style.scss中:

@import "example";
/* the rest of our styles */

当你运行sass命令时,你会这样写:

sass --watch ./css:./sass --load-path ./sass/fallback

请注意,fallback目录不必位于sass目录中,它位于您喜欢的文件系统的任何位置。

另请参阅:How does SCSS locate partials?

选项2:指南针扩展

如果您在多个项目中使用此技术,您可能会发现创建Compass扩展更方便。它会自动为您设置加载路径。您可以在此处详细了解如何创建扩展程序:http://compass-style.org/help/tutorials/extensions/

答案 1 :(得分:2)

可能使用sass-globbing并且可选文件的命名约定遵循特定的加载顺序。

考虑以下树:

stackoverflow-14975341/
├── .gitignore
├── config.rb
├── css/
│   └── screen.css
└── sass/
    ├── optionals/
    │   ├── .gitkeep
    │   ├── _01_style.scss
    │   └── _03_style.scss
    └── screen.scss

使用这些文件:

# config.rb
require 'sass-globbing'

sass_dir   = 'sass'
css_dir    = 'css'
relative_assets = true

// sass/screen.scss
@import "optionals/*";

// sass/optionals/_01_style.scss
.optional1 {
  background-color: red;
}

// sass/optionals/_03_style.scss
.optional3 {
  background-color: green;
}

和,在.gitignore

sass/optional/*

最后,要保留optional文件夹,请创建一个名为.gitkeep的空文件(文件名不重要)。

编译样式表时,即使文件screen.css丢失,Compass也会生成_02_style.scss

// css/screen.css
/* line 1, ../sass/optionals/_01_style.scss */
.optional1 {
  background-color: red;
}

/* line 1, ../sass/optionals/_03_style.scss */
.optional3 {
  background-color: green;
}

当然可以根据导入的附加路径改进系统。

相关问题