【问题标题】:How to import scss file in compass only if it exists?仅当存在时如何在指南针中导入scss文件?
【发布时间】:2013-02-05 04:30:47
【问题描述】:

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

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

【问题讨论】:

  • 只包含一个空白文件有什么问题?
  • 我需要把它放在源代码管理下,所以这些项目依赖的 scss 模块应该被 gitignored。

标签: compass-sass sass


【解决方案1】:

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

选项 1:香草 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

请注意,备用目录不必在您的 sass 目录中,它可以在您喜欢的文件系统上的任何位置。

另请参阅:How does SCSS locate partials?

选项 2:指南针扩展

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

【讨论】:

  • 谢谢,这正是我需要的。我实际上采取了一些不同的方式,并将additional_import_paths = ["./lib"] 设置在我放置可能被覆盖的模块的位置,以便它查找本地文件,然后在“./lib”中查找
【解决方案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 的空文件(文件名不重要)。

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

// 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;
}

当然可以基于导入附加路径来改进系统。

【讨论】:

  • 所以我想如果没有额外的插件就不可能开箱即用?我正在考虑通过两条路径进行 scss 模块查找来覆盖特定 scss 模块的可能性。因此,如果未找到覆盖模块,则导入默认模块。有可能吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-20
  • 1970-01-01
  • 1970-01-01
  • 2020-01-31
  • 1970-01-01
  • 1970-01-01
  • 2020-05-13
相关资源
最近更新 更多