Comprehensive configuration options for babel-plugin-emotion, controlling optimization behavior, labeling, source maps, and import mapping.
The default export that creates the Babel plugin with specified configuration.
/**
* Main Babel plugin factory function
* @param babel - Babel instance providing types and utilities
* @param options - Plugin configuration options
* @returns Babel plugin object with name, inherits, and visitor
* @throws Error if autoLabel option is invalid
*/
function emotionBabelPlugin(babel: any, options: EmotionPluginOptions): BabelPlugin;Usage Example:
// In Babel configuration
{
"plugins": [
["@emotion", {
"sourceMap": true,
"autoLabel": "dev-only",
"labelFormat": "[filename]--[local]",
"cssPropOptimization": true
}]
]
}Controls source map generation for transformed styles.
/**
* Source map configuration option
* @default true
*/
sourceMap?: boolean;When enabled:
Usage Example:
// Enable source maps (default)
{
"plugins": [["@emotion", { "sourceMap": true }]]
}
// Disable source maps
{
"plugins": [["@emotion", { "sourceMap": false }]]
}Controls automatic generation of contextual class names.
/**
* Auto label behavior configuration
* @default "dev-only"
*/
autoLabel?: 'dev-only' | 'always' | 'never';Values:
'dev-only': Adds labels in development, optimizes them out in production'always': Always adds labels when possible'never': Disables automatic labeling entirelyUsage Example:
// Input with autoLabel: 'dev-only'
const brownStyles = css({ color: 'brown' });
// Output
const brownStyles = /*#__PURE__*/ css({ color: 'brown' }, 'label:brownStyles;');
// Result class: css-1q8eu9e-brownStylesDefines the format of generated labels when autoLabel is enabled.
/**
* Label format string with variable substitution
* @default "[local]"
*/
labelFormat?: string;Available variables:
[local]: Variable name that the css/styled result is assigned to[filename]: File name (without extension) containing the expression[dirname]: Directory name containing the fileUsage Example:
// Configuration
{
"labelFormat": "[filename]--[local]"
}
// Input in BrownView.js
const brownStyles = css({ color: 'brown' });
// Output
const brownStyles = /*#__PURE__*/ css(
{ color: 'brown' },
'label:BrownView--brownStyles;'
);
// Result class: css-hash-BrownView--brownStylesControls optimization of the css prop in JSX elements.
/**
* CSS prop optimization configuration
* @default true
*/
cssPropOptimization?: boolean;When enabled:
Usage Example:
// With cssPropOptimization: true (default)
<div css={{ color: 'blue', fontSize: 16 }} />
// Gets optimized during transformationEnables transformation of re-exported emotion APIs through custom import mapping.
/**
* Custom import mapping for re-exported emotion APIs
*/
importMap?: ImportMap;
interface ImportMap {
[importSource: string]: {
[exportName: string]: {
/** [packageName, exportName] tuple pointing to canonical emotion export */
canonicalImport: [string, string];
/** Additional options for the specific transformer */
[key: string]: any;
};
};
}Usage Example:
{
"importMap": {
"my-package": {
"anotherExport": {
"canonicalImport": ["@emotion/styled", "default"],
"styledBaseImport": ["my-package/base", "anotherExport"]
}
},
"some-package": {
"someExport": {
"canonicalImport": ["@emotion/react", "css"]
},
"thisIsTheJsxExport": {
"canonicalImport": ["@emotion/react", "jsx"]
}
}
}
}interface EmotionPluginOptions {
sourceMap?: boolean;
autoLabel?: 'dev-only' | 'always' | 'never';
labelFormat?: string;
cssPropOptimization?: boolean;
importMap?: ImportMap;
}
interface BabelPlugin {
name: string;
inherits: any;
visitor: BabelVisitor;
}
/** Constant array of valid autoLabel values */
const AUTO_LABEL_VALUES: ['dev-only', 'never', 'always'];