我正在使用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目录(这是一个充满SASS-partials的目录),如@import罗盘或什么?
如果你在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行导入的文件名复杂得多。
为了解决加载顺序,我们将需要首先加载的文件 - mixins,变量等 - 放在早期加载目录中。否则,加载顺序是并且应该是无关紧要的...如果我们正在做正确的事情。
这对我来说很好
@import 'folder/*';
您可以生成自动导入所有内容的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
]
此功能永远不会成为Sass的一部分。一个主要原因是进口订单。在CSS中,最后导入的文件可以覆盖之前描述的样式。如果导入目录,如何确定导入顺序?没有办法不会引入一些新的复杂程度。通过保留导入列表(就像您在示例中所做的那样),您明确了导入顺序。如果您希望能够自信地覆盖在另一个文件中定义的样式或在一个文件中编写mixins并在另一个文件中使用它们,则这是必不可少的。
有关更全面的讨论,请在此处查看this closed feature request:
我在__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"
。
您可以通过将sass文件放在要导入的文件夹中来使用一些解决方法,并导入该文件中的所有文件,如下所示:
文件路径:main / current / _current.scss
@import "placeholders";
@import "colors";
在下一个dir级别文件中,您只需使用import for file从该目录导入所有文件:
文件路径:main / main.scss
@import "EricMeyerResetCSSv20";
@import "clearfix";
@import "current";
这样你就有相同数量的文件,就像你导入整个目录一样。请注意顺序,最后出现的文件将覆盖匹配的页眉。
您可能希望保留源订单,然后您可以使用它。
@import
'foo',
'bar';
我更喜欢这个。
Dennis Best接受的答案是“否则,负载顺序是,而且应该是无关紧要的...如果我们正在做正确的事情。”这完全是错误的。如果你正确地做事,你可以利用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文件的结构应该太深。
虽然我使用这种方法,但我仍然认为在顺序无关紧要的情况下,应该存在一个glob导入,比如一个mixins甚至动画目录。
http://sass-lang.com/docs/yardoc/file.SASS_REFERENCE.html#import
看起来不像。
如果这些文件中的任何一个总是需要其他文件,那么让它们导入其他文件并仅导入顶级文件。如果它们都是独立的,那么我认为你将不得不像现在这样继续这样做。
我也在搜索你的问题的答案。对应答案正确导入所有功能不存在。
这就是为什么我写了一个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';