@modern-js/main-doc 2.41.0 → 2.42.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (28) hide show
  1. package/docs/en/apis/app/commands.mdx +3 -1
  2. package/docs/en/configure/app/output/enable-inline-route-manifests.mdx +14 -4
  3. package/docs/en/configure/app/tools/bundler-chain.mdx +0 -4
  4. package/docs/en/guides/advanced-features/build-performance.mdx +156 -0
  5. package/docs/en/guides/advanced-features/optimize-bundle.mdx +115 -0
  6. package/docs/en/guides/advanced-features/rspack-start.mdx +1 -1
  7. package/docs/en/guides/advanced-features/source-build.mdx +161 -0
  8. package/docs/en/guides/advanced-features/testing.mdx +2 -2
  9. package/docs/en/guides/advanced-features/using-storybook.mdx +1 -1
  10. package/docs/en/guides/advanced-features/web-server.mdx +1 -1
  11. package/docs/en/guides/get-started/tech-stack.mdx +1 -1
  12. package/docs/en/guides/troubleshooting/builder.mdx +550 -21
  13. package/docs/en/guides/troubleshooting/hmr.mdx +148 -0
  14. package/docs/zh/apis/app/commands.mdx +3 -1
  15. package/docs/zh/configure/app/output/enable-inline-route-manifests.mdx +6 -6
  16. package/docs/zh/configure/app/tools/bundler-chain.mdx +0 -4
  17. package/docs/zh/guides/advanced-features/build-performance.mdx +156 -0
  18. package/docs/zh/guides/advanced-features/optimize-bundle.mdx +115 -0
  19. package/docs/zh/guides/advanced-features/rspack-start.mdx +1 -1
  20. package/docs/zh/guides/advanced-features/source-build.mdx +161 -0
  21. package/docs/zh/guides/advanced-features/testing.mdx +1 -1
  22. package/docs/zh/guides/advanced-features/using-storybook.mdx +1 -1
  23. package/docs/zh/guides/advanced-features/web-server.mdx +1 -1
  24. package/docs/zh/guides/get-started/tech-stack.mdx +1 -1
  25. package/docs/zh/guides/topic-detail/generator/plugin/structure.md +1 -1
  26. package/docs/zh/guides/troubleshooting/builder.mdx +553 -20
  27. package/docs/zh/guides/troubleshooting/hmr.mdx +148 -0
  28. package/package.json +7 -7
@@ -276,7 +276,9 @@ Normally, only the part of the code modified by this commit needs to be checked
276
276
  Usage: modern test [options]
277
277
 
278
278
  Options:
279
- -h, --help show command help
279
+ -u --updateSnapshot use this flag to re-record snapshots.
280
+ --watch watch files for changes and rerun tests related to changed files.
281
+ -h, --help show command help
280
282
  ```
281
283
 
282
284
  :::tip
@@ -1,10 +1,20 @@
1
1
  ---
2
- sidebar_label: enableInlineRouteManifests
2
+ sidebar_label: disableInlineRouteManifests
3
3
  ---
4
4
 
5
- # output.enableInlineRouteManifests
5
+ # output.disableInlineRouteManifests
6
6
 
7
7
  - **Type:** `boolean`
8
- - **Default:** `true`
8
+ - **Default:** `false`
9
9
 
10
- When using convention-based routing, the framework injects routing information into the client for optimization purposes. By default, routing information is injected into the html, but when this is configured to `false`, routing information is injected into a separate JS file.
10
+ When using convention-based routing, the framework injects routing information into the client for optimization purposes. By default, routing information is injected into the html, but when this is configured to `true`, routing information is injected into a separate JS file.
11
+
12
+ Example:
13
+
14
+ ```ts
15
+ export default {
16
+ output: {
17
+ disableInlineRouteManifests: true,
18
+ },
19
+ };
20
+ ```
@@ -4,10 +4,6 @@ sidebar_label: bundlerChain
4
4
 
5
5
  # tools.bundlerChain
6
6
 
7
- :::tip
8
- This config is provided by Modern.js Builder, more detail can see [tools.bundlerChain](https://modernjs.dev/builder/en/api/config-tools.html#toolsbundlerchain).
9
- :::
10
-
11
7
  import Main from '@modern-js/builder-doc/docs/en/config/tools/bundlerChain.mdx';
12
8
 
13
9
  <Main />
@@ -0,0 +1,156 @@
1
+ ---
2
+ sidebar_position: 12
3
+ ---
4
+
5
+ # Improve Build Performance
6
+
7
+ Modern.js optimizes build performance by default, but as the project becomes larger, you may encounter some build performance problems.
8
+
9
+ This document provides some optional speed-up strategies, developers can choose some of them to improve the build performance.
10
+
11
+ :::tip 📢 Notice
12
+ The strategies in [Bundle Size Optimization](/guides/advanced-features/optimize-bundle.html) can also be used to improve build performance, so we won't repeat them here.
13
+ :::
14
+
15
+ ## General optimization strategy
16
+
17
+ The following are some general optimization strategies, which can speed up the development build and production build, and some of them also optimize the bundle size.
18
+
19
+ ### Upgrade Node.js version
20
+
21
+ In general, updating Node.js to the latest [LTS release](https://github.com/nodejs/release#release-schedule) will help improve build performance.
22
+
23
+ Especially for devices with Apple M1/M2 chips, it is recommended to use Node 18.
24
+
25
+ Node >= 16 provides Apple Silicon binaries by default, so the performance on M1/M2 models will be greatly improved than Node 14. According to our tests, **After switching from Node 14 to Node >= 16, the compilation speed can be improved by more than 100%**.
26
+
27
+ You can switch to Node 18 by following steps:
28
+
29
+ ```bash
30
+ # Install Node v18
31
+ nvm install 18
32
+
33
+ # switch to Node 18
34
+ nvm use 18
35
+
36
+ # Set Node 18 as the default version
37
+ nvm default 18
38
+
39
+ # View Node version
40
+ node -v
41
+ ```
42
+
43
+ ### Using Rspack build
44
+
45
+ If you have higher build performance requirements, you can easily switch to Rspack build mode, see [Using Rspack](/guides/advanced-features/rspack-start.html) for more information.
46
+
47
+ ### Using SWC or esbuild
48
+
49
+ [SWC](https://SWC.rs/) (Speedy Web Compiler) is a transformer and minimizer for JavaScript and TypeScript based on `Rust`. SWC can provide the same abilities with Babel, and it's more than 10x faster than Babel.
50
+
51
+ [esbuild](https://esbuild.github.io/) is a front-end build tool based on Golang. It has the functions of bundling, compiling and minimizing JavaScript code. Compared with traditional tools, the performance is significantly improved. When minimizing code, compared to webpack's built-in terser minimizer, esbuild has dozens of times better performance.
52
+
53
+ Modern.js provides SWC plugin and esbuild plugin that allow you to use SWC or esbuild instead of babel-loader, ts-loader and terser for transformation and minification process. See:
54
+
55
+ - [SWC plugin document](/configure/app/tools/swc.html)
56
+ - [esbuild plugin document](/configure/app/tools/esbuild.html)
57
+
58
+ :::tip SWC vs esbuild
59
+ The SWC compiled outputs has better compatibility, supports polyfills such as core-js, and has more complete features, so it is recommended to use the SWC plugin first.
60
+ :::
61
+
62
+ ### Avoid using ts-loader
63
+
64
+ By default, Modern.js uses Babel to compile TS files. After enabling the [tools.tsLoader](/en/configure/app/tools/ts-loader.html) option, `ts-loader` will be used to compile TS files.
65
+
66
+ Please avoid using `ts-loader` because it requires additional parsing and type checking, which will slow down the build.
67
+
68
+ ```js
69
+ export default {
70
+ tools: {
71
+ // remove this config
72
+ tsLoader: {},
73
+ },
74
+ };
75
+ ```
76
+
77
+ See the [tools.tsLoader documentation](/en/configure/app/tools/ts-loader.html) for details.
78
+
79
+ ## Development optimization strategies
80
+
81
+ The following are strategies for improve build performance in development environment.
82
+
83
+ ### Adjust Source Map format
84
+
85
+ In order to provide a good debugging experience, Modern.js uses the `cheap-module-source-map` format Source Map by default in the development environment, which is a high-quality Source Map format and will bring certain performance overhead.
86
+
87
+ You can improve build speed by adjusting the source map format of your development environment.
88
+
89
+ For example to disable Source Map:
90
+
91
+ ```js
92
+ export default {
93
+ tools: {
94
+ bundlerChain(chain, { env }) {
95
+ if (env === 'development') {
96
+ chain.devtool(false);
97
+ }
98
+ },
99
+ },
100
+ };
101
+ ```
102
+
103
+ Or set the source map format of the development environment to the cheapest `eval` format:
104
+
105
+ ```js
106
+ export default {
107
+ tools: {
108
+ bundlerChain(chain, { env }) {
109
+ if (env === 'development') {
110
+ chain.devtool('eval');
111
+ }
112
+ },
113
+ },
114
+ };
115
+ ```
116
+
117
+ > For detailed differences between different Source Map formats, see [webpack - devtool](https://webpack.js.org/configuration/devtool/).
118
+
119
+ ### Adjust Browserslist for development
120
+
121
+ This strategy is similar to ["Adjust Browserslist"](/guides/advanced-features/optimize-bundle.html#adjust-browserslist), the difference is that we can set different browserslist for development and production environment, thereby reducing the compilation overhead in the development environment.
122
+
123
+ For example, you can add the following config to `package.json`, which means that only the latest browsers are compatible in the development environment, and the actual browsers are compatible in the production environment:
124
+
125
+ ```json
126
+ {
127
+ "browserslist": {
128
+ "production": [">0.2%", "not dead", "not op_mini all"],
129
+ "development": [
130
+ "last 1 chrome version",
131
+ "last 1 firefox version",
132
+ "last 1 safari version"
133
+ ]
134
+ }
135
+ }
136
+ ```
137
+
138
+ Note that this strategy can lead to some differences in the build result of development production environment.
139
+
140
+ ## Production optimization strategies
141
+
142
+ The following are strategies for improve build performance in production environment.
143
+
144
+ ### Disable Source Map
145
+
146
+ If your project does not need Source Map in the production, you can turn it off through the `disableSourceMap` config to improve the build speed.
147
+
148
+ ```js
149
+ export default {
150
+ output: {
151
+ disableSourceMap: true,
152
+ },
153
+ };
154
+ ```
155
+
156
+ See [output.disableSourceMap](/en/configure/app/output/disable-source-map.html) for details.
@@ -0,0 +1,115 @@
1
+ ---
2
+ sidebar_position: 13
3
+ ---
4
+
5
+ # Bundle Size Optimization
6
+
7
+ Bundle size optimization is an important part in production environment because it directly affects the user experience of online users. In this document, we will introduce some common bundle size optimization methods in Builder.
8
+
9
+ ## Reduce duplicate dependencies
10
+
11
+ In real projects, there will be some third-party dependencies installed with multiple versions. Duplicate dependencies can lead to larger bundles and slower builds.
12
+
13
+ We can detect or eliminate duplicate dependencies with some community tools.
14
+
15
+ - If you are using `pnpm >= 7.26.0`, you can use the [pnpm dedupe](https://pnpm.io/cli/dedupe) command to upgrade and eliminate duplicate dependencies.
16
+
17
+ ```bash
18
+ pnpm dedupe
19
+ ```
20
+
21
+ - If you are using `pnpm < 7.26.0`, you can use [pnpm-deduplicate](https://github.com/ocavue/pnpm-deduplicate) to analyze all duplicate dependencies, then update dependencies or declare [pnpm overrides](https://pnpm.io/package_json#pnpmoverrides) to merge duplicated dependencies.
22
+
23
+ ```bash
24
+ npx pnpm-deduplicate --list
25
+ ```
26
+
27
+ - If you are using `yarn`, you can use [yarn-deduplicate](https://github.com/scinos/yarn-deduplicate) to automatically merge duplicated dependencies:
28
+
29
+ ```bash
30
+ npx yarn-deduplicate && yarn
31
+ ```
32
+
33
+ ## Use lightweight libraries
34
+
35
+ It is recommended to using lightweight libraries in your project, such as replacing [moment](https://momentjs.com/) with [day.js](https://day.js.org/).
36
+
37
+ If you want to find out the large libraries in the project, you can add the `BUNDLE_ANALYZE=true` environment variable when building:
38
+
39
+ ```bash
40
+ BUNDLE_ANALYZE=true pnpm build
41
+ ```
42
+
43
+ See the [performance.bundleAnalyze](/configure/app/performance/bundle-analyze.html) configuration for details.
44
+
45
+ ## Adjust Browserslist
46
+
47
+ The Builder will compile the code according to the project's Browserslist config, and inject some Polyfills. If the project does not need to be compatible with legacy browsers, you can adjust the Browserslist and drop some legacy browsers, thereby reducing the compilation overhead on syntax and polyfill.
48
+
49
+ Builder's default Browserslist config is:
50
+
51
+ ```js
52
+ ['> 0.01%', 'not dead', 'not op_mini all'];
53
+ ```
54
+
55
+ For example, if you only need to be compatible with browsers above Chrome 61, you can change it to:
56
+
57
+ ```js
58
+ ['Chrome >= 61'];
59
+ ```
60
+
61
+ :::tip
62
+ Please read the [Browserslist](https://modernjs.dev/builder/en/guide/advanced/browserslist.html) chapter to know more about the usage of Browserslist.
63
+ :::
64
+
65
+ ## Usage polyfill
66
+
67
+ When it is clear that third-party dependencies do not require additional polyfill, you can set [output.polyfill](/configure/app/output/polyfill.html) to `usage`.
68
+
69
+ In `usage` mode, Builder analyzes the syntax used in the source code and injects the required polyfill code on demand to reduce the size of polyfill.
70
+
71
+ ```js
72
+ export default {
73
+ output: {
74
+ polyfill: 'usage',
75
+ },
76
+ };
77
+ ```
78
+
79
+ :::tip
80
+ Please read the [Browser Compatibility](https://modernjs.dev/builder/en/guide/advanced/browser-compatibility.html) chapter to know more about the usage of Browserslist.
81
+ :::
82
+
83
+ ## Image Compression
84
+
85
+ In general front-end projects, the size of image often accounts for a large proportion of the total bundle size of the project.So if the image size can be reduced as much as possible, it will have a significant optimization effect on the project bundle size. You can enable image compression by registering a plugin in the Builder:
86
+
87
+ ```js
88
+ import { builderPluginImageCompress } from '@modern-js/builder-plugin-image-compress';
89
+
90
+ // Add the plugin to the Builder
91
+ builder.addPlugins([builderPluginImageCompress()]);
92
+ ```
93
+
94
+ See details in [plugin-image-compress](https://modernjs.dev/builder/en/plugins/plugin-image-compress.html).
95
+
96
+ ## Split Chunk
97
+
98
+ A great chunk splitting strategy is very important to improve the loading performance of the application. It can make full use of the browser's caching mechanism to reduce the number of requests and improve the loading speed of the application.
99
+
100
+ A variety of [chunk splitting strategies](https://modernjs.dev/builder/en/guide/optimization/split-chunk) are built into Builder, which can meet the needs of most applications. You can also customize the chunk splitting config according to your own business scenarios.
101
+
102
+ For example, split the `axios` library under node_modules into `axios.js`:
103
+
104
+ ```js
105
+ export default {
106
+ performance: {
107
+ chunkSplit: {
108
+ strategy: 'split-by-experience',
109
+ forceSplitting: {
110
+ axios: /node_modules\/axios/,
111
+ },
112
+ },
113
+ },
114
+ };
115
+ ```
@@ -83,7 +83,7 @@ For example, if you are using pnpm, you can update the Rspack version with `over
83
83
  "pnpm": {
84
84
  "overrides": {
85
85
  "@rspack/core": "nightly",
86
- "@rspack/dev-client": "nightly",
86
+ "@rspack/plugin-react-refresh": "nightly",
87
87
  "@rspack/plugin-html": "nightly"
88
88
  }
89
89
  }
@@ -0,0 +1,161 @@
1
+ ---
2
+ sidebar_position: 11
3
+ ---
4
+
5
+ # Source Code Build Mode
6
+
7
+ The source code build mode is used in the monorepo development scenario, allowing developers to directly reference the source code of other sub-projects within the monorepo for development.
8
+
9
+ ## Use Cases
10
+
11
+ In a monorepo, there are two main ways for projects to reference each other: artifact referencing and source code referencing. Let's use a simple monorepo as an example to illustrate the use case of source code referencing.
12
+
13
+ For example, the monorepo contains an app application and a lib:
14
+
15
+ ```ts
16
+ monorepo
17
+ ├── app
18
+ └── lib
19
+ └── src
20
+ └── index.ts
21
+ ```
22
+
23
+ The app is built using Modern.js and relies on some methods from the lib:
24
+
25
+ ```json
26
+ {
27
+ "name": "app",
28
+ "dependencies": {
29
+ "lib": "workspace:*"
30
+ }
31
+ }
32
+ ```
33
+
34
+ ### Artifact Referencing
35
+
36
+ **When using artifact referencing, the current project references the artifacts built from other sub-projects.**
37
+
38
+ In the example above, the lib is written in TypeScript. Typically, we need to build the lib's code in advance to generate JavaScript artifacts so that the app can reference it correctly. When the lib's code is updated, we need to rebuild it (or use `tsc --watch`) to ensure that the app can use the latest code.
39
+
40
+ The advantages of this approach are:
41
+
42
+ - The build processes of each sub-project are completely independent and can have different build configurations.
43
+ - Build caching can be applied to individual sub-projects.
44
+
45
+ The disadvantages are:
46
+
47
+ - The HMR chain becomes longer during local development.
48
+ - The process becomes cumbersome when a project contains multiple lib packages.
49
+
50
+ ### Source Code Referencing
51
+
52
+ **When using source code referencing, the current project references the source code of other sub-projects for building.**
53
+
54
+ In the example mentioned earlier, when you enable the source code build mode and add the relevant configuration in the `lib` directory, Modern.js will automatically reference the `src/index.ts` source code of the lib. This means that you don't need to build the lib's code in advance, and when the source code of the lib is updated, it can trigger automatic hot updates for the app.
55
+
56
+ The advantages of this approach are:
57
+
58
+ - The sub-project does not rely on a build tool and does not require build configurations. The code of the sub-project will be compiled by the build tool of the current project.
59
+ - There is no need to execute the build process for the sub-projects in advance.
60
+ - HMR is more efficient during local development.
61
+
62
+ The disadvantages are:
63
+
64
+ - The current project needs to support syntax features used by sub-projects and follow the same syntax specifications, such as using a consistent version of decorator syntax. If the current project and sub-projects require different build configurations, building from source code may not be suitable.
65
+ - The current project requires compiling more code, which may result in longer build times.
66
+
67
+ ## Building from Source Code
68
+
69
+ ### Enabling Configuration
70
+
71
+ You can enable source code build mode by setting [experiments.sourceBuild](/configure/app/experiments/source-build.html) to `true`.
72
+
73
+ ```ts
74
+ export default {
75
+ experiments: {
76
+ sourceBuild: true,
77
+ },
78
+ };
79
+ ```
80
+
81
+ ### Configuring Sub-projects
82
+
83
+ When the source code build mode is enabled, the Modern.js will prioritize reading the file specified in the `source` field of the sub-project during the build process. Therefore, you need to configure the `source` field in the package.json file of the sub-project and point it to the source code file.
84
+
85
+ For example, in the following example, when the lib package is referenced, the `./src/index.ts` file will be read for building:
86
+
87
+ ```json title="package.json"
88
+ {
89
+ "name": "lib",
90
+ "main": "./dist/index.js",
91
+ "source": "./src/index.ts"
92
+ }
93
+ ```
94
+
95
+ If the sub-project uses [exports](https://nodejs.org/api/packages.html#package-entry-points) field, you also need to add the `source` field in the `exports` field.
96
+
97
+ ```json title="package.json"
98
+ {
99
+ "name": "lib",
100
+ "exports": {
101
+ ".": {
102
+ "source": "./src/index.ts",
103
+ "default": "./dist/index.js"
104
+ },
105
+ "./features": {
106
+ "source": "./src/features/index.ts",
107
+ "default": "./dist/features/index.js"
108
+ }
109
+ }
110
+ }
111
+ ```
112
+
113
+ ## Configure Project Reference
114
+
115
+ In a TypeScript project, you need to use the capability provided by TypeScript called [Project Reference](https://www.typescriptlang.org/docs/handbook/project-references.html). It helps you develop source code more effectively.
116
+
117
+ ### Introduction
118
+
119
+ Project reference provides the following capabilities:
120
+
121
+ - It allows TypeScript to correctly recognize the types of other sub-projects without the need to build them.
122
+ - When you navigate the code in VS Code, it automatically takes you to the corresponding source code file of the module.
123
+ - Modern.js reads the project reference configuration and automatically recognizes the `tsconfig.compilerOptions.path` configuration of the sub-project, so that the use of aliases in the sub-project works correctly.
124
+
125
+ ### Example
126
+
127
+ In the example mentioned earlier, since the app project references the lib sub-project, we need to configure the `composite` and `references` options in the app project's `tsconfig.json` file and point them to the corresponding relative directory of lib:
128
+
129
+ ```json title="app/tsconfig.json"
130
+ {
131
+ "compilerOptions": {
132
+ "composite": true
133
+ },
134
+ "references": [
135
+ {
136
+ "path": "../lib"
137
+ }
138
+ ]
139
+ }
140
+ ```
141
+
142
+ After adding these two options, the project reference is already configured. You can restart VS Code to see the effects of the configuration.
143
+
144
+ Note that the above example is a simplified one. In real monorepo projects, there may be more complex dependency relationships. You need to add a complete `references` configuration for the functionality to work correctly.
145
+
146
+ :::tip
147
+ If you want to learn more about project reference, please refer to the official documentation on [TypeScript - Project References](https://www.typescriptlang.org/docs/handbook/project-references.html).
148
+ :::
149
+
150
+ ## Caveat
151
+
152
+ When using source code build mode, there are a few things to keep in mind:
153
+
154
+ 1. Ensure that the current project can compile the syntax or features used in the sub-project. For example, if the sub-project uses Stylus to write CSS, the current app needs to support Stylus compilation.
155
+ 2. Ensure that the current project has the same code syntax and features as the sub-project, such as consistent syntax versions for decorators.
156
+ 3. Source code building may have some limitations. When encountering issues, you can remove the `source` field from the sub-project's package.json and debug using the built artifacts of the sub-project.
157
+ 4. When `composite: true` is enabled, TypeScript will generate `*.tsbuildinfo` temporary files. You need to add these temporary files to the `.gitignore` file.
158
+
159
+ ```text title=".gitignore"
160
+ *.tsbuildinfo
161
+ ```
@@ -1,12 +1,12 @@
1
1
  ---
2
- sidebar_position: 11
2
+ sidebar_position: 14
3
3
  ---
4
4
 
5
5
  # Using Jest
6
6
 
7
7
  Modern.js integrates the testing capabilities of [Jest](https://jestjs.io/) by default.
8
8
 
9
- First need to execute `pnpm run new` to enable [unit test/integration test] features:
9
+ First need to execute `pnpm run new` to enable "unit test/integration test" features:
10
10
 
11
11
  ```bash
12
12
  ? Please select the operation you want: Enable features
@@ -1,5 +1,5 @@
1
1
  ---
2
- sidebar_position: 12
2
+ sidebar_position: 15
3
3
  ---
4
4
 
5
5
  # Using StorybookV6
@@ -1,5 +1,5 @@
1
1
  ---
2
- sidebar_position: 13
2
+ sidebar_position: 16
3
3
  ---
4
4
 
5
5
  # Custom Web Server
@@ -14,7 +14,7 @@ In this document, you can learn about the main technology stack involved in the
14
14
 
15
15
  Modern.js uses [React 18](https://react.dev/) to build user interfaces and is also compatible with React 17.
16
16
 
17
- The underlying Builder of Modern.js also supports building Vue applications. If you need to use Vue, you can refer to ["Building Vue App"](https://modernjs.dev/builder/en/guide/framework/vue3.html).
17
+ Rsbuild supports building Vue applications. If you need to use Vue, you can refer to ["Rsbuild - Vue"](https://rsbuild.dev/guide/framework/vue3).
18
18
 
19
19
  ---
20
20