@modern-js/main-doc 0.0.0-nightly-20240903170747 → 0.0.0-nightly-20240904170727
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/en/components/init-app.mdx +0 -1
- package/docs/en/components/init-rspack-app.mdx +0 -1
- package/docs/en/configure/app/tools/tailwindcss.mdx +1 -1
- package/docs/en/guides/advanced-features/_meta.json +0 -1
- package/docs/en/guides/advanced-features/rsbuild-plugin.mdx +2 -2
- package/docs/en/guides/advanced-features/rspack-start.mdx +4 -10
- package/docs/en/guides/basic-features/_meta.json +25 -9
- package/docs/en/guides/basic-features/css/_meta.json +1 -0
- package/docs/en/guides/basic-features/css/css-in-js.mdx +34 -0
- package/docs/en/guides/basic-features/{css-modules.mdx → css/css-modules.mdx} +0 -4
- package/docs/en/guides/basic-features/css/css.mdx +25 -0
- package/docs/en/guides/basic-features/{css.mdx → css/tailwindcss.mdx} +5 -66
- package/docs/en/guides/basic-features/data/data-fetch.mdx +1 -1
- package/docs/en/guides/basic-features/data/data-write.mdx +1 -1
- package/docs/en/guides/basic-features/debug/_meta.json +1 -0
- package/docs/en/guides/basic-features/debug/rsdoctor.mdx +57 -0
- package/docs/en/guides/{advanced-features → basic-features/debug}/using-storybook.mdx +2 -0
- package/docs/en/guides/basic-features/static-assets/_meta.json +1 -0
- package/docs/en/guides/basic-features/static-assets.mdx +1 -1
- package/docs/en/guides/basic-features/testing/_meta.json +1 -0
- package/docs/en/guides/basic-features/testing/cypress.mdx +95 -0
- package/docs/en/guides/basic-features/testing/jest.mdx +148 -0
- package/docs/en/guides/basic-features/testing/playwright.mdx +111 -0
- package/docs/en/guides/basic-features/testing/vitest.mdx +100 -0
- package/docs/en/guides/get-started/quick-start.mdx +1 -1
- package/docs/en/guides/get-started/tech-stack.mdx +4 -4
- package/docs/en/guides/topic-detail/generator/create/config.mdx +0 -10
- package/docs/en/guides/topic-detail/generator/create/use.mdx +0 -1
- package/docs/en/tutorials/first-app/c03-css.mdx +1 -1
- package/docs/zh/components/init-app.mdx +0 -1
- package/docs/zh/components/init-rspack-app.mdx +0 -1
- package/docs/zh/configure/app/tools/tailwindcss.mdx +1 -1
- package/docs/zh/guides/advanced-features/_meta.json +0 -1
- package/docs/zh/guides/advanced-features/rsbuild-plugin.mdx +2 -2
- package/docs/zh/guides/advanced-features/rspack-start.mdx +4 -10
- package/docs/zh/guides/basic-features/_meta.json +25 -9
- package/docs/zh/guides/basic-features/css/_meta.json +1 -0
- package/docs/zh/guides/basic-features/css/css-in-js.mdx +34 -0
- package/docs/zh/guides/basic-features/css/css.mdx +25 -0
- package/docs/zh/guides/basic-features/{css.mdx → css/tailwindcss.mdx} +3 -64
- package/docs/zh/guides/basic-features/debug/_meta.json +1 -0
- package/docs/zh/guides/basic-features/debug/rsdoctor.mdx +57 -0
- package/docs/zh/guides/{advanced-features → basic-features/debug}/using-storybook.mdx +1 -1
- package/docs/zh/guides/basic-features/static-assets/_meta.json +1 -0
- package/docs/zh/guides/basic-features/static-assets.mdx +2 -6
- package/docs/zh/guides/basic-features/testing/_meta.json +1 -0
- package/docs/zh/guides/basic-features/testing/cypress.mdx +95 -0
- package/docs/zh/guides/basic-features/testing/jest.mdx +148 -0
- package/docs/zh/guides/basic-features/testing/playwright.mdx +112 -0
- package/docs/zh/guides/basic-features/testing/vitest.mdx +100 -0
- package/docs/zh/guides/get-started/quick-start.mdx +1 -1
- package/docs/zh/guides/get-started/tech-stack.mdx +8 -8
- package/docs/zh/guides/topic-detail/generator/create/config.mdx +0 -10
- package/docs/zh/guides/topic-detail/generator/create/use.mdx +0 -1
- package/docs/zh/tutorials/first-app/c03-css.mdx +1 -1
- package/i18n.json +16 -4
- package/package.json +4 -4
- /package/docs/en/guides/basic-features/{mock.mdx → debug/mock.mdx} +0 -0
- /package/docs/en/guides/basic-features/{proxy.mdx → debug/proxy.mdx} +0 -0
- /package/docs/en/guides/basic-features/{json-files.mdx → static-assets/json-files.mdx} +0 -0
- /package/docs/en/guides/basic-features/{svg-assets.mdx → static-assets/svg-assets.mdx} +0 -0
- /package/docs/en/guides/basic-features/{wasm-assets.mdx → static-assets/wasm-assets.mdx} +0 -0
- /package/docs/zh/guides/basic-features/{css-modules.mdx → css/css-modules.mdx} +0 -0
- /package/docs/zh/guides/basic-features/{mock.mdx → debug/mock.mdx} +0 -0
- /package/docs/zh/guides/basic-features/{proxy.mdx → debug/proxy.mdx} +0 -0
- /package/docs/zh/guides/basic-features/{json-files.mdx → static-assets/json-files.mdx} +0 -0
- /package/docs/zh/guides/basic-features/{svg-assets.mdx → static-assets/svg-assets.mdx} +0 -0
- /package/docs/zh/guides/basic-features/{wasm-assets.mdx → static-assets/wasm-assets.mdx} +0 -0
@@ -4,7 +4,6 @@
|
|
4
4
|
? Please select the type of project you want to create: Web App
|
5
5
|
? Please select the programming language: TS
|
6
6
|
? Please select the package manager: pnpm
|
7
|
-
? Please select the bundler: webpack
|
8
7
|
```
|
9
8
|
|
10
9
|
After create the project, Modern.js will automatically install dependencies and create a git repository.
|
@@ -23,7 +23,7 @@ Used to modify the configuration of [Tailwind CSS](https://tailwindcss.com/docs/
|
|
23
23
|
|
24
24
|
Before using `tools.tailwindcss`, you need to enable the Tailwind CSS plugin for Modern.js.
|
25
25
|
|
26
|
-
Please refer to the section [Using Tailwind CSS](/guides/basic-features/css
|
26
|
+
Please refer to the section [Using Tailwind CSS](/guides/basic-features/css/tailwindcss) for instructions on how to enable it.
|
27
27
|
|
28
28
|
### Function Type
|
29
29
|
|
@@ -41,8 +41,8 @@ Here are the official Rsbuild plugins built into Modern.js:
|
|
41
41
|
| [CSS Minimizer Plugin](https://github.com/rspack-contrib/rsbuild-plugin-css-minimizer) | Used to customize CSS minimizer, switch to [cssnano](https://cssnano.co/) or other tools for CSS compression | [tools.minifyCss](/configure/app/tools/minify-css.html) |
|
42
42
|
| [Pug Plugin](https://github.com/rspack-contrib/rsbuild-plugin-pug) | Provides support for the Pug template engine | [tools.pug](/configure/app/tools/pug.html) |
|
43
43
|
| [Rem Plugin](https://github.com/rspack-contrib/rsbuild-plugin-rem) | Implements the rem adaptive layout for mobile pages | [output.convertToRem](/configure/app/output/convert-to-rem.html) |
|
44
|
-
| [YAML Plugin](https://github.com/rspack-contrib/rsbuild-plugin-yaml) | Used to import YAML files and convert them into JavaScript objects | [TOML File](/guides/basic-features/json-files.html#toml-file) |
|
45
|
-
| [TOML Plugin](https://github.com/rspack-contrib/rsbuild-plugin-toml) | Used to import TOML files and convert them into JavaScript objects | [YAML File](/guides/basic-features/json-files.html#yaml-file) |
|
44
|
+
| [YAML Plugin](https://github.com/rspack-contrib/rsbuild-plugin-yaml) | Used to import YAML files and convert them into JavaScript objects | [TOML File](/guides/basic-features/static-assets/json-files.html#toml-file) |
|
45
|
+
| [TOML Plugin](https://github.com/rspack-contrib/rsbuild-plugin-toml) | Used to import TOML files and convert them into JavaScript objects | [YAML File](/guides/basic-features/static-assets/json-files.html#yaml-file) |
|
46
46
|
|
47
47
|
### Un-builtin Plugins
|
48
48
|
|
@@ -14,7 +14,7 @@ This document will show you how to enable Rspack build mode in Modern.js.
|
|
14
14
|
|
15
15
|
## Initializing an Rspack project
|
16
16
|
|
17
|
-
The Modern.js
|
17
|
+
The Modern.js new project has enabled Rspack by default. Just execute [Initialize Project](/guides/get-started/quick-start.html#initialize) to create an Rspack project:
|
18
18
|
|
19
19
|
import InitRspackApp from '@site-docs-en/components/init-rspack-app';
|
20
20
|
|
@@ -24,7 +24,7 @@ After the project is created, you can experience the project by running `pnpm ru
|
|
24
24
|
|
25
25
|
## Enable Rspack build
|
26
26
|
|
27
|
-
Since Modern.js MAJOR_VERSION.
|
27
|
+
Since Modern.js MAJOR_VERSION.59.0, you can enable Rspack build by add the following configuration in `modern.config.ts`:
|
28
28
|
|
29
29
|
```diff title=modern.config.ts
|
30
30
|
import { appTools, defineConfig } from '@modern-js/app-tools';
|
@@ -32,14 +32,14 @@ import { appTools, defineConfig } from '@modern-js/app-tools';
|
|
32
32
|
export default defineConfig({
|
33
33
|
plugins: [
|
34
34
|
appTools({
|
35
|
-
+ bundler: '
|
35
|
+
+ bundler: 'rspack',
|
36
36
|
}),
|
37
37
|
],
|
38
38
|
});
|
39
39
|
```
|
40
40
|
|
41
41
|
:::tip
|
42
|
-
If your current version is lower than MAJOR_VERSION.
|
42
|
+
If your current version is lower than MAJOR_VERSION.59.0, you can upgrade by executing `npx modern upgrade`.
|
43
43
|
:::
|
44
44
|
|
45
45
|
import RspackPrecautions from '@modern-js/builder-doc/docs/en/shared/rspackPrecautions.md';
|
@@ -70,12 +70,6 @@ export default {
|
|
70
70
|
};
|
71
71
|
```
|
72
72
|
|
73
|
-
:::tip
|
74
|
-
After using Rspack for build, there are currently a few configs that are not supported in Rspack, such as [source.moduleScopes](/configure/app/source/module-scopes).
|
75
|
-
|
76
|
-
For unsupported configurations, we have marked `Bundler: only support webpack` or `Bundler: only support Rspack` in the document. Please refer to the specific configuration introduction.
|
77
|
-
:::
|
78
|
-
|
79
73
|
## Modify transpile configuration
|
80
74
|
|
81
75
|
Modern.js uses Rspack [builtin:swc-loader](https://rspack.dev/guide/features/builtin-swc-loader) for code translation in Rspack mode.
|
@@ -12,17 +12,33 @@
|
|
12
12
|
"label": "rendering",
|
13
13
|
"collapsed": true
|
14
14
|
},
|
15
|
-
|
15
|
+
{
|
16
|
+
"type": "dir",
|
17
|
+
"name": "css",
|
18
|
+
"label": "css-solution",
|
19
|
+
"collapsed": true
|
20
|
+
},
|
21
|
+
"html",
|
22
|
+
{
|
23
|
+
"type": "dir",
|
24
|
+
"name": "static-assets",
|
25
|
+
"label": "static-assets",
|
26
|
+
"collapsed": true
|
27
|
+
},
|
28
|
+
{
|
29
|
+
"type": "dir",
|
30
|
+
"name": "debug",
|
31
|
+
"label": "debug",
|
32
|
+
"collapsed": true
|
33
|
+
},
|
34
|
+
{
|
35
|
+
"type": "dir",
|
36
|
+
"name": "testing",
|
37
|
+
"label": "testing",
|
38
|
+
"collapsed": true
|
39
|
+
},
|
16
40
|
"alias",
|
17
|
-
"mock",
|
18
|
-
"proxy",
|
19
41
|
"env-vars",
|
20
|
-
"html",
|
21
42
|
"output-files",
|
22
|
-
"static-assets",
|
23
|
-
"svg-assets",
|
24
|
-
"json-files",
|
25
|
-
"wasm-assets",
|
26
|
-
"css-modules",
|
27
43
|
"deploy"
|
28
44
|
]
|
@@ -0,0 +1 @@
|
|
1
|
+
["css", "css-modules", "css-in-js", "tailwindcss"]
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# Using CSS-in-JS
|
2
|
+
|
3
|
+
CSS-in-JS is a technique that allows you to write CSS styles in JS files.
|
4
|
+
|
5
|
+
Modern.js integrates 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.
|
6
|
+
|
7
|
+
When you need to write a `div` component with an internal font color of red, you can implement it as follows:
|
8
|
+
|
9
|
+
```js
|
10
|
+
import styled from '@modern-js/runtime/styled';
|
11
|
+
|
12
|
+
const RedDiv = styled.div`
|
13
|
+
color: red;
|
14
|
+
`;
|
15
|
+
```
|
16
|
+
|
17
|
+
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:
|
18
|
+
|
19
|
+
```js
|
20
|
+
import styled from '@modern-js/runtime/styled';
|
21
|
+
|
22
|
+
const Button = styled.button`
|
23
|
+
color: ${props => (props.primary ? 'white' : 'red')};
|
24
|
+
font-size: 1em;
|
25
|
+
`;
|
26
|
+
```
|
27
|
+
|
28
|
+
For more usage of styled-components, please refer to [styled-components official website](https://styled-components.com/).
|
29
|
+
|
30
|
+
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).
|
31
|
+
|
32
|
+
:::tip
|
33
|
+
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.
|
34
|
+
:::
|
@@ -1,7 +1,3 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 14
|
3
|
-
---
|
4
|
-
|
5
1
|
# Use CSS Modules
|
6
2
|
|
7
3
|
[CSS Modules](https://github.com/css-modules/css-modules) allows us to write CSS code in a modular way, and these styles can be imported and used in JavaScript files. Using CSS Modules can automatically generate unique class names, isolate styles between different modules, and avoid class name conflicts.
|
@@ -0,0 +1,25 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 1
|
3
|
+
---
|
4
|
+
|
5
|
+
# Styling
|
6
|
+
|
7
|
+
Modern.js has built-in a variety of commonly used CSS solutions, including Less / Sass / Stylus preprocessors, PostCSS, CSS Modules, CSS-in-JS, and Tailwind CSS.
|
8
|
+
|
9
|
+
## Using Less, Sass and Stylus
|
10
|
+
|
11
|
+
Modern.js has built-in popular community CSS preprocessors, including Less and Sass.
|
12
|
+
|
13
|
+
By default, you don't need to configure Less and Sass. If you have custom loader configuration requirements, you can set them up by configuring [tools.less](/configure/app/tools/less) and [tools.sass](/configure/app/tools/sass).
|
14
|
+
|
15
|
+
You can also use Stylus in Modern.js by installing the Stylus plugin provided by Rsbuild. For usage, please refer to [Stylus Plugin](https://rsbuild.dev/plugins/list/plugin-stylus).
|
16
|
+
|
17
|
+
## Using PostCSS
|
18
|
+
|
19
|
+
Modern.js has built-in [PostCSS](https://postcss.org/) to transform CSS code.
|
20
|
+
|
21
|
+
Please refer to [Rsbuild - Using PostCSS](https://rsbuild.dev/guide/basic/css-usage#using-postcss) for detailed usage.
|
22
|
+
|
23
|
+
## Using Uno CSS
|
24
|
+
|
25
|
+
Please read the [Rsbuild - Using UnoCSS](https://rsbuild.dev/zh/guide/basic/unocss) for detailed usage.
|
@@ -1,65 +1,4 @@
|
|
1
|
-
|
2
|
-
sidebar_position: 1
|
3
|
-
---
|
4
|
-
|
5
|
-
# Styling
|
6
|
-
|
7
|
-
Modern.js has built-in a variety of commonly used CSS solutions, including Less / Sass / Stylus preprocessors, PostCSS, CSS Modules, CSS-in-JS, and Tailwind CSS.
|
8
|
-
|
9
|
-
## Using Less, Sass and Stylus
|
10
|
-
|
11
|
-
Modern.js has built-in popular community CSS preprocessors, including Less and Sass.
|
12
|
-
|
13
|
-
By default, you don't need to configure Less and Sass. If you have custom loader configuration requirements, you can set them up by configuring [tools.less](/configure/app/tools/less) and [tools.sass](/configure/app/tools/sass).
|
14
|
-
|
15
|
-
You can also use Stylus in Modern.js by installing the Stylus plugin provided by Rsbuild. For usage, please refer to [Stylus Plugin](https://rsbuild.dev/plugins/list/plugin-stylus).
|
16
|
-
|
17
|
-
## Using PostCSS
|
18
|
-
|
19
|
-
Modern.js has built-in [PostCSS](https://postcss.org/) to transform CSS code.
|
20
|
-
|
21
|
-
Please refer to [Rsbuild - Using PostCSS](https://rsbuild.dev/guide/basic/css-usage#using-postcss) for detailed usage.
|
22
|
-
|
23
|
-
## Using CSS Modules
|
24
|
-
|
25
|
-
Please read the [Using CSS Modules](/guides/basic-features/css-modules) section to learn about the complete usage of CSS Modules.
|
26
|
-
|
27
|
-
## Using CSS-in-JS
|
28
|
-
|
29
|
-
CSS-in-JS is a technique that allows you to write CSS styles in JS files.
|
30
|
-
|
31
|
-
Modern.js integrates 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.
|
32
|
-
|
33
|
-
When you need to write a `div` component with an internal font color of red, you can implement it as follows:
|
34
|
-
|
35
|
-
```js
|
36
|
-
import styled from '@modern-js/runtime/styled';
|
37
|
-
|
38
|
-
const RedDiv = styled.div`
|
39
|
-
color: red;
|
40
|
-
`;
|
41
|
-
```
|
42
|
-
|
43
|
-
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:
|
44
|
-
|
45
|
-
```js
|
46
|
-
import styled from '@modern-js/runtime/styled';
|
47
|
-
|
48
|
-
const Button = styled.button`
|
49
|
-
color: ${props => (props.primary ? 'white' : 'red')};
|
50
|
-
font-size: 1em;
|
51
|
-
`;
|
52
|
-
```
|
53
|
-
|
54
|
-
For more usage of styled-components, please refer to [styled-components official website](https://styled-components.com/).
|
55
|
-
|
56
|
-
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).
|
57
|
-
|
58
|
-
:::tip
|
59
|
-
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.
|
60
|
-
:::
|
61
|
-
|
62
|
-
## Using Tailwind CSS
|
1
|
+
# Using Tailwind CSS
|
63
2
|
|
64
3
|
[Tailwind CSS](https://tailwindcss.com/) is a CSS framework and design system based on Utility Class, which can quickly add common styles to components, and support flexible extension of theme styles.
|
65
4
|
|
@@ -112,7 +51,7 @@ const Hello = () => (
|
|
112
51
|
);
|
113
52
|
```
|
114
53
|
|
115
|
-
|
54
|
+
## Configuring Tailwind CSS
|
116
55
|
|
117
56
|
In Modern.js, you have two ways to configure Tailwind CSS:
|
118
57
|
|
@@ -144,7 +83,7 @@ export default {
|
|
144
83
|
|
145
84
|
If you are using both the `tailwind.config.{ts,js}` file and `tools.tailwindcss` option, the configuration defined in `tools.tailwindcss` will take precedence and override the content defined in `tailwind.config.{ts,js}`.
|
146
85
|
|
147
|
-
|
86
|
+
## Tailwind CSS Autocomplete
|
148
87
|
|
149
88
|
Tailwind CSS provides an official extension called [Tailwind CSS IntelliSense](https://github.com/tailwindlabs/tailwindcss-intellisense) for autocompletion of Tailwind CSS class names, CSS functions, and directives in VS Code.
|
150
89
|
|
@@ -153,7 +92,7 @@ You can follow the steps below to enable the autocomplete feature:
|
|
153
92
|
1. Install the [Tailwind CSS IntelliSense](https://github.com/tailwindlabs/tailwindcss-intellisense) extension in VS Code.
|
154
93
|
2. If the root directory of your project does not have a `tailwind.config.{ts,js}` file, you need to create one and write the Tailwind CSS configuration for your current project. Otherwise, the IDE plugin will not work correctly.
|
155
94
|
|
156
|
-
|
95
|
+
## Tailwind CSS Version
|
157
96
|
|
158
97
|
Modern.js supports both Tailwind CSS v2 and v3 versions, and the framework will recognize the version of the `tailwindcss` dependency in the project `package.json` file and enable the corresponding configuration. By default, we will install Tailwind CSS v3 version for you.
|
159
98
|
|
@@ -162,7 +101,7 @@ If your project is still using Tailwind CSS v2, we recommend that you upgrade to
|
|
162
101
|
- [Tailwind CSS v3.0](https://tailwindcss.com/blog/tailwindcss-v3)
|
163
102
|
- [Upgrade Guide](https://tailwindcss.com/docs/upgrade-guide)
|
164
103
|
|
165
|
-
|
104
|
+
## Browser Compatibility
|
166
105
|
|
167
106
|
Tailwind CSS v2 and v3 do not support the IE 11 browser, please refer to:
|
168
107
|
|
@@ -425,4 +425,4 @@ export const loader = async (): Promise<ProfileData> => {
|
|
425
425
|
|
426
426
|
In CSR projects, the `loader` executes on the client side and can directly call BFF functions to make API requests.
|
427
427
|
|
428
|
-
In SSR projects, each `loader` is also a server-side API. We recommend using `loader` instead of BFF functions with an HTTP method of `get` to avoid an extra layer of forwarding and execution.
|
428
|
+
In SSR projects, each `loader` is also a server-side API. We recommend using `loader` instead of BFF functions with an HTTP method of `get` to avoid an extra layer of forwarding and execution.
|
@@ -96,7 +96,7 @@ export const action = async ({ params }: ActionFunctionArgs) => {
|
|
96
96
|
|
97
97
|
### request
|
98
98
|
|
99
|
-
`request` is an instance of [Fetch Request](https://developer.mozilla.org/en-US/docs/Web/API/Request).
|
99
|
+
`request` is an instance of [Fetch Request](https://developer.mozilla.org/en-US/docs/Web/API/Request).
|
100
100
|
|
101
101
|
Using `request`, you can retrieve data submitted from the client in the action function, such as `request.json()`, `request.formData()`, `request.json()`, etc. Refer to [Data Types](#data-types) for which API to use.
|
102
102
|
|
@@ -0,0 +1 @@
|
|
1
|
+
["mock", "proxy", "rsdoctor", "using-storybook"]
|
@@ -0,0 +1,57 @@
|
|
1
|
+
import { PackageManagerTabs } from '@theme';
|
2
|
+
|
3
|
+
# Using Rsdoctor
|
4
|
+
|
5
|
+
Rsdoctor is a build analysis tool that supports both Webpack and Rspack. In Modern.js, we recommend using Rsdoctor to diagnose and analyze the build process and build outputs.
|
6
|
+
|
7
|
+
## Install Dependencies
|
8
|
+
|
9
|
+
Depending on whether your project uses Rspack or Webpack, choose the corresponding plugin installation.
|
10
|
+
|
11
|
+
### Rspack Plugin
|
12
|
+
|
13
|
+
<PackageManagerTabs command="add @rsdoctor/rspack-plugin -D" />
|
14
|
+
|
15
|
+
### Webpack Plugin
|
16
|
+
|
17
|
+
<PackageManagerTabs command="add @rsdoctor/webpack-plugin -D" />
|
18
|
+
|
19
|
+
## Register Plugin
|
20
|
+
|
21
|
+
In `modern.config.ts`, you can register the Rspack or Webpack plugin via `tools.bundlerChain`. Refer to the example below:
|
22
|
+
|
23
|
+
```ts title="modern.config.ts"
|
24
|
+
import { RsdoctorRspackPlugin } from '@rsdoctor/rspack-plugin';
|
25
|
+
|
26
|
+
export default {
|
27
|
+
// ...
|
28
|
+
tools: {
|
29
|
+
rspack(config, { appendPlugins }) {
|
30
|
+
// Only register the plugin when RSDOCTOR is true, as the plugin will increase build time.
|
31
|
+
if (process.env.RSDOCTOR) {
|
32
|
+
appendPlugins(
|
33
|
+
new RsdoctorRspackPlugin({
|
34
|
+
// Plugin options
|
35
|
+
}),
|
36
|
+
);
|
37
|
+
}
|
38
|
+
},
|
39
|
+
},
|
40
|
+
};
|
41
|
+
```
|
42
|
+
|
43
|
+
:::note
|
44
|
+
The above code is an example of using Rspack. If you are using Webpack, please switch the plugin accordingly.
|
45
|
+
:::
|
46
|
+
|
47
|
+
## Execute Build
|
48
|
+
|
49
|
+
You can execute the build command within your project. After the build is complete, Rsdoctor will automatically open the analysis page for the build.
|
50
|
+
|
51
|
+
```bash
|
52
|
+
RSDOCTOR=true npm run build
|
53
|
+
```
|
54
|
+
|
55
|
+
## Related Documentation
|
56
|
+
|
57
|
+
For more information, please refer to the [Rsdoctor Website](https://rsdoctor.dev/).
|
@@ -228,3 +228,5 @@ export default config;
|
|
228
228
|
The default config file path is `modern.config.(j|t)s`, for the detail config, see [modern.js config](/configure/app/usage).
|
229
229
|
|
230
230
|
If the original project includes configurations for Babel, they need to be written in the modern configuration. Most Babel configurations have already been included in Modern.js.
|
231
|
+
|
232
|
+
After installation, make the corresponding [configuration](/guides/basic-features/debug/using-storybook#configurations).
|
@@ -0,0 +1 @@
|
|
1
|
+
["json-files", "svg-assets", "wasm-assets"]
|
@@ -21,7 +21,7 @@ The following are the formats supported by Modern.js by default:
|
|
21
21
|
If you need to import assets in other formats, please refer to [Extend Asset Types](#extend-asset-types).
|
22
22
|
|
23
23
|
:::tip SVG images
|
24
|
-
SVG image is a special case. Modern.js support convert SVG to React components, so SVG is processed separately. For details, see [Import SVG Assets](/guides/basic-features/svg-assets.html).
|
24
|
+
SVG image is a special case. Modern.js support convert SVG to React components, so SVG is processed separately. For details, see [Import SVG Assets](/guides/basic-features/static-assets/svg-assets.html).
|
25
25
|
:::
|
26
26
|
|
27
27
|
## Import Assets in JS file
|
@@ -0,0 +1 @@
|
|
1
|
+
["playwright", "vitest", "jest", "cypress"]
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# Cypress
|
2
|
+
|
3
|
+
Cypress is a framework for E2E testing and component testing.
|
4
|
+
|
5
|
+
To use Cypress in Modern.js, you need to install the dependencies first. You can run the following commands:
|
6
|
+
|
7
|
+
import { PackageManagerTabs } from '@theme';
|
8
|
+
|
9
|
+
<PackageManagerTabs command={{ npm: "npm install -D cypress", yarn: "yarn add -D cypress", pnpm: "pnpm install -D cypress" }} />
|
10
|
+
|
11
|
+
Next, create a `cypress.config.ts` file and add the following content:
|
12
|
+
|
13
|
+
```ts
|
14
|
+
import { defineConfig } from 'cypress'
|
15
|
+
|
16
|
+
export default defineConfig({
|
17
|
+
e2e: {
|
18
|
+
setupNodeEvents(on, config) {},
|
19
|
+
},
|
20
|
+
})
|
21
|
+
```
|
22
|
+
|
23
|
+
## Writing Test Cases
|
24
|
+
|
25
|
+
Now, use Cypress to write an E2E test case by first creating two Modern.js pages.
|
26
|
+
|
27
|
+
```tsx title="routes/page.tsx"
|
28
|
+
import { Link } from '@modern-js/runtime/router';
|
29
|
+
|
30
|
+
const Index = () => (
|
31
|
+
<div>
|
32
|
+
<h1>Home</h1>
|
33
|
+
<Link to="/about">About</Link>
|
34
|
+
</div>
|
35
|
+
);
|
36
|
+
|
37
|
+
export default Index;
|
38
|
+
```
|
39
|
+
|
40
|
+
```tsx title="routes/about/page.tsx"
|
41
|
+
import { Link } from '@modern-js/runtime/router';
|
42
|
+
|
43
|
+
const Index = () => (
|
44
|
+
<div>
|
45
|
+
<h1>About</h1>
|
46
|
+
<Link to="/">Home</Link>
|
47
|
+
</div>
|
48
|
+
);
|
49
|
+
|
50
|
+
export default Index;
|
51
|
+
```
|
52
|
+
|
53
|
+
Next, create the test case file:
|
54
|
+
|
55
|
+
```ts title="cypress/e2e/app.cy.ts"
|
56
|
+
describe('Navigation', () => {
|
57
|
+
it('should navigate to the about page', () => {
|
58
|
+
// Start from the index page
|
59
|
+
cy.visit('http://localhost:8080/')
|
60
|
+
|
61
|
+
// Find a link with an href attribute containing "about" and click it
|
62
|
+
cy.get('a[href*="about"]').click()
|
63
|
+
|
64
|
+
// The new url should include "/about"
|
65
|
+
cy.url().should('include', '/about')
|
66
|
+
|
67
|
+
// The new page should contain an h1 with "About"
|
68
|
+
cy.get('h1').contains('About')
|
69
|
+
})
|
70
|
+
})
|
71
|
+
```
|
72
|
+
|
73
|
+
The test file may lack type definitions for the API. You can refer to the [Cypress - Typescript](https://docs.cypress.io/guides/tooling/typescript-support#Configure-tsconfigjson) documentation to resolve this.
|
74
|
+
|
75
|
+
You can add the command to your `package.json`:
|
76
|
+
|
77
|
+
```json title="package.json"
|
78
|
+
{
|
79
|
+
"scripts": {
|
80
|
+
"test": "cypress open"
|
81
|
+
}
|
82
|
+
}
|
83
|
+
```
|
84
|
+
|
85
|
+
## Run Test Cases
|
86
|
+
|
87
|
+
Execute the above `test` command to run the test cases:
|
88
|
+
|
89
|
+
```bash
|
90
|
+
DevTools listening on ws://127.0.0.1:55203/devtools/browser/xxxxx
|
91
|
+
```
|
92
|
+
|
93
|
+
Cypress will open a headless browser. Following the prompts, you can find the corresponding test files and automatically run the E2E tests:
|
94
|
+
|
95
|
+

|
@@ -0,0 +1,148 @@
|
|
1
|
+
# Jest
|
2
|
+
|
3
|
+
Jest is a JavaScript testing framework that is primarily used with React Testing Library for unit testing and Snapshot testing.
|
4
|
+
|
5
|
+
To use Jest in Modern.js, you need to install the dependencies first. You can run the following commands:
|
6
|
+
|
7
|
+
import { PackageManagerTabs } from '@theme';
|
8
|
+
|
9
|
+
<PackageManagerTabs command={{
|
10
|
+
npm: "npm install -D jest jest-environment-jsdom @testing-library/react @testing-library/dom @testing-library/jest-dom",
|
11
|
+
yarn: "yarn add -D jest jest-environment-jsdom @testing-library/react @testing-library/dom @testing-library/jest-dom",
|
12
|
+
pnpm: "pnpm install -D jest jest-environment-jsdom @testing-library/react @testing-library/dom @testing-library/jest-dom"
|
13
|
+
}} />
|
14
|
+
|
15
|
+
Next, you can run the following commands to automatically initialize Jest in your project and generate a basic `jest.config.[jt]s` configuration:
|
16
|
+
|
17
|
+
<PackageManagerTabs command={{
|
18
|
+
npm: "npm init jest@latest",
|
19
|
+
yarn: "yarn create jest@latest",
|
20
|
+
pnpm: "pnpm create jest@latest"
|
21
|
+
}} />
|
22
|
+
|
23
|
+
## Configuration File
|
24
|
+
|
25
|
+
:::note
|
26
|
+
This section will use `.ts` files for Jest testing.
|
27
|
+
:::
|
28
|
+
|
29
|
+
Compared to other testing frameworks, Jest requires more configuration at the build level, such as handling JSX and ESM syntax. Therefore, you need to install some additional dependencies:
|
30
|
+
|
31
|
+
<PackageManagerTabs command={{
|
32
|
+
npm: "npm install -D babel-jest @babel/core @babel/preset-env @babel/preset-react @babel/preset-typescript",
|
33
|
+
yarn: "yarn add -D babel-jest @babel/core @babel/preset-env @babel/preset-react @babel/preset-typescript",
|
34
|
+
pnpm: "pnpm install -D babel-jest @babel/core @babel/preset-env @babel/preset-react @babel/preset-typescript"
|
35
|
+
}} />
|
36
|
+
|
37
|
+
### Configure Jest
|
38
|
+
|
39
|
+
You need to further configure the `jest.config.ts` file to allow Jest to correctly compile and run test cases. Here is a basic configuration:
|
40
|
+
|
41
|
+
```ts title="jest.config.ts"
|
42
|
+
import type { Config } from 'jest';
|
43
|
+
|
44
|
+
const config: Config = {
|
45
|
+
coverageProvider: 'babel',
|
46
|
+
setupFilesAfterEnv: ['<rootDir>/jest.setup.ts'],
|
47
|
+
testEnvironment: 'jsdom',
|
48
|
+
transform: {
|
49
|
+
'^.+\\.(js|jsx|ts|tsx)$': 'babel-jest',
|
50
|
+
},
|
51
|
+
transformIgnorePatterns: [],
|
52
|
+
};
|
53
|
+
|
54
|
+
export default config;
|
55
|
+
```
|
56
|
+
|
57
|
+
In the configuration, the `transformIgnorePatterns` is set to an empty array, meaning that all files will be compiled. If you want to speed up the test run, you can configure it as needed.
|
58
|
+
|
59
|
+
The `setupFilesAfterEnv` will be executed at startup. In `jest.setup.ts`, you can import `@testing-library/jest-dom`, which includes a set of convenient custom matchers, such as `.toBeInTheDocument()`, to make writing tests easier:
|
60
|
+
|
61
|
+
```ts title="jest.setup.ts"
|
62
|
+
import '@testing-library/jest-dom';
|
63
|
+
```
|
64
|
+
|
65
|
+
### Configure Babel
|
66
|
+
|
67
|
+
You need to configure Babel to allow Jest to automatically compile JSX and other syntax. Here is a basic configuration:
|
68
|
+
|
69
|
+
```js title="babel.config.js"
|
70
|
+
module.exports = {
|
71
|
+
presets: [
|
72
|
+
['@babel/preset-env', { targets: { node: 'current' } }],
|
73
|
+
['@babel/preset-react', { runtime: 'automatic' }],
|
74
|
+
'@babel/preset-typescript',
|
75
|
+
],
|
76
|
+
};
|
77
|
+
```
|
78
|
+
|
79
|
+
## Writing Test Cases
|
80
|
+
|
81
|
+
Now, you can start writing tests. First, add a `test` command in `package.json`:
|
82
|
+
|
83
|
+
```json title="package.json"
|
84
|
+
{
|
85
|
+
"scripts": {
|
86
|
+
"test": "jest"
|
87
|
+
}
|
88
|
+
}
|
89
|
+
```
|
90
|
+
|
91
|
+
Create a simple page for testing:
|
92
|
+
|
93
|
+
```tsx title="routes/page.tsx"
|
94
|
+
import { Link } from '@modern-js/runtime/router';
|
95
|
+
|
96
|
+
const Index = () => (
|
97
|
+
<div>
|
98
|
+
<h1>Home</h1>
|
99
|
+
<Link to="/about">About</Link>
|
100
|
+
</div>
|
101
|
+
);
|
102
|
+
|
103
|
+
export default Index;
|
104
|
+
```
|
105
|
+
|
106
|
+
Add a test case to check if the page has the expected text:
|
107
|
+
|
108
|
+
```tsx title="__tests__/page.test.tsx"
|
109
|
+
import '@testing-library/jest-dom';
|
110
|
+
import { render, screen } from '@testing-library/react';
|
111
|
+
import { BrowserRouter as Router } from '@modern-js/runtime/router';
|
112
|
+
import Page from '../routes/page';
|
113
|
+
|
114
|
+
describe('Page', () => {
|
115
|
+
it('renders a heading', () => {
|
116
|
+
render(
|
117
|
+
<Router>
|
118
|
+
<Page />
|
119
|
+
</Router>,
|
120
|
+
);
|
121
|
+
|
122
|
+
const heading = screen.getByRole('heading', { level: 1 });
|
123
|
+
|
124
|
+
expect(heading).toBeInTheDocument();
|
125
|
+
});
|
126
|
+
});
|
127
|
+
```
|
128
|
+
|
129
|
+
In the above test case, we imported the `<Router>` component from `@modern-js/runtime/router` because React Router requires the corresponding context when rendering some route-related components.
|
130
|
+
|
131
|
+
:::note
|
132
|
+
When running directly in the Modern.js application, the `<Router>` component will be automatically injected.
|
133
|
+
:::
|
134
|
+
|
135
|
+
## Run Test Cases
|
136
|
+
|
137
|
+
Execute the above `test` command to run the test cases:
|
138
|
+
|
139
|
+
```bash
|
140
|
+
PASS src/__tests__/page.test.tsx
|
141
|
+
Page
|
142
|
+
✓ renders a heading (31 ms)
|
143
|
+
|
144
|
+
Test Suites: 1 passed, 1 total
|
145
|
+
Tests: 1 passed, 1 total
|
146
|
+
Snapshots: 0 total
|
147
|
+
Time: 0.959 s, estimated 1 s
|
148
|
+
```
|