11.0.0 • Published 3 years ago

babel-plugin-emotion v11.0.0

Weekly downloads
4,138,521
License
MIT
Repository
github
Last release
3 years ago

babel-plugin-emotion

Babel plugin for the minification and optimization of emotion styles.

babel-plugin-emotion is highly recommended, but not required in version 8 and above of emotion.

Features

Example

In

const myStyles = css`
  font-size: 20px;
  @media (min-width: 420px) {
    color: blue;
    ${css`
      width: 96px;
      height: 96px;
    `};
    line-height: 26px;
  }
  background: green;
  ${{ backgroundColor: 'hotpink' }};
`

Out

const myStyles = /* #__PURE__ */ css(
  'font-size:20px;@media(min-width:420px){color:blue;',
  /* #__PURE__ */ css('width:96px;height:96px;'),
  ';line-height:26px;}background:green;',
  { backgroundColor: 'hotpink' },
  ';'
)

Installation

yarn add --dev babel-plugin-emotion

or if you prefer npm

npm install --save-dev babel-plugin-emotion

Usage

Via .babelrc (Recommended)

.babelrc

Without options:

{
  "plugins": ["emotion"]
}

With options:

Defaults Shown

{
  "plugins": [
    [
      "emotion",
      {
        "sourceMap": false,
        "autoLabel": process.env.NODE_ENV !== 'production',
        "labelFormat": "[local]",
        "cssPropOptimization": true
      }
    ]
  ]
}

Recommended Setup

Use Babel's env property

.babelrc

{
  "env": {
    "production": {
      "plugins": [["emotion", { "hoist": true }]]
    },
    "development": {
      "plugins": [["emotion", { "sourceMap": true, "autoLabel": true }]]
    }
  }
}

Via CLI

babel --plugins babel-plugin-emotion script.js

Via Node API

require('@babel/core').transform('code', {
  plugins: ['babel-plugin-emotion']
})

Options

sourceMap

boolean, defaults to false.

This option enables the following:

  • Injected source maps for use in browser dev tools

Documentation

autoLabel

boolean, defaults to process.env.NODE_ENV !== 'production'.

This option enables the following:

  • Automatically adds the label property to styles so that class names generated by css or styled include the name of the variable the result is assigned to.
  • Please note that non word characters in the variable will be removed (Eg. iconStyles$1 will become iconStyles1) because $ is not valid CSS ClassName Selector

css

In

const brownStyles = css({ color: 'brown' })

Out

const brownStyles = /*#__PURE__*/ css({ color: 'brown' }, 'label:brownStyles;')

brownStyles's value would be css-1q8eu9e-brownStyles

labelFormat

string, defaults to "[local]".

This option only works when autoLabel is set to true. It allows you to define the format of the resulting label. The format is defined via string where variable parts are enclosed in square brackets []. For example labelFormat: "my-classname--[local]", where [local] will be replaced with the name of the variable the result is assigned to.

Allowed values:

  • [local] - the name of the variable the result of the css or styled expression is assigned to.
  • [filename] - name of the file (without extension) where css or styled expression is located.

This format only affects the label property of the expression, meaning that the css prefix and hash will be prepended automatically.

css

In

// BrownView.js
// autoLabel: true
// labelFormat: [filename]--[local]
const brownStyles = css({ color: 'brown' })

Out

const brownStyles = /*#__PURE__*/ css(
  { color: 'brown' },
  'label:BrownView--brownStyles;'
)

BrownView--brownStyles's value would be css-hash-BrownView--brownStyles

styled

In

const H1 = styled.h1({
  borderRadius: '50%',
  transition: 'transform 400ms ease-in-out',
  boxSizing: 'border-box',
  display: 'flex',
  ':hover': {
    transform: 'scale(1.2)'
  }
})

Out

const H1 = /*#__PURE__*/ styled('h1', {
  label: 'H1'
})({
  borderRadius: '50%',
  transition: 'transform 400ms ease-in-out',
  boxSizing: 'border-box',
  display: 'flex',
  ':hover': {
    transform: 'scale(1.2)'
  }
})

H1's class name attribute would be css-hash-H1

instances

Array<string>, defaults to

;['emotion']

This option allows babel-plugin-emotion to know which imports to treat as emotion imports and transform as such. This option is only required if you use a custom instance of emotion created with create-emotion or you're importing emotion from somewhere other than the paths above. Relative paths are resolved relative to process.cwd()(the current working directory).

Documentation

cssPropOptimization

boolean, defaults to true.

This option assumes that you are using something to make @emotion/core's jsx function work for all jsx. If you are not doing so and you do not want such optimizations to occur, disable this option.

Babel Macros

Instead of using babel-plugin-emotion, you can use emotion with babel-plugin-macros. Add babel-plugin-macros to your babel config and use the imports/packages shown below.

import styled from 'react-emotion/macro'
import { css, keyframes, injectGlobal, flush, hydrate } from 'emotion/macro'
import css from '@emotion/css.macro'
import styled from '@emotion/styled.macro'

create-react-app issue discussing macros.

killi8n-react-native-fast-image@rowsncolumns/spreadsheet@pil0t/gatsby-theme-novela@thirdwave-network/thirdwave-gatsby-theme@travelshift/websapphire-ui@softbankrobotics/component-lib@kyrelldixon/gatsby-theme-novela@viren.ratan/react-scripts-jssoev-test-plugin@component-driven/gatsby-theme-content-collectionsnd-ably-consolecustom-gatsby-theme-novela@jesseburke/basic-react-components@jesseburke/graphics-components@lite-v3/babel-config-react@infinitebrahmanuniverse/nolb-babel-plugin-e@s-isabella/scripts@everything-registry/sub-chunk-1202@itoa/app-admin-uinois-react-toastmikatre-thememethod-crmplank-components-naspreact-emotionpetrifiedpigment-scriptsms-archetype-corenature-graphics-pattern-library@cartiv/web-appbetterlyearn2@charlietango/babel-preset-sx-propblog-theme@coli.codes/editorbs-react-scripts@clifmo/gatsby-theme-novela@closet-design-system/core-framer@draftbox-co/gatsby-ghost-novela-theme@draftbox-co/gatsby-wordpress-novela-theme@codewitchbella/scripts@cthsiao/gatsby-theme-novela-modcatalog-guidecatalog_ds_docs@davidsemakula/keystone-app-admin-ui@davidway/gatsby-theme-novelacatalogcatalog-docs@cute-apocalypse/react-tree@curve-technology/web-ds@cyberspace/webpack-configchialing-axe@freddieridell/babel-presetwebpack-features@binance-name/gatsby-theme-novela@binoy14/react-scripts@brisk-docs/website@bumbag/emotionbabel-preset-zkitbabel-preset-zapierbabel-preset-mkittbabel-preset-duflbabel-preset-enhanced-react-stylebabel-preset-brunonobabel-preset-cute@blkmarketco/components-library@getzeroday/gatsby-theme-novela@alec.brunelle/gatsby-theme-novelau-library@agney/peapal-react-scriptsxometry-babel-preset-react-appanaizing-components@alchemy-se/catalog@alexghenderson/babel-preset@gourmet/plugin-react-emotioncomponents-base@helpscout/zero@guiym/react-select-no-css@gofore/react-scriptscodekite@lskjs/build@lskjs/generalmusicbed-dzsneutrino-preset-emotionreact-magic-scrollreactify-uireact-scripts-componentsreact-scripts-test-for-babelreact-emotionalreact-galeriste-scriptsrobot-managementsequelcomponentsettings-editorsetup-react-frontendslackercode-ui-reactsmile-xin-react-selectterampilalphastyled-scssselect-test-a11ysvelte-component-libreact-select-with-firefox-touch-patch
10.2.2

3 years ago

11.0.0

3 years ago

11.0.0-rc.0

4 years ago

10.0.33

4 years ago

10.0.29

4 years ago

10.0.28

4 years ago

11.0.0-next.11

4 years ago

11.0.0-next.10

4 years ago

10.0.27

4 years ago

11.0.0-next.8

4 years ago

11.0.0-next.6

4 years ago

11.0.0-next.4

4 years ago

11.0.0-next.3

4 years ago

11.0.0-next.1

4 years ago

11.0.0-next.0

4 years ago

10.0.23

4 years ago

10.0.22

5 years ago

10.0.21

5 years ago

10.0.20

5 years ago

10.0.19

5 years ago

10.0.17

5 years ago

10.0.16

5 years ago

10.0.15

5 years ago

10.0.14

5 years ago

10.0.13

5 years ago

10.0.9

5 years ago

10.0.8

5 years ago

10.0.7

5 years ago

10.0.6

5 years ago

10.0.5

5 years ago

10.0.4

5 years ago

10.0.3

5 years ago

10.0.2

5 years ago

10.0.0

5 years ago

10.0.0-beta.13

5 years ago

10.0.0-beta.12

5 years ago

10.0.0-beta.11

5 years ago

10.0.0-beta.10

5 years ago

10.0.0-beta.9

5 years ago

10.0.0-beta.8

5 years ago

10.0.0-beta.7

5 years ago

10.0.0-beta.6

6 years ago

10.0.0-beta.5

6 years ago

10.0.0-beta.4

6 years ago

10.0.0-beta.3

6 years ago

10.0.0-beta.2

6 years ago

10.0.0-beta.1

6 years ago

10.0.0-beta.0

6 years ago

9.2.11

6 years ago

9.2.10

6 years ago

9.2.9

6 years ago

9.2.8

6 years ago

9.2.6

6 years ago

9.2.5

6 years ago

9.2.4

6 years ago

9.2.0

6 years ago

9.1.2

6 years ago

9.1.0

6 years ago

9.0.1

6 years ago

9.0.0

6 years ago

9.0.0-3

6 years ago

9.0.0-2

6 years ago

9.0.0-1

6 years ago

9.0.0-0

6 years ago

8.0.12

6 years ago

8.0.11

6 years ago

8.0.10

6 years ago

8.0.9

7 years ago

8.0.6

7 years ago

8.0.4

7 years ago

8.0.3

7 years ago

8.0.2

7 years ago

8.0.2-11

7 years ago

8.0.2-10

7 years ago

8.0.2-7

7 years ago

8.0.2-6

7 years ago

8.0.2-5

7 years ago

8.0.2-3

7 years ago

8.0.2-2

7 years ago

8.0.2-1

7 years ago

8.0.1-0

7 years ago

8.0.0-0

7 years ago

7.3.2

7 years ago

7.3.1

7 years ago

7.3.0

7 years ago

7.2.2

7 years ago

7.1.0

7 years ago