Giter Club home page Giter Club logo

ainsley's Introduction

Update:

This project started as an attempt to see how efficient it would be for CSS to be decompressed client side.

The answer: VERY.

However, to make this useful, new devtools need to be created to allow it to integrate with other tools.

Additionally, it's not reasonable to assume developers will be happy to write styles in JSON.

The most exciting part of these results is that this makes "declarative CSS" - where the developer themself declares what is included in their stylesheet - a possibility.

As those objectives require a big reshuffle, this repo is being archived and work continues here:


πŸ‘¨πŸΎβ€πŸ³ 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

Name Minified Gzip Brotli CSS Rules Rules per byte
ainsley 7,845 3,463 2,984 22,809 7.64
tailwindcss 710,997 97,417 10,199 14,445 1.42
tachyons 73,497 13,697 2,421 2,113 0.87
sane-tachyons 49,793 9,200 1,957 1,278 0.65
turretcss 93,542 17,025 4,311 1,588 0.37
solid 82,482 12,585 2,497 1,469 0.59
basscss 11,326 2,477 589 260 0.44
bootstrap 159,515 23,681 4,762 2,027 0.43
bulma 194,420 25,511 5,705 2,142 0.38
materialize 141,841 21,558 5,579 1,609 0.29
spectre 45,964 9,631 1,992 638 0.32
foundation 132,474 17,219 3,471 1,420 0.41
milligram 8,718 2,295 442 90 0.20
skeleton 5,879 1,630 356 84 0.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!)

ainsley's People

Contributors

dependabot[bot] avatar hatched-tom avatar monkeywithacupcake avatar tbjgolden avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

Forkers

opencollective

ainsley's Issues

The automated release is failing 🚨

🚨 The automated release from the master branch failed. 🚨

I recommend you give this issue a high priority, so other packages depending on you could benefit from your bug fixes and new features.

You can find below the list of errors reported by semantic-release. Each one of them has to be resolved in order to automatically publish your package. I’m sure you can resolve this πŸ’ͺ.

Errors are usually caused by a misconfiguration or an authentication problem. With each error reported below you will find explanation and guidance to help you to resolve it.

Once all the errors are resolved, semantic-release will release your package the next time you push a commit to the master branch. You can also manually restart the failed CI job that runs semantic-release.

If you are not sure how to resolve this, here is some links that can help you:

If those don’t help, or if this issue is reporting something you think isn’t right, you can always ask the humans behind semantic-release.


No npm token specified.

An npm token must be created and set in the NPM_TOKEN environment variable on your CI environment.

Please make sure to create an npm token and to set it in the NPM_TOKEN environment variable on your CI environment. The token must allow to publish to the registry https://registry.npmjs.org/.


Good luck with your project ✨

Your semantic-release bot πŸ“¦πŸš€

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.