Skip to content

Cannot put import by reference inside of a mixin #2162

Closed

Description

@blakemann

I have been working on a complex importing strategy where I set all of my paths in a config file, and then use a mixin to import various different components as needed.

This approach doesn't seem to work well with files imported by reference, however. It seems that importing a file by reference inside of a mixin in this way causes it to output all of the contents of the file, and not just what is used.

To give you a very simplified example, I have something like this:

// config.less

// Normally the sub-mixin calls would be controlled by variables passed in, but for simplicity, let's say both get called always
.importer(){
    .import-certain-file();
    .import-last-file();
}

.import-certain-file(){
    @import "certain-file";
}

.import-last-file(){
    @import (reference) "last-file";
}
.import-last-file();

// certain-file.less

h1 {
    color: black;
}
// last-file.less

._color-white {
    color: white;
}
// main.less

@import "config";
.importer();

h1 {
    &:extend(._color-white);
}

So, certain-file.less contains a rule setting h1 to be black, but I want to override that in my main less file.. using a class name that was specifically made to be extended (imported by reference so that the original class name of ._color-white is not output into my code)

But that messes with the (reference) import, giving me this final output:

h1 {
    color: black;
}
._color-white, h1 {
    color: white;
}

Which technically works, but means I've got extra unwanted selectors being dropped into my code.. which is especially bad when in reality, my last-file.less is a big file full of "extension" classes, many of which don't even get used on every project.

I believe the problem lies in the fact that importing by reference inside of a mixin causes all styles inside that file to be output, rather than just when they are used.

Activity

  1. changed the title [-]Cannot import by reference after an import through a mixin[/-] [+]Cannot put import by reference after an import through a mixin (precedence problem)[/+] on Aug 24, 2014
  2. blakemann commented on Aug 24, 2014

    @blakemann
    Author

    I think I may have rushed the logic on this a bit, as I'm now realizing that the order of me calling the mixin and importing the file does make sense the way it is, as I am calling the mixin after the original import call. That's what I get for staring at it for too long. I think the focus of this issue should be shifted to the fact that I can't put the (reference) import inside of the mixin without losing the reference functionality. I'm going to edit the text a bit to reflect this.

  3. changed the title [-]Cannot put import by reference after an import through a mixin (precedence problem)[/-] [+]Cannot put import by reference inside of a mixin[/+] on Aug 24, 2014
  4. blakemann commented on Aug 24, 2014

    @blakemann
    Author

    Alright, I've adjusted the issue to take out the parts related to my original misconceptions. Your note about the mixin being able to be called before it is defined is actually a really useful point, which could solve my problem completely, regardless of the issue that's now highlighted in this case, but I still think this issue could be a potential problem in some other use-cases.

    Sorry for the confusion!

  5. seven-phases-max commented on Aug 24, 2014

    @seven-phases-max
    Member

    Ah, I guess this way it becomes similar to #1896 (not exactly the same but internally it's identical stuff I guess) but probably it's better to keep open just to have more test examples for reference when worked out (I'm assigning same lables as in #1896).

  6. self-assigned this
    on Oct 10, 2015
  7. SomMeri commented on Dec 7, 2015

    @SomMeri
    Member

    This should be fixed by #2729 .

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions