How do I stop a file being imported more than once with SASS and Midscape?

别等时光非礼了梦想. 提交于 2019-12-13 04:13:59

问题


I am just learning Sass and have used the @import. I have the following situation...

_master.scss <-- this contains all my global variables like color

_child1.scss <-- this is for one of my child pages but it needs stuff from master

_child2.scss <-- this is for one of other my child pages but it also needs stuff from master

Now the problem is _master is getting quite big. So when I save my Scss files (the children) using Mindscape tools in Visual Studio 2012 I notice that the generated css files include ALL the css from the master file for both children.

Essentially I am getting duplicate css every time I want to reference the master file with '@import' which is not good for performance. Is there any way to avoid this?


回答1:


If I'm understanding, it sounds like you could really benefit from better use of partials. If You don't want all of _master.scss to be imported, you should break it apart into components and then import those as you need them.

For example, if master.scss contains all the fonts on your site, the colors, and the column layout, you could break it down to something like the following:

_fonts.scss
_colors.scss
_column.scss

In master.scss, you'd import each. Then you could import these smaller files into child1 and child2 as you need them.

If this isn't what you're looking for, please try clarifying your question and I might be able to help a bit more.

UPDATE: After discussing with Imjared he said that I should just create a file with variables in only. This would seem to work for me so I am updating this answer and marking it as correct.




回答2:


I solved this duplicate SCSS using this snippet to import content:

// src/main/scss/import-once-workaround.scss
$imported-once-files: () !default;

@function not-imported($name) {
  $imported-once-files: $imported-once-files !global;
  $module_index: index($imported-once-files, $name);
  @if (($module_index == null) or ($module_index == false)) {
    $imported-once-files: append($imported-once-files, $name);
    @return true;
  }
  @return false;
}

Importing files are done then by:

@if not-imported("font") { @import "font"; }

I used this approach instead of Zurb's to get StyleDocco work, see http://paul.wellnerbou.de/2015/05/18/avoid-multiple-imports-of-the-same-scss-file-with-sass/



来源:https://stackoverflow.com/questions/19096303/how-do-i-stop-a-file-being-imported-more-than-once-with-sass-and-midscape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!