@modern-js/main-doc 0.0.0-nightly-20240903170747 → 0.0.0-nightly-20240905170702
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
@@ -0,0 +1,111 @@
|
|
1
|
+
# Playwright
|
2
|
+
|
3
|
+
Playwright is a testing framework that allows you to run tests automatically in Chromium, Firefox, and WebKit environments using a single API. You can use it to **write E2E tests**.
|
4
|
+
|
5
|
+
To use Playwright 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 init playwright", yarn: "yarn create playwright", pnpm: "pnpm create playwright" }} />
|
10
|
+
|
11
|
+
The above commands will automatically install Playwright dependencies and help you install and configure it in your project through a series of prompts, including adding a `playwright.config.ts` file.
|
12
|
+
|
13
|
+
:::info
|
14
|
+
Refer to the official Playwright documentation at [Install Playwright](https://playwright.dev/docs/intro#installation) for a more detailed guide.
|
15
|
+
:::
|
16
|
+
|
17
|
+
After creating with the default configuration, you can see the following files in your project:
|
18
|
+
|
19
|
+
```ts title="tests/example.spec.ts"
|
20
|
+
import { test, expect } from '@playwright/test';
|
21
|
+
|
22
|
+
test('has title', async ({ page }) => {
|
23
|
+
await page.goto('https://playwright.dev/');
|
24
|
+
|
25
|
+
// Expect a title "to contain" a substring.
|
26
|
+
await expect(page).toHaveTitle(/Playwright/);
|
27
|
+
});
|
28
|
+
|
29
|
+
test('get started link', async ({ page }) => {
|
30
|
+
await page.goto('https://playwright.dev/');
|
31
|
+
|
32
|
+
// Click the get started link.
|
33
|
+
await page.getByRole('link', { name: 'Get started' }).click();
|
34
|
+
|
35
|
+
// Expects page to have a heading with the name of Installation.
|
36
|
+
await expect(
|
37
|
+
page.getByRole('heading', { name: 'Installation' }),
|
38
|
+
).toBeVisible();
|
39
|
+
});
|
40
|
+
```
|
41
|
+
|
42
|
+
This is the default test file. Now create some new pages and test them.
|
43
|
+
|
44
|
+
## Create E2E Tests
|
45
|
+
|
46
|
+
First, create two Modern.js pages.
|
47
|
+
|
48
|
+
```tsx title="routes/page.tsx"
|
49
|
+
import { Link } from '@modern-js/runtime/router';
|
50
|
+
|
51
|
+
const Index = () => (
|
52
|
+
<div>
|
53
|
+
<h1>Home</h1>
|
54
|
+
<Link to="/about">About</Link>
|
55
|
+
</div>
|
56
|
+
);
|
57
|
+
|
58
|
+
export default Index;
|
59
|
+
```
|
60
|
+
|
61
|
+
```tsx title="routes/about/page.tsx"
|
62
|
+
import { Link } from '@modern-js/runtime/router';
|
63
|
+
|
64
|
+
const Index = () => (
|
65
|
+
<div>
|
66
|
+
<h1>About</h1>
|
67
|
+
<Link to="/">Home</Link>
|
68
|
+
</div>
|
69
|
+
);
|
70
|
+
|
71
|
+
export default Index;
|
72
|
+
```
|
73
|
+
|
74
|
+
Next, add test cases to ensure the links on your pages work properly.
|
75
|
+
|
76
|
+
```ts title="tests/example.spec.ts"
|
77
|
+
import { test, expect } from '@playwright/test'
|
78
|
+
|
79
|
+
test('should navigate to the about page', async ({ page }) => {
|
80
|
+
// Start from the index page (the baseURL is set via the webServer in the playwright.config.ts)
|
81
|
+
await page.goto('http://localhost:8080/')
|
82
|
+
// Find an element with the text 'About' and click on it
|
83
|
+
await page.click('text=About')
|
84
|
+
// The new URL should be "/about" (baseURL is used there)
|
85
|
+
await expect(page).toHaveURL('http://localhost:8080/about')
|
86
|
+
// The new page should contain an h1 with "About"
|
87
|
+
await expect(page.locator('h1')).toContainText('About')
|
88
|
+
})
|
89
|
+
```
|
90
|
+
|
91
|
+
## Run Test Cases
|
92
|
+
|
93
|
+
Playwright requires your Modern.js server to be running. We recommend running your test cases under production builds; you can run `pnpm run build` and `pnpm run serve` to simulate the production environment locally.
|
94
|
+
|
95
|
+
```bash
|
96
|
+
info Starting production server...
|
97
|
+
|
98
|
+
> Local: http://localhost:8080/
|
99
|
+
> Network: http://10.94.59.30:8080/
|
100
|
+
```
|
101
|
+
|
102
|
+
After the project is successfully built and running, you can run Playwright cases in another terminal by executing `npx playwright test`:
|
103
|
+
|
104
|
+
```bash
|
105
|
+
Running 3 tests using 3 workers
|
106
|
+
3 passed (2.0s)
|
107
|
+
|
108
|
+
To open last HTML report run:
|
109
|
+
|
110
|
+
npx playwright show-report
|
111
|
+
```
|
@@ -0,0 +1,100 @@
|
|
1
|
+
# Vitest
|
2
|
+
|
3
|
+
Vitest is a testing framework driven by Vite, which can be used for unit testing in combination with React Testing Library.
|
4
|
+
|
5
|
+
To use Vitest 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 vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom",
|
11
|
+
yarn: "yarn add -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom",
|
12
|
+
pnpm: "pnpm install -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom",
|
13
|
+
bun: "bun add -D vitest @vitejs/plugin-react jsdom @testing-library/react @testing-library/dom"
|
14
|
+
}} />
|
15
|
+
|
16
|
+
Next, you need to create a Vitest configuration file `vitest.config.ts` with the following content:
|
17
|
+
|
18
|
+
```ts title="vitest.config.ts"
|
19
|
+
import { defineConfig } from 'vitest/config'
|
20
|
+
import react from '@vitejs/plugin-react'
|
21
|
+
|
22
|
+
export default defineConfig({
|
23
|
+
plugins: [react()],
|
24
|
+
test: {
|
25
|
+
environment: 'jsdom',
|
26
|
+
},
|
27
|
+
})
|
28
|
+
```
|
29
|
+
|
30
|
+
For more information about Vitest configuration, you can refer to the [Vitest configuration documentation](https://vitest.dev/config/#configuration).
|
31
|
+
|
32
|
+
You can optionally add the `vitest` command to `package.json`:
|
33
|
+
|
34
|
+
```json title="package.json"
|
35
|
+
{
|
36
|
+
"scripts": {
|
37
|
+
"test": "vitest"
|
38
|
+
}
|
39
|
+
}
|
40
|
+
```
|
41
|
+
|
42
|
+
After running this command, Vitest will automatically watch your file changes and rerun the test cases.
|
43
|
+
|
44
|
+
## Create Unit Tests
|
45
|
+
|
46
|
+
First, create a simple page for testing:
|
47
|
+
|
48
|
+
```tsx title="routes/page.tsx"
|
49
|
+
import { Link } from '@modern-js/runtime/router';
|
50
|
+
|
51
|
+
const Index = () => (
|
52
|
+
<div>
|
53
|
+
<h1>Home</h1>
|
54
|
+
<Link to="/about">About</Link>
|
55
|
+
</div>
|
56
|
+
);
|
57
|
+
|
58
|
+
export default Index;
|
59
|
+
```
|
60
|
+
|
61
|
+
Add a test case to check if the page has the expected text:
|
62
|
+
|
63
|
+
```tsx title="__tests__/page.test.tsx"
|
64
|
+
import { expect, test } from 'vitest';
|
65
|
+
import { render, screen } from '@testing-library/react';
|
66
|
+
import { BrowserRouter as Router } from '@modern-js/runtime/router';
|
67
|
+
import Page from '../routes/page';
|
68
|
+
|
69
|
+
test('Page', () => {
|
70
|
+
render(
|
71
|
+
<Router>
|
72
|
+
<Page />
|
73
|
+
</Router>,
|
74
|
+
);
|
75
|
+
expect(screen.getByRole('heading', { level: 1, name: 'Home' })).toBeDefined();
|
76
|
+
});
|
77
|
+
```
|
78
|
+
|
79
|
+
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.
|
80
|
+
|
81
|
+
:::note
|
82
|
+
When running directly in the Modern.js application, the `<Router>` component will be automatically injected.
|
83
|
+
:::
|
84
|
+
|
85
|
+
## Run Test Cases
|
86
|
+
|
87
|
+
Execute the above `test` command to run the test cases:
|
88
|
+
|
89
|
+
```bash
|
90
|
+
✓ src/__tests__/page.test.tsx (1)
|
91
|
+
✓ Page
|
92
|
+
|
93
|
+
Test Files 1 passed (1)
|
94
|
+
Tests 1 passed (1)
|
95
|
+
Start at 15:37:12
|
96
|
+
Duration 999ms (transform 119ms, setup 0ms, collect 365ms, tests 33ms, environment 421ms, prepare 44ms)
|
97
|
+
|
98
|
+
|
99
|
+
PASS Waiting for file changes...
|
100
|
+
```
|
@@ -60,7 +60,7 @@ During production builds, Modern.js uses [Terser](https://github.com/terser/ters
|
|
60
60
|
|
61
61
|
Modern.js uses [PostCSS](https://postcss.org/) to transform CSS code and enables [autoprefixer](https://github.com/postcss/autoprefixer) by default to add CSS prefixes.
|
62
62
|
|
63
|
-
Modern.js supports enabling ["Tailwind CSS"](/
|
63
|
+
Modern.js supports enabling ["Tailwind CSS"](/guides/basic-features/css/tailwindcss) and is compatible with both Tailwind CSS v2 and v3.
|
64
64
|
|
65
65
|
## CSS Preprocessors
|
66
66
|
|
@@ -73,11 +73,11 @@ Modern.js supports three CSS preprocessors: [Sass](https://sass-lang.com/), [Les
|
|
73
73
|
|
74
74
|
Modern.js provides out-of-the-box support for [CSS Modules](https://github.com/css-modules/css-modules), which is implemented internally based on [css-loader](https://www.npmjs.com/package/css-loader).
|
75
75
|
|
76
|
-
Please refer to ["Use CSS Modules"](/guides/basic-features/css-modules) for usage instructions.
|
76
|
+
Please refer to ["Use CSS Modules"](/guides/basic-features/css/css-modules) for usage instructions.
|
77
77
|
|
78
78
|
## CSS-in-JS
|
79
79
|
|
80
|
-
Modern.js supports the use of [styled-components](https://styled-components.com/). Please refer to ["Using CSS-in-JS"](/en/guides/basic-features/css
|
80
|
+
Modern.js supports the use of [styled-components](https://styled-components.com/). Please refer to ["Using CSS-in-JS"](/en/guides/basic-features/css/css-in-js) for usage instructions.
|
81
81
|
|
82
82
|
If you need to use other CSS-in-JS solutions, you can integrate them into your project on your own.
|
83
83
|
|
@@ -89,7 +89,7 @@ Additionally, Modern.js provides built-in support for [on-demand import](/config
|
|
89
89
|
|
90
90
|
## Component Development
|
91
91
|
|
92
|
-
Modern.js supports the use of [Storybook](https://storybook.js.org/) for developing UI components. This feature is optional. Please refer to ["Using Storybook"](/
|
92
|
+
Modern.js supports the use of [Storybook](https://storybook.js.org/) for developing UI components. This feature is optional. Please refer to ["Using Storybook"](/guides/basic-features/debug/using-storybook) to enable it.
|
93
93
|
|
94
94
|
## Node.js Framework
|
95
95
|
|
@@ -40,16 +40,6 @@ import PackageManager from '@site-docs-en/components/package-manager';
|
|
40
40
|
|
41
41
|
<PackageManager />
|
42
42
|
|
43
|
-
### buildTools
|
44
|
-
|
45
|
-
Question: Please select the bundler.
|
46
|
-
|
47
|
-
Options:
|
48
|
-
|
49
|
-
- webpack -- webpack
|
50
|
-
|
51
|
-
- Rspack -- rspack
|
52
|
-
|
53
43
|
## Npm Module
|
54
44
|
|
55
45
|
### packageName
|
@@ -35,7 +35,6 @@ npx @modern-js/create@latest web-app
|
|
35
35
|
? Please select the type of project you want to create: Web App
|
36
36
|
? Please select the programming language: TS
|
37
37
|
? Please select the package manager: pnpm
|
38
|
-
? Please select the bundler: webpack
|
39
38
|
```
|
40
39
|
|
41
40
|
### Create an Npm Module Project
|
@@ -294,7 +294,7 @@ A Utility Class named `custom-text-gray` is implemented in `src/routes/styles/ut
|
|
294
294
|
```
|
295
295
|
|
296
296
|
:::info note
|
297
|
-
Modern.js integrates with [PostCSS](/guides/basic-features/css) and supports modern CSS syntax features such as [custom properties](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties).
|
297
|
+
Modern.js integrates with [PostCSS](/guides/basic-features/css/css) and supports modern CSS syntax features such as [custom properties](https://developer.mozilla.org/en-US/docs/Web/CSS/Using_CSS_custom_properties).
|
298
298
|
|
299
299
|
:::
|
300
300
|
|
@@ -23,7 +23,7 @@ const tailwind = {
|
|
23
23
|
|
24
24
|
在使用 `tools.tailwindcss` 之前,你需要启用 Modern.js 的 Tailwind CSS 插件。
|
25
25
|
|
26
|
-
请阅读[「使用 Tailwind CSS」](/guides/basic-features/css
|
26
|
+
请阅读[「使用 Tailwind CSS」](/guides/basic-features/css/tailwindcss) 章节来了解开启方式。
|
27
27
|
|
28
28
|
### Function 类型
|
29
29
|
|
@@ -41,8 +41,8 @@ Modern.js 从 `MAJOR_VERSION.46.0` 起开始将底层的构建工具升级为 [R
|
|
41
41
|
| [CSS Minimizer 插件](https://github.com/rspack-contrib/rsbuild-plugin-css-minimizer) | 用于自定义 CSS 压缩工具,切换到 [cssnano](https://cssnano.co/) 或其他工具进行 CSS 压缩。 | [tools.minifyCss](/configure/app/tools/minify-css.html) |
|
42
42
|
| [Pug 插件](https://github.com/rspack-contrib/rsbuild-plugin-pug) | 提供对 Pug 模板引擎的支持 | [tools.pug](/configure/app/tools/pug.html) |
|
43
43
|
| [Rem 插件](https://github.com/rspack-contrib/rsbuild-plugin-rem) | 用于实现移动端页面的 rem 自适应布局 | [output.convertToRem](/configure/app/output/convert-to-rem.html) |
|
44
|
-
| [YAML 插件](https://github.com/rspack-contrib/rsbuild-plugin-yaml) | 用于引用 YAML 文件,并将其转换为 JavaScript 对象 | [TOML 文件](/guides/basic-features/json-files.html#toml-文件) |
|
45
|
-
| [TOML 插件](https://github.com/rspack-contrib/rsbuild-plugin-toml) | 用于引用 TOML 文件,并将其转换为 JavaScript 对象 | [YAML 文件](/guides/basic-features/json-files.html#yaml-文件) |
|
44
|
+
| [YAML 插件](https://github.com/rspack-contrib/rsbuild-plugin-yaml) | 用于引用 YAML 文件,并将其转换为 JavaScript 对象 | [TOML 文件](/guides/basic-features/static-assets/json-files.html#toml-文件) |
|
45
|
+
| [TOML 插件](https://github.com/rspack-contrib/rsbuild-plugin-toml) | 用于引用 TOML 文件,并将其转换为 JavaScript 对象 | [YAML 文件](/guides/basic-features/static-assets/json-files.html#yaml-文件) |
|
46
46
|
|
47
47
|
### 未内置插件
|
48
48
|
|
@@ -14,7 +14,7 @@ import Rspack from '@modern-js/builder-doc/docs/zh/shared/rspackTip.mdx';
|
|
14
14
|
|
15
15
|
## 初始化 Rspack 项目
|
16
16
|
|
17
|
-
Modern.js
|
17
|
+
Modern.js 新项目已默认启用 Rspack 构建,只需执行 [初始化项目](/guides/get-started/quick-start.html#初始化项目),即可创建一个 Rspack 项目:
|
18
18
|
|
19
19
|
import InitRspackApp from '@site-docs/components/init-rspack-app';
|
20
20
|
|
@@ -24,7 +24,7 @@ import InitRspackApp from '@site-docs/components/init-rspack-app';
|
|
24
24
|
|
25
25
|
## 开启 Rspack 构建
|
26
26
|
|
27
|
-
从 Modern.js MAJOR_VERSION.
|
27
|
+
从 Modern.js MAJOR_VERSION.59.0 版本起,在一个已有的 Modern.js 项目中,你仅需在 `modern.config.ts` 中添加以下配置,即可启用 Rspack 构建:
|
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
|
-
如果你当前版本低于 MAJOR_VERSION.
|
42
|
+
如果你当前版本低于 MAJOR_VERSION.59.0, 可通过执行 `npx modern upgrade` 进行升级。
|
43
43
|
:::
|
44
44
|
|
45
45
|
import RspackPrecautions from '@modern-js/builder-doc/docs/zh/shared/rspackPrecautions.md';
|
@@ -70,12 +70,6 @@ export default {
|
|
70
70
|
};
|
71
71
|
```
|
72
72
|
|
73
|
-
:::tip
|
74
|
-
开启 Rspack 构建后,目前有个别配置暂不支持在 Rspack 中使用,如 [source.moduleScopes](/configure/app/source/module-scopes)。
|
75
|
-
|
76
|
-
对于不支持的配置,我们在文档中有标注 `打包工具:仅支持 webpack` 或 `打包工具:仅支持 rspack`,可参考具体配置介绍。
|
77
|
-
:::
|
78
|
-
|
79
73
|
## 修改转译配置
|
80
74
|
|
81
75
|
Modern.js 在 Rspack 模式下使用 Rspack [builtin:swc-loader](https://rspack.dev/zh/guide/features/builtin-swc-loader) 进行代码转译。
|
@@ -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
|
+
# 使用 CSS-in-JS
|
2
|
+
|
3
|
+
CSS-in-JS 是一种可以将 CSS 样式写在 JS 文件里的技术。
|
4
|
+
|
5
|
+
Modern.js 集成了社区常用的 CSS-in-JS 实现库 [styled-components](https://styled-components.com/),它使用 JavaScript 的新特性 [Tagged template](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates) 编写组件的 CSS 样式。可以直接从 `@modern-js/runtime/styled` 引入 [styled-components](https://styled-components.com/) 的 API 进行使用。
|
6
|
+
|
7
|
+
当需要编写一个内部字体为红色的 `div` 组件时,可以如下实现:
|
8
|
+
|
9
|
+
```js
|
10
|
+
import styled from '@modern-js/runtime/styled';
|
11
|
+
|
12
|
+
const RedDiv = styled.div`
|
13
|
+
color: red;
|
14
|
+
`;
|
15
|
+
```
|
16
|
+
|
17
|
+
当需要根据组件的 `props` 动态设置组件样式时,例如 `props` 的属性 `primary` 为 `true` 时,按钮的颜色为白色,其他情况为红色,实现代码如下:
|
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
|
+
关于 styled-components 的更多用法,请参考 [styled-components 官网](https://styled-components.com/)。
|
29
|
+
|
30
|
+
Modern.js 内部集成了 Babel 的 [babel-plugin-styled-components](https://github.com/styled-components/babel-plugin-styled-components) 插件,你可以通过 [tools.styledComponents](/configure/app/tools/styled-components) 对插件进行配置。
|
31
|
+
|
32
|
+
:::tip 提示
|
33
|
+
如果需要使用 [styled-jsx](https://www.npmjs.com/package/styled-jsx)、[Emotion](https://emotion.sh/) 等其他 CSS-in-JS 库,需要先安装对应库的依赖。具体使用方式请参考对应库的官网。
|
34
|
+
:::
|
@@ -0,0 +1,25 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 1
|
3
|
+
---
|
4
|
+
|
5
|
+
# 引入 CSS
|
6
|
+
|
7
|
+
Modern.js 内置多种常用的 CSS 开发方案,包括 Less / Sass / Stylus 预处理器、PostCSS、CSS Modules、CSS-in-JS 和 Tailwind CSS。
|
8
|
+
|
9
|
+
## 使用 Less、Sass 和 Stylus
|
10
|
+
|
11
|
+
Modern.js 内置了社区流行的 CSS 预处理器,包括 Less 和 Sass。
|
12
|
+
|
13
|
+
默认情况下,你不需要对 Less 和 Sass 进行任何配置。如果你有自定义 loader 配置的需求,可以通过配置 [tools.less](/configure/app/tools/less)、[tools.sass](/configure/app/tools/sass) 来进行设置。
|
14
|
+
|
15
|
+
你也可以在 Modern.js 中使用 Stylus,只需要安装 Rsbuild 提供的 Stylus 插件即可,使用方式请参考 [Stylus 插件](https://rsbuild.dev/zh/plugins/list/plugin-stylus)。
|
16
|
+
|
17
|
+
## 使用 PostCSS
|
18
|
+
|
19
|
+
Modern.js 内置了 [PostCSS](https://postcss.org/) 来转换 CSS 代码。
|
20
|
+
|
21
|
+
请阅读 [Rsbuild - 使用 PostCSS](https://rsbuild.dev/zh/guide/basic/css-usage#%E4%BD%BF%E7%94%A8-postcss) 了解更多用法。
|
22
|
+
|
23
|
+
## 使用 Uno CSS
|
24
|
+
|
25
|
+
请阅读 [Rsbuild - 使用 UnoCSS](https://rsbuild.dev/zh/guide/basic/unocss) 了解更多用法。
|
@@ -1,65 +1,4 @@
|
|
1
|
-
|
2
|
-
sidebar_position: 1
|
3
|
-
---
|
4
|
-
|
5
|
-
# 样式开发
|
6
|
-
|
7
|
-
Modern.js 内置多种常用的 CSS 开发方案,包括 Less / Sass / Stylus 预处理器、PostCSS、CSS Modules、CSS-in-JS 和 Tailwind CSS。
|
8
|
-
|
9
|
-
## 使用 Less、Sass 和 Stylus
|
10
|
-
|
11
|
-
Modern.js 内置了社区流行的 CSS 预处理器,包括 Less 和 Sass。
|
12
|
-
|
13
|
-
默认情况下,你不需要对 Less 和 Sass 进行任何配置。如果你有自定义 loader 配置的需求,可以通过配置 [tools.less](/configure/app/tools/less)、[tools.sass](/configure/app/tools/sass) 来进行设置。
|
14
|
-
|
15
|
-
你也可以在 Modern.js 中使用 Stylus,只需要安装 Rsbuild 提供的 Stylus 插件即可,使用方式请参考 [Stylus 插件](https://rsbuild.dev/zh/plugins/list/plugin-stylus)。
|
16
|
-
|
17
|
-
## 使用 PostCSS
|
18
|
-
|
19
|
-
Modern.js 内置了 [PostCSS](https://postcss.org/) 来转换 CSS 代码。
|
20
|
-
|
21
|
-
请阅读 [Rsbuild - 使用 PostCSS](https://rsbuild.dev/zh/guide/basic/css-usage#%E4%BD%BF%E7%94%A8-postcss) 了解更多用法。
|
22
|
-
|
23
|
-
## 使用 CSS Modules
|
24
|
-
|
25
|
-
请阅读 [使用 CSS Modules](/guides/basic-features/css-modules) 章节来了解 CSS Modules 的完整用法。
|
26
|
-
|
27
|
-
## 使用 CSS-in-JS
|
28
|
-
|
29
|
-
CSS-in-JS 是一种可以将 CSS 样式写在 JS 文件里的技术。
|
30
|
-
|
31
|
-
Modern.js 集成了社区常用的 CSS-in-JS 实现库 [styled-components](https://styled-components.com/),它使用 JavaScript 的新特性 [Tagged template](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates) 编写组件的 CSS 样式。可以直接从 `@modern-js/runtime/styled` 引入 [styled-components](https://styled-components.com/) 的 API 进行使用。
|
32
|
-
|
33
|
-
当需要编写一个内部字体为红色的 `div` 组件时,可以如下实现:
|
34
|
-
|
35
|
-
```js
|
36
|
-
import styled from '@modern-js/runtime/styled';
|
37
|
-
|
38
|
-
const RedDiv = styled.div`
|
39
|
-
color: red;
|
40
|
-
`;
|
41
|
-
```
|
42
|
-
|
43
|
-
当需要根据组件的 `props` 动态设置组件样式时,例如 `props` 的属性 `primary` 为 `true` 时,按钮的颜色为白色,其他情况为红色,实现代码如下:
|
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
|
-
关于 styled-components 的更多用法,请参考 [styled-components 官网](https://styled-components.com/)。
|
55
|
-
|
56
|
-
Modern.js 内部集成了 Babel 的 [babel-plugin-styled-components](https://github.com/styled-components/babel-plugin-styled-components) 插件,你可以通过 [tools.styledComponents](/configure/app/tools/styled-components) 对插件进行配置。
|
57
|
-
|
58
|
-
:::tip 提示
|
59
|
-
如果需要使用 [styled-jsx](https://www.npmjs.com/package/styled-jsx)、[Emotion](https://emotion.sh/) 等其他 CSS-in-JS 库,需要先安装对应库的依赖。具体使用方式请参考对应库的官网。
|
60
|
-
:::
|
61
|
-
|
62
|
-
## 使用 Tailwind CSS
|
1
|
+
# 使用 Tailwind CSS
|
63
2
|
|
64
3
|
[Tailwind CSS](https://tailwindcss.com/) 是一个以 Utility Class 为基础的 CSS 框架和设计系统,可以快速地为组件添加常用样式,同时支持主题样式的灵活扩展。
|
65
4
|
|
@@ -153,7 +92,7 @@ Tailwind CSS 官方提供了 [Tailwind CSS IntelliSense](https://github.com/tail
|
|
153
92
|
1. 在 VS Code 中安装 [Tailwind CSS IntelliSense](https://github.com/tailwindlabs/tailwindcss-intellisense) 插件。
|
154
93
|
2. 如果项目的根目录没有 `tailwind.config.{ts,js}` 文件,那么你需要创建该文件,并写入当前项目的 Tailwind CSS 配置,否则 IDE 插件将无法正确生效。
|
155
94
|
|
156
|
-
|
95
|
+
## Tailwind CSS 版本
|
157
96
|
|
158
97
|
Modern.js 同时支持 Tailwind CSS v2 和 v3 版本,框架会识别项目 `package.json` 中的 `tailwindcss` 依赖版本,并启用相应的配置。默认情况下,我们会为你安装 Tailwind CSS v3 版本。
|
159
98
|
|
@@ -162,7 +101,7 @@ Modern.js 同时支持 Tailwind CSS v2 和 v3 版本,框架会识别项目 `pa
|
|
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
|
+
## 浏览器兼容性
|
166
105
|
|
167
106
|
Tailwind CSS v2 和 v3 均不支持 IE 11 浏览器,相关背景请参考:
|
168
107
|
|
@@ -0,0 +1 @@
|
|
1
|
+
["mock", "proxy", "rsdoctor", "using-storybook"]
|
@@ -0,0 +1,57 @@
|
|
1
|
+
import { PackageManagerTabs } from '@theme';
|
2
|
+
|
3
|
+
# 使用 Rsdoctor
|
4
|
+
|
5
|
+
Rsdoctor 是一个支持 Webpack 及 Rspack 构建分析工具。在 Modern.js 中,我们推荐使用 Rsdoctor 来对构建过程与构建产物进行诊断和分析。
|
6
|
+
|
7
|
+
## 安装依赖
|
8
|
+
|
9
|
+
根据项目是 Rspack 或 Webpack 构建,选择对应的插件安装。
|
10
|
+
|
11
|
+
### Rspack 插件
|
12
|
+
|
13
|
+
<PackageManagerTabs command="add @rsdoctor/rspack-plugin -D" />
|
14
|
+
|
15
|
+
### Webpack 插件
|
16
|
+
|
17
|
+
<PackageManagerTabs command="add @rsdoctor/webpack-plugin -D" />
|
18
|
+
|
19
|
+
## 注册插件
|
20
|
+
|
21
|
+
在 `modern.config.ts` 中,你可以通过 `tools.bundlerChain` 来注册 Rspack 或 Webpack 插件,参考:
|
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
|
+
// 仅在 RSDOCTOR 为 true 时注册插件,因为插件会增加构建耗时
|
31
|
+
if (process.env.RSDOCTOR) {
|
32
|
+
appendPlugins(
|
33
|
+
new RsdoctorRspackPlugin({
|
34
|
+
// 插件选项
|
35
|
+
}),
|
36
|
+
);
|
37
|
+
}
|
38
|
+
},
|
39
|
+
},
|
40
|
+
};
|
41
|
+
```
|
42
|
+
|
43
|
+
:::note
|
44
|
+
上述代码是使用 Rspack 时的示例,如果使用 Webpack 请自行切换插件。
|
45
|
+
:::
|
46
|
+
|
47
|
+
## 执行构建
|
48
|
+
|
49
|
+
在你可以在项目内执行 build 命令,在完成构建后,Rsdoctor 会自动打开本次构建的分析页面。
|
50
|
+
|
51
|
+
```bash
|
52
|
+
RSDOCTOR=true npm run build
|
53
|
+
```
|
54
|
+
|
55
|
+
## 相关文档
|
56
|
+
|
57
|
+
更多内容请查阅 [Rsdoctor 官网文档](https://rsdoctor.dev/)。
|
@@ -229,4 +229,4 @@ Modern.js 的配置文件默认为 `modern.config.(j|t)s`,配置请查看 [mod
|
|
229
229
|
|
230
230
|
若原来项目中包含了 Babel 等配置,需要对应的写在 modern 配置中,大部分 Babel 配置已经包含进了 Modern.js。
|
231
231
|
|
232
|
-
安装完成后进行相应的[配置](/guides/
|
232
|
+
安装完成后进行相应的[配置](/guides/basic-features/debug/using-storybook#配置)。
|
@@ -0,0 +1 @@
|
|
1
|
+
["json-files", "svg-assets", "wasm-assets"]
|