Skip to content

[stylus] tap into Stylus to use its javascript API #6354

@qnp

Description

@qnp

Clear and concise description of the problem

Hello,

I would like to add some functionalities in my stylus code by defining new functions in order to transform the output CSS. 1

To do so I would need to use the stylus.define function, as mentioned in Stylus’ javascript API documentation.

There is no possibility to do that using Vite. I suggest to tap directly into imported nodeStylus into the stylus preprocessor, using a tap options :

{
   tap: stylus => 
     stylus
       .define('$var', 32)
       .define('fn', () => /* do something useful */)
   ...otherOptions // additionalData, filename, imports...
}

As set, include, import, define and use are chainable (they return the stylus instance), it is safe to use a tap-like mechanics. The case of deps, resolver and the final render can break the chain and lead to unexpected results, and thus may be addressed (e.g. not accessible, maybe using a subset of the stylus instance ?)

What do you think ?

Suggested solution

I can implement the thing, but I wanted to make sure it is relevant.

Alternative

For the final Vite config API, instead of tap: (stylus: Stylus) => Stylus which can lead to unexpected behavior if used unwisely, maybe we can use explicitly a define option which is an object [key: string]: any:

{
  define: {
    $var: 32,
    fn: () => /* do soemthing useful here */
  }
}

Additional context

No response

Validations

Footnotes

  1. In my use case, I target specific elements using selectors like [fill="#33C5FF"] (to override some style of iframes that are reversed-proxied, but I guess other use cases might be found). I loop over all the palette colors and do {s('[fill="%s"]', $from)}. The thing is the s function using the only available %s ends up with a lower-cased color, hence the new selectors [fill="#33c5ff"] do not target anything. This is a stylus problem, that can be bypassed by defining a custom function for that very operation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Approved

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions