我用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

有没有办法包括整个partials目录(它是一个充满sas -partials的目录),比如@import compass之类的?


当前回答

这对我来说很有效

@import 'folder/*';

其他回答

你可能想要保留源顺序,那么你可以使用这个。

@import
  'foo',
  'bar';

我更喜欢这个。

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

看起来不像。

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

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导入,用于顺序无关紧要的情况,比如mixin目录甚至动画。

This feature will never be part of Sass. One major reason is import order. In CSS, the files imported last can override the styles stated before. If you import a directory, how can you determine import order? There's no way that doesn't introduce some new level of complexity. By keeping a list of imports (as you did in your example), you're being explicit with import order. This is essential if you want to be able to confidently override styles that are defined in another file or write mixins in one file and use them in another.

有关更详细的讨论,请查看此封闭功能请求:

如果您在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、变量等)放在一个早期加载目录中。否则,加载顺序是和应该是无关的…如果我们做得正确的话。