maoberlehner / node-sass-magic-importer

Custom node-sass importer for selector specific imports, module importing, globbing support and importing files only once.
MIT License
292 stars 28 forks source link

RegEx selector ignore @font-face #197

Open SylRob opened 5 years ago

SylRob commented 5 years ago

there is a weird behavior with Regex and @font-face

let's take a css file with some css classes in it and a font-face declaration

this

@import '{ /^\.alert/ } from mycssfile.css';

will import all the .alert*** classes AND the @font-face from mycssfile.css

and this

@import '{ /^\..+/ } from mycssfile.css';

still add the @font-face in the import

the expected behavior is to exclude @font-face if a regexp is used (and it does not match @font-face of course)

maoberlehner commented 5 years ago

Hi @SylRob,

at-rules are a little bit special and there is no perfect way of how to deal with them in the context of selector extraction.

You expect them to be excluded but let's consider the following example.

@keyframes fade {
  0%   { opacity: 0; }
  100% { opacity: 1; }
}

.thing {
  animation: fade 0.2s;
}

Would you also expect the keyframe at-rule to be excluded in such a case?

Another example would be @charset "UTF-8";.

In those two cases I prefer to include them by default.

With nested at-rules like @media it works as expected I think. So the only "problem" is @font-face 🤔

Im open for recommendations for improvement but there are three caveats:

SylRob commented 5 years ago

I see your point of view. One solution would be to include the @font-face, @media and @keyframes only if they are mentioned / used in the selected classes. In that case they would be part of the classes (like a dependency)

but this solution exclude the @charset, I would not consider @charset as part of a class definitions. It's more a indication to the browser on how to read the file.

and more: considering @keyframes and @font-face you might want to extract them on their own (like from a css animation lib)


BTW let me thank you for this repo. Really helpfull !