Code Monkey home page Code Monkey logo

caligula's Introduction

Caligula logo, portrait bust of emperor Caligula

caligula

NPM Build

Optimize CSS stylesheets for your Svelte apps.

This library statically analyzes and extracts class selectors from Svelte components using the svelte compiler. Given an external CSS file, the library outputs an optimized stylesheet by removing unused class rules.

Table of Contents

Motivation

One of the quickest ways to style Svelte applications (or web apps in general) is to define an external CSS stylesheet in the HTML head tag.

<!-- index.html -->
<head>
  <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/css/uswds.css" />
</head>

By design, styles written in Svelte are scoped to the component. While scoped styles improve encapsulation, it is cumbersome to prefix globally applied rules with :global.

<!-- App.svelte -->
<script>
  import Accordion from "./Accordion.svelte";
</script>

<style>
  :global(.line-height-heading) {
    line-height: 1.2;
  }

  :global(.font-size-sm) {
    font-size: 0.93rem;
  }
</style>

<Accordion class="line-height-heading font-size-sm" />

The problem is that pre-built stylesheets contain far more rules than are actually used. The CSS file for a design system or library can be hundreds of kilobytes, even after minification.

For example, the stylesheet for the U.S. Web Design System weighs in at 268 kB minified.

Against Preprocessors

One solution is to use a preprocessor to import smaller SASS/SCSS partials.

This has two main drawbacks:

  • Extra build configuration: Using CSS preprocessors involves additional tooling and set-up. This is overkill, especially for rapid prototyping.
  • Still a manual process: SASS partials must be manually added or removed, which can be inefficient and error prone.

Usage

Install caligula as a development dependency.

yarn add -D caligula

In this example, only several class selectors are used from a localy copy of the U.S. Web Design System CSS file (unminified).

// postbuild.js
const { caligula } = require("caligula");

caligula({
  include: ["src/**/*.svelte"],
  input: "css/uswds.css",
});

Output

node postbuild.js

# Detected 7 classes from 2 Svelte components
# Removed 4660 classes from "css/uswds.css"
- Original size: 357.915 kB
+ New size: 10.53 kB

+ > 347.385 kB (97.1%) smaller!
# > Saved output to "css/uswds.8a6dce134044.css"

The output file is minified and hashed. Its size is significantly smaller than that of the original.

API

caligula({
  /**
   * glob of Svelte components
   * @type {Array.<string>}
   */
  include: ["src/**/*.svelte"],

  /**
   * path to the original CSS file
   * @type {string}
   */
  input: "css/uswds.css",

  /**
   * optional output file path
   * @type {string} [output=undefined]
   */
  output: "dist/uswds.min.css",

  /**
   * hash the output file name
   * @type {boolean} [hash=true]
   */
  hash: false,

  /**
   * cssnano minification config options
   * @type {object} [minifyOptions={ from: undefined }]
   */
  minifyOptions: {},

  /**
   * hook triggered after minifying the CSS
   * useful for appending metadata like licenses
   * @param {string} - minified CSS
   * @returns {string} - modified CSS
   */
  onMinify: (css) => `/*! uswds v2.6.0 */${css}`,
});

License

MIT

caligula's People

Contributors

metonym avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

caligula's Issues

Support external stylesheets as input

Arguably, external stylesheets are used more often than not when it comes to prototyping.

  1. Check if input path is a URL
    1a. If so, fetch the resource and parse
    1b. If not, read the file from disk

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.