SWC plugin for emotion css-in-js library that provides compile-time transformations to optimize emotion-based styling code
—
Configuration options for controlling @swc/plugin-emotion behavior including source maps, automatic labeling, and label formatting.
The main configuration interface for the emotion plugin.
/**
* Configuration options for @swc/plugin-emotion
* All options are optional and have sensible defaults
*/
interface EmotionPluginConfig {
/** Enable/disable source map generation (default: true in development, false in production) */
sourceMap?: boolean;
/** Control when automatic labels are added to CSS */
autoLabel?: "never" | "dev-only" | "always";
/** Format string for generated labels with placeholder support */
labelFormat?: string;
}Controls whether inline source maps are generated for transformed CSS.
/**
* Source map generation setting
* @default true in development environment, false in production
*/
sourceMap?: boolean;Usage Examples:
// Enable source maps in all environments
{
jsc: {
experimental: {
plugins: [["@swc/plugin-emotion", { sourceMap: true }]]
}
}
}
// Disable source maps completely
{
jsc: {
experimental: {
plugins: [["@swc/plugin-emotion", { sourceMap: false }]]
}
}
}Controls when automatic CSS class name labels are added to emotion styles.
/**
* Automatic label generation setting
* @default "dev-only"
*/
autoLabel?: "never" | "dev-only" | "always";Label Modes:
Usage Examples:
// Always add labels
{
jsc: {
experimental: {
plugins: [["@swc/plugin-emotion", { autoLabel: "always" }]]
}
}
}
// Never add labels
{
jsc: {
experimental: {
plugins: [["@swc/plugin-emotion", { autoLabel: "never" }]]
}
}
}Customizes the format of automatically generated CSS labels using placeholder tokens.
/**
* Label format template with placeholder support
* @default "[local]"
*/
labelFormat?: string;Supported Placeholders:
Usage Examples:
// Custom label format with filename
{
jsc: {
experimental: {
plugins: [["@swc/plugin-emotion", {
labelFormat: "[filename]--[local]"
}]]
}
}
}
// Include directory context
{
jsc: {
experimental: {
plugins: [["@swc/plugin-emotion", {
labelFormat: "[dirname]-[local]"
}]]
}
}
}Label Generation Examples:
// With labelFormat: "[filename]--[local]"
const buttonStyles = css`color: blue;`;
// Generates: label: "MyComponent--buttonStyles"
// With labelFormat: "[local]"
const headerStyles = css`font-size: 24px;`;
// Generates: label: "headerStyles"Full configuration with all available options:
{
jsc: {
parser: {
syntax: "typescript",
tsx: true
},
experimental: {
plugins: [
["@swc/plugin-emotion", {
sourceMap: true,
autoLabel: "dev-only",
labelFormat: "[filename]--[local]"
}]
]
}
}
}The plugin automatically adapts behavior based on the build environment:
/**
* Environment detection affects default behavior:
* - Development: sourceMap defaults to true, autoLabel defaults to active
* - Production: sourceMap defaults to false, autoLabel follows setting
*/Environment Examples:
// Development environment behavior
process.env.NODE_ENV = "development";
// sourceMap: true (default), autoLabel: active if "dev-only" or "always"
// Production environment behavior
process.env.NODE_ENV = "production";
// sourceMap: false (default), autoLabel: active only if "always"For custom emotion library configurations, the plugin supports import mapping:
/**
* Import map for custom emotion libraries (advanced usage)
* Maps import sources to canonical emotion transformations
*/
interface ImportMap {
[importSource: string]: {
[localExportName: string]: {
canonicalImport: [string, string]; // [packageName, exportName]
styledBaseImport?: [string, string];
};
};
}Install with Tessl CLI
npx tessl i tessl/npm-swc--plugin-emotion