title: Creating a Stylus Library published: true date: 2020-12-14 05:59:18 UTC tags: CSS,Nodejs,JavaScript canonical_url: https://h.shadowtime2000.com/creating-a-stylus-library cover_image: https://upload.wikimedia.org/wikipedia/commons/thumb/d/d8/Stylus-logo.svg/393px-Stylus-logo.svg.png
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)
})
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";
Then, we can create an index.styl
within the my-lib
subfolder, and change it to this:
@import "foo";
So now, if you do this:
@import "my-lib";
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.
shadowtime2000
If you are looking at this you probably wonder who I am; teenage open source maintainer