All Projects → Igorbek → Typescript Plugin Styled Components

Igorbek / Typescript Plugin Styled Components

TypeScript transformer for improving the debugging experience of styled-components

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to Typescript Plugin Styled Components

React Bootstrap Webpack Starter
ReactJS 16.4 + new React Context API +react Router 4 + webpack 4 + babel 7+ hot Reload + Bootstrap 4 + styled-components
Stars: ✭ 103 (-68.79%)
Mutual labels:  webpack, styled-components
Reeakt
A modern React boilerplate to awesome web applications
Stars: ✭ 116 (-64.85%)
Mutual labels:  webpack, styled-components
Cjstoesm
A tool that can transform CommonJS to ESM
Stars: ✭ 109 (-66.97%)
Mutual labels:  webpack, transformer
React Ssr Starter
All have been introduced React environment
Stars: ✭ 20 (-93.94%)
Mutual labels:  webpack, styled-components
React Redux Styled Hot Universal
react boilerplate used best practices and focus on performance
Stars: ✭ 147 (-55.45%)
Mutual labels:  webpack, styled-components
Muiscrr Boilerplate
Material-UI Styled Components React Redux boilerplate with offline mode support 🎬
Stars: ✭ 35 (-89.39%)
Mutual labels:  webpack, styled-components
Web Extension Starter
Typescript, React, Redux, Styled-Component and Webpack based sample extension boilerplate. Runs on Chrome and Firefox. Sample chrome extension.
Stars: ✭ 115 (-65.15%)
Mutual labels:  webpack, styled-components
Alpha
Craft your own web-based chatbot
Stars: ✭ 113 (-65.76%)
Mutual labels:  webpack, styled-components
Project Webcube
Continuously updated JS infrastructure for modern web dev
Stars: ✭ 141 (-57.27%)
Mutual labels:  webpack, styled-components
Blockstack.org
The Blockstack website
Stars: ✭ 132 (-60%)
Mutual labels:  webpack, styled-components
Naomi
Sublime Text enhanced syntax highlighting for JavaScript ES6/ES7/ES2015/ES2016/ES2017+, Babel, FlowType, React JSX, Styled Components, HTML5, SCSS3, PHP 7, phpDoc, PHPUnit, MQL4. Basic: Git config files.
Stars: ✭ 544 (+64.85%)
Mutual labels:  webpack, styled-components
Ssr Sample
A minimum sample of Server-Side-Rendering, Single-Page-Application and Progressive Web App
Stars: ✭ 285 (-13.64%)
Mutual labels:  webpack, styled-components
Nyancss
🌈 Write plain CSS while reaping benefits of CSS-in-JS
Stars: ✭ 544 (+64.85%)
Mutual labels:  webpack, styled-components
Molecule
⚛️ – :atom: – ⚛️ Boilerplate for cross platform web/native react apps with electron.
Stars: ✭ 95 (-71.21%)
Mutual labels:  webpack, styled-components
Razzle Material Ui Styled Example
Razzle Material-UI example with Styled Components using Express with compression
Stars: ✭ 117 (-64.55%)
Mutual labels:  webpack, styled-components
Styled React Boilerplate
Minimal & Modern boilerplate for building apps with React & styled-components
Stars: ✭ 198 (-40%)
Mutual labels:  webpack, styled-components
React Bolt
⚡ The most simple & robust boilerplate for your React projects.
Stars: ✭ 298 (-9.7%)
Mutual labels:  webpack, styled-components
Bridgetown
A Webpack-aware, Ruby-powered static site generator for the modern Jamstack era
Stars: ✭ 317 (-3.94%)
Mutual labels:  webpack
Gpt2client
✍🏻 gpt2-client: Easy-to-use TensorFlow Wrapper for GPT-2 117M, 345M, 774M, and 1.5B Transformer Models 🤖 📝
Stars: ✭ 322 (-2.42%)
Mutual labels:  transformer
Instapy Gui
gui for instapy automation
Stars: ✭ 313 (-5.15%)
Mutual labels:  webpack

typescript-plugin-styled-components

This is a TypeScript transformer that improves development experience of styled-components.

The main purpose is to provide compile-time information of creates styled components, such as names of these components, for the run-time, allowing to operate with proper names of such the components.

The plugin was mostly inspired by great Babel's plugin babel-plugin-styled-components and partially provides similar functionality for TypeScript users.

If you like it, consider Buy me a coffee

Note: This transformer will be useful to you only when you are transpiling your TS code using actual TS compiler, like tsc ts-loader or awesome-typescript-loader. If your TS code is transpiled using babel-plugin-transform-typescript, you should use babel-plugin-styled-components instead.

Installation

The following command adds the packages to the project as a development-time dependency:

yarn add typescript-plugin-styled-components --dev

Documentation

Integration with Webpack

This section describes how to integrate the plugin into the build/bundling process driven by Webpack and its TypeScript loaders.

There are two popular TypeScript loaders that support specifying custom transformers:

Both loaders use the same setting getCustomTransformers which is an optional function that returns { before?: Transformer[], after?: Transformer[] }. In order to inject the transformer into compilation, add it to before transformers array, like: { before: [styledComponentsTransformer] }.

awesome-typescript-loader

In the webpack.config.js file in the section where awesome-typescript-loader is configured as a loader:

// 1. import default from the plugin module
const createStyledComponentsTransformer = require('typescript-plugin-styled-components').default;

// 2. create a transformer;
// the factory additionally accepts an options object which described below
const styledComponentsTransformer = createStyledComponentsTransformer();

// 3. add getCustomTransformer method to the loader config
var config = {
    ...
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                loader: 'awesome-typescript-loader',
                options: {
                    ... // other loader's options
                    getCustomTransformers: () => ({ before: [styledComponentsTransformer] })
                }
            }
        ]
    }
    ...
};

Please note, that in the development mode, awesome-typescript-loader uses multiple separate processes to speed up compilation. In that mode the above configuration cannot work because functions, which getCustomTransformers is, are not transferrable between processes. To solve this please refer to Forked process configuration section.

ts-loader

In the webpack.config.js file in the section where ts-loader is configured as a loader:

// 1. import default from the plugin module
const createStyledComponentsTransformer = require('typescript-plugin-styled-components').default;

// 2. create a transformer;
// the factory additionally accepts an options object which described below
const styledComponentsTransformer = createStyledComponentsTransformer();

// 3. add getCustomTransformer method to the loader config
var config = {
    ...
    module: {
        rules: [
            {
                test: /\.tsx?$/,
                loader: 'ts-loader',
                options: {
                    ... // other loader's options
                    getCustomTransformers: () => ({ before: [styledComponentsTransformer] })
                }
            }
        ]
    }
    ...
};

Please note, when awesome-typescript-loader is used with HappyPack or thread-loader, they use multiple separate processes to speed up compilation. In that mode the above configuration cannot work because functions, which getCustomTransformers is, are not transferrable between processes. To solve this please refer to Forked process configuration section.

Forked process configuration

To configure the transformer for development mode in awesome-typescript-loader or ts-loader with HappyPack or thread-loader, you need to make getCustomTransformers a resolvoble module name instead of the function. Since the configuration is very similar, here's an example:

1. Move styledComponentsTransformer into a separate file

Let's assume it is in the same folder as your webpack.config and has name webpack.ts-transformers.js:

// 1. import default from the plugin module
const createStyledComponentsTransformer = require('typescript-plugin-styled-components').default;

// 2. create a transformer;
// the factory additionally accepts an options object which described below
const styledComponentsTransformer = createStyledComponentsTransformer();

// 3. create getCustomTransformer function
const getCustomTransformers = () => ({ before: [styledComponentsTransformer] });

// 4. export getCustomTransformers
module.exports = getCustomTransformers;

2. Change the loader's options to point to that file instead of explicit function

-const createStyledComponentsTransformer = require('typescript-plugin-styled-components').default;
-const styledComponentsTransformer = createStyledComponentsTransformer();

options: {
    ... // other loader's options
-    getCustomTransformers: () => ({ before: [styledComponentsTransformer] })
+    getCustomTransformers: path.join(__dirname, './webpack.ts-transformers.js')
}

Integration with Rollup

This section describes how to integrate the plugin into the build/bundling process driven by Rollup and its TypeScript loader - rollup-plugin-typescript2.

In the rollup.config.js file in the section where rollup-plugin-typescript2 is configured as a loader:

// 1. import default from the plugin module
const createStyledComponentsTransformer = require('typescript-plugin-styled-components').default;

// 2. create a transformer;
// the factory additionally accepts an options object which described below
const styledComponentsTransformer = createStyledComponentsTransformer();

// 3. add getCustomTransformer method to the loader config
var config = {
    ...
    plugins: [
      rollupTypescript({
        ...
        transformers: [
          () => ({
            before: [styledComponentsTransformer],
          }),
        ],
      }),
    ...
    ],
    ...
};

TypeScript compiler (CLI)

TypeScript command line compiler tool (tcs) does not support using of pluggable modules and transformers. For that reason there are other tools created that do support pluggable transformers. See ttypescript compiler section.

ttypescript compiler

The ttypescript compiler is a CLI tool that allows to use TypeScript compiler with pluggable transformers. To use the transformer with that tool you can configure it by updating tsconfig.json the following way:

{
    "compilerOptions": {
        "plugins": [
            {
                "transform": "typescript-plugin-styled-components",
                "type": "config",

                // other typescript-plugin-styled-components options can be added here
                "minify": true,
                "ssr": true
            }
        ]
    }
}

API

createTransformer

function createTransformer(options?: Partial<Options>): TransformerFactory<SourceFile>;

A factory that creates an instance of a TypeScript transformer (which is a factory itself).

It allows to optionally pass options that allow to tweak transformer's behavior. See Options for details.

Options

interface Options {
    getDisplayName(filename: string, bindingName: string | undefined): string | undefined;
    identifiers: CustomStyledIdentifiers;
    ssr: boolean;
    displayName: boolean;
    minify: boolean;
}

getDisplayName

This method is used to determine component display name from filename and its binding name.

filename is the file name, relative to the project base directory, of the file where the styled component defined.

bindingName is the name that is used in the source code to bind the component. It can be null if the component was not bound or assigned.

Default strategy is to use bindingName if it's defined and use inference algorithm from filename otherwise.

Sample:

function getStyledComponentDisplay(filename, bindingName) {
    return bindingName || makePascalCase(filename);
}

ssr

By adding a unique identifier to every styled component, this plugin avoids checksum mismatches due to different class generation on the client and on the server.

This option allows to disable component id generation by setting it to false.

Default value is true which means that component id is being injected.

displayName

This option enhances the attached CSS class name on each component with richer output to help identify your components in the DOM without React DevTools.

It also adds allows you to see the component's displayName in React DevTools.

To disable displayName generation set this option to false

Default value is true which means that display name is being injected.

minify

The option allows to turn on minification of inline styles used in styled components. It is similar to babel-plugin-styled-components's same option. The minification is not exactly the same and may produce slightly different results.

⚠️ Warning: The minification is an experimental feature, please use with care.

Default value is false which means the minification is not being performed.

identifiers

This option allows to customize identifiers used by styled-components API functions.

Warning. By providing custom identifiers, predefined ones are not added automatically. Make sure you add standard APIs in case you meant to use them.

interface CustomStyledIdentifiers {
    styled: string[];
    attrs: string[];
    keyframes: string[];
    css: string[];
    createGlobalStyle: string[];
}
  • styled - list of identifiers of styled API (default ['styled'])
  • attrs - list of identifiers of attrs API (default ['attrs'])
  • keyframes - list of identifiers of keyframes API (default ['keyframes'])
  • css - list of identifiers of css API (default ['css'])
  • createGlobalStyle - list of identifiers of createGlobalStyle API (default ['createGlobalStyle'])

Example

const styledComponentsTransformer = createStyledComponentsTransformer({
    identifiers: {
        styled: ['styled', 'typedStyled'] // typedStyled is an additional identifier of styled API
    }
});

Notes

Technically, typescript-plugin-styled-components is not a TypeScript plugin, since it is only exposed as a TypeScript transformer.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].