question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

@import inside a selector (feature request)

See original GitHub issue

Allowing imports inside of selectors would open up an awesome collection of possibilities for code reuse and style scoping. Something like:

.foo{
    @import "bar";
}

Would prove especially useful for creating less modules for CSS backed widgets. For example, if I author e.g., AutoRotator and a StackContainer and had the ability to nest imports inside selectors, then I could do:

# AutoRotator/pane.less
.pane{
    border: 1px solid gray;
    width: 300px;
    /* etc. ... */
}

# StackContainer/pane.less
.pane{
    border: 1px solid orange;
    width: 200px;
    /* etc. ... */
} 

And the user of either library would be able to decide the scope of the various styles like:

# main.less
.myRotator {
    @import "AutoRotator/pane";
}

# main.less
.myStack {
    @import "StackContainer/pane";
}

Notice how simple the selectors seem inside the files. I think this basically just simulates http://www.w3.org/TR/css3-namespace/

Issue Analytics

  • State:closed
  • Created 12 years ago
  • Reactions:10
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
marcboekercommented, Feb 24, 2012

I’ve found a dirty hack to allow namespaced imports. Just comment the following lines:

https://github.com/cloudhead/less.js/blob/master/lib/less/tree/ruleset.js#L20

and

https://github.com/cloudhead/less.js/blob/master/lib/less/tree/ruleset.js#L27

to look like

// Evaluate imports
//if (ruleset.root || ruleset.allowImports) {
    for (var i = 0; i < ruleset.rules.length; i++) {
        if (ruleset.rules[i] instanceof tree.Import) {
            Array.prototype.splice
                 .apply(ruleset.rules, [i, 1].concat(ruleset.rules[i].eval(env)));
        }
    }
//}

But I have no clue what this also affects on compiling. I’ve done a small test with bootstrap from Twitter:

#test {
  @import "../../bootstrap/less/bootstrap";
}

Compiles down to:

#test article,
#test aside,
#test details,
#test figcaption,
#test figure,
#test footer,
#test header,
#test hgroup,
#test nav,
#test section {
  display: block;
}
...

And It has worked in my case. Can anyone confirm this on a larger project?

@cloudhead Why is importing only executed on the root level? And why is ruleset.allowImports always undefined and how can I overwrite it to be always true? 😃

Thanks

3reactions
cloudheadcommented, Feb 28, 2012

Ok, done… it’s on by default, as performance hit was not that high… option to disable is --strict-imports

Read more comments on GitHub >

github_iconTop Results From Across the Web

import - CSS: Cascading Style Sheets - MDN Web Docs
The @import CSS at-rule is used to import style rules from other valid stylesheets. An @import rule must be defined at the top...
Read more >
HTML Imports - web.dev
Imports provide convention for bundling HTML/CSS/JS (even other HTML Imports) into a single deliverable. It's an intrinsic feature, but a powerful one.
Read more >
If '<selector>' is an Angular component, then verify that it is ...
When I added this import the error message disappeared. Check all imports inside the component. Hope it helps someone.
Read more >
@import and @require | Stylus
@import works by iterating an array of directories, and checking if this file lives in any of them (similar to node's require.paths )....
Read more >
Possibiliti to change selector order - Web Scraper
I recently ran into this same requirement. I did use the export and import features to re-order the JSON. As a developer that's...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found