TypeScript Plugin for Styled Components
This package, `typescript-plugin-styled-components`, serves as a TypeScript transformer designed to enhance the development and debugging experience when working with `styled-components`. It achieves this by providing compile-time information, specifically the names of created styled components, to the runtime. This functionality is crucial for tools like style linting, inspecting components in development tools, and server-side rendering hydration. The current stable version is 3.0.0, which requires TypeScript 4.8+ or 5.0+. The release cadence is generally tied to significant TypeScript version updates. It's a key differentiator for projects that transpile TypeScript code directly using `tsc`, `ts-loader`, or `awesome-typescript-loader`, as opposed to those using Babel for TypeScript transformation, which should instead use `babel-plugin-styled-components`.
Common errors
-
TypeError: customTransformers.before is not iterable
cause The `getCustomTransformers` function in your webpack loader configuration is returning an object that either lacks a `before` property or its value is not an array.fixEnsure your `getCustomTransformers` function returns an object in the format `{ before: [styledComponentsTransformer] }`. -
Error: Cannot read properties of undefined (reading 'default')
cause Attempting to access the `default` export incorrectly in a CommonJS environment, or destructuring a module that primarily exports a default.fixFor CommonJS, use `require('typescript-plugin-styled-components').default`. For ESM, use `import createStyledComponentsTransformer from 'typescript-plugin-styled-components'`. -
TSxxxx: [Related to AST transformation or compiler API]
cause Mismatch between the installed `typescript` version and the version required by `typescript-plugin-styled-components`, leading to incompatible TypeScript AST or API usage.fixCheck the plugin's peer dependencies and your installed TypeScript version. Upgrade TypeScript to meet the plugin's requirements (e.g., TS 4.8+/5.0+ for plugin v3.0.0). -
awesome-typescript-loader: functions are not transferrable between processes in forked mode.
cause You are using `awesome-typescript-loader` in a forked process setup, which prevents passing functions like `getCustomTransformers` directly.fixFollow the 'Forked process configuration' instructions in the plugin's documentation for `awesome-typescript-loader`.
Warnings
- breaking Version 3.0.0 introduces a breaking change, requiring TypeScript 4.8+ or 5.0+. Projects on older TypeScript versions will encounter compilation errors.
- breaking Version 2.0.0 upgraded its internal TypeScript API usage to align with TypeScript 4+, making it incompatible with TypeScript versions prior to 4.0.
- gotcha This plugin is designed for projects transpiling TypeScript with `tsc` or loaders like `ts-loader` and `awesome-typescript-loader`. If your project uses Babel (specifically `babel-plugin-transform-typescript`) for TypeScript transpilation, this plugin will not function; you should instead use `babel-plugin-styled-components`.
- deprecated The `.extend` transformation pattern for styled-components was removed in version 1.5.0. Using this deprecated pattern will no longer be transformed correctly by the plugin.
- gotcha When `awesome-typescript-loader` operates in development mode with forked processes, the standard method of configuring `getCustomTransformers` (passing a function) does not work due to limitations in function serialization between processes.
Install
-
npm install typescript-plugin-styled-components -
yarn add typescript-plugin-styled-components -
pnpm add typescript-plugin-styled-components
Imports
- createStyledComponentsTransformer
import { createStyledComponentsTransformer } from 'typescript-plugin-styled-components';import createStyledComponentsTransformer from 'typescript-plugin-styled-components';
- createStyledComponentsTransformer
const createStyledComponentsTransformer = require('typescript-plugin-styled-components');const createStyledComponentsTransformer = require('typescript-plugin-styled-components').default;
Quickstart
const createStyledComponentsTransformer = require('typescript-plugin-styled-components').default;
const path = require('path');
// Create a transformer; the factory additionally accepts an options object.
// Example options: { ssr: true, displayName: true, componentIdPrefix: 'sc' }
const styledComponentsTransformer = createStyledComponentsTransformer();
module.exports = {
mode: 'development', // or 'production'
entry: './src/index.ts',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'awesome-typescript-loader', // or 'ts-loader'
options: {
// Other loader options like `configFileName`
getCustomTransformers: () => ({ before: [styledComponentsTransformer] }),
// Important note: If using awesome-typescript-loader in forked process mode,
// this setup might require a different configuration approach documented
// on the plugin's GitHub page under 'Forked process configuration'.
}
}
]
},
resolve: {
extensions: ['.ts', '.tsx', '.js', '.jsx']
}
};