GithubHelp home page GithubHelp logo

sndrs / postcss-atomised Goto Github PK

View Code? Open in Web Editor NEW
86.0 1.0 2.0 154 KB

Atomise standard CSS

Home Page: https://sndrs.github.io/atomised-css-repl

JavaScript 83.45% HTML 16.55%
proof-of-concept css atomic-css postcss

postcss-atomised's Introduction

postcss-atomised

npm version Build Status Coverage Status Dependency Status Dependency Status devDependency Status

This is probably not stable. It was initially developed for use on the Guardian website, but it feels like it's the wrong solution to the problem of bloat + complexity. Leaving it here in case anyone finds it useful or we pick it up again.


PostCSS plugin that atomises a standard set of CSS, and provides a json map from the original classes to the atomised ones.

Enables you to write CSS in an insolated, super-modular fashion without worrying about the bloat of duplication (the only way you could serve a smaller stylesheet would be to use fewer styles).

Example

Take your stylesheet…

/* original.css */
.one {
    background-color: red;
    margin: 1rem;
}
.two {
    background-color: red;
    margin-top: 1rem;
}
@media (min-width: 100px) {
    .two:hover {
        background-color: hotpink;
    }
}

Pass it through the plugin…

// load the original CSS file and atomise it

import {readFileSync} from 'fs';

import postcss from 'postcss';
import atomised from 'postcss-atomised';

const css = readFileSync('./original.css', 'utf8');
const options = {};

postcss([atomised(options)]).process(css).then(result => {
    // do something with `result`
});

result.css is a String containing the atomised CSS:

.a { background-color: red; }
.b { margin-top: 1rem; }
.c { margin-right: 1rem; }
.d { margin-bottom: 1rem; }
.e { margin-left: 1rem; }
@media (min-width: 100px) {
    .f:hover { background-color: hotpink; }
}

You now also have a file called atomised-map.json in cwd.

// atomised-map.json
{
  "one": ["a", "b", "c"," d", "e"],
  "two": ["a", "b", "f"]
}

This can be used to transform your templates.

<div class="one"></div>
<div class="two"></div>

into…

<div class="a b c d e f"></div>
<div class="a c g h"></div>

Options

Type: Object | Null

No options are required. By default, a file called atomised-map.json will be written to cwd containing the atomised JSON map.

options.mapPath

Type: (String | Null) optional

Alternative location for the atomised JSON map to be saved. null will prevent the output being written to disk.

options.mapHandler

Type: (Function) optional

Callback function that receives one arguement – the JSON map object.

Restrictions

  • only single class selectors can be atomised (other selectors will pass straight through)
  • multiple/duplicate and pseudo selectors/elements are fine
Selector Ok
.a .b { }
.a.b { }
a { }
a b { }
#a { }
a[b] { }
a > b { }
a + b { }
a ~ b { }
*
.a:b { }
.a, .b { }
.a { } .a { }
.a:hover { }

Development

Run npm start to run the test runner in watch mode, or npm test for a one-off. Node 6 or greater is needed for development.

Node.js 0.*

Node 4 or greater is needed to use the plugin.

postcss-atomised's People

Contributors

desbo avatar greenkeeperio-bot avatar sndrs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

desbo gtrufitt

postcss-atomised's Issues

Why is this a wrong solution?

Hey,

I remember starring this project a few months ago and, wanting to check how it went I just saw your update:

it feels like it's the wrong solution to the problem of bloat + complexity

Would you mind sharing how you came to this conclusion? It'd be really interesting to hear :)

Thanks

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.