Skip to content
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

Support for cssnext nested at-rule syntax (apply, nested) #2335

Open
adamwathan opened this issue Nov 6, 2017 · 15 comments
Open

Support for cssnext nested at-rule syntax (apply, nested) #2335

adamwathan opened this issue Nov 6, 2017 · 15 comments

Comments

@adamwathan
Copy link

Hey folks! I noticed currently Stylus doesn't elegantly handle at-rules inside of selectors like @apply or @nested which are new CSS specifications and currently transpilable with cssnext.

For example, Stylus compiles this:

.foo {
  @nest span & {
    color: #f00;
  }
}

...to just this (notice the selector has disappeared):

@nest span & {
  color: #f00;
}

...and this:

.danger {
  color: #f00;
  @apply --danger-theme;
}

...to this (notice @apply has been hoisted outside of the rule):

.danger {
  color: #f00;
}
@apply --danger-theme;

Are there any known workarounds or escaping strategies that can be used to preserve the original formatting for processing by cssnext or other PostCSS plugins that make use of at-rules in this way?

@adamwathan
Copy link
Author

Just a quick follow-up, I hadn't seen the @css syntax for CSS literals which does mostly work here. The one caveat I would mention is that this:

.foo {
  @css {
    @nest span & {
      color: #f00;
    }
  }
}

...still compiles to this:


  @nest span & {
    color: #f00;
  }

...removing the actual selector. Definitely work-aroundable though.

@jhessin
Copy link

jhessin commented Mar 26, 2018

That makes postCSS libraries that rely on @ rules all but unusable within stylus. I am seriously rethinking adopting stylus at this point.

@MartinMuzatko
Copy link

MartinMuzatko commented Jul 30, 2018

I would love to see stylus to either

  • adopt new css language features like @apply
  • or create their own alternative to @apply

This is mostly relevant for issue tailwindlabs/discuss#11
Generally, I'm missing a way to transclude existing css rules within a new css rule.
Are there any plans regarding this issue @Panya ?

@MartinMuzatko
Copy link

Maybe a workaround would be to write a stylus plugin to just ignore @apply.
Is there a way to do that?

@acidjazz
Copy link
Contributor

acidjazz commented Jan 2, 2019

@tj @Panya @kizu do you think we can get 2c from any of y'all on how we can approach this? maybe we can do this via the javascript API?

all that really needs to happen is to ignore @apply, @nested etc and not treat it as an other @rule

this would be huge for the tailwind and cssnext communities!

@acidjazz
Copy link
Contributor

acidjazz commented Jan 3, 2019

Hey everyone, while I'm messing with stylus and looking for a solution I've come up with an alternative plugin. I do not see a why to ignore @apply from the javascript api @MartinMuzatko but i did come up with a plugin that is much easier on the eyes:

.kevin
  apply(bg-orange text-blue)
  border 20px solid blue

will result in

.kevin {
  @apply bg-orange text-blue;
  border: 20px solid blue;
}

Here is the plugin, I'll later on link a repo as I update and test it:

var stylus = require('stylus')

exports = module.exports = plugin;

function plugin() {
  return function (style) {
    style.define('apply', function() {
      let strings = Object.keys(arguments).map( (key) => arguments[key].string)
      return new stylus.nodes.String(` @apply ${strings.join(' ')};`, ' ')
    });
  }
};

here is how i've implemented it in my nuxt.config.js (you just need to get it into the use [] array when using stylus-loader

  build: {
    extractCSS: true,
    loaders: {
      stylus: {
        use: [require('./apply.js')()],
      }
    },

@acidjazz
Copy link
Contributor

acidjazz commented Jan 5, 2019

@adamwathan @MartinMuzatko @jhessin got a fork going with proper @apply support!

give it a try if ya want https://github.com/acidjazz/stylus

@MartinMuzatko
Copy link

MartinMuzatko commented Jan 11, 2019

I tried to implement this in my nuxt config but with no success. @acidjazz
Did I forget anything?

I did everything in my nuxt.config.js - including const stylus = require('stylus')
image

@acidjazz
Copy link
Contributor

@MartinMuzatko just use my fork and use @apply naturally

in package.json:

    "stylus": "acidjazz/stylus#dev",

@MartinMuzatko
Copy link

works like a charm @acidjazz ❤️ you are the best :)

@acidjazz
Copy link
Contributor

@adamwathan my PR was merged and is included with v0.54.6 @MartinMuzatko you no longer have to use my branch!

@iChenLei
Copy link
Member

iChenLei commented Aug 21, 2021

@apply is removed , more detailed -> https://www.xanthir.com/b4o00

2021-08-21 16 12 20

Support a unstable css spec is dangrous, @nest is same.


Append: No plan to rollback @apply support #2442 !

@hybridvision
Copy link

Hello @iChenLei,

Does this mean you're planning to rollback the changes from #2442? Even if @apply isn't going to be in the official CSS spec, it's very important for those of us who use Stylus with Tailwind CSS (see: https://tailwindcss.com/docs/extracting-components#extracting-component-classes-with-apply).

All we really need is for Stylus to leave the @apply alone so that Tailwind's PostCSS can process it. I don't see the harm in leaving this in the Stylus core as a whitelisted exception. Tailwind CSS has a huge user base and it would be a shame to make Stylus incompatible with it once again.

@iChenLei
Copy link
Member

@hybridvision No plan to remove @apply support. Don't worry. we respect all existing features. Don't break our ecosystem is my first Criterion. ❤️

@hybridvision
Copy link

Thanks for the clarification, @iChenLei! 😌

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

6 participants