Skip to content

Commit 9de9720

Browse files
authored
feat: add styled-components plugin options and fix docs (#7890)
1 parent c4f1aeb commit 9de9720

File tree

22 files changed

+131
-246
lines changed

22 files changed

+131
-246
lines changed

.changeset/whole-numbers-visit.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@modern-js/plugin-styled-components': patch
3+
---
4+
5+
feat: add styled-components plugin options and fix docs
6+
feat: styled-components 插件增加配置参数并修复相关文档

packages/cli/plugin-styled-components/src/index.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import type { AppTools, CliPlugin } from '@modern-js/app-tools';
2-
import { pluginStyledComponents } from '@rsbuild/plugin-styled-components';
2+
import {
3+
type PluginStyledComponentsOptions,
4+
pluginStyledComponents,
5+
} from '@rsbuild/plugin-styled-components';
36

4-
export const styledComponentsPlugin = (): CliPlugin<AppTools> => ({
7+
export const styledComponentsPlugin = (
8+
options?: PluginStyledComponentsOptions,
9+
): CliPlugin<AppTools> => ({
510
name: '@modern-js/plugin-styled-components',
611

712
setup(api) {
813
api.config(() => {
914
return {
1015
builderPlugins: [
1116
pluginStyledComponents({
12-
displayName: true,
13-
minify: process.env.NODE_ENV === 'production',
17+
...options,
18+
// https://github.com/styled-components/babel-plugin-styled-components/issues/287
19+
topLevelImportPaths: ['@modern-js/plugin-styled-components/styled'],
1420
}),
1521
],
16-
tools: {
17-
// styledComponents: {
18-
// // https://github.com/styled-components/babel-plugin-styled-components/issues/287
19-
// topLevelImportPaths: ['@modern-js/plugin-styled-components/styled'],
20-
// },
21-
},
2222
resolve: {
2323
alias: {
2424
'styled-components': require.resolve('styled-components'),

packages/document/docs/en/apis/app/runtime/utility/css-in-js.mdx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
---
22
title: CSS-In-JS API
33
---
4+
45
# CSS-In-JS API
56

67
Use Style Component to write CSS.
78

89
## Usage
910

1011
```ts
11-
import styled from '@modern-js/runtime/styled';
12+
import styled from '@modern-js/plugin-styled-components/styled';
1213
```
1314

1415
## Function Signature
@@ -18,7 +19,7 @@ see [styled-component API](https://styled-components.com/docs/api).
1819
## Example
1920

2021
```tsx
21-
import styled from '@modern-js/runtime/styled';
22+
import styled from '@modern-js/plugin-styled-components/styled';
2223

2324
const Button = styled.button`
2425
background: palevioletred;

packages/document/docs/en/configure/app/tools/styled-components.mdx

Lines changed: 0 additions & 55 deletions
This file was deleted.

packages/document/docs/en/configure/app/tools/ts-loader.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: tsLoader
66

77
import { Badge } from '@theme';
88

9-
<Badge type="danger" >Webpack Only</Badge>
9+
<Badge type="danger">Webpack Only</Badge>
1010

1111
- **Type:** `Object | Function | undefined`
1212
- **Default:** `undefined`
@@ -15,7 +15,7 @@ import { Badge } from '@theme';
1515

1616
ts-loader is not recommended for use in the project, because:
1717

18-
- ts-loader cannot be used with certain features such as [source.transformImport](/configure/app/source/transform-import) and [tools.styledComponents](/configure/app/tools/styled-components) provided by Babel & SWC.
18+
- ts-loader cannot be used with certain features such as [source.transformImport](/configure/app/source/transform-import)provided by Babel & SWC.
1919
- Rspack does not support ts-loader.
2020

2121
:::

packages/document/docs/en/guides/advanced-features/low-level.mdx

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,12 @@ When the value is `Function`, the first parameter is the default configuration v
3030

3131
Currently provided is as follows:
3232

33-
| Tools | Config |
34-
| ----------------- | ---------------------------------------------------------------- |
35-
| DevServer | [tools.devServer](/configure/app/tools/dev-server) |
36-
| Babel | [tools.babel](/configure/app/tools/babel) |
37-
| styled-components | [tools.styledComponents](/configure/app/tools/styled-components) |
38-
| PostCSS | [tools.postcss](/configure/app/tools/postcss) |
39-
| Less | [tools.less](/configure/app/tools/less) |
40-
| Sass | [tools.sass](/configure/app/tools/sass) |
41-
| Minify CSS | [tools.minifyCss](/configure/app/tools/minify-css) |
42-
| Autoprefixer | [tools.autoprefixer](/configure/app/tools/autoprefixer) |
33+
| Tools | Config |
34+
| ------------ | ------------------------------------------------------- |
35+
| DevServer | [tools.devServer](/configure/app/tools/dev-server) |
36+
| Babel | [tools.babel](/configure/app/tools/babel) |
37+
| PostCSS | [tools.postcss](/configure/app/tools/postcss) |
38+
| Less | [tools.less](/configure/app/tools/less) |
39+
| Sass | [tools.sass](/configure/app/tools/sass) |
40+
| Minify CSS | [tools.minifyCss](/configure/app/tools/minify-css) |
41+
| Autoprefixer | [tools.autoprefixer](/configure/app/tools/autoprefixer) |
Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,34 @@
11
# Using CSS-in-JS
22

3-
CSS-in-JS is a technique that allows you to write CSS styles in JS files.
3+
CSS-in-JS is a technique that allows you to write CSS styles within JS files.
44

5-
Modern.js supports the popular CSS-in-JS implementation library [styled-components](https://styled-components.com/), which uses the new JavaScript feature [Tagged template](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates) to write component CSS styles. You can directly import the API of [styled-components](https://styled-components.com/) from `@modern-js/runtime/styled` for use.
5+
Modern.js supports the commonly used community CSS-in-JS library [styled-components](https://styled-components.com/), which uses JavaScript's new feature [Tagged template](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates) to write component CSS styles.
6+
7+
The Modern.js plugin `@modern-js/plugin-styled-components` provides support for styled-components and adds server-side rendering capability for styled-components. You can use styled-components by installing the `@modern-js/plugin-styled-components` plugin.
68

79
## Using styled-components in Modern.js
8-
First, you need to install the `styled-components` dependency:
910

10-
```bash
11-
# npm
12-
npm add @rsbuild/plugin-styled-components -D
11+
First, you need to install the `styled-components` plugin dependency:
1312

14-
# yarn
15-
yarn add @rsbuild/plugin-styled-components -D
13+
import { PackageManagerTabs } from '@theme';
1614

17-
# pnpm
18-
pnpm add @rsbuild/plugin-styled-components -D
19-
```
20-
Then, configure the `styled-components` plugin in `modern.config.ts`:
15+
<PackageManagerTabs
16+
command={{
17+
npm: 'npm install @modern-js/plugin-styled-components -D',
18+
yarn: 'yarn add @modern-js/plugin-styled-components -D',
19+
pnpm: 'pnpm install @modern-js/plugin-styled-components -D',
20+
}}
21+
/>
22+
23+
Then configure the `styled-components` plugin in `modern.config.ts`:
2124

2225
```ts
2326
import { defineConfig } from '@modern-js/app-tools';
24-
import { pluginStyledComponents } from '@rsbuild/plugin-styled-components';
27+
import { styledComponentsPlugin } from '@modern-js/plugin-styled-components';
2528

2629
export default defineConfig({
27-
builderPlugins: [
28-
pluginStyledComponents({
30+
plugins: [
31+
styledComponentsPlugin({
2932
// ...
3033
displayName: true,
3134
minify: process.env.NODE_ENV === 'production',
@@ -34,33 +37,33 @@ export default defineConfig({
3437
});
3538
```
3639

37-
## Writing Styles with styled-components
40+
The configuration options of the styledComponentsPlugin plugin are the same as those of the [@rsbuild/plugin-styled-components](https://www.npmjs.com/package/@rsbuild/plugin-styled-components) plugin. You can refer to the documentation of [@rsbuild/plugin-styled-components](https://www.npmjs.com/package/@rsbuild/plugin-styled-components) for configuration.
3841

39-
When you need to write a `div` component with an internal font color of red, you can implement it as follows:
42+
## Writing styles with styled-components
43+
44+
When you need to write a `div` component with red text inside, you can implement it as follows:
4045

4146
```js
42-
import styled from '@modern-js/runtime/styled';
47+
import styled from '@modern-js/plugin-styled-components/styled';
4348

4449
const RedDiv = styled.div`
4550
color: red;
4651
`;
4752
```
4853

49-
If you need to dynamically set component styles based on the component's `props`, for example, the `primary` property of `props` is `true`, the button color is white, otherwise it is red, you can implement the code as follows:
54+
When you need to dynamically set component styles based on the component's `props`, for example, when the `primary` attribute of `props` is `true`, the button's color is white, otherwise red, the implementation is as follows:
5055

5156
```js
52-
import styled from '@modern-js/runtime/styled';
57+
import styled from '@modern-js/plugin-styled-components/styled';
5358

5459
const Button = styled.button`
5560
color: ${props => (props.primary ? 'white' : 'red')};
5661
font-size: 1em;
5762
`;
5863
```
5964

60-
For more usage of styled-components, please refer to [styled-components official website](https://styled-components.com/).
61-
62-
Modern.js integrates Babel's [babel-plugin-styled-components](https://github.com/styled-components/babel-plugin-styled-components) plugin internally, and you can configure the plugin through [tools.styledComponents](/configure/app/tools/styled-components).
65+
For more usage of styled-components, please refer to the [styled-components official website](https://styled-components.com/).
6366

64-
:::tip
65-
If you need to use other CSS-in-JS libraries such as [styled-jsx](https://www.npmjs.com/package/styled-jsx) and [Emotion](https://emotion.sh/), you need to install the corresponding dependencies first. For specific usage, please refer to the library's official website.
67+
:::tip Tip
68+
If you want to use other CSS-in-JS libraries such as [styled-jsx](https://www.npmjs.com/package/styled-jsx), [Emotion](https://emotion.sh/), etc., you need to install the corresponding dependencies first. Please refer to the official websites of the respective libraries for specific usage.
6669
:::

packages/document/docs/en/guides/troubleshooting/builder.mdx

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -188,20 +188,6 @@ export default {
188188
};
189189
```
190190

191-
In addition to the reasons mentioned above, there is another possibility that can cause Babel compilation to hang, which is when Babel compiles a large JS file exceeding 10,000 lines (usually a large file in the node_modules directory that is compiled using `source.include`).
192-
193-
When Babel compiles large files, the built-in babel-plugin-styled-components in Modern.js can cause the compilation to hang. There is already a [relevant issue](https://github.com/styled-components/babel-plugin-styled-components/issues/374) in the community .
194-
195-
In the future, Modern.js will consider removing the built-in babel-plugin-styled-components. In the current version, you can set [tools.styledComponents](/configure/app/tools/styled-components.html) to `false` to remove this plugin.
196-
197-
```ts title="modern.config.ts"
198-
export default {
199-
tools: {
200-
styledComponents: false,
201-
},
202-
};
203-
```
204-
205191
---
206192

207193
### The webpack cache does not work?

0 commit comments

Comments
 (0)