TypeScript Plugin for Styled Components

3.0.0 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Demonstrates how to integrate `typescript-plugin-styled-components` into a Webpack configuration using `awesome-typescript-loader` to enable compile-time styled component name generation.

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']
  }
};

view raw JSON →