Thanks to visit codestin.com
Credit goes to github.com

Skip to content
This repository was archived by the owner on Jun 20, 2023. It is now read-only.

Commit 785cd8f

Browse files
eds-snippetsnchanged
authored andcommitted
feat: Add StyledComponentsPlugin #1366 (#1449)
* feat: Add StyledComponentsPlugin #1366 * Move styled-components and typescript-plugin-styled-components dependencies to devDependencies * Add documentation for StyledComponentsPlugin * Remove unnecessary code from StyledComponentsPlugin * feat: Move typescript-plugins-styled-components to init * feat: Add additional check for require.resolve function in StyledComponentsPlugin.ts
1 parent 344e320 commit 785cd8f

File tree

6 files changed

+181
-1
lines changed

6 files changed

+181
-1
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
"description": "Fuse-Box a bundler that does it right",
6666
"devDependencies": {
6767
"@types/node": "^6.0.116",
68+
"@types/styled-components": "^4.1.3",
6869
"babel-core": "^6.26.3",
6970
"babel-generator": "^6.26.1",
7071
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.2",
@@ -109,9 +110,11 @@
109110
"reflect-metadata": "^0.1.8",
110111
"run-sequence": "^1.2.2",
111112
"should": "^11.1.0",
113+
"styled-components": "^4.1.2",
112114
"stylus": "^0.54.5",
113115
"terser": "^3.10.11",
114116
"typescript": "^3.1.0",
117+
"typescript-plugin-styled-components": "^1.0.0",
115118
"uglify-es": "^3.3.9",
116119
"uglify-js": "^3.4.7",
117120
"validate-commit-msg": "^2.14.0",
@@ -150,4 +153,4 @@
150153
},
151154
"typings": "index.d.ts",
152155
"version": "3.7.0-next.9"
153-
}
156+
}

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ export { CopyPlugin } from "./plugins/CopyPlugin";
4747
export { WebIndexPlugin } from "./plugins/WebIndexPlugin";
4848
export { PlainJSPlugin } from "./plugins/PlainJSPlugin";
4949
export { ConsolidatePlugin } from "./plugins/ConsolidatePlugin";
50+
export { StyledComponentsPlugin } from "./plugins/StyledComponentsPlugin";
5051
export { File } from "./core/File";
5152
import * as _SparkyCollection from "./sparky/index";
5253
export const SparkyCollection = _SparkyCollection;
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { WorkFlowContext } from "./../core/WorkflowContext";
2+
import { Plugin } from "../core/WorkflowContext";
3+
4+
/**
5+
* @export
6+
* @class StyledComponentsPluginClass
7+
* @implements {Plugin}
8+
*/
9+
export class StyledComponentsPluginClass implements Plugin {
10+
public options;
11+
constructor(options = {}) {
12+
this.options = options;
13+
}
14+
15+
public init(context: WorkFlowContext) {
16+
if (typeof require.resolve === 'function') {
17+
try {
18+
require.resolve('typescript-plugin-styled-components');
19+
} catch(e) {
20+
throw new Error(`Cannot find module 'typescript-plugin-styled-components', install it to devDependency\nnpm install --save-dev typescript-plugin-styled-components`)
21+
}
22+
}
23+
const createStyledComponentsTransformer = require('typescript-plugin-styled-components').default;
24+
context.allowExtension(".tsx");
25+
const styledComponentsTransformer = createStyledComponentsTransformer(this.options);
26+
context.fuse.opts.transformers = {
27+
before: [styledComponentsTransformer]
28+
};
29+
}
30+
}
31+
32+
export const StyledComponentsPlugin = (options) => {
33+
return new StyledComponentsPluginClass(options);
34+
};
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { should } from "fuse-test-runner";
2+
import { createEnv } from "../stubs/TestEnvironment";
3+
import { StyledComponentsPlugin } from "../../plugins/StyledComponentsPlugin";
4+
5+
export class StyledComponentsPluginTest {
6+
"Should return styled component"() {
7+
return createEnv({
8+
project: {
9+
files: {
10+
"index.tsx": `
11+
import * as React from 'react';
12+
import * as ReactDOM from 'react-dom';
13+
import styled from 'styled-components'
14+
const MyTestTitle = styled.h1\`
15+
color: red;
16+
\`;
17+
ReactDOM.render(
18+
<MyHeader>Test</MyHeader>, document.getElementById('root'));`,
19+
},
20+
plugins: [StyledComponentsPlugin()],
21+
instructions: ">index.tsx",
22+
},
23+
}).then(result => {
24+
result.project.FuseBox.import("./index");
25+
const contents = result.projectContents.toString();
26+
should(contents).findString("React.createElement(MyHeader, null, \"Test\")");
27+
});
28+
}
29+
30+
"Should return styled component with option getDisplayName"() {
31+
return createEnv({
32+
project: {
33+
files: {
34+
"index.tsx": `
35+
import * as React from 'react';
36+
import * as ReactDOM from 'react-dom';
37+
import styled from 'styled-components'
38+
const MyTestTitle = styled.h1\`
39+
color: red;
40+
\`;
41+
ReactDOM.render(
42+
<MyHeader>Test</MyHeader>, document.getElementById('root'));`,
43+
},
44+
plugins: [StyledComponentsPlugin({
45+
getDisplayName: function(filename, bindingName) {
46+
return 'Testing_' + bindingName + '_value';
47+
}
48+
})],
49+
instructions: ">index.tsx",
50+
},
51+
}).then(result => {
52+
result.project.FuseBox.import("./index");
53+
const contents = result.projectContents.toString();
54+
should(contents).findString("Testing_MyTestTitle_value");
55+
});
56+
}
57+
58+
}

website/sidebars.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"plugins/replace-plugin",
5959
"plugins/sass-plugin",
6060
"plugins/source-map-plain-js-plugin",
61+
"plugins/styled-components-plugin",
6162
"plugins/stylus-plugin",
6263
"plugins/svg-plugin",
6364
"plugins/uglifyes-plugin",
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
---
2+
id: version-3.6.0-styled-components-plugin
3+
title: CSSModules
4+
original_id: styled-components-plugin
5+
---
6+
7+
## Description
8+
9+
StyledComponentsPlugin improves development experience of `styled-components` via transformer/plugin `typescript-plugin-styled-components`. Which 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.
10+
11+
## Install
12+
13+
Using the StyledComponentsPlugin requires `styled-components` and `typescript-plugin-styled-components` to transform styled components to reactive components.
14+
15+
```bash
16+
yarn add styled-components --dev
17+
yarn add typescript-plugin-styled-components --dev
18+
19+
// OR
20+
21+
npm install styled-components --save-dev
22+
npm install typescript-plugin-styled-components --save-dev
23+
```
24+
25+
## Usage
26+
27+
After that you can use the `StyledComponentsPlugin` in your configuration.
28+
29+
```js
30+
Fusebox.init({
31+
homeDir: "src",
32+
output: "dist/$name.js",
33+
plugins: [StyledComponentsPlugin()],
34+
})
35+
.bundle("app")
36+
.instructions(">index.js");
37+
```
38+
39+
This will create styled components with `bundleName` variable. For example if you have `header.tsx` file:
40+
41+
```tsx
42+
const MyHeader = styled.header`
43+
background: red;
44+
`
45+
render {
46+
<MyHeader>
47+
This is my styled header
48+
</MyHeader>
49+
}
50+
```
51+
52+
you will get rendered header like this:
53+
54+
```html
55+
<header class="MyHeader-hkmgDS fuUxah">This is my styled header</header>
56+
```
57+
58+
## Options
59+
60+
### getDisplayName
61+
62+
By default, `StyledComponentsPlugin` uses `bundleName` variable, but you can change it by using `getDisplayName` function:
63+
64+
```js
65+
StyledComponentsPlugin({
66+
getDisplayName: (filename, bindingName) => {
67+
// typescript-plugin-styled-components gives path to a filename variable
68+
// we change a path to a real filename in PascalCase
69+
filename = filename.replace(/^.*[\\\/]/, '').match(/[a-z]+/gi).map(function (word) {
70+
return word.charAt(0).toUpperCase() + word.substr(1).toLowerCase()
71+
}).join('').replace('Tsx', '').replace('Ts', '');
72+
return filename + '_' + bindingName
73+
}
74+
});
75+
```
76+
77+
You can do whatever you like with filename or bindingName variables or return other string if you like. The result of previous `header.tsx` file would look like this:
78+
79+
```html
80+
<header class="Header_MyHeader-hkmgDS fuUxah">This is my styled header</header>
81+
```
82+
83+
`StyledComponentsPlugin` just passes options straight to the `typescript-plugin-styled-components` transformer as object and without any additional check. It will allow to use modified transformer in the future without changing `StyledComponentsPlugin`. For more how to use the `typescript-plugin-styled-components` transformer read [here](https://github.com/Igorbek/typescript-plugin-styled-components#options)

0 commit comments

Comments
 (0)