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.

Reference @import? (variables and mixins only)

See original GitHub issue

TL;DR: yes, what I want is something similar to LESS reference import to be implemented in Stylus

A long and boring story:

Let’s say we have the following file structure:

|
|-- main
|    |-- button.styl
|    |-- authform.styl
|    `-- mypage.styl
`-- main.styl

main/button.styl

// variables
$button-height = 33px
$button-rounding = 4px

// mixins
button_color($base-color)
  background-color: $base-color
  &:hover
    background-color: darken($base-color, 20%)

// styles
.button
  height: $button-height
  border-radius: $button-rounding
  color: white

  .normal
    button_color(#90091e)

  .alternate
    button_color(#bada55)

main/authform.styl

// variables
$authform-pad = 5px
$authform-height = $button-height + (2 * $authform-pad)

// styles
.authform
  box-sizing: border-box
  padding: $authform-pad
  height: $authform-height

main/mypage.styl

// styles
.mypage

  header
    height: $authform-height

  .someblock .button
    button_color(#906090)

main.styl

@import 'main/authform'
@import 'main/button'
@import 'main/mypage'

initial state Trying to compile main.styl gives obviously bad results such as height: $button-height10px; for authform etc.

@import Ok, so variables need to be already defined when used. So, we add @import 'button' in the beginning of authform.styl, and both @import 'authform' and @import 'button' to mypage.styl Now, we have all styles compiled correctly, but all authform styles are repeated twice, and button styles are repeated three times.

@require As we may know, @require constructs prevent repeated imports, so we change all @import to @require. Yay, styles are no more repeated!

But… In output css, button styles come first, then authform styles, and then mypage styles. But the order in main.css was: authform, button, mypage. This order is broken because authform first imports button (and thus outputs its styles), and only after that outputs its own styles.

In this synthetic situation the order seems to be no object. But in real life, the styles order can be just crucial, whereas we cannot control it.

build with no button Now let’s create one more build which represents some page with authform but without button (may it be the form has only input, but this form must have the same height)

main/otherpage.styl

@require 'authform'

.otherpage

  .myblock
    min-height: $authform-height

nobutton.styl

@require 'main/authform'
@require 'main/otherpage'

Compiling nobutton.styl, we get button styles which we didn’t really need in this css build.

The only chance to avoid this is to split every component file to at least two parts (“styles” file and “vars+mixins” file), and import them separately.

The solution that should be great LESS has a very good approach for aforesaid problems solution: reference import option

Let us have something similar in Stylus? May it be @reference, or @use, or @refer, or whatever, which would work as “import only variables and mixins from the file, but don’t output any styles”.

Thank you in advance!

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:2
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

13reactions
FezVrastacommented, Jun 1, 2016

Any updates on it? I think something like the import feature of ES6 would be awesome.

@import { myMixin } from '../mixins'
@import { $blue, $red } from '../colors'
12reactions
kizucommented, Mar 10, 2016

Thanks for the issue, we have some plans on enhancing the @import/@require in Stylus, very close to what Less have, but with a slightly different syntax. Stay tuned! 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sass: @import
Sass extends CSS's @import rule with the ability to import Sass and CSS stylesheets, providing access to mixins, functions, and variables and combining ......
Read more >
How can one import only variables and mixins from Sass ...
It's in the files which make use of Foundation mixins: Can I import only the mixins from an SCSS file, without importing the...
Read more >
The New SASS Module System: Out with @import, In with @use
The new @use rule is the replacement for @import which allows other stylesheets to use its variables, functions, and mixins. What makes the...
Read more >
Features In-Depth | Less.js
Variables and mixins defined in a mixin are visible and can be used in caller's scope. There is only one exception: a variable...
Read more >
SASS | Use variables across multiple files - GeeksforGeeks
@import rule which import Sass and CSS stylesheets for providing variables, @mixins and functions.Such that combine all stylesheets together ...
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