DEV Community

Giulia Chiola
Giulia Chiola

Posted on β€’ Edited on β€’ Originally published at giuliachiola.dev

Styleguide setup

Regardless of which tool I use to build my styleguides, it helps a lot to have a checklist of packages I need for (almost) every project setup.

Styles

Node-sass is a library that provides binding for Node.js to LibSass, the C version of the popular stylesheet preprocessor, Sass.

npm install node-sass --save-dev
Enter fullscreen mode Exit fullscreen mode

PostCSS is a tool for transforming styles with JS plugins. These plugins can lint your CSS, support variables and mixins, transpile future CSS syntax, inline images, and more.

npm install postcss postcss-cli postcss-preset-env postcss-import --save-dev
Enter fullscreen mode Exit fullscreen mode

To configure postcss, add .postcss.config.js in project root folder

πŸ™ GitHub snippet

A mighty, modern linter that helps you avoid errors and enforce conventions in your styles.

npm install stylelint stylelint-scss stylelint-order --save-dev
Enter fullscreen mode Exit fullscreen mode

To configure stylelint, add .stylelintrc.json in project root folder

πŸ™ GitHub snippet

A Sass mixin that helps you compose media queries in an elegant way.

npm install sass-mq --save-dev
Enter fullscreen mode Exit fullscreen mode

Add sass-mq configuration in styles folder src/scss/00-settings/_sass-mq-config.scss

πŸ™ GitHub snippet

In a static project, as a styleguide, to use sass-mq in our styles, we have to import it from node_modules and add our configuration

// src/scss/00-settings/__settings.scss

@import 'node_modules/sass-mq/_mq.scss';
@import 'sass-mq-config';
Enter fullscreen mode Exit fullscreen mode

In projects that use webpack, we could add sass-mq using this syntax

// With webpack (and boilerplates such as create-react-app)
@import '~sass-mq';
Enter fullscreen mode Exit fullscreen mode

This project is the Sass version of Normalize.css, a collection of HTML element and attribute rulesets to normalize styles across all browsers.

npm install normalize-scss --save-dev
Enter fullscreen mode Exit fullscreen mode

Import normalize-scss from node_modules

// src/scss/02-generic/__generic.scss

@import 'node_modules/normalize-scss/sass/_normalize.scss';
Enter fullscreen mode Exit fullscreen mode

Styles structure

  • scss folder contains BEMIT SASS file structure
  • css folder contains compiled CSS output
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ scss/
β”‚   β”‚   β”œβ”€β”€ 00-settings/
β”‚   β”‚   β”œβ”€β”€ 01-tools/
β”‚   β”‚   β”œβ”€β”€ 02-generic/
β”‚   β”‚   β”œβ”€β”€ 03-elements/
β”‚   β”‚   β”œβ”€β”€ 04-objects/
β”‚   β”‚   β”œβ”€β”€ 06-components/
β”‚   β”‚   β”œβ”€β”€ 07-utilities/
β”‚   β”‚   β”œβ”€β”€ style.scss
β”‚   β”œβ”€β”€ css/
β”‚   β”‚   β”œβ”€β”€ style.css
Enter fullscreen mode Exit fullscreen mode

πŸ“š More info

BEMIT: Taking the BEM Naming Convention a Step Further

Styles scripts

Use package.json scripts object to list all styles commands

"scripts": {
    "_____________________________Styles_____________________________": "",
    "stylelint": "stylelint 'src/scss/**/*.scss' || echo \"Stylelint failed for some file(s).\"",
    "cleanup:dev": "rimraf dist src/css/*.css",
    "scss:to:css:dev": "node-sass --output-style expanded src/scss/ -o src/css/",
    "css:to:postcss:dev": "postcss src/css --dir dist/css",
    "styles:dev": "npm run stylelint && npm run scss:to:css:dev && npm run css:to:postcss:dev",
  },
Enter fullscreen mode Exit fullscreen mode

SVG

Node.js tool for optimizing SVG files

npm install svgo --save-dev
Enter fullscreen mode Exit fullscreen mode

Create two folders, one for original SVG files src/svg and a second one optimized SVGs src/svgo

β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ svg/
β”‚   β”‚   git.svg
β”‚   β”‚   nodejs.svg
β”‚   β”‚   vuejs.svg
β”‚   β”œβ”€β”€ svgo/
β”‚   β”‚   git.svg (optimized)
β”‚   β”‚   nodejs.svg (optimized)
β”‚   β”‚   vuejs.svg (optimized)
Enter fullscreen mode Exit fullscreen mode

Add SVGO script configuration

πŸ™ GitHub snippet

SVG scripts

Use package.json scripts object to list all SVG manipulation commands

"scripts": {
  "_____________________________SVG________________________________________________": "",
  "clean:svgo": "rimraf src/icons/svgo/*",
  "svg:optimize": "npm run clean:svgo && node scripts/svgo.js",
}
Enter fullscreen mode Exit fullscreen mode

Ok now I am ready to develop my components and build a styleguide! πŸ’ͺ🏻

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 (1)

Collapse
 
titungdup profile image
dhondup β€’

Can we use stylelint-order to sort CSS working files?

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay