All Projects → lttb → Postjss

lttb / Postjss

Licence: mit
Use the power of PostCSS in compiling with JSS

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Postjss

Css In React
🍭 CSS in React - Learn the best CSS in JS frameworks by example
Stars: ✭ 101 (+152.5%)
Mutual labels:  styled-components, postcss, css-in-js, jss
Astexplorer.app
https://astexplorer.net with ES Modules support and Hot Reloading
Stars: ✭ 65 (+62.5%)
Mutual labels:  babel, babel-plugin, postcss
Babel Plugin Css Prop
Babel plugin to transpile `css` prop to a styled component. (Experimental)
Stars: ✭ 56 (+40%)
Mutual labels:  babel, babel-plugin, styled-components
Babel Plugin Polished
Compile polished helper functions at build time
Stars: ✭ 133 (+232.5%)
Mutual labels:  babel, babel-plugin, styled-components
Naomi
Sublime Text enhanced syntax highlighting for JavaScript ES6/ES7/ES2015/ES2016/ES2017+, Babel, FlowType, React JSX, Styled Components, HTML5, SCSS3, PHP 7, phpDoc, PHPUnit, MQL4. Basic: Git config files.
Stars: ✭ 544 (+1260%)
Mutual labels:  babel, styled-components, postcss
Prejss
Get the power of PostCSS with plugins in your JSS styles. 🎨 Just put CSS into JS and get it as JSS object.
Stars: ✭ 238 (+495%)
Mutual labels:  styled-components, postcss, jss
Styled Tools
Useful interpolated functions for CSS-in-JS
Stars: ✭ 761 (+1802.5%)
Mutual labels:  styled-components, css-in-js, jss
Css In Js
Autocomplete React Native / JS Styles and converting plain CSS to JS styles
Stars: ✭ 192 (+380%)
Mutual labels:  styled-components, css-in-js, jss
tsstyled
A small, fast, and simple CSS-in-JS solution for React.
Stars: ✭ 52 (+30%)
Mutual labels:  styled-components, css-in-js, jss
Vscode Stylelint
A Visual Studio Code extension to lint CSS/SCSS/Less with stylelint
Stars: ✭ 260 (+550%)
Mutual labels:  styled-components, postcss, css-in-js
Babel Plugin Tailwind Components
Use Tailwind with any CSS-in-JS library
Stars: ✭ 320 (+700%)
Mutual labels:  babel, babel-plugin, css-in-js
Compiled
A familiar and performant compile time CSS-in-JS library for React.
Stars: ✭ 1,235 (+2987.5%)
Mutual labels:  babel, babel-plugin, css-in-js
Babel Plugin Styled Components
Improve the debugging experience and add server-side rendering support to styled-components
Stars: ✭ 878 (+2095%)
Mutual labels:  babel, babel-plugin, styled-components
Xwind
Tailwind CSS as a templating language in JS and CSS-in-JS
Stars: ✭ 249 (+522.5%)
Mutual labels:  babel, babel-plugin, css-in-js
Css In Js Benchmarks
Stars: ✭ 360 (+800%)
Mutual labels:  styled-components, css-in-js, jss
Twin.macro
🦹‍♂️ Twin blends the magic of Tailwind with the flexibility of css-in-js (emotion, styled-components, stitches and goober) at build time.
Stars: ✭ 5,137 (+12742.5%)
Mutual labels:  babel, styled-components, css-in-js
React Redux Saga Boilerplate
Starter kit with react-router, react-helmet, redux, redux-saga and styled-components
Stars: ✭ 535 (+1237.5%)
Mutual labels:  styled-components, css-in-js
Glamorous Native
React Native component styling solved💄
Stars: ✭ 566 (+1315%)
Mutual labels:  styled-components, css-in-js
Design System
Priceline.com Design System
Stars: ✭ 604 (+1410%)
Mutual labels:  styled-components, css-in-js
Language Babel
ES2017, flow, React JSX and GraphQL grammar and transpilation for ATOM
Stars: ✭ 476 (+1090%)
Mutual labels:  babel, styled-components

PostJSS

This project allows to use PostCSS features (plugins, syntaxes etc.) for compiling to JSS object via babel, so you can take benefits from both.

Advantages

  • With PostJSS very easy to start using JSS from SASS/SCSS/PostCSS etc. All you need - update your components and use PostJSS Babel Plugin
  • You can continue use your favorite syntax and the power of PostCSS plugins (stylelint, sort-order etc.), but take advantages of CSS in JS - awesome!
  • You can write styles in styled-components way, css-modules way or in both - the choice is yours
  • You can build your project just with babel
  • Static compilation, no runtime overhead!
  • You can use any library that is compatible with the JSS-object, not only JSS

Installation

npm i postjss -S

Babel Plugin Options

This plugin uses postcss-load-config, so you need to set PostCSS options in package.json/.postcssrc/postcss.config.js/.postcssrc.js

Plugin Options:

  • extensionsRe: String - RegExp for extensions. By default (c|(s[ac]?))ss - for css, sass, scss, sss
  • namespace: String - Set your custom namespace for tagged literals. By default its postjss
  • throwError: Boolean - Plugin will throw an error and stop transpiling, if error caused by PostCSS (eg styling errors). By default false

.babelrc example:

plugins: [
  [
    'postjss/babel', {
      extensionsRe: 's[ac]?ss',
      namespace: 'customPostJSSNamespace',
      throwError: false
    }
  ]
]

How it works?

Please check the counter example

postjss example

As tagged template literal

Babel PostJSS plugin transforms tagged literal into the JSS-object by PostCSS, like:

const styles = postjss`
  .${selector}
    left: ${() => 0}

    margin-${marginType}: 10px

    transition: ${'opacity'} 1s

    color: ${color}

    &::before
      content: '😱'
`

After transpile it would look like:

const styles = {
  [selector]: {
    left: () => 0,
    [`margin-${marginType}`]: '10px',
    transition: 'opacity 1s',
    color: color,
    '&::before': {
      content: "'😱'"
    }
  }
}

You are free to use all PostCSS feature and custom postjss syntax (see bellow)

Notice that if you are using stylelint and property-no-unknown rule, you need to set an option like this (it's required for current postjss parser implementation): property-no-unknown: [true, { ignoreProperties: ['/\$\^var__/'] }]

As a separate styles file

After transpiling imported styles inlined into variable (import name) as a function expression, that accepts an object with arguments and returns a JSS object with styles with arguments usage. Notice that arguments name has uniq scope, so you need not worry about names conflict.

Say you have this styles.sss (with SugarSS i.e.):

.lang-list
  display: flex

  margin: 0
  padding: 0

  list-style: none

.lang
  margin-right: 10px
  padding: 5px

  &.current
    border-bottom: 1px solid red

And a component using it:

import style from './style.sss'

After babel transpiling your component become as:

const styles = function (izexozk) {
  izexozk = Object.assign({}, izexozk);
  return {
    "langList": {
      "display": "flex",
      "margin": "0",
      "padding": "0",
      "listStyle": "none"
    },
    "lang": {
      "marginRight": "10px",
      "padding": "5px",
      "&.current": {
        "borderBottom": "1px solid red"
      }
    }
  };
}

And you can use this with JSS like:

injectSheet(style())

Custom PostJSS Syntax

You can use specific syntax for some JSS-features in your CSS:

  • /JS Code/ - you can place JS-block wrapped by / in every property value
.app
  display: /({ visible }) => visible ? 'block' : 'none'/
  • $^variableName - allows you to use variables passed as arguments to the style function
.app
  background-color: /^color || $color/
  • defaults block for default values - it would be default for accepting args
defaults:
  prop: /^prop || 'test'/
  selector: ''
  • $^propertyName - for custom property/selector names
.app
  $^prop: 100vw

$^selector:
  display: none

Hot Module Replacement

For tagged literals HMR supported out of the box. But for separate styles you need to use postjss hot-loader with webpack: Notice, that you need to set this loader after babel-loader.

use: [
  'babel-loader',
  'postjss/webpack/hot-loader',
]

Linting

You can use stylelint for linting and postcss-reporter for warnings and errors. So with PostJSS it works like a charm:

For CSS files:

sss

For tagged literals:

postjss

Rebuild optimization

Let's say you use some tool for linting. But if you set throwError: true for postjss, it will cause a babel transpilling error, so babel will have to build other files next time, not only fixed.

You can set throwError: false for dev building to avoid this, and use postjss report-loader for webpack (set this loader before babel-loader:

use: [
  'postjss/webpack/report-loader',
  'babel-loader',
]

This will break webpack compiling if there are some errors in PostJSS, but not babel transpiling. And then babel needs to rebuild only fixed file.

Runtime

You can use this module in runtime without babel-plugin.

  • it works just on node.js
  • you can use async and sync versions, so async is more preferable, because sync is blocking your process
  • and it may cause some performance issues

So I recommend this usage only for tests and first steps :)

Sync version:

import postjss from 'postjss'

console.log(postjss`
  .app
    color: red
`)

// output: { app: { color: 'red' } }

Async version:

import postjssAsync from 'postjss/runtime/async'

;(async () => {
  const postjss = await postjssAsync

  console.log(await postjss`
    .app
      color: red
  `)

  // output: { app: { color: 'red' } }
})()

You may also be interested in this project for runtime usage: jss-from-postcss

Full Example

style.sss

@import 'vars.sss'
@import 'mixins.sss'

$color: 'green'

:root
  --color: $color

defaults:
  prop: /$^prop || 'test'/
  selector: ''

.app
  position: absolute
  top: 0
  left: 0

  display: /({ name }) => name + 1 + $color-from-import/
  overflow-y: auto
  flex-direction: column

  width: 100vw
  height: 100vh

  color: var(--color)
  background-color: /$^value || $color/

  $^prop: 100vw

$^selector:
  display: none

.content
  @mixin container

  padding: 20px

.header
  border-bottom: 1px solid #eee

component.jsx

const style = function (izezix) {
  izezix = Object.assign({
    "prop": izezix.prop || 'test',
    "selector": "''"
  }, izezix);
  return {
    "app": {
      "position": "absolute",
      "top": "0",
      "left": "0",
      "display": function ({
        name
      }) {
        return name + 1 + 'black';
      },
      "overflowY": "auto",
      "flexDirection": "column",
      "width": "100vw",
      "height": "100vh",
      "color": "green",
      "backgroundColor": izezix.value || 'green',
      [izezix.prop]: "100vw"
    },
    [izezix.selector]: {
      "display": "none"
    },
    "content": {
      "position": "absolute",
      "margin": "0 auto",
      "&::before": {
        "content": "''"
      },
      "padding": "20px"
    },
    "header": {
      "borderBottom": "1px solid #eee"
    }
  };
}

Links

  • JSS - Great lib for CSS in JS
  • PostCSS - Awesome tool for customizable style transform

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].