Skip to content

When using an @import (reference), mixins that contain an & selector get added to the compiled output improperly #1968

Closed

Description

@bcullman

When I import a less file with the (reference) directive, I expect to see no CSS output from that file.

However, I am seeing content being output from the imported file when a mixin contains an & selector.

mixin.less

.mixin-no-parent-selector() {
  background-color: red;
}
.mixin-with-parent-selector() {
  &:first-child{ //  USING AN & SELECTOR HERE
    background-color: blue;
 }
}

less-for-reference.less

.container-1 {
   .mixin-no-parent-selector();
}
.container-2{
   .mixin-with-parent-selector()
}

style.less

/*!
 * styles.less
 */
@import "mixin.less";
@import (reference) "less-for-reference.less";

Expected Output

/*!
 * styles.less
 */

Actual Output

/*!
 * styles.less
 */

.container-2:first-child{
    background-color: blue;
}

Activity

  1. changed the title [-]When importing using reference, mixins that container an & selector get added to the compiled output[/-] [+]When @importing using (reference), mixins that contain a & selector get added to the compiled output[/+] on Apr 12, 2014
  2. changed the title [-]When @importing using (reference), mixins that contain a & selector get added to the compiled output[/-] [+]When using @import (reference), mixins that contain an & selector get added to the compiled output[/+] on Apr 12, 2014
  3. changed the title [-]When using @import (reference), mixins that contain an & selector get added to the compiled output[/-] [+]When using an @import (reference), mixins that contain an & selector get added to the compiled output improperly[/+] on Apr 12, 2014
  4. thomaswelton commented on Apr 22, 2014

    @thomaswelton

    I can replicate this error. Using twitter bootstrap and importing it as a reference generates a lot of output I did not expect to see

  5. bcullman commented on Apr 22, 2014

    @bcullman
    Author

    Yes, I discovered this when working with bootstrap too. I built this example to simplify the issue down a bit.

  6. seven-phases-max commented on Apr 23, 2014

    @seven-phases-max
    Member

    I can't reproduce the CSS output of your example with Less 1.7.0.

    As for Bootstrap, the non empty reference output is the result of extend directives (&:extend(.clearfix all);, &:extend(.btn-*); etc.) which is a different issue (see #1851, #1878).

  7. bcullman commented on Apr 25, 2014

    @bcullman
    Author

    Here is the same issue using bootstrap files, with one tweak to remove the 2 extend statements you mention are problematic.

    style.less

    @import "bootstrap-3.1/less/variables.less";
    @import "bootstrap-3.1/less/mixins.less"; //modified to comment out &:exend() statements
    
    @import (reference) "bootstrap-3.1/less/buttons.less";

    Output should be nothing. However, I get

    .btn-default:hover,
    .btn-default:focus,
    .btn-default:active,
    .btn-default.active,
    .open .dropdown-toggle.btn-default {
      color: #333333;
      background-color: #ebebeb;
      border-color: #adadad;
    }
    .btn-default:active,
    .btn-default.active,
    .open .dropdown-toggle.btn-default {
      background-image: none;
    }
    .btn-default.disabled,
    .btn-default[disabled],
    fieldset[disabled] .btn-default,
    .btn-default.disabled:hover,
    .btn-default[disabled]:hover,
    fieldset[disabled] .btn-default:hover,
    .btn-default.disabled:focus,
    .btn-default[disabled]:focus,
    fieldset[disabled] .btn-default:focus,
    .btn-default.disabled:active,
    .btn-default[disabled]:active,
    fieldset[disabled] .btn-default:active,
    .btn-default.disabled.active,
    .btn-default[disabled].active,
    fieldset[disabled] .btn-default.active {
      background-color: #ffffff;
      border-color: #cccccc;
    }
    .btn-default .badge {
      color: #ffffff;
      background-color: #333333;
    }
    .btn-primary:hover,
    .btn-primary:focus,
    .btn-primary:active,
    .btn-primary.active,
    .open .dropdown-toggle.btn-primary {
      color: #ffffff;
      background-color: #3276b1;
      border-color: #285e8e;
    }
    .btn-primary:active,
    .btn-primary.active,
    .open .dropdown-toggle.btn-primary {
      background-image: none;
    }
    .btn-primary.disabled,
    .btn-primary[disabled],
    fieldset[disabled] .btn-primary,
    .btn-primary.disabled:hover,
    .btn-primary[disabled]:hover,
    fieldset[disabled] .btn-primary:hover,
    .btn-primary.disabled:focus,
    .btn-primary[disabled]:focus,
    fieldset[disabled] .btn-primary:focus,
    .btn-primary.disabled:active,
    .btn-primary[disabled]:active,
    fieldset[disabled] .btn-primary:active,
    .btn-primary.disabled.active,
    .btn-primary[disabled].active,
    fieldset[disabled] .btn-primary.active {
      background-color: #428bca;
      border-color: #357ebd;
    }
    .btn-primary .badge {
      color: #428bca;
      background-color: #ffffff;
    }
    .btn-success:hover,
    .btn-success:focus,
    .btn-success:active,
    .btn-success.active,
    .open .dropdown-toggle.btn-success {
      color: #ffffff;
      background-color: #47a447;
      border-color: #398439;
    }
    .btn-success:active,
    .btn-success.active,
    .open .dropdown-toggle.btn-success {
      background-image: none;
    }
    .btn-success.disabled,
    .btn-success[disabled],
    fieldset[disabled] .btn-success,
    .btn-success.disabled:hover,
    .btn-success[disabled]:hover,
    fieldset[disabled] .btn-success:hover,
    .btn-success.disabled:focus,
    .btn-success[disabled]:focus,
    fieldset[disabled] .btn-success:focus,
    .btn-success.disabled:active,
    .btn-success[disabled]:active,
    fieldset[disabled] .btn-success:active,
    .btn-success.disabled.active,
    .btn-success[disabled].active,
    fieldset[disabled] .btn-success.active {
      background-color: #5cb85c;
      border-color: #4cae4c;
    }
    .btn-success .badge {
      color: #5cb85c;
      background-color: #ffffff;
    }
    .btn-info:hover,
    .btn-info:focus,
    .btn-info:active,
    .btn-info.active,
    .open .dropdown-toggle.btn-info {
      color: #ffffff;
      background-color: #39b3d7;
      border-color: #269abc;
    }
    .btn-info:active,
    .btn-info.active,
    .open .dropdown-toggle.btn-info {
      background-image: none;
    }
    .btn-info.disabled,
    .btn-info[disabled],
    fieldset[disabled] .btn-info,
    .btn-info.disabled:hover,
    .btn-info[disabled]:hover,
    fieldset[disabled] .btn-info:hover,
    .btn-info.disabled:focus,
    .btn-info[disabled]:focus,
    fieldset[disabled] .btn-info:focus,
    .btn-info.disabled:active,
    .btn-info[disabled]:active,
    fieldset[disabled] .btn-info:active,
    .btn-info.disabled.active,
    .btn-info[disabled].active,
    fieldset[disabled] .btn-info.active {
      background-color: #5bc0de;
      border-color: #46b8da;
    }
    .btn-info .badge {
      color: #5bc0de;
      background-color: #ffffff;
    }
    .btn-warning:hover,
    .btn-warning:focus,
    .btn-warning:active,
    .btn-warning.active,
    .open .dropdown-toggle.btn-warning {
      color: #ffffff;
      background-color: #ed9c28;
      border-color: #d58512;
    }
    .btn-warning:active,
    .btn-warning.active,
    .open .dropdown-toggle.btn-warning {
      background-image: none;
    }
    .btn-warning.disabled,
    .btn-warning[disabled],
    fieldset[disabled] .btn-warning,
    .btn-warning.disabled:hover,
    .btn-warning[disabled]:hover,
    fieldset[disabled] .btn-warning:hover,
    .btn-warning.disabled:focus,
    .btn-warning[disabled]:focus,
    fieldset[disabled] .btn-warning:focus,
    .btn-warning.disabled:active,
    .btn-warning[disabled]:active,
    fieldset[disabled] .btn-warning:active,
    .btn-warning.disabled.active,
    .btn-warning[disabled].active,
    fieldset[disabled] .btn-warning.active {
      background-color: #f0ad4e;
      border-color: #eea236;
    }
    .btn-warning .badge {
      color: #f0ad4e;
      background-color: #ffffff;
    }
    .btn-danger:hover,
    .btn-danger:focus,
    .btn-danger:active,
    .btn-danger.active,
    .open .dropdown-toggle.btn-danger {
      color: #ffffff;
      background-color: #d2322d;
      border-color: #ac2925;
    }
    .btn-danger:active,
    .btn-danger.active,
    .open .dropdown-toggle.btn-danger {
      background-image: none;
    }
    .btn-danger.disabled,
    .btn-danger[disabled],
    fieldset[disabled] .btn-danger,
    .btn-danger.disabled:hover,
    .btn-danger[disabled]:hover,
    fieldset[disabled] .btn-danger:hover,
    .btn-danger.disabled:focus,
    .btn-danger[disabled]:focus,
    fieldset[disabled] .btn-danger:focus,
    .btn-danger.disabled:active,
    .btn-danger[disabled]:active,
    fieldset[disabled] .btn-danger:active,
    .btn-danger.disabled.active,
    .btn-danger[disabled].active,
    fieldset[disabled] .btn-danger.active {
      background-color: #d9534f;
      border-color: #d43f3a;
    }
    .btn-danger .badge {
      color: #d9534f;
      background-color: #ffffff;
    }
  8. seven-phases-max commented on Apr 26, 2014

    @seven-phases-max
    Member

    @bcullman

    OK, I see now. The problem there is that (reference) is not inheritable across multiple files. If mixins.less is imported from buttons.less it inherits the reference modifier so the output is empty. But when mixins.less is included directly in the master file with no sign of reference, all classes generated there get into the output. (Workaround is obvious: put (reference) on mixins.less too).

    Suggestion: add @import "mixin.less"; to the style.less in your original example. (Otherwise it's not that evident how to reproduce your results. Many developers follow the "import things where they are used" guidelines and that way (i.e. mixin.less imported within less-for-reference.less) it works as expected).

  9. bcullman commented on Apr 28, 2014

    @bcullman
    Author

    @seven-phases-max

    That does work. I am willing to consider this matter closed.

  10. 16 remaining items

  11. seven-phases-max commented on Dec 16, 2014

    @seven-phases-max
    Member

    This is my use case:

    What you need is to split the front-end (e.g. the CSS) part of file1.less from the shared "config" part (i.e. Bootstrap overrides), e.g.:

    // file1.less:
    @import "bootstrap/bootstrap.less";
    @import "bootstrap/file3.less"; // <- your shared stuff (incl. Bootstrap overrides)
    
    // file3.less:
    // Some variables and some mixins there
    
    // file2.less:
    @import "bootstrap/variables.less";
    @import "bootstrap/mixins.less";
    @import "file3.less";
    

    It's important to realize that even if this (and similar) bug is fixed once it will be no guarantee it's not broken with further Bootstrap releases - simply because "bootstrap.less" is meant to generate CSS - so they never will bother if or if not it's compatible with whatever sort-of-kludge features that may try to hack it externally.

  12. gdelhumeau commented on Dec 16, 2014

    @gdelhumeau
    Contributor

    Then I don't see what is the goal of the reference option.

  13. bcullman commented on Dec 16, 2014

    @bcullman
    Author

    I think the issue is that you should avoid mixing-in against compiled CSS (or, in your case what is the equivalent of compiled CSS as you are importing essentially a manifest-like LESS file).

    This is how I layout my Less files - it might prove helpful to others. Because I segregate the base less files from bootstrap (into /third-party), and further differentiate between custom and override styles, I have been able to keep pace with bootstrap version updates with minimal disruption.

    Directory Structure

    • /static
      • /third-party
        • /bootstrap-3.3.1 (unmodified content from bootstrap 3.3.1)
          • /less
            • variables.less (I do not use this file)
            • mixins.less
            • ...(all other bootstrap .less files)
          • /js (this is where all unedited js files from bootstrap reside)
      • /less
        • /corp
          • /global
            • styles.less (site style used throughout corp site)
          • /views
            • page1.less (site style for just page 1)
            • page2.less (site style for just page 2)
            • ... (all other per page .less files)
          • /shared
            • prerequisites.less (contains references to variables and mix-ins from all sources)
            • postrequisites.less (contains references to utils and responsive utils from all sources)
        • /corp-boostrap-3.3.1
          • /custom (contains customizations not found in bootstrap)
            • variables.less (custom variables.less not defined by bootstrap)
            • mixins.less (custom mixins not defined by bootstrap)
          • /override (contains overrides of bootstrap styles)
            • variables.less (same as variables.less from bootstrap, but with values for my corp)
            • mixins.less (overrides from bootstrap)

    Files

    Pre- and post- requisites.less

    prerequisites.less
    This is the file that facilitates sharing of variables and mixins with site styles and page styles.

    @import (reference) "../../corp-bootstrap-3.3.1/override/variables.less";
    @import (reference) "../../corp-bootstrap-3.3.1/custom/variables.less";
    @import (reference) "../../../_third-party/bootstrap-3.3.1/less/mixins.less";
    @import (reference) "../../corp-bootstrap-3.3.1/override/mixins.less";
    @import (reference) "../../corp-bootstrap-3.3.1/custom/mixins.less";

    postrequisites.less

    @import "../../../_third-party/bootstrap-3.3.1/less/utilities.less";
    @import "../../../_third-party/bootstrap-3.3.1/less/responsive-utilities.less";
    @import "../../corp-bootstrap-3.3.1/override/responsive-utilities.less";

    html page referenced .less files

    styles.less (used on all pages)

    //
    // similar to bootstrap.less, but contains overrides and customizations.
    //
    @import "../shared/prerequisites.less"
    
    // Reset and dependencies
    @import "../../../_third-party/bootstrap-3.3.1/less/normalize.less";
    @import "../../../_third-party/bootstrap-3.3.1/less/print.less";
    
    // Core CSS 
    @import "../../../_third-party/bootstrap-3.3.1/less/scaffolding.less";
    @import "../../../_third-party/bootstrap-3.3.1/less/type.less";
    ... (other core CSS @imports from _third-party/)
    
    // Components (only @import what is used by corp site)
    @import "../../../_third-party/bootstrap-3.3.1/less/dropdowns.less";
    ... (other components @imports from _third-party/)
    
    // Components w/ JavaScript (only @import what is used by corp site)
    @import "../../../_third-party/bootstrap-3.3.1/less/modals.less";
    @import "../../../_third-party/bootstrap-3.3.1/less/carousel.less";
    
    // Components: Corp Customizations
    @import "../../corp-bootstrap-3.3.1/custom/global.less";
    @import "../../corp-bootstrap-3.3.1/custom/chrome.less";
    
    // Components: Corp Override
    @import "../../corp-bootstrap-3.3.1/override/grid.less";
    @import "../../corp-bootstrap-3.3.1/override/type.less";
    ... (other @imports overrides)
    
    @import "../shared/postrequisites.less"

    page1.less (example of page-specific style)

    @import "../shared/prerequisites.less"
    
    ... page css here
    
    @import "../shared/postrequisites.less"
  14. almeidap commented on Dec 16, 2014

    @almeidap

    @bcullman I suppose that any frontend developer that extends a CSS framework will end up with the same kind of structure and import ordering (even if yours sounds, imho, over-complicated and probably misses the point with one dedicated stylesheet per page).

    The problem here, as @gdelhumeau spots it, is that the reference feature does not respect its contract as stated in the docs (http://lesscss.org/features/):

    Use @import (reference) to import external files, but without adding the imported styles to the compiled output unless referenced.

    Therefore, whatever messy imports I may be using, the @import (reference) statement line should output exactly zero characters. Otherwise, please, update the docs explaining why it's not the case if this issue does not deserve to be fixed.

  15. bcullman commented on Dec 16, 2014

    @bcullman
    Author

    @almeidap for the record, I don't have anywhere near one dedicated stylesheet per page. But when they are needed, I have a place to put them.

    As for the issue at hand, I think that @seven-phases-max noted in an earlier answer to this that (reference) is not inheritable across multiple files.

    This is why I posted how my files are structured. I never double (reference) because all my necessary references are in my prerequisites.less file, which is shared by my overall corp style as well as any individual page styles I need.

  16. seven-phases-max commented on Dec 17, 2014

    @seven-phases-max
    Member

    @almeidap

    Otherwise, please, update the docs explaining

    You forgot the project is a volunteer-based open source one, did you? The docs are open source as well. So please, let's take it a bit more easy (the issue is marked as bug and my comments are mostly intended to help to avoid stepping into this bug with a bit more structured project files organization, that's it).

  17. almeidap commented on Dec 17, 2014

    @almeidap

    @seven-phases-max For sure, I did not forget that less.js is a volunteer-based project and I am very grateful to the contributors. I just wanted to bring the focus back to the issue itself instead of not-so-useful (imo) discussions about the usage of imports.

    I recently started refactoring our LESS resources as our framework hit the IE stylesheets limit when concatenated with other CSS project files. Fixing the (reference) import issue will help me to avoid duplicate selectors and better control outputted resources.

  18. assadtony commented on Apr 28, 2015

    @assadtony

    Any updates on this issue? :)

  19. self-assigned this
    on Oct 11, 2015
  20. thybzi commented on Oct 25, 2015

    @thybzi

    Wish that to be fixed. My .clearfix mixin runs many-many times on the same selector, because it contains &:after

  21. SomMeri commented on Oct 26, 2015

    @SomMeri
    Member

    I am working on import reference now, but I am not able to give out estimate.

  22. added a commit that references this issue on Nov 20, 2015
  23. 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