|
| 1 | +# CSS by JS |
| 2 | + |
| 3 | +The next iteration of CSS-in-JS. Turn your CSS into JS that turns it into CSS. |
| 4 | + |
| 5 | +## Description |
| 6 | + |
| 7 | +This a CLI tool that will turn a CSS file into JS, which will then apply the styles to the DOM when loaded in the browser. So, you write CSS, but you never load a single `*.css` file onto your page. It's left to JS, which is doing eveything else these days anyway. |
| 8 | + |
| 9 | +For example, this CSS: |
| 10 | + |
| 11 | +```css |
| 12 | +:root { |
| 13 | + --my-color: #1a1a1a; |
| 14 | +} |
| 15 | + |
| 16 | +body, |
| 17 | +html { |
| 18 | + font-family: "Source Sans Pro", sans-serif; |
| 19 | + background: linear-gradient(45deg, #f5d9ff, #fefdff); |
| 20 | + color: var(--my-color); |
| 21 | +} |
| 22 | + |
| 23 | +main { |
| 24 | + text-align: center; |
| 25 | + max-width: 700px; |
| 26 | + margin: 0 auto; |
| 27 | +} |
| 28 | + |
| 29 | +h1, |
| 30 | +h3 { |
| 31 | + margin: 0; |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +Turns into this, which is then passed to a `applyCSS` function: |
| 36 | + |
| 37 | +```javascript |
| 38 | +[ |
| 39 | + { name: ":root", properties: [{ name: "--my-color", value: "#1a1a1a" }] }, |
| 40 | + { |
| 41 | + name: "body, html", |
| 42 | + properties: [ |
| 43 | + { name: "font-family", value: "'Source Sans Pro', sans-serif" }, |
| 44 | + { name: "background", value: "linear-gradient(45deg, #f5d9ff, #fefdff)" }, |
| 45 | + { name: "color", value: "var(--my-color)" } |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "main", |
| 50 | + properties: [ |
| 51 | + { name: "text-align", value: "center" }, |
| 52 | + { name: "max-width", value: "700px" }, |
| 53 | + { name: "margin", value: "0 auto" } |
| 54 | + ] |
| 55 | + }, |
| 56 | + { name: "h1, h3", properties: [{ name: "margin", value: "0" }] } |
| 57 | +]; |
| 58 | +``` |
| 59 | + |
| 60 | +And when the output is loaded on your page, you get styles: |
| 61 | + |
| 62 | +```html |
| 63 | +<script src=".output/css-by-js.js"></script> |
| 64 | +``` |
| 65 | + |
| 66 | + |
| 67 | + |
| 68 | +## Installation |
| 69 | + |
| 70 | +`npm install css-by-js` |
| 71 | + |
| 72 | +## Usage |
| 73 | + |
| 74 | +You just need to pass in a reference to a CSS file, as well as location where the file will be outputted. |
| 75 | + |
| 76 | +`css-by-js --file=./assets/sample.css --output=./assets/output.js` |
| 77 | + |
| 78 | +A `--file` is required. If no `--output` is specified, a `css-by-js.js` file will be created in the current directory. |
| 79 | + |
| 80 | +## Support |
| 81 | + |
| 82 | +I can't imagine it works perfectly in every scenario, but I've designed the tool to handle simple element CSS rules (`div{}`), pseudo-elements, and even CSS custom properties. If you find an oddball out there, make file an issue or make a contribution. |
| 83 | + |
| 84 | +## Why would I use this? |
| 85 | + |
| 86 | +I'll leave that up to you. |
| 87 | + |
| 88 | +## License |
| 89 | + |
| 90 | +MIT © [Alex MacArthur](https://macarthur.me) |
0 commit comments