Socket
Socket
Sign inDemoInstall

babel-plugin-emotion

Package Overview
Dependencies
24
Maintainers
2
Versions
90
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    babel-plugin-emotion

A recommended babel preprocessing plugin for emotion, The Next Generation of CSS-in-JS.


Version published
Maintainers
2
Install size
3.45 MB
Created

Package description

What is babel-plugin-emotion?

The babel-plugin-emotion npm package is designed to enhance the development experience when using Emotion, a popular CSS-in-JS library. It provides optimizations and custom transformations for styles defined in JavaScript, enabling more efficient and powerful styling solutions in React applications. This plugin facilitates the use of Emotion by offering features such as source maps for easier debugging, auto-labeling for better readability of class names, and the optimization of styles for production.

What are babel-plugin-emotion's main functionalities?

Source Maps

Generates source maps for styles, improving the debugging process by allowing developers to trace back to the original location of a style definition within their code.

"use strict";

var _emotion = require("@emotion/core");

function App() {
  return /*#__PURE__*/_emotion.jsx("div", {
    css: {
      color: 'hotpink'
    }
  }, "Hello World");
}

Auto-labeling

Automatically adds labels to the generated class names based on the name of the variable or component. This enhances readability in the DOM and helps in identifying components during debugging.

"use strict";

var _emotion = require("@emotion/core");

var _styled = /*#__PURE__*/(0, _emotion.default)("div")
/*#__PURE__*/
.emotionLabel('MyComponent_styled_h1nx9f');

Optimization for Production

In production, the plugin optimizes the styles by compacting and efficient handling, reducing the size of the generated CSS and improving load times.

process.env.NODE_ENV === 'production' ? /*#__PURE__*/require('@emotion/css').css('label:MyComponent;', 'color:hotpink;') : /*#__PURE__*/require('@emotion/css').css('color:hotpink;');

Other packages similar to babel-plugin-emotion

Readme

Source

babel-plugin-emotion

Babel plugin for the minification and optimization of emotion styles.

The Babel Plugin is highly recommended, but not required in version 8 and above.

Feature table

Feature/SyntaxNativeBabel Plugin RequiredNotes
css``
css(...)Generally used for object styles.
styled('div')`` syntaxBoth string and object styles work without this plugin.
styled.div`` syntaxSupporting the shortcut syntax without the Babel plugin requires a large list of valid elements to be included in the bundle.
components as selectorsAllows an emotion component to be used as a CSS selector.
MinificationAny leading/trailing space between properties in your css and styled blocks is removed. This can reduce the size of your final bundle.
Dead Code EliminationUglifyjs will use the injected /*#__PURE__*/ flag comments to mark your css and styled blocks as candidates for dead code elimination.
Static ExtractionGenerated CSS that is eligible for extraction can be moved to an external css file.
Source MapsWhen enabled, navigate directly to the style declaration in your javascript file.
css as PropConvenient helper for calling css and appending the generated className during compile time.
Contextual Class NamesGenerated class names include the name of the variable or component they were defined in.

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

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

Usage

.babelrc

Without options:

{
  "plugins": ["emotion"]
}

With options:

Defaults Shown

{
  "plugins": [
    ["emotion", {
      "hoist": false,
      "sourceMap": false,
      "autoLabel": false,
      "extractStatic": false,
      "importedNames": {
        "styled": "styled",
        "css": "css",
        "keyframes": "keyframes",
        "injectGlobal": "injectGlobal",
        "fontFace": "fontFace",
        "merge": "merge"
      }
    }]
  ]
}

Recommended Setup

Use Babel's env property

.babelrc

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

Via CLI

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

Via Node API

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

Options

hoist

boolean, defaults to false.

This option enables the following:

  • Any argument supplied to css or styled is hoisted.

By hoisting the argument, or assigning the value to a variable, emotion is able to leverage the use of a WeakMap cache to increase performance. Users of object styles will benefit the most from enabling this option.

In

css({ color: 'brown' });

Out

var _ref = { color: 'brown' };
css(_ref);

sourceMap

boolean, defaults to false.

This option enables the following:

  • Injected source maps for use in browser dev tools

Documentation

autoLabel

boolean, defaults to false.

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.
css

In

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

Out

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

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

styled

In

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

Out

const Profile = () => {
  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-13djram-H1

extractStatic

boolean, defaults to false.

This option enables the following:

  • Extract static styles into CSS files.

Documentation

importedNames

object, defaults to

{
  "styled": "styled",
  "css": "css",
  "keyframes": "keyframes",
  "injectGlobal": "injectGlobal",
  "fontFace": "fontFace",
  "merge": "merge"
}

This option enables the following:

  • Configurable import names

Documentation

Keywords

FAQs

Last updated on 30 Nov 2017

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc