Gathering detailed insights and metrics for typescript-plugin-styled-components
Gathering detailed insights and metrics for typescript-plugin-styled-components
Gathering detailed insights and metrics for typescript-plugin-styled-components
Gathering detailed insights and metrics for typescript-plugin-styled-components
TypeScript transformer for improving the debugging experience of styled-components
npm install typescript-plugin-styled-components
Typescript
Module System
Node Version
NPM Version
97.9
Supply Chain
100
Quality
75.9
Maintenance
100
Vulnerability
99.6
License
TypeScript (97.28%)
JavaScript (2.72%)
Total Downloads
19,142,490
Last Day
11,239
Last Week
48,343
Last Month
206,570
Last Year
3,408,650
419 Stars
293 Commits
33 Forks
3 Watching
28 Branches
15 Contributors
Latest Version
3.0.0
Package Id
typescript-plugin-styled-components@3.0.0
Unpacked Size
43.60 kB
Size
9.96 kB
File Count
18
NPM Version
6.14.12
Node Version
10.24.1
Publised On
02 Jun 2023
Cumulative downloads
Total Downloads
Last day
4.3%
11,239
Compared to previous day
Last week
-11.7%
48,343
Compared to previous week
Last month
6.2%
206,570
Compared to previous month
Last year
-18.2%
3,408,650
Compared to previous year
1
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.
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.
The following command adds the packages to the project as a development-time dependency:
yarn add typescript-plugin-styled-components --dev
Webpack
Rollup
ttypescript
compilerWebpack
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// 1. import default from the plugin module 2const createStyledComponentsTransformer = require('typescript-plugin-styled-components').default; 3 4// 2. create a transformer; 5// the factory additionally accepts an options object which described below 6const styledComponentsTransformer = createStyledComponentsTransformer(); 7 8// 3. add getCustomTransformer method to the loader config 9var config = { 10 ... 11 module: { 12 rules: [ 13 { 14 test: /\.tsx?$/, 15 loader: 'awesome-typescript-loader', 16 options: { 17 ... // other loader's options 18 getCustomTransformers: () => ({ before: [styledComponentsTransformer] }) 19 } 20 } 21 ] 22 } 23 ... 24};
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// 1. import default from the plugin module 2const createStyledComponentsTransformer = require('typescript-plugin-styled-components').default; 3 4// 2. create a transformer; 5// the factory additionally accepts an options object which described below 6const styledComponentsTransformer = createStyledComponentsTransformer(); 7 8// 3. add getCustomTransformer method to the loader config 9var config = { 10 ... 11 module: { 12 rules: [ 13 { 14 test: /\.tsx?$/, 15 loader: 'ts-loader', 16 options: { 17 ... // other loader's options 18 getCustomTransformers: () => ({ before: [styledComponentsTransformer] }) 19 } 20 } 21 ] 22 } 23 ... 24};
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.
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 resolvable module name instead of the function. Since the configuration is very similar, here's an example:
styledComponentsTransformer
into a separate fileLet's assume it is in the same folder as your webpack.config
and has name webpack.ts-transformers.js
:
1// 1. import default from the plugin module 2const createStyledComponentsTransformer = require('typescript-plugin-styled-components').default; 3 4// 2. create a transformer; 5// the factory additionally accepts an options object which described below 6const styledComponentsTransformer = createStyledComponentsTransformer(); 7 8// 3. create getCustomTransformer function 9const getCustomTransformers = () => ({ before: [styledComponentsTransformer] }); 10 11// 4. export getCustomTransformers 12module.exports = getCustomTransformers;
1-const createStyledComponentsTransformer = require('typescript-plugin-styled-components').default; 2-const styledComponentsTransformer = createStyledComponentsTransformer(); 3 4options: { 5 ... // other loader's options 6- getCustomTransformers: () => ({ before: [styledComponentsTransformer] }) 7+ getCustomTransformers: path.join(__dirname, './webpack.ts-transformers.js') 8}
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// 1. import default from the plugin module 2const createStyledComponentsTransformer = require('typescript-plugin-styled-components').default; 3 4// 2. create a transformer; 5// the factory additionally accepts an options object which described below 6const styledComponentsTransformer = createStyledComponentsTransformer(); 7 8// 3. add getCustomTransformer method to the loader config 9var config = { 10 ... 11 plugins: [ 12 rollupTypescript({ 13 ... 14 transformers: [ 15 () => ({ 16 before: [styledComponentsTransformer], 17 }), 18 ], 19 }), 20 ... 21 ], 22 ... 23};
TypeScript command line compiler tool (tsc
) 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
compilerThe 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:
1{ 2 "compilerOptions": { 3 "plugins": [ 4 { 5 "transform": "typescript-plugin-styled-components", 6 "type": "config", 7 8 // other typescript-plugin-styled-components options can be added here 9 "minify": true, 10 "ssr": true 11 } 12 ] 13 } 14}
createTransformer
1function 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
1interface Options { 2 getDisplayName(filename: string, bindingName: string | undefined): string | undefined; 3 identifiers: CustomStyledIdentifiers; 4 ssr: boolean; 5 displayName: boolean; 6 minify: boolean; 7 componentIdPrefix: string; 8}
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:
1function getStyledComponentDisplay(filename, bindingName) {
2 return bindingName || makePascalCase(filename);
3}
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: Warning: The minification is an experimental feature, please use with care.
Default value is false
which means the minification is not being performed.
componentIdPrefix
To avoid colisions when running more than one insance of typescript-plugin-styled-components at a time, you can add a componentIdPrefix by providing an arbitrary string to this option.
Default value is ''
which means that no namespacing will happen.
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.
1interface CustomStyledIdentifiers { 2 styled: string[]; 3 attrs: string[]; 4 keyframes: string[]; 5 css: string[]; 6 createGlobalStyle: string[]; 7 extend: string[]; 8}
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']
)extend
- list of identifiers of extend
API (default []
). Note this API has been deprecated in styled-components
so starting from 1.5
this option by default has empty set, which means it does not recognize this API by default.Example
1const styledComponentsTransformer = createStyledComponentsTransformer({
2 identifiers: {
3 styled: ['styled', 'typedStyled'] // typedStyled is an additional identifier of styled API
4 }
5});
Technically, typescript-plugin-styled-components
is not a TypeScript plugin, since it is only exposed as a TypeScript transformer.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
Found 3/18 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
security policy file not detected
Details
Reason
license file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
62 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-02-03
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn Moretypescript-styled-plugin
TypeScript language service plugin that adds IntelliSense for styled components
typescript-styled-components-plugin
A styled-components plugin to minify & set componentId & displayName for typescript
@styled/typescript-styled-plugin
TypeScript language service plugin that adds IntelliSense for styled components
ts-styled-components-plugin
TypeScript transformer plugin that adds minification support to styled-components