A Babel preprocessing plugin for optimizing and minifying Emotion styles in CSS-in-JS applications. This plugin provides compile-time transformations that optimize emotion CSS at build time, including minification, dead code elimination, source maps, and automatic labeling.
Important Migration Notice: As of version 11.0.0, this package has been renamed to @emotion/babel-plugin. The original babel-plugin-emotion package now serves as a migration stub that throws an error directing users to update their Babel configuration.
npm install --save-dev babel-plugin-emotion (deprecated) or npm install --save-dev @emotion/babel-plugin (current)"plugins": ["emotion"] with "plugins": ["@emotion"] in your Babel configThe plugin is used via Babel configuration and does not require direct imports in your code. However, the plugin itself exports the following:
// Main plugin export (for Babel)
const emotionPlugin = require('babel-plugin-emotion');
// Or with the new package name
const emotionPlugin = require('@emotion/babel-plugin');For macro usage:
// Emotion macros (when using babel-plugin-macros)
import { css, keyframes, injectGlobal, flush, hydrate } from '@emotion/css/macro';
import { jsx, css, Global, keyframes } from '@emotion/react/macro';
import styled from '@emotion/styled/macro';.babelrc
{
"plugins": ["@emotion"]
}With options:
{
"plugins": [
[
"@emotion",
{
"sourceMap": true,
"autoLabel": "dev-only",
"labelFormat": "[local]",
"cssPropOptimization": true
}
]
]
}Input:
const myStyles = css`
font-size: 20px;
@media (min-width: 420px) {
color: blue;
${css`
width: 96px;
height: 96px;
`};
line-height: 26px;
}
background: green;
${{ backgroundColor: 'hotpink' }};
`;Output:
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' },
';'
);The babel-plugin-emotion is built around several key components:
Main plugin configuration options and behavior for Babel integration.
/**
* Main Babel plugin factory function
* @param babel - Babel instance with types
* @param options - Plugin configuration options
* @returns Babel plugin object with visitors
*/
function emotionBabelPlugin(babel: any, options: EmotionPluginOptions): BabelPlugin;
interface EmotionPluginOptions {
/** Enable source map generation (default: true) */
sourceMap?: boolean;
/** Auto label behavior: 'dev-only' | 'always' | 'never' (default: 'dev-only') */
autoLabel?: 'dev-only' | 'always' | 'never';
/** Label format string (default: '[local]') */
labelFormat?: string;
/** Enable CSS prop optimization (default: true) */
cssPropOptimization?: boolean;
/** Custom import mapping for re-exported emotion APIs */
importMap?: ImportMap;
}
interface ImportMap {
[importSource: string]: {
[exportName: string]: {
canonicalImport: [string, string];
[key: string]: any;
};
};
}
interface BabelPlugin {
name: string;
inherits: any;
visitor: BabelVisitor;
}Core transformation functions for optimizing CSS expressions and styled components.
/**
* Transform CSS call expressions for optimization
*/
function transformCssCallExpression(params: TransformCssParams): void;
/**
* Transform CSS array expressions without css wrapper
*/
function transformCsslessArrayExpression(params: TransformParams): void;
/**
* Transform CSS object expressions without css wrapper
*/
function transformCsslessObjectExpression(params: TransformParams): void;
interface TransformCssParams {
state: any;
babel: any;
path: any;
sourceMap?: string;
annotateAsPure?: boolean;
}
interface TransformParams {
state: any;
babel: any;
path: any;
}Macro transformers for different emotion packages and their configuration.
/**
* Collection of macro transformers for different emotion packages
*/
const macros: EmotionMacros;
interface EmotionMacros {
/** Core macro for @emotion/react */
core: MacroFunction;
/** Styled macro for @emotion/native */
nativeStyled: MacroFunction;
/** Styled macro for @emotion/primitives */
primitivesStyled: MacroFunction;
/** Styled macro for @emotion/styled */
webStyled: MacroFunction;
/** Macro for @emotion/css */
vanillaEmotion: MacroFunction;
}
type MacroFunction = (params: MacroParams) => void;
interface MacroParams {
path: any;
references: any;
state: any;
babel: any;
isEmotionCall: boolean;
isBabelMacrosCall: boolean;
}/** Type alias for Babel AST paths */
type BabelPath = any;
/** Type alias for Babel plugin pass state */
type EmotionBabelPluginPass = any;
interface BabelVisitor {
ImportDeclaration(path: BabelPath, state: EmotionBabelPluginPass): void;
Program(path: BabelPath, state: EmotionBabelPluginPass): void;
JSXAttribute(path: BabelPath, state: EmotionBabelPluginPass): void;
CallExpression: {
exit(path: BabelPath, state: EmotionBabelPluginPass): void;
};
}