236

我正在使用 SASS 部分模块化我的样式表,如下所示:

@import partials/header
@import partials/viewport
@import partials/footer
@import partials/forms
@import partials/list_container
@import partials/info_container
@import partials/notifications
@import partials/queues

有没有办法像@import compass 之类的包含整个partials 目录(它是一个充满SASS-partials 的目录)?

4

13 回答 13

218

如果您在 Rails 项目中使用 Sass,则 sass-rails gem,https://github.com/rails/sass-rails具有 glob 导入功能。

@import "foo/*"     // import all the files in the foo folder
@import "bar/**/*"  // import all the files in the bar tree

要回答另一个答案中的问题“如果导入目录,如何确定导入顺序?没有办法不引入一些新的复杂程度。”

有些人会争辩说,将文件组织到目录中可以降低复杂性。

我组织的项目是一个相当复杂的应用程序。17 个目录中有 119 个 Sass 文件。这些大致对应于我们的观点,主要用于调整,繁重的工作由我们的自定义框架处理。对我来说,几行导入的目录比 119 行导入的文件名简单一点。

为了解决加载顺序,我们将需要首先加载的文件——mixin、变量等——放在一个早期加载目录中。否则,加载顺序是并且应该是无关紧要的……如果我们做事正确的话。

于 2013-05-15T21:35:59.380 回答
100

此功能永远不会成为 Sass 的一部分。一个主要原因是进口订单。在 CSS 中,最后导入的文件可以覆盖前面所述的样式。如果导入目录,如何确定导入顺序?没有办法不引入一些新的复杂程度。通过保留导入列表(如您在示例中所做的那样),您可以明确导入顺序。如果您希望能够自信地覆盖在另一个文件中定义的样式或在一个文件中编写 mixin 并在另一个文件中使用它们,那么这是必不可少的。

如需更深入的讨论,请在此处查看此关闭的功能请求

于 2011-01-24T06:56:29.990 回答
41

查看sass-globbing 项目

于 2012-04-07T06:20:00.190 回答
36

__partials__.scss在同一目录中创建了一个文件partials

|- __partials__.scss
|- /partials
   |- __header__.scss
   |- __viewport__.scss
   |- ....

__partials__.scss中,我写了这些:

@import "partials/header";
@import "partials/viewport";
@import "partials/footer";
@import "partials/forms";
....

所以,当我想导入整个 时partials,只需写@import "partials". 如果我想导入其中任何一个,我也可以写@import "partials/header".

于 2012-10-18T01:53:12.170 回答
6

这可能是一个老问题,但在 2020 年仍然相关,所以我可能会发布一些更新。自 10 月 19 日更新以来,我们通常应该使用@use而不是@import,但这只是一个备注。这个问题的解决方案是使用索引文件来简化包括整个文件夹。下面的例子。

// foundation/_code.scss
code {
  padding: .25em;
  line-height: 0;
}

// foundation/_lists.scss
ul, ol {
  text-align: left;

  & & {
    padding: {
      bottom: 0;
      left: 0;
    }
  }
}

// foundation/_index.scss
@use 'code';
@use 'lists';

// style.scss
@use 'foundation';

https://sass-lang.com/documentation/at-rules/use#index-files

于 2020-01-20T22:09:22.270 回答
4

您可以通过将 sass 文件放置在您想要导入的文件夹中来使用一些解决方法,并像这样导入该文件中的所有文件:

文件路径:main/current/_current.scss

@import "placeholders"; @import "colors";

在下一个目录级别文件中,您只需使用 import for file 从该目录导入所有文件:

文件路径:main/main.scss

@import "EricMeyerResetCSSv20"; @import "clearfix"; @import "current";

这样你就有相同数量的文件,就像你正在导入整个目录一样。注意顺序,最后出现的文件将覆盖匹配的阶梯。

于 2014-05-26T18:33:59.813 回答
3

http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#import

看起来不像。

如果这些文件中的任何一个总是需要其他文件,那么让他们导入其他文件并仅导入顶级文件。如果它们都是独立的,那么我认为您将不得不像现在一样继续这样做。

于 2011-01-24T04:40:04.187 回答
3

您可能希望保留源订单,然后您可以使用它。

@import
  'foo',
  'bar';

我更喜欢这个。

于 2016-10-21T16:23:04.183 回答
2

我也在寻找你的问题的答案。对应于答案正确的导入所有功能不存在。

这就是为什么我编写了一个 python 脚本,您需要将其放入 scss 文件夹的根目录中,如下所示:

- scss
  |- scss-crawler.py
  |- abstract
  |- base
  |- components
  |- layout
  |- themes
  |- vender

然后它将遍历树并找到所有 scss 文件。一旦执行,它将创建一个名为 main.scss 的 scss 文件

#python3
import os

valid_file_endings = ["scss"]

with open("main.scss", "w") as scssFile:
    for dirpath, dirs, files in os.walk("."):
        # ignore the current path where the script is placed
        if not dirpath == ".":
            # change the dir seperator
            dirpath = dirpath.replace("\\", "/")

            currentDir = dirpath.split("/")[-1]
            # filter out the valid ending scss
            commentPrinted = False
            for file in files:
                # if there is a file with more dots just focus on the last part
                fileEnding = file.split(".")[-1]
                if fileEnding in valid_file_endings:
                    if not commentPrinted:
                        print("/* {0} */".format(currentDir), file = scssFile)
                        commentPrinted = True
                    print("@import '{0}/{1}';".format(dirpath, file.split(".")[0][1:]), file = scssFile)

输出文件的示例:

/* abstract */
@import './abstract/colors';
/* base */
@import './base/base';
/* components */
@import './components/audioPlayer';
@import './components/cardLayouter';
@import './components/content';
@import './components/logo';
@import './components/navbar';
@import './components/songCard';
@import './components/whoami';
/* layout */
@import './layout/body';
@import './layout/header';
于 2016-10-19T17:09:33.757 回答
2

Dennis Best 接受的回答指出:“否则,加载顺序是并且应该是无关紧要的......如果我们做事正确的话。” 这是完全不正确的。如果你做事正确,你可以使用 css 命令来帮助你减少特殊性并保持你的 css 简单和干净。

我组织导入的方法是在目录中添加一个_all.scss文件,在该目录中以正确的顺序导入所有相关文件。这样,我的主要导入文件将简单而干净,如下所示:

// Import all scss in the project

// Utilities, mixins and placeholders
@import 'utils/_all';

// Styles
@import 'components/_all';
@import 'modules/_all';
@import 'templates/_all';

如果需要,您也可以对子目录执行此操作,但我认为您的 css 文件的结构不应太深。

虽然我使用这种方法,但我仍然认为在 sass 中应该存在 glob 导入,用于顺序无关紧要的情况,例如 mixins 甚至动画的目录。

于 2018-04-30T07:51:24.180 回答
1

您可以生成自动导入所有内容的 SASS 文件,我使用这个 Gulp 任务:

concatFilenames = require('gulp-concat-filenames')

let concatFilenamesOptions = {
    root: './',
    prepend: "@import '",
    append: "'"
}
gulp.task('sass-import', () => {
    gulp.src(path_src_sass)
        .pipe(concatFilenames('app.sass', concatFilenamesOptions))
        .pipe(gulp.dest('./build'))
})

您还可以通过像这样对文件夹进行排序来控制导入顺序:

path_src_sass = [
    './style/**/*.sass', // mixins, variables - import first
    './components/**/*.sass', // singule components
    './pages/**/*.sass' // higher-level templates that could override components settings if necessary
]
于 2019-04-11T11:10:35.333 回答
-1

这对我来说很好

@import 'folder/*';
于 2013-04-20T06:03:37.323 回答
-4

通过定义要导入的文件,可以使用所有文件夹的通用定义。

因此,@import "style/*"将编译样式文件夹中的所有文件。

有关 Sass 中导入功能的更多信息,您可以在此处找到。

于 2018-03-08T10:44:20.313 回答