@modern-js/main-doc 2.0.0-beta.3 → 2.0.0-beta.5
Sign up to get free protection for your applications and to get access to all the features.
- package/.turbo/turbo-build.log +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/commands/dev.md +8 -3
- package/en/docusaurus-plugin-content-docs/current/apis/app/commands/inspect.md +31 -10
- package/en/docusaurus-plugin-content-docs/current/apis/app/commands/serve.md +32 -0
- package/en/docusaurus-plugin-content-docs/current/apis/app/hooks/src/server.md +31 -0
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/core/bootstrap.md +4 -3
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/core/create-app.md +2 -3
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/core/use-module-apps.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/components/init-app.md +42 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/builder-plugins.md +70 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/dev/with-master-app.md +0 -1
- package/en/docusaurus-plugin-content-docs/current/configure/app/plugins.md +11 -5
- package/en/docusaurus-plugin-content-docs/current/configure/app/server/routes.md +2 -4
- package/en/docusaurus-plugin-content-docs/current/configure/app/source/disable-entry-dirs.md +38 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/source/entries.md +66 -2
- package/en/docusaurus-plugin-content-docs/current/configure/app/tools/esbuild.md +16 -39
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/css-in-js.md +38 -0
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/css-modules.md +86 -0
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/less-sass.md +17 -0
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/postcss.md +81 -0
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/css/tailwindcss.md +95 -0
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/data-fetch.md +66 -0
- package/en/docusaurus-plugin-content-docs/current/guides/basic-features/routes.md +270 -0
- package/en/docusaurus-plugin-content-docs/current/guides/concept/entries.md +116 -0
- package/en/docusaurus-plugin-content-docs/current/guides/get-started/quick-start.md +162 -0
- package/en/docusaurus-plugin-content-docs/current/guides/get-started/upgrade.md +78 -0
- package/{zh/tutorials/first-app → en/docusaurus-plugin-content-docs/current/guides}/overview.md +4 -4
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/framework-plugin/lifecycle.md +15 -0
- package/en/docusaurus-plugin-content-docs/current/tutorials/foundations/introduction.md +1 -1
- package/en/docusaurus-plugin-content-docs/current.json +11 -11
- package/package.json +4 -4
- package/zh/apis/app/commands/dev.md +9 -4
- package/zh/apis/app/commands/inspect.md +32 -11
- package/zh/apis/app/commands/new.md +1 -1
- package/zh/apis/app/commands/{start.md → serve.md} +3 -3
- package/zh/apis/app/hooks/src/index_.md +5 -4
- package/zh/apis/app/hooks/src/server.md +31 -0
- package/zh/apis/app/runtime/core/bootstrap.md +3 -4
- package/zh/apis/app/runtime/core/create-app.md +1 -18
- package/zh/apis/app/runtime/core/use-module-apps.md +64 -33
- package/zh/apis/app/runtime/web-server/hook.md +1 -1
- package/zh/apis/app/runtime/web-server/middleware.md +1 -0
- package/zh/components/debug-app.md +18 -0
- package/zh/components/default-mwa-generate.md +5 -0
- package/zh/components/deploy.md +1 -0
- package/zh/components/enable-micro-frontend.md +13 -0
- package/zh/components/global-proxy.md +28 -0
- package/zh/components/init-app.md +44 -0
- package/zh/components/micro-runtime-config.md +18 -0
- package/zh/components/prerequisites.md +19 -0
- package/zh/components/release-note.md +1 -0
- package/zh/configure/app/builder-plugins.md +72 -0
- package/zh/configure/app/deploy/_category_.json +4 -0
- package/zh/configure/app/deploy/microFrontend.md +64 -0
- package/zh/configure/app/dev/with-master-app.md +0 -2
- package/zh/configure/app/plugins.md +10 -4
- package/zh/configure/app/runtime/master-app.md +33 -36
- package/zh/configure/app/server/routes.md +2 -4
- package/zh/configure/app/source/disable-entry-dirs.md +37 -0
- package/zh/configure/app/source/entries-dir.md +0 -3
- package/zh/configure/app/source/entries.md +66 -3
- package/zh/configure/app/tools/esbuild.md +16 -39
- package/zh/guides/advanced-features/bff/bff-proxy.md +1 -1
- package/zh/guides/advanced-features/compatibility.md +14 -39
- package/zh/guides/advanced-features/eslint.md +21 -21
- package/zh/guides/advanced-features/ssg.md +20 -9
- package/zh/guides/advanced-features/ssr.md +95 -52
- package/zh/guides/advanced-features/testing.md +44 -1
- package/zh/guides/advanced-features/web-server.md +14 -3
- package/zh/guides/basic-features/css/tailwindcss.md +13 -6
- package/zh/guides/basic-features/data-fetch.md +398 -5
- package/zh/guides/basic-features/html.md +182 -0
- package/zh/guides/basic-features/mock.md +3 -9
- package/zh/guides/basic-features/proxy.md +2 -27
- package/zh/guides/basic-features/routes.md +35 -3
- package/zh/guides/concept/entries.md +108 -19
- package/zh/guides/get-started/quick-start.md +14 -83
- package/zh/guides/get-started/upgrade.md +11 -9
- package/zh/guides/{concept → topic-detail/framework-plugin}/lifecycle.md +0 -0
- package/zh/guides/topic-detail/micro-frontend/c01-introduction.md +29 -0
- package/zh/guides/topic-detail/micro-frontend/c02-development.md +191 -0
- package/zh/guides/topic-detail/micro-frontend/c03-main-app.md +246 -0
- package/zh/guides/topic-detail/micro-frontend/c04-communicate.md +54 -0
- package/zh/guides/topic-detail/micro-frontend/{mixed-stack.md → c05-mixed-stack.md} +3 -3
- package/zh/guides/topic-detail/model/quick-start.md +1 -1
- package/zh/guides/topic-detail/model/test-model.md +2 -2
- package/zh/guides/topic-detail/monorepo/create-sub-project.md +2 -2
- package/zh/guides/topic-detail/monorepo/intro.md +1 -1
- package/zh/guides/troubleshooting/dependencies.md +0 -69
- package/zh/tutorials/first-app/_category_.json +1 -1
- package/zh/tutorials/first-app/c01-start.md +99 -0
- package/zh/tutorials/first-app/{c05-component/5.1-use-ui-library.md → c02-component.md} +13 -15
- package/zh/tutorials/first-app/c03-css.md +324 -0
- package/zh/tutorials/first-app/{c08-client-side-routing/8.1-code-based-routing.md → c04-routes.md} +52 -39
- package/zh/tutorials/first-app/c05-loader.md +82 -0
- package/zh/tutorials/first-app/c06-model.md +256 -0
- package/zh/tutorials/first-app/c07-container.md +283 -0
- package/zh/tutorials/first-app/c08-entries.md +137 -0
- package/zh/tutorials/foundations/introduction.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/commands/start.md +0 -32
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/enable-modern-mode.md +0 -34
- package/zh/apis/generator/overview.md +0 -32
- package/zh/configure/app/output/enable-modern-mode.md +0 -34
- package/zh/guides/advanced-features/custom-app.md +0 -72
- package/zh/guides/topic-detail/micro-frontend/communicate.md +0 -39
- package/zh/guides/topic-detail/micro-frontend/debugging.md +0 -168
- package/zh/guides/topic-detail/micro-frontend/introduction.md +0 -13
- package/zh/guides/topic-detail/micro-frontend/route-mode.md +0 -110
- package/zh/guides/topic-detail/monorepo/deploy.md +0 -43
- package/zh/tutorials/first-app/c01-getting-started/1.1-prerequisites.md +0 -25
- package/zh/tutorials/first-app/c01-getting-started/1.2-minimal-mwa.md +0 -118
- package/zh/tutorials/first-app/c01-getting-started/1.3-dev-command.md +0 -29
- package/zh/tutorials/first-app/c01-getting-started/1.4-enable-ssr.md +0 -47
- package/zh/tutorials/first-app/c01-getting-started/1.5-start-command.md +0 -18
- package/zh/tutorials/first-app/c01-getting-started/1.6-create-repo.md +0 -31
- package/zh/tutorials/first-app/c01-getting-started/_category_.json +0 -3
- package/zh/tutorials/first-app/c02-generator-and-studio/2.1-generator.md +0 -79
- package/zh/tutorials/first-app/c02-generator-and-studio/2.2-boilerplates.md +0 -34
- package/zh/tutorials/first-app/c02-generator-and-studio/2.3-configuration.md +0 -19
- package/zh/tutorials/first-app/c02-generator-and-studio/_category_.json +0 -3
- package/zh/tutorials/first-app/c03-ide/3.1-setting-up.md +0 -55
- package/zh/tutorials/first-app/c03-ide/3.2-hints-in-ide.md +0 -60
- package/zh/tutorials/first-app/c03-ide/3.3-autofix-in-ide.md +0 -11
- package/zh/tutorials/first-app/c03-ide/3.4-autofix-in-cli.md +0 -63
- package/zh/tutorials/first-app/c03-ide/_category_.json +0 -3
- package/zh/tutorials/first-app/c04-es6-plus-and-ts/4.1-use-es6-plus.md +0 -54
- package/zh/tutorials/first-app/c04-es6-plus-and-ts/4.2-use-typescript.md +0 -135
- package/zh/tutorials/first-app/c04-es6-plus-and-ts/4.3-compatibility.md +0 -67
- package/zh/tutorials/first-app/c04-es6-plus-and-ts/_category_.json +0 -3
- package/zh/tutorials/first-app/c05-component/5.2-use-standalone-component.md +0 -72
- package/zh/tutorials/first-app/c05-component/_category_.json +0 -3
- package/zh/tutorials/first-app/c06-css-and-component/6.1-css-in-js.md +0 -110
- package/zh/tutorials/first-app/c06-css-and-component/6.2-utility-class.md +0 -143
- package/zh/tutorials/first-app/c06-css-and-component/6.3-postcss.md +0 -84
- package/zh/tutorials/first-app/c06-css-and-component/6.4-design-system.md +0 -83
- package/zh/tutorials/first-app/c06-css-and-component/6.5-storybook.md +0 -77
- package/zh/tutorials/first-app/c06-css-and-component/6.6-testing.md +0 -104
- package/zh/tutorials/first-app/c06-css-and-component/_category_.json +0 -3
- package/zh/tutorials/first-app/c07-app-entry/7.1-intro.md +0 -69
- package/zh/tutorials/first-app/c07-app-entry/7.2-add-entry-in-cli.md +0 -100
- package/zh/tutorials/first-app/c07-app-entry/7.3-manage-entries-by-hand.md +0 -69
- package/zh/tutorials/first-app/c07-app-entry/_category_.json +0 -3
- package/zh/tutorials/first-app/c08-client-side-routing/_category_.json +0 -3
- package/zh/tutorials/first-app/c09-bff/9.1-serverless.md +0 -30
- package/zh/tutorials/first-app/c09-bff/9.2-enable-bff.md +0 -95
- package/zh/tutorials/first-app/c09-bff/9.3-fetch-bff.md +0 -131
- package/zh/tutorials/first-app/c09-bff/_category_.json +0 -3
- package/zh/tutorials/first-app/c10-model/10.1-application-architecture.md +0 -21
- package/zh/tutorials/first-app/c10-model/10.2-add-model.md +0 -185
- package/zh/tutorials/first-app/c10-model/10.3-use-model.md +0 -55
- package/zh/tutorials/first-app/c10-model/10.4-testing.md +0 -69
- package/zh/tutorials/first-app/c10-model/_category_.json +0 -3
- package/zh/tutorials/first-app/c11-container/11.1-use-model-with-app-state.md +0 -240
- package/zh/tutorials/first-app/c11-container/11.2-add-container.md +0 -109
- package/zh/tutorials/first-app/c11-container/11.3-use-loader.md +0 -63
- package/zh/tutorials/first-app/c11-container/11.4-testing.md +0 -56
- package/zh/tutorials/first-app/c11-container/_category_.json +0 -3
@@ -0,0 +1,86 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 5
|
3
|
+
---
|
4
|
+
|
5
|
+
# CSS Modules
|
6
|
+
|
7
|
+
Modern.js out of the box support for [CSS Modules](https://github.com/css-modules/css-modules).
|
8
|
+
|
9
|
+
## File Suffix Form CSS Modules
|
10
|
+
|
11
|
+
By default, files ending in `.module.(css|scss|sass|less)` are treated as CSS Modules files, for example:
|
12
|
+
|
13
|
+
```css title="button.module.css"
|
14
|
+
.redColor {
|
15
|
+
color: red;
|
16
|
+
}
|
17
|
+
```
|
18
|
+
|
19
|
+
```js title="Button.jsx"
|
20
|
+
import styles from './button.module.css';
|
21
|
+
|
22
|
+
export default function Button() {
|
23
|
+
return (
|
24
|
+
<button type="button" className={styles.redColor}>
|
25
|
+
red button
|
26
|
+
</button>
|
27
|
+
);
|
28
|
+
}
|
29
|
+
```
|
30
|
+
|
31
|
+
Will eventually be compiled as:
|
32
|
+
|
33
|
+
```js
|
34
|
+
<button type="button" className="button_redColor__1-RBg">
|
35
|
+
red button
|
36
|
+
</button>;
|
37
|
+
```
|
38
|
+
|
39
|
+
## Global CSS Modules
|
40
|
+
|
41
|
+
If you want to remove the `.module` suffix from the filename, you can set [`output.disable CssModuleExtension`](/docs/configure/app/output/disable-css-module-extension).
|
42
|
+
|
43
|
+
After setting, all style files except the style files in the `node_modules/` directory and the file name format of `[name].global.(css|scss|sass|less)` will be processed as CSS Modules.
|
44
|
+
|
45
|
+
If you need global styles at this point, you can solve it by creating a style file with the filename format `[name].global.(css|scss|sass|less)`, for example:
|
46
|
+
|
47
|
+
```css title="app.global.css"
|
48
|
+
.bg-blue {
|
49
|
+
background-color: blue;
|
50
|
+
}
|
51
|
+
```
|
52
|
+
|
53
|
+
```css title="button.css"
|
54
|
+
.redColor {
|
55
|
+
color: red;
|
56
|
+
}
|
57
|
+
```
|
58
|
+
|
59
|
+
```js title="App.jsx"
|
60
|
+
import './app.global.css';
|
61
|
+
import styles from './button.css';
|
62
|
+
|
63
|
+
export default function Button() {
|
64
|
+
return (
|
65
|
+
<button type="button" className={`${styles.redColor} bg-blue`}>
|
66
|
+
button
|
67
|
+
</button>
|
68
|
+
);
|
69
|
+
}
|
70
|
+
```
|
71
|
+
|
72
|
+
Will eventually be compiled as:
|
73
|
+
|
74
|
+
```js
|
75
|
+
<button type="button" className="button__redColor--JsFYl bg-blue">
|
76
|
+
button
|
77
|
+
</button>;
|
78
|
+
```
|
79
|
+
|
80
|
+
The final effect is as follows:
|
81
|
+
|
82
|
+

|
83
|
+
|
84
|
+
:::tip
|
85
|
+
When using [babel-plugin-react-css-modules](https://github.com/gajus/babel-plugin-react-css-modules), it is important to note that the configuration option `generateScopedName` of this plugin needs to be the same as [`output.css ModuleLocalIdentName`](/docs/configure/app/output/css-module-local-ident-name).
|
86
|
+
:::
|
@@ -0,0 +1,17 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 4
|
3
|
+
---
|
4
|
+
|
5
|
+
# Less and Sass
|
6
|
+
|
7
|
+
[Less](https://lesscss.org/) and [Sass](https://sass-lang.com/) are two commonly used CSS preprocessors that Modern.js built-in support for the Less and Sass compile capabilities.
|
8
|
+
|
9
|
+
## Customized Configuration
|
10
|
+
|
11
|
+
- If you need to customize the configuration of [less-loader](https://github.com/webpack-contrib/less-loader), please refer to the [tools.less](/docs/configure/app/tools/less).
|
12
|
+
- If you need to customize the configuration of [sass-loader](https://github.com/webpack-contrib/sass-loader), please refer to the [tools.less](/docs/configure/app/tools/sass).
|
13
|
+
|
14
|
+
|
15
|
+
::: Tips
|
16
|
+
CSS files pre-compiled by Less and Sass will still undergo Modern.js build-in [PostCSS](https://postcss.org/) conversion, which has good browser compatibility. For related content, please refer to [[PostCSS](/docs/guides/basic-features/css/postcss)].
|
17
|
+
:::
|
@@ -0,0 +1,81 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 3
|
3
|
+
---
|
4
|
+
|
5
|
+
# PostCSS
|
6
|
+
|
7
|
+
[PostCSS](https://postcss.org/) is a tool for converting CSS code with JavaScript tools and plugins. Modern.js built-in PostCSS and integrates common PostCSS plugins such as [Autoprefixer](https://github.com/postcss/autoprefixer) to meet the style development needs of most projects.
|
8
|
+
|
9
|
+
By default, Modern.js compile and transform CSS as follows:
|
10
|
+
|
11
|
+
1. [Autoprefixer](https://github.com/postcss/autoprefixer) Automatically add the required browser vendor prefix to CSS rules according to the required browser range. Modern.js default supported browser ranges are: `['> 0.01%', 'not dead', 'not op_mini all']`.
|
12
|
+
|
13
|
+
:::info Note
|
14
|
+
- [Supported browser range: `> 0.01%`] means that the browser market share is greater than 0.01%.
|
15
|
+
- `Not dead` means excluding browsers that are no longer officially supported and browsers that have not been updated in the past 24 months.
|
16
|
+
- `not op_mini all` means exclude Opera Mini.
|
17
|
+
:::
|
18
|
+
|
19
|
+
|
20
|
+
:::info Additional
|
21
|
+
If you need to modify the default browser support range, you can configure `browserslist` in the project's `package.json` file, and set the rule to refer to the use of [Browserslist](https://github.com/browserslist/browserslist). The following is an example:
|
22
|
+
```json title ="package.json"
|
23
|
+
{
|
24
|
+
"Browserslist": [
|
25
|
+
"The last 1 versions",
|
26
|
+
"> 1%",
|
27
|
+
"IE 10"
|
28
|
+
]
|
29
|
+
}
|
30
|
+
```
|
31
|
+
:::
|
32
|
+
|
33
|
+
2. Provide [CSS custom properties](https://www.w3.org/TR/css-variables-1/) support, you can define and use custom variables in CSS, such as:
|
34
|
+
|
35
|
+
```css
|
36
|
+
:root {
|
37
|
+
--main-bg-color: pink;
|
38
|
+
}
|
39
|
+
|
40
|
+
body {
|
41
|
+
background-color: var(--main-bg-color);
|
42
|
+
}
|
43
|
+
```
|
44
|
+
3. Provide [CSS Nesting](https://drafts.csswg.org/css-nesting-1/) support, you can use nested writing in CSS, such as:
|
45
|
+
|
46
|
+
```css
|
47
|
+
table.colortable td {
|
48
|
+
text-align: center;
|
49
|
+
}
|
50
|
+
table.colortable td.c {
|
51
|
+
text-transform: uppercase;
|
52
|
+
}
|
53
|
+
```
|
54
|
+
It can also be rewritten as CSS nested:
|
55
|
+
```css
|
56
|
+
table.colortable {
|
57
|
+
& td {
|
58
|
+
text-align: center;
|
59
|
+
&.c { text-transform: uppercase }
|
60
|
+
}
|
61
|
+
}
|
62
|
+
```
|
63
|
+
|
64
|
+
4. Fix known [Flexbugs](https://github.com/philipwalton/flexbugs).
|
65
|
+
5. Provide compatibility with the following CSS features:
|
66
|
+
- [`initial` attribute value](https://developer.mozilla.org/en-US/docs/Web/CSS/initial_value)
|
67
|
+
- [`break-` attribute](https://developer.mozilla.org/en-US/docs/Web/CSS/break-after)
|
68
|
+
- [`font-variant`](https://developer.mozilla.org/en-US/docs/Web/CSS/font-variant)
|
69
|
+
- [Media Query Ranges](https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries#syntax_improvements_in_level_4)
|
70
|
+
|
71
|
+
When you need to modify the PostCSS configuration, you can implement it through the underlying configuration [`tools.postcss`](/docs/configure/app/tools/postcss), here is an example:
|
72
|
+
|
73
|
+
```typescript title="modern.config.ts"
|
74
|
+
export default defineConfig({
|
75
|
+
tools: {
|
76
|
+
postcss: {
|
77
|
+
plugins: ['autoprefixer', ('postcss-flexbugs-fixes': {})],
|
78
|
+
},
|
79
|
+
},
|
80
|
+
});
|
81
|
+
```
|
@@ -0,0 +1,95 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 2
|
3
|
+
---
|
4
|
+
|
5
|
+
# Tailwind CSS
|
6
|
+
|
7
|
+
|
8
|
+
|
9
|
+
[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. To use [Tailwind CSS](https://tailwindcss.com/) in the Modern.js, just execute `pnpm run new` in the project root directory and turn it on.
|
10
|
+
|
11
|
+
Choose as follows:
|
12
|
+
|
13
|
+
```bash
|
14
|
+
? Action: Enable features
|
15
|
+
? Enable features: Enable Tailwind CSS
|
16
|
+
```
|
17
|
+
|
18
|
+
When using, add the following code to the root component of the entry (such as `src/App.jsx`):
|
19
|
+
|
20
|
+
```js
|
21
|
+
import 'tailwindcss/base.css';
|
22
|
+
import 'tailwindcss/components.css';
|
23
|
+
import 'tailwindcss/utilities.css';
|
24
|
+
```
|
25
|
+
|
26
|
+
You can then use the Utility Class provided by Tailwind CSS in each component.
|
27
|
+
|
28
|
+
::: info Additional
|
29
|
+
According to different needs, you can optionally import the CSS files provided by Tailwind CSS. Since the use of `@taiwind` is equivalent to directly importing CSS files, you can refer to the content in the annotate in the [`@tailwind` usage](https://tailwindcss.com/docs/functions-and-directives#tailwind) document for the purpose of the CSS files provided by Tailwind CSS.
|
30
|
+
:::
|
31
|
+
|
32
|
+
When you need to customize the [theme](https://tailwindcss.com/docs/theme) configuration of Tailwind CSS, you can modify it in the configuration [`source.designSystem`](/docs/configure/app/source/design-system), for example, add a color theme `primary`:
|
33
|
+
|
34
|
+
```typescript title="modern.config.ts"
|
35
|
+
export default defineConfig({
|
36
|
+
source: {
|
37
|
+
designSystem: {
|
38
|
+
extend: {
|
39
|
+
colors: {
|
40
|
+
primary: '#5c6ac4',
|
41
|
+
},
|
42
|
+
},
|
43
|
+
},
|
44
|
+
},
|
45
|
+
});
|
46
|
+
```
|
47
|
+
|
48
|
+
When you need special configuration for Tailwind CSS other than [theme](https://tailwindcss.com/docs/theme), you can configure it in [`tools.tailwindcss`](/docs/configure/app/tools/tailwindcss), for example setting `variants`:
|
49
|
+
|
50
|
+
```typescript title="modern.config.ts"
|
51
|
+
export default defineConfig({
|
52
|
+
tools: {
|
53
|
+
tailwindcss: {
|
54
|
+
variants: {
|
55
|
+
extend: {
|
56
|
+
backgroundColor: ['active'],
|
57
|
+
},
|
58
|
+
},
|
59
|
+
},
|
60
|
+
},
|
61
|
+
});
|
62
|
+
```
|
63
|
+
|
64
|
+
## Use the [`Twin`](https://github.com/ben-rogerson/twin.macro)
|
65
|
+
|
66
|
+
In the previous chapter, we introduced what CSS-in-JS is and the CSS-in-JS library [styled-components](https://styled-components.com/) commonly used by the community. This section will explain how to use [Tailwind CSS](https://tailwindcss.com/) in CSS with [`Twin`](https://github.com/ben-rogerson/twin.macro). Using [`Twin`](https://github.com/ben-rogerson/twin.macro) makes it easier to use Tailwind CSS in CSS-in-JS code. [`Twin`](https://github.com/ben-rogerson/twin.macro) for its own description is:
|
67
|
+
|
68
|
+
> *Twin blends the magic of Tailwind with the flexibility of css-in-js*
|
69
|
+
|
70
|
+
After enabling the "Tailwind CSS" function, you first need to install the ['Twin'](https://github.com/ben-rogerson/twin.macro) dependency:
|
71
|
+
|
72
|
+
``` bash
|
73
|
+
pnpm add twin.macro -D
|
74
|
+
```
|
75
|
+
|
76
|
+
When the project installs the `twin.macro` dependency, Modern.js detects the dependency and adds twin.macro related configuration to the `baby-plugin-macro` of build-in. So after installing the dependency, there is no need for manual configuration. Here is an example of a simple use of twin.macro:
|
77
|
+
|
78
|
+
``` js
|
79
|
+
import tw from 'twin.macro'
|
80
|
+
|
81
|
+
const Input = tw.input`border hover:border-black`
|
82
|
+
```
|
83
|
+
|
84
|
+
:::tip
|
85
|
+
If a `MacroError: /project/App.tsx` error occurs during runtime, it may be caused by a lack of `twin.macro` dependency.
|
86
|
+
:::
|
87
|
+
|
88
|
+
For more usage, please refer to the [twin.macro](https://github.com/ben-rogerson/twin.macro/blob/master/docs/index.md).
|
89
|
+
|
90
|
+
`twin.macro` reads the `tailwindcss.config.js` files in the project directory by default, or reads the Tailwind CSS configuration via the file path specified by [twin.config](https://github.com/ben-rogerson/twin.macro/blob/master/docs/options.md#options) on the `babel-plugin-macro`. However, these additional configurations are not required in the Modern.js.
|
91
|
+
|
92
|
+
When the Tailwind CSS is configured in the `modern.config.ts` file by [`source.designSystem`](/docs/configure/app/source/design-system) and [`tools.tailwindcss`](/docs/configure/app/tools/tailwindcss), these configurations will also take effect on the `twin.macro`.
|
93
|
+
|
94
|
+
> When configuring Tailwind CSS for a project, the combination of the two configurations [`source.designSystem`](/docs/configure/app/source/design-system) and [`tools.tailwindcss`](/docs/configure/app/tools/tailwindcss) is equivalent to a separate configuration `tailwindcss.config.js` file.
|
95
|
+
> Where [`source.designSystem`](/docs/configure/app/source/design-system) is equivalent to the [`theme`](https://v2.tailwindcss.com/docs/configuration#theme) configuration of Tailwind CSS.
|
@@ -0,0 +1,66 @@
|
|
1
|
+
---
|
2
|
+
title: Fetch Data
|
3
|
+
sidebar_position: 3
|
4
|
+
---
|
5
|
+
|
6
|
+
Modern.js provides out of the box fetching data capabilities, developers can use these APIs to develop in CSR and SSR environments isomorphic.
|
7
|
+
|
8
|
+
It should be noted that these APIs do not help applications to initiate requests, but help developers better manage the relationship between data and routing.
|
9
|
+
|
10
|
+
## useLoader(Old)
|
11
|
+
|
12
|
+
**`useLoader`** is an API in Modern.js old version. The API is a React Hook specially provided for SSR applications, allowing developers to fetch data in components.
|
13
|
+
|
14
|
+
:::tip
|
15
|
+
CSR don't need to use `useLoader` to fetch data.
|
16
|
+
:::
|
17
|
+
|
18
|
+
Here is the simplest example:
|
19
|
+
|
20
|
+
```tsx
|
21
|
+
import { useLoader } from '@modern-js/runtime';
|
22
|
+
|
23
|
+
export default () => {
|
24
|
+
const { data } = useLoader(async () => {
|
25
|
+
console.log('fetch in useLoader');
|
26
|
+
|
27
|
+
// No real request is sent here, just a hard coding data is returned.
|
28
|
+
// In a real project, the data obtained from the remote end should be returned.
|
29
|
+
return {
|
30
|
+
name: 'modern.js',
|
31
|
+
};
|
32
|
+
});
|
33
|
+
|
34
|
+
return <div>Hello, {data?.name}</div>;
|
35
|
+
};
|
36
|
+
```
|
37
|
+
|
38
|
+
After the above code starts, visit the page. You can see that the log is printed at terminal, but not at console in browser.
|
39
|
+
|
40
|
+
This is because Modern.js server-side rendering, the data returned by the `useLoader` is collected and injected into the HTML of the response. If SSR rendering succeeds, the following code snippet can be seen in the HTML:
|
41
|
+
|
42
|
+
```html
|
43
|
+
<script>
|
44
|
+
window._SSR_DATA = {};
|
45
|
+
</script>
|
46
|
+
```
|
47
|
+
|
48
|
+
In this global variable, every piece of data is recorded, and this data will be used first in the process of rendering on the browser side. If the data does not exist, the `useLoader` function will be re-executed.
|
49
|
+
|
50
|
+
:::note
|
51
|
+
During the build phase, Modern.js will automatically generate a Loader ID for each `useLoader` and inject it into the JS bundle of SSR and CSR, which is used to associate Loader and data.
|
52
|
+
:::
|
53
|
+
|
54
|
+
Compared with `getServerSideProps` in the Next.js, get data in advance before rendering. Using `useLoader`, you can get the data required by the local UI in the component without passing the data layer by layer. Similarly, it will not add redundant logic to the outermost data acquisition function because different routes require different data requests. Of course, `useLoader` also has some problems, such as the difficulty of Treeshaking server-level code, and the need for one more pre-render at the server level.
|
55
|
+
|
56
|
+
Modern.js in the new version, a new Loader solution is designed. The new solution solves these problems and can cooperate with **nested routing** to optimize page performance.
|
57
|
+
|
58
|
+
:::note
|
59
|
+
Detailed APIs can be found at [useLoader](/docs/apis/app/runtime/core/use-loader).
|
60
|
+
:::
|
61
|
+
|
62
|
+
## Route Loader
|
63
|
+
|
64
|
+
:::note
|
65
|
+
Stay tuned.
|
66
|
+
:::
|
@@ -0,0 +1,270 @@
|
|
1
|
+
---
|
2
|
+
title: Routes
|
3
|
+
sidebar_position: 1
|
4
|
+
---
|
5
|
+
|
6
|
+
Modern.js build-in provides partial support for [React Router 6](https://reactrouter.com/en/main) and provides various types of routing modes. According to different [entry](/docs/guides/concept/entries) types, routing is divided into three modes, namely **Conventional routing**, **Self-controlled routing** and **Other**.
|
7
|
+
|
8
|
+
:::note
|
9
|
+
The routes mentioned in this section are client routes, that is, SPA routes.
|
10
|
+
:::
|
11
|
+
|
12
|
+
## Conventional routing
|
13
|
+
|
14
|
+
With `routes/` as the agreed entry, Modern.js will automatically generate the corresponding routing structure based on the file system.
|
15
|
+
|
16
|
+
Modern.js supports the popular convention routing mode in the industry: **nested routing**. When using nested routing, the routing of the page corresponds the UI structure, and we will introduce this routing mode in detail.
|
17
|
+
|
18
|
+
|
19
|
+
```
|
20
|
+
/user/johnny/profile /user/johnny/posts
|
21
|
+
+------------------+ +-----------------+
|
22
|
+
| User | | User |
|
23
|
+
| +--------------+ | | +-------------+ |
|
24
|
+
| | Profile | | +------------> | | Posts | |
|
25
|
+
| | | | | | | |
|
26
|
+
| +--------------+ | | +-------------+ |
|
27
|
+
+------------------+ +-----------------+
|
28
|
+
```
|
29
|
+
|
30
|
+
### Routing file convention
|
31
|
+
|
32
|
+
There are two file conventions in the `routes/` directory `layout.[jt]sx` and `page.[jt]sx`(abbreviated as `.tsx` later). These two files determine the layout hierarchy of the application, where `layout.tsx` is used as the layout component, and `page.tsx` is used as the content component, which is the leaf node of the entire routing table.
|
33
|
+
|
34
|
+
For example, here `routes/layout.tsx` will be used as the layout component of all components under the `/` route:
|
35
|
+
|
36
|
+
```bash
|
37
|
+
.
|
38
|
+
└── routes
|
39
|
+
├── layout.tsx
|
40
|
+
├── page.tsx
|
41
|
+
└── user
|
42
|
+
├── layout.tsx
|
43
|
+
└── page.tsx
|
44
|
+
```
|
45
|
+
|
46
|
+
When the route is `/`, there will be the following UI layout:
|
47
|
+
|
48
|
+
```tsx
|
49
|
+
<Layout>
|
50
|
+
<Page />
|
51
|
+
</Layout>
|
52
|
+
```
|
53
|
+
|
54
|
+
Similarly, `routes/user/layout.tsx` will be used as a layout component for all components under the `/user` route. When the route is `/user`, the following UI layout will be available:
|
55
|
+
|
56
|
+
```tsx
|
57
|
+
<Layout>
|
58
|
+
<UserLayout>
|
59
|
+
<UserPage>
|
60
|
+
<UserLayout>
|
61
|
+
</Layout>
|
62
|
+
```
|
63
|
+
|
64
|
+
#### Layout
|
65
|
+
|
66
|
+
`<Layout>` component refers to all `layout.tsx` files in the `routes/` directory, which represent the layout of the corresponding route segment, and use `<Outlet>` to represent sub-components.
|
67
|
+
|
68
|
+
:::note
|
69
|
+
`<Outlet>` is a new API in React Router 6, see [Outlet](https://reactrouter.com/en/main/components/outlet#outlet) for details.
|
70
|
+
:::
|
71
|
+
|
72
|
+
In order to facilitate the introduction of the relationship between `<Layout>` and `<Outlet>`, the following file directory example:
|
73
|
+
|
74
|
+
```bash
|
75
|
+
.
|
76
|
+
└── routes
|
77
|
+
├── blog
|
78
|
+
│ └── page.tsx
|
79
|
+
├── layout.tsx
|
80
|
+
├── page.tsx
|
81
|
+
└── user
|
82
|
+
├── layout.tsx
|
83
|
+
└── page.tsx
|
84
|
+
```
|
85
|
+
|
86
|
+
1. When the route is `/`, `<Outlet>` in `routes/layout.tsx` represents the component exported in `routes/page.tsx`, generating the following UI structure:
|
87
|
+
|
88
|
+
```tsx
|
89
|
+
<Layout>
|
90
|
+
<Page />
|
91
|
+
</Layout>
|
92
|
+
```
|
93
|
+
|
94
|
+
2. When the route is `/blog`, `<Outlet>` in `routes/layout.tsx` represents the component exported in `routes/blog/page.tsx`, generating the following UI structure:
|
95
|
+
|
96
|
+
```tsx
|
97
|
+
<Layout>
|
98
|
+
<BlogPage />
|
99
|
+
</Layout>
|
100
|
+
```
|
101
|
+
|
102
|
+
3. When the route is `/user`, `<Outlet>` in `routes/layout.tsx` represents the component exported in `routes/user/layout.tsx`. `<Outlet>` in `routes/user/layout.tsx` represents the component exported in `routes/user/page.tsx`. Generate the following UI structure:
|
103
|
+
|
104
|
+
```tsx
|
105
|
+
<Layout>
|
106
|
+
<UserLayout>
|
107
|
+
<UserPage>
|
108
|
+
<UserLayout>
|
109
|
+
</Layout>
|
110
|
+
```
|
111
|
+
|
112
|
+
In summary, if there is a `layout.tsx` in the file directory of the subroute, the `<Outlet>` in the previous `layout.tsx` is the `layout.tsx` under the file directory of the subroute, otherwise it is the `page.tsx` under the file directory of the subroute.
|
113
|
+
|
114
|
+
#### Page
|
115
|
+
|
116
|
+
All routes should end with the `<Page>` component. In the `page.tsx` file, if the developer introduces the `<Outlet>` component, it will have no effect.
|
117
|
+
|
118
|
+
### Dynamic routing
|
119
|
+
|
120
|
+
With a file directory named `[]`, the generated route will be used as a dynamic route. For example the following file directory:
|
121
|
+
|
122
|
+
```
|
123
|
+
└── routes
|
124
|
+
├── [id]
|
125
|
+
│ └── page.tsx
|
126
|
+
├── blog
|
127
|
+
│ └── page.tsx
|
128
|
+
└── page.tsx
|
129
|
+
```
|
130
|
+
|
131
|
+
The `routes/[id]/page.tsx` file is converted to the `/:id` route. Except for the `/blog` route that exactly matches, all other `/xxx` will match this route.
|
132
|
+
|
133
|
+
In component, you can get the corresponding named parameters through [useParams](/docs/apis/app/runtime/router/#useparams).
|
134
|
+
|
135
|
+
### Layout with No Path
|
136
|
+
|
137
|
+
When a directory name begins with `__`, the corresponding directory name is not converted to the actual routing path, such as the following file directory:
|
138
|
+
|
139
|
+
```
|
140
|
+
.
|
141
|
+
└── routes
|
142
|
+
├── __auth
|
143
|
+
│ ├── layout.tsx
|
144
|
+
│ ├── login
|
145
|
+
│ │ └── page.tsx
|
146
|
+
│ └── signup
|
147
|
+
│ └── page.tsx
|
148
|
+
├── layout.tsx
|
149
|
+
└── page.tsx
|
150
|
+
```
|
151
|
+
|
152
|
+
Modern.js will generate two routes, `/login` and `/sign`, `__auth/layout.tsx` component will be used as the layout component of `login/page.tsx` and `signup/page.tsx`, but __auth will not be used as the route path fragment.
|
153
|
+
|
154
|
+
|
155
|
+
This feature is useful when you need to do separate layouts for certain types of routes, or when you want to categorize routes.
|
156
|
+
|
157
|
+
### No Layout
|
158
|
+
|
159
|
+
In some cases, the project needs more sophisticated routes, but these routes do not have independent UI layouts. If you create a route like a normal file directory, the directory level will be deeper.
|
160
|
+
|
161
|
+
Therefore Modern.js supports splitting routing fragments by `.` instead of file directory. For example, when you need `/user/profile/2022/edit`, you can directly create the following file:
|
162
|
+
|
163
|
+
```
|
164
|
+
└── routes
|
165
|
+
├── user.profile.[id].edit
|
166
|
+
│ └── page.tsx
|
167
|
+
├── layout.tsx
|
168
|
+
└── page.tsx
|
169
|
+
```
|
170
|
+
|
171
|
+
When accessing a route, you will get the following UI layout:
|
172
|
+
|
173
|
+
```tsx
|
174
|
+
<RootLayout>
|
175
|
+
<UserProfileEdit /> // routes/user.profile.[id].edit/page.tsx
|
176
|
+
</RootLayout>
|
177
|
+
```
|
178
|
+
|
179
|
+
### Loading
|
180
|
+
|
181
|
+
In each layer directory under `routes/`, developers can create `loading.tsx` files and export a `<Loading>` component by default.
|
182
|
+
|
183
|
+
When the component exists in the routing directory, all routing switches under this level of subrouting will use the `<Loading>` component as the Fallback UI when JS Chunk is loaded. When the `layout.tsx` file is not defined in this directory, the `<Loading>` component will not take effect. For example, the following file directory:
|
184
|
+
|
185
|
+
```bash
|
186
|
+
.
|
187
|
+
└── routes
|
188
|
+
├── blog
|
189
|
+
│ ├── [id]
|
190
|
+
│ │ └── page.tsx
|
191
|
+
│ └── page.tsx
|
192
|
+
├── layout.tsx
|
193
|
+
├── loading.tsx
|
194
|
+
└── page.tsx
|
195
|
+
```
|
196
|
+
|
197
|
+
When a route jumps from `/` to `/blog`, if the JS Chunk of the `<Blog>` component is not loaded, the component UI exported in `loading.tsx` will be displayed first. But when jumping from `/blog` to `/blog/20220101`, it will not be displayed.
|
198
|
+
|
199
|
+
### ErrorBoundary
|
200
|
+
|
201
|
+
In each layer directory under `routes/`, the developer can also define a `error.tsx` file, and export a `<ErrorBoundary>` component by default.
|
202
|
+
|
203
|
+
When the component exists in a routing directory, the component render error is caught by the `ErrorBoundary` component. The `<ErrorBoundary>` component does not take effect when the directory does not have a `layout.tsx` file defined.
|
204
|
+
|
205
|
+
`<ErrorBoundary>` can return the UI view when the error occurred. When the `<ErrorBoundary>` component is not declared at the current level, the error will bubble up to the higher component until it is caught or throws an error. At the same time, when a component fails, it will only affect the routed component and sub-component that caught the error. The state and view of other components are not affected and can continue to interact.
|
206
|
+
|
207
|
+
<!-- Todo API 路由-->
|
208
|
+
Within the `<ErrorBoundary>` component, you can use [useRouteError](/docs/apis/app/runtime/router/#useparams) to get the specific information of the error:
|
209
|
+
|
210
|
+
```tsx
|
211
|
+
import { useRouteError } from '@modern-js/runtime/router';
|
212
|
+
export default const ErrorBoundary = () => {
|
213
|
+
const error = useRouteError();
|
214
|
+
return (
|
215
|
+
<div>
|
216
|
+
<h1>{error.status}</h1>
|
217
|
+
<h2>{error.message}</h2>
|
218
|
+
</div>
|
219
|
+
)
|
220
|
+
}
|
221
|
+
```
|
222
|
+
|
223
|
+
## Self-controlled routing
|
224
|
+
|
225
|
+
With `src/App.tsx` as the agreed entry, Modern.js will not do additional operations with multiple routes, developers can use the React Router 6 API for development by themselves, for example:
|
226
|
+
|
227
|
+
```tsx
|
228
|
+
import { Route, Routes, BrowserRouter } from '@modern-js/runtime/router';
|
229
|
+
import { StaticRouter } from '@modern-js/runtime/router/server';
|
230
|
+
|
231
|
+
const Router = typeof window === 'undefined' ? StaticRouter : BrowserRouter;
|
232
|
+
export default () => {
|
233
|
+
return (
|
234
|
+
<Router location={context.request.pathname}>
|
235
|
+
<Routes>
|
236
|
+
<Route index element={<div>index</div>} />
|
237
|
+
<Route path="about" element={<div>about</div>} />
|
238
|
+
</Routes>
|
239
|
+
</Router>
|
240
|
+
);
|
241
|
+
};
|
242
|
+
```
|
243
|
+
|
244
|
+
:::note
|
245
|
+
Under self-controlled routing, if developers want to use the [Loader API](https://reactrouter.com/en/main/hooks/use-loader-data#useloaderdata) capabilities in React Router 6 in SSR will be relatively complicated, it is recommended to use conventional routing directly. Modern.js has already encapsulated everything for you.
|
246
|
+
<!-- Todo 嵌套路由带来的优化可以补充下文档-->
|
247
|
+
If the project only wants to upgrade to React Router 6 and does not want to use the optimizations brought by nested routing, then [useLoader](/docs/apis/app/runtime/core/use-loader) will still work under SSR.
|
248
|
+
:::
|
249
|
+
|
250
|
+
## Other
|
251
|
+
|
252
|
+
By default, Modern.js turn on the built-in routing scheme, React Router.
|
253
|
+
|
254
|
+
```js
|
255
|
+
export default defineConfig({
|
256
|
+
runtime: {
|
257
|
+
router: true,
|
258
|
+
}
|
259
|
+
})
|
260
|
+
```
|
261
|
+
|
262
|
+
Modern.js exposes the React Router API from the `@modern-js/runtime/router` namespace for developers to use, ensuring that developers and Modern.js use the same code. In addition, in this case, the React Router code will be packaged into JS products. If the project already has its own routing scheme, or does not need to use client routing, this feature can be turned off.
|
263
|
+
|
264
|
+
```js
|
265
|
+
export default defineConfig({
|
266
|
+
runtime: {
|
267
|
+
router: false,
|
268
|
+
}
|
269
|
+
})
|
270
|
+
```
|