Skip to content

Commit 8568fda

Browse files
Add options, update README
1 parent 0cca4aa commit 8568fda

File tree

2 files changed

+58
-7
lines changed

2 files changed

+58
-7
lines changed

README.md

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
Add the [`typescript-plugin-styled-components`](https://github.com/Igorbek/typescript-plugin-styled-components) to your `create-react-app` app via [`react-app-rewired`](https://github.com/timarney/react-app-rewired) and [`react-scripts-ts`](https://github.com/wmonk/create-react-app-typescript).
44

5+
## What does it do?
6+
7+
```JS
8+
const Header = styled.h1`...`;
9+
```
10+
11+
transpiles to
12+
13+
```JS
14+
const Header = styled.h1.withConfig({ displayName: 'Header' })`...`;
15+
```
16+
17+
This generates class names that are recognizable and easier to debug.
18+
519
## Installation
620

721
```sh
@@ -13,14 +27,49 @@ yarn add react-app-rewire-styled-components-typescript
1327

1428
## Usage
1529

16-
In the `config-overrides.js` you created for `react-app-rewired` add this code:
30+
In the `config-overrides.js` you created for `react-app-rewired` add the following:
31+
32+
> Note that `rewiredStyledComponents` is required before this plugin.
1733
1834
```JS
35+
// config-overides.js
36+
37+
const { compose } = require("react-app-rewired");
1938
const rewireStyledComponentsTypescriptPlugin = require('react-app-rewire-styled-components-typescript');
2039

21-
module.exports = function override(config, env) {
40+
// react-app-rewired v1.3.4+ (right-to-left)
41+
module.exports = compose(
42+
rewireStyledComponentsTypescriptPlugin,
43+
rewireStyledComponents
44+
);
45+
46+
// older versions
47+
module.exports = function(config, env) {
48+
config = rewireStyledComponents(config, env);
2249
config = rewireStyledComponentsTypescriptPlugin(config, env);
2350

2451
return config;
25-
}
52+
};
53+
```
54+
55+
## Options
56+
57+
You can pass options directly to [`typescript-plugin-styled-components`](https://github.com/Igorbek/typescript-plugin-styled-components) in the last argument
58+
59+
```JS
60+
// config-overrides.js
61+
62+
module.exports = function(config, env) {
63+
const pluginOptions = {
64+
// fancy custom display name
65+
getDisplayName(filename, bindingName) {
66+
return (bindingName || filename).toUpperCase();
67+
}
68+
};
69+
70+
config = rewireStyledComponents(config, env);
71+
config = rewireStyledComponentsTypescriptPlugin(config, env, pluginOptions);
72+
73+
return config;
74+
};
2675
```

src/index.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
const { getLoader, loaderNameMatches } = require('react-app-rewired');
2-
32
const { createTransformer } = require('typescript-plugin-styled-components');
4-
const styledComponentsTransformer = createTransformer();
53

64
const tsLoaderMatcher = rule => loaderNameMatches(rule, 'ts-loader');
75
const getTsLoader = rules => getLoader(rules, tsLoaderMatcher);
86

9-
const rewireStyledComponentsTypescriptPlugin = (config, _env) => {
7+
const rewireStyledComponentsTypescriptPlugin = (config, _env, options) => {
108
const tsLoader = getTsLoader(config.module.rules);
119

1210
if (!tsLoader) {
13-
console.log('ts-loader not found');
11+
console.error(
12+
'Skipping styled components typescript plugin: ts-loader not found.'
13+
);
1414

1515
return config;
1616
}
1717

18+
const styledComponentsTransformer = createTransformer(options);
19+
1820
tsLoader.options.getCustomTransformers = () => ({
1921
before: [styledComponentsTransformer],
2022
});

0 commit comments

Comments
 (0)