SWC plugin for emotion css-in-js library that provides compile-time transformations to optimize emotion-based styling code
npx @tessl/cli install tessl/npm-swc--plugin-emotion@11.1.0@swc/plugin-emotion is a WebAssembly-based SWC plugin that provides compile-time transformations for the emotion CSS-in-JS library. It optimizes emotion-based styling code by transforming emotion API calls, adding automatic labeling, generating source maps, and performing CSS minification during the build process.
npm install @swc/plugin-emotionThis plugin does not export importable functions directly. Instead, it processes existing emotion imports in your code. The plugin transforms these common emotion imports:
import { css } from "@emotion/react";
import styled from "@emotion/styled";
import { Global } from "@emotion/react";const { css } = require("@emotion/react");
const styled = require("@emotion/styled");Configure the plugin in your SWC configuration:
{
jsc: {
experimental: {
plugins: [
["@swc/plugin-emotion", {
sourceMap: true,
autoLabel: "dev-only",
labelFormat: "[local]"
}]
]
}
}
}The plugin then transforms your emotion code:
// Input
const styles = css`
color: red;
font-size: 16px;
`;
// Output (transformed)
const styles = css(
"color:red;font-size:16px;",
"label:styles;",
"/*# sourceMappingURL=... */"
);@swc/plugin-emotion is built around several key components:
Configure the plugin behavior including source maps, automatic labeling, and label formats. Essential for integrating with your build pipeline and development workflow.
interface EmotionPluginConfig {
sourceMap?: boolean;
autoLabel?: "never" | "dev-only" | "always";
labelFormat?: string;
}Transforms emotion css function calls and template literals, adding labels and source maps while minifying CSS content.
// Transforms these patterns:
// css`styles...`
// css({ styles })
// customEmotion.css`styles...`Transforms emotion styled component calls, adding target class names and labels for better debugging and optimization.
// Transforms these patterns:
// styled.div`styles...`
// styled('div')`styles...`
// styled('div')({ styles })Transforms emotion Global JSX components by converting styles attributes to optimized array format with source maps.
// Transforms this pattern:
// <Global styles={cssStyles} />The plugin supports these official emotion libraries:
The plugin handles various error scenarios: