Style your Svelte website faster with Stylify

machy8

Vladimรญr Machรกฤek

Posted on July 11, 2022

Style your Svelte website faster with Stylify

Stylify + Svelte + Vite. Style your Svelte website faster with Stylify. Don't study selectors and syntax. Use pure CSS syntax and get generated CSS with advanced optimization for production.

For easier start, you can checkout the Stylify Stackblitz playground ๐ŸŽฎ.

๐Ÿ’Ž Stylify Introduction

Stylify generates CSS dynamically based on what you write. The syntax is similar to css property:value. Defined utilities are combined with components selectors and in production minified to bare minimum like .color\:red,.button {color:red} to ._zx, ._ga{color:red}.

Stylify allows you to get very small bundles, generate additional lazyloaded CSS chunks and style the page by writting HTML and selectors ๐ŸคŸ.

๐Ÿš€ Svelte Setup

The easiest way to Setup the Svelte is using cli:

  • Run yarn create vite app
  • Select svelte or svelte-ts
  • Then cd app

This way you will get the default Svelte application skeleton.

๐Ÿ”Œ Stylify Integration

Install the @stylify/unplugin package using NPM or Yarn:

yarn add @stylify/unplugin
npm i @stylify/unplugin
Enter fullscreen mode Exit fullscreen mode

Open the vite.config.js and copy the following content into it:

import { defineConfig } from 'vite'
import { svelte } from '@sveltejs/vite-plugin-svelte'
import { vitePlugin } from '@stylify/unplugin';

const stylifyPlugin = vitePlugin({
    transformIncludeFilter: (id) => {
        return id.endsWith('svelte');
    },
    bundles: [{
        outputFile: './src/stylify.css',
        files: ['./src/**/*.svelte'],
    }]
});

export default defineConfig({
    plugins: [stylifyPlugin, svelte()]
});
Enter fullscreen mode Exit fullscreen mode

The last step, open the src/main.js and add path to stylify.css:

// ...
import './stylify.css'
Enter fullscreen mode Exit fullscreen mode

Styling the website

If yout copy the code bellow into the src/App.svelte and run yarn dev you will get a styled Hello World! ๐ŸŽ‰ text:

<main class="max-width:800px margin:0__auto">
    <h1 class="text-align:center margin-top:100px font-size:42px">
        Hello World! ๐ŸŽ‰
    </h1>
</main>
Enter fullscreen mode Exit fullscreen mode

Stylify watches any change in the files that matches mask in the bundle files and generates css into the src/stylify.css.

If you add for example color:blue the CSS will be automatically updated ๐ŸŽ‰.

Go ahead and try Stylify directly on Stackblitz.com ๐Ÿ’ก.

Components

To avoid bloated templates with utilities, you can use
components directly in files, where they are used through content options (expects javascript object without brackets) or in the compiler config.

<!--
stylify-components
  container: 'max-width:800px margin:0__auto',
  title: 'text-align:center margin-top:100px font-size:42px'
/stylify-components
-->
<main class="container">
    <h1 class="title">
        Hello World! ๐ŸŽ‰
    </h1>
</main>
Enter fullscreen mode Exit fullscreen mode

Variables

If you like clean code, you also want avoid hardcoded values in selectors. Variables can be defined the same way as components:

<!--
stylify-variables
  titleFontSize: '42px',
  containerWidth: '800px'
/stylify-variables

stylify-components
  container: 'max-width:$containerWidth margin:0__auto',
  title: 'text-align:center margin-top:100px font-size:$titleFontSize'
/stylify-components
-->
<main class="container">
    <h1 class="title">
        Hello World! ๐ŸŽ‰
    </h1>
</main>
Enter fullscreen mode Exit fullscreen mode

Building for production

If you run yarn build + yarn preview, the svelte markup will be mangled to this:

<main class="_7tcrv">
    <h1 class="_88io">
        Hello World! ๐ŸŽ‰
    </h1>
</main>
Enter fullscreen mode Exit fullscreen mode

The css is shortened too:

:root {--titleFontSize: 42px;--containerWidth: 800px;}
._bcda8,._7tcrv{max-width:800px}
._m0vnad,._7tcrv{margin:0 auto}
._1vegb8,._88io{text-align:center}
._jimir,._88io{margin-top:100px}
._qe393,._88io{font-size:42px}
Enter fullscreen mode Exit fullscreen mode

Configure anything youย need

The examples above doesn't include everything Stylify can do:

Feel free to checkout the docs to learn more ๐Ÿ’Ž.


Stay in touch:
๐Ÿ‘‰ @8machy
๐Ÿ‘‰ @stylifycss
๐Ÿ‘‰ stylifycss.com
๐Ÿ‘‰ dev.to/machy8
๐Ÿ‘‰ medium.com/@8machy

๐Ÿ’– ๐Ÿ’ช ๐Ÿ™… ๐Ÿšฉ
machy8
Vladimรญr Machรกฤek

Posted on July 11, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Introduction to Svelte
svelte Introduction to Svelte

March 2, 2023

Intro to Calcite with Svelte
javascript Intro to Calcite with Svelte

August 8, 2022

Learning Svelte Part #4
svelte Learning Svelte Part #4

November 14, 2021

ยฉ TheLazy.dev

About