Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More โ†’
Socket
Sign inDemoInstall
Socket

ainsley

Package Overview
Dependencies
Maintainers
1
Versions
36
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ainsley

๐Ÿ‘จ๐Ÿพโ€๐Ÿณ Ainsley is to CSS what Markdown is to HTML

  • 1.2.11
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

๐Ÿ‘จ๐Ÿพโ€๐Ÿณ ainsley

npm Coveralls Github GitHub License GitHub Workflow Status

ainsley is a more efficient way to define your stylesheet.

It promises to let you:

  • have an unmatched developer experience
  • use your existing CSS knowledge
  • compress your bytes sent by an order of magnitude
  • serialize your framework as tiny, readable JSON
// Define your stylesheet using JavaScript, or JSON
const breakpoints = Object.entries({
  s: 384,
  m: 768,
  l: 1024
}).map(([prefix, pixels]) => [prefix, `@media(min-width:${pixels}px)`])

// This tiny object contains all the instructions to assemble a stylesheet
const ainsley = {
  // `variations` allow you to add modifiers to children
  // e.g. breakpoints, or hover styles
  variations: [breakpoints],
  // `variables` allow you to reuse groups of properties and values
  variables: {
    color: { b: 'black', w: 'white' }
  },
  children: [
    // You may use `"$..."` syntax to import configs and remote urls;
    // it is able to import CSS and JSON.
    '$https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.css',
    // You may also use it to import configs installed by npm (or yarn);
    // this one would import the npm package "ainsley-config-example".
    '$example',
    // You may nest ainsley objects;
    // this allows you to scope variables, variations and configs.
    {
      variables: {
        // `variables` prefixed with a `+` will merge with any
        // definition higher up (otherwise, it behaves like normal).
        '+color': {
          lg: '#eee',
          g: '#888',
          dg: '#222'
        },
        // `variables` prefixed with a `?` will only be defined
        // if they have not been already been defined higher up.
        '?length': {
          0: 0,
          1: '1px',
          2: '2px'
        }
      },
      children: [
        // This is a "utility rule" - it looks like a typical CSS rule.
        // It uses a variable, which will output every possible permutation!
        ['bg', [['background-color', '{color}']]],
        // This string is the prefix of the "utility class".
        // โ†™ Abbreviations of `variable` values will be appended to it.
        [
          'b',
          [
            // "Utility rules" support multiple declarations.
            // "Utility declarations" may use any number of variables.
            ['border', '{length} {color}'],
            ['border-style', 'solid']
          ]
        ]
      ]
    }
  ]
}

// flatten replaces external dependencies with their contents
// (i.e. CSS/JSON urls, configs)
// ๐Ÿ’ž โžก ๐Ÿ’–
const configWithoutDependencies = await flatten(ainsley)
// minify generates a config which is designed to use less bytes
// after it has been compressed; this is how it should be sent to the client
// ๐Ÿ’– โžก ๐Ÿ’Œ
const minifiedConfig = minify(configWithoutDependencies)

// (ON THE CLIENT) to generate CSS, and embed it into the page
// ๐Ÿ’Œ โžก ๐Ÿงก๐Ÿ’›๐Ÿ’š๐Ÿ’™๐Ÿ’œ
Ainsley.embed(Ainsley.generate(minifiedConfig /* , options */))

Compression potential

Instead of writing a stylesheet in CSS, you write it in a small JavaScript object, which can be optionally serialized as JSON.

The browser receives this small object and recursively compiles it into CSS. This compresses it massively.

                 โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
                 โ”‚    JS โ”‚ JS-to-CSS โ”‚ Equivalent โ”‚
                 โ”‚ input โ”‚  compiler โ”‚ CSS output โ”‚
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ minified bytes โ”‚ 5,094 โ”‚     2,751 โ”‚    786,877 โ”‚
โ””โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
 โ”‚ gzipped bytes โ”‚ 2,146 โ”‚     1,317 โ”‚    139,296 โ”‚
 โ””โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
  โ”‚ brotli bytes โ”‚ 1,821 โ”‚     1,163 โ”‚     23,747 โ”‚
  โ””โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
    โ”‚ TOTAL SENT โ”‚             2,984 โ”‚     23,747 โ”‚
    โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

(using default settings with ainsley-config-starter)

Comparisons to others

NameMinifiedGzipBrotliCSS RulesRules per byte
ainsley7,8453,4632,98422,8097.64
tailwindcss710,99797,41710,19914,4451.42
tachyons73,49713,6972,4212,1130.87
sane-tachyons49,7939,2001,9571,2780.65
turretcss93,54217,0254,3111,5880.37
solid82,48212,5852,4971,4690.59
basscss11,3262,4775892600.44
bootstrap159,51523,6814,7622,0270.43
bulma194,42025,5115,7052,1420.38
materialize141,84121,5585,5791,6090.29
spectre45,9649,6311,9926380.32
foundation132,47417,2193,4711,4200.41
milligram8,7182,295442900.20
skeleton5,8791,630356840.24

Ideas for ways to use it

  • Use the interactive online tool to fetch configs and flatten
  • Use Webpack with ainsley-loader to write your config in a .ainsley file
  • Write it in JavaScript and unit test it
  • Moving a project from CSS to ainsley - embed and gradually migrate
  • Moving a project from SASS to ainsley - compile, embed and gradually migrate

Contribute

If you'd like to help, send me a message! ๐Ÿ‘จ๐Ÿพโ€๐Ÿณ

I didn't make this project to make money, but donating can help keep projects like this maintained properly. Contribute

Or perhaps you might like to donate to a top rated charity instead (they need the money more!)

Keywords

FAQs

Package last updated on 15 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with โšก๏ธ by Socket Inc