Skip to content

Cannot put import by reference inside of a mixin #2162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
blakemann opened this issue Aug 24, 2014 · 4 comments
Closed

Cannot put import by reference inside of a mixin #2162

blakemann opened this issue Aug 24, 2014 · 4 comments

Comments

@blakemann
Copy link

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.

@blakemann blakemann 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) Aug 24, 2014
@blakemann
Copy link
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.

@blakemann blakemann 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 Aug 24, 2014
@blakemann
Copy link
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!

@seven-phases-max
Copy link
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).

@SomMeri SomMeri self-assigned this Oct 10, 2015
SomMeri pushed a commit to SomMeri/less-rhino.js that referenced this issue Nov 20, 2015
- refactored how import reference works
- refactored to-css-visitor (this is side product, it was getting
  complicated)
- fixes issues less#1851, less#1896, less#1878, less#2716, less#1968, less#2162 (same as less#1896)
SomMeri pushed a commit to SomMeri/less-rhino.js that referenced this issue Nov 20, 2015
@SomMeri
Copy link
Member

SomMeri commented Dec 7, 2015

This should be fixed by #2729 .

@SomMeri SomMeri closed this as completed Dec 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants
@SomMeri @blakemann @seven-phases-max and others