# `typescript-plugin-styled-components`
This is a TypeScript transformer that improves development experience of [`styled-components`](https://www.styled-components.com/).
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`](https://github.com/styled-components/babel-plugin-styled-components) and partially provides similar functionality for TypeScript users.
If you like it, consider [![Buy me a coffee](https://www.buymeacoffee.com/assets/img/custom_images/yellow_img.png)](https://www.buymeacoffee.com/igorbek)
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`](https://babeljs.io/docs/en/babel-plugin-transform-typescript), you should use [`babel-plugin-styled-components`](https://github.com/styled-components/babel-plugin-styled-components) instead.
# Installation
The following command adds the packages to the project as a development-time dependency:
<pre><code><strong>yarn</strong> add <em>typescript-plugin-styled-components</em> --dev</code></pre>
# Documentation
- [Integration with `Webpack`](#Integration-with-Webpack)
- [`awesome-typescript-loader`](#awesome-typescript-loader)
- [`ts-loader`](#ts-loader)
- [Forked process configuration](#Forked-process-configuration)
- [Integration with `Rollup`](#Integration-with-Rollup)
- [TypeScript compiler (CLI)](#TypeScript-compiler-CLI)
- [`ttypescript` compiler](#ttypescript-compiler)
- [API](#API)
- [`createTransformer`](#createTransformer)
- [`Options`](#Options)
- [Notes](#Notes)
# Integration with `Webpack`
This section describes how to integrate the plugin into the build/bundling process driven by [**Webpack**](https://webpack.js.org/) and its TypeScript loaders.
There are two popular TypeScript loaders that support specifying custom transformers:
- [**awesome-typescript-loader**](https://github.com/s-panferov/awesome-typescript-loader), supports custom transformers since v3.1.3
- [**ts-loader**](https://github.com/TypeStrong/ts-loader), supports custom transformers since v2.2.0
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:
```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. 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](#forked-process-configuration) section.
## `ts-loader`
In the `webpack.config.js` file in the section where **ts-loader** is configured as a loader:
```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. 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](#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 resolvable 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`:
```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
```diff
-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**](https://rollupjs.org/guide/en/) and its TypeScript loader - [**rollup-plugin-typescript2**](https://github.com/Igorbek/typescript-plugin-styled-components).
In the `rollup.config.js` file in the section where **rollup-plugin-typescript2** is configured as a loader:
```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. add getCustomTransformer method to the loader config
var config = {
...
plugins: [
rollupTypescript({
...
transformers: [
() => ({
before: [styledComponentsTransformer],
}),
],
}),
...
],
...
};
```
# TypeScript compiler (CLI)
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 plugga
没有合适的资源?快使用搜索试试~ 我知道了~
用于改善样式化组件调试体验的TypeScript转换器_TypeScript_JavaScript_下载.zip
共99个文件
baseline:41个
ts:29个
tsx:11个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 137 浏览量
2023-04-21
10:49:49
上传
评论
收藏 189KB ZIP 举报
温馨提示
用于改善样式化组件调试体验的TypeScript转换器_TypeScript_JavaScript_下载.zip
资源推荐
资源详情
资源评论
收起资源包目录
用于改善样式化组件调试体验的TypeScript转换器_TypeScript_JavaScript_下载.zip (99个子文件)
typescript-plugin-styled-components-master
yarn.lock 156KB
.github
dependabot.yml 988B
src
__tests__
ssr-baselines.test.ts 330B
componentIdPrefix-baselines.test.ts 387B
baselines.test.ts 243B
minify.test.ts 282B
baselines
minification
issue36.tsx.baseline 2KB
issue44.tsx.baseline 762B
minify-css-to-use-without-transpilation.ts.baseline 3KB
simple2.ts.baseline 2KB
issue40.ts.baseline 497B
minify-css-in-helpers.ts.baseline 2KB
issue142.tsx.baseline 1KB
minify-css-to-use-with-transpilation.ts.baseline 2KB
issue36-extended.ts.baseline 1KB
issue233.ts.baseline 2KB
simple.ts.baseline 8KB
minify-single-line-comments-with-interpolations.ts.baseline 4KB
ssr
sample1.ts.baseline 3KB
multiple-components.tsx.baseline 2KB
style-objects.ts.baseline 3KB
issue33.ts.baseline 1KB
sample3.tsx.baseline 2KB
sample2.ts.baseline 1KB
minification-only
issue36.tsx.baseline 1KB
issue44.tsx.baseline 762B
minify-css-to-use-without-transpilation.ts.baseline 3KB
simple2.ts.baseline 2KB
issue40.ts.baseline 497B
minify-css-in-helpers.ts.baseline 2KB
issue142.tsx.baseline 1KB
minify-css-to-use-with-transpilation.ts.baseline 2KB
issue36-extended.ts.baseline 1KB
issue233.ts.baseline 2KB
simple.ts.baseline 8KB
minify-single-line-comments-with-interpolations.ts.baseline 4KB
componentIdPrefix
sample1.ts.baseline 3KB
multiple-components.tsx.baseline 2KB
style-objects.ts.baseline 3KB
issue33.ts.baseline 1KB
sample3.tsx.baseline 2KB
sample2.ts.baseline 1KB
base
sample1.ts.baseline 3KB
style-objects.ts.baseline 3KB
issue33.ts.baseline 1KB
sample3.tsx.baseline 2KB
sample2.ts.baseline 1KB
minification.test.ts 273B
fixtures
minification
issue40.ts 69B
minify-css-to-use-without-transpilation.ts 588B
issue36.tsx 327B
simple2.ts 340B
simple.ts 2KB
issue44.tsx 133B
issue233.ts 480B
issue142.tsx 208B
issue36-extended.ts 284B
minify-css-in-helpers.ts 420B
minify-css-to-use-with-transpilation.ts 383B
minify-single-line-comments-with-interpolations.ts 835B
ssr
multiple-components.tsx 326B
componentIdPrefix
multiple-components.tsx 326B
base
style-objects.ts 489B
sample1.ts 474B
sample3.tsx 326B
sample2.ts 187B
issue33.ts 298B
expectTransform.ts 2KB
minification-only.test.ts 298B
themed-styled.ts 1018B
hash.ts 2KB
runner.ts 861B
models
Options.ts 3KB
ts-is-kind.ts 2KB
createTransformer.ts 11KB
index.ts 118B
minify.ts 9KB
jest.config.json 365B
CHANGELOG.md 3KB
package.json 1KB
.gitignore 28B
tsconfig.json 5KB
example
yarn.lock 148KB
tsconfig.server.json 247B
.gitattributes 65B
src
app
App.tsx 497B
Component1.tsx 369B
AppClient.tsx 380B
server
index.tsx 1KB
AppServer.tsx 658B
webpack
transformer.js 218B
config.ts 729B
config.server.js 998B
transformer.js 25B
package.json 1KB
.gitignore 186B
tsconfig.json 398B
README.md 371B
README.md 13KB
共 99 条
- 1
资源评论
快撑死的鱼
- 粉丝: 1w+
- 资源: 9156
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功