DEV Community

Cover image for Creating a Stylus Library
shadowtime2000
shadowtime2000

Posted on • Originally published at h.shadowtime2000.com on

3 2

Creating a Stylus Library

Creating a Stylus Library

Hi, in this article I will be showing how to create a Stylus library.

Wait what is Stylus?

Stylus is a CSS preprocessor which has features such as whitespace sensitivity but all that is optional. Kind of like a mixture of SASS and SCSS.

So how will we do it?

Stylus has a JavaScript API which exposes a .include function. The .include function allows you to add another path which will be searched when importing.

Let's Do It

We will be using the Plugin API of the JavaScript API so it can become a little more organized.

const stylus = require("stylus");

const plugin = () => (style) => {
    style.include(__dirname)
}

stylus("@import 'my-lib/foo';")
    .use(plugin())
    .render((err, css) => {
        if (err) throw err;
        console.log(css)
    })
Enter fullscreen mode Exit fullscreen mode

If you have a my-lib subfolder, it will use that and import my-lib/foo.styl.

Getting an Import All Entry

But, what if a user wants to import everything? Like:

@import "my-lib";
Enter fullscreen mode Exit fullscreen mode

Then, we can create an index.styl within the my-lib subfolder, and change it to this:

@import "foo";
Enter fullscreen mode Exit fullscreen mode

So now, if you do this:

@import "my-lib";
Enter fullscreen mode Exit fullscreen mode

It will import everything from my-lib/index.styl, so make sure index.styl is importing every other file there is.

Conclusion

So in this post I showed how to use the JavaScript API to create a Stylus library, allow submodules, and allow one main index.styl entry point.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs