@modern-js/main-doc 2.58.1 → 2.58.3
Sign up to get free protection for your applications and to get access to all the features.
- package/docs/en/apis/app/hooks/config/icon.mdx +1 -1
- package/docs/en/apis/app/runtime/web-server/unstable_middleware.mdx +2 -2
- package/docs/en/community/blog/_meta.json +1 -6
- package/docs/en/components/deploy.mdx +1 -1
- package/docs/en/configure/_meta.json +1 -1
- package/docs/en/configure/app/tools/esbuild.mdx +1 -1
- package/docs/en/configure/app/tools/swc.mdx +2 -2
- package/docs/en/guides/_meta.json +5 -0
- package/docs/en/guides/advanced-features/bff/_meta.json +1 -6
- package/docs/en/guides/advanced-features/rspack-start.mdx +1 -8
- package/docs/en/guides/advanced-features/ssr/_meta.json +1 -5
- package/docs/en/guides/basic-features/data/_meta.json +1 -4
- package/docs/en/guides/basic-features/data/data-fetch.mdx +1 -1
- package/docs/en/guides/basic-features/routes.mdx +1 -1
- package/docs/en/guides/concept/_meta.json +1 -4
- package/docs/en/guides/concept/entries.mdx +70 -46
- package/docs/en/guides/get-started/_meta.json +1 -7
- package/docs/en/guides/get-started/introduction.mdx +1 -1
- package/docs/en/guides/get-started/quick-start.mdx +0 -1
- package/docs/en/guides/get-started/tech-stack.mdx +0 -2
- package/docs/en/guides/get-started/upgrade.mdx +16 -2
- package/docs/en/guides/rsbuild-plugins/plugin-esbuild.mdx +205 -0
- package/docs/en/guides/rsbuild-plugins/plugin-swc.mdx +356 -0
- package/docs/en/guides/topic-detail/framework-plugin/_meta.json +1 -1
- package/docs/en/guides/topic-detail/generator/_meta.json +1 -1
- package/docs/en/guides/topic-detail/generator/create/_meta.json +1 -5
- package/docs/en/guides/topic-detail/generator/new/_meta.json +1 -5
- package/docs/en/guides/topic-detail/generator/plugin/_meta.json +1 -1
- package/docs/en/guides/troubleshooting/_meta.json +1 -6
- package/docs/zh/apis/app/hooks/config/icon.mdx +1 -1
- package/docs/zh/apis/app/runtime/web-server/unstable_middleware.mdx +2 -2
- package/docs/zh/community/blog/_meta.json +1 -6
- package/docs/zh/components/deploy.mdx +1 -1
- package/docs/zh/configure/_meta.json +1 -1
- package/docs/zh/configure/app/tools/esbuild.mdx +1 -1
- package/docs/zh/configure/app/tools/swc.mdx +2 -2
- package/docs/zh/guides/_meta.json +5 -0
- package/docs/zh/guides/advanced-features/bff/_meta.json +1 -6
- package/docs/zh/guides/advanced-features/rspack-start.mdx +1 -8
- package/docs/zh/guides/advanced-features/ssr/_meta.json +1 -5
- package/docs/zh/guides/basic-features/data/_meta.json +1 -4
- package/docs/zh/guides/basic-features/data/data-fetch.mdx +3 -4
- package/docs/zh/guides/basic-features/routes.mdx +1 -1
- package/docs/zh/guides/concept/_meta.json +1 -4
- package/docs/zh/guides/concept/entries.mdx +76 -57
- package/docs/zh/guides/get-started/_meta.json +1 -7
- package/docs/zh/guides/get-started/introduction.mdx +2 -2
- package/docs/zh/guides/get-started/quick-start.mdx +0 -1
- package/docs/zh/guides/get-started/tech-stack.mdx +0 -2
- package/docs/zh/guides/get-started/upgrade.mdx +15 -1
- package/docs/zh/guides/rsbuild-plugins/plugin-esbuild.mdx +201 -0
- package/docs/zh/guides/rsbuild-plugins/plugin-swc.mdx +344 -0
- package/docs/zh/guides/topic-detail/framework-plugin/_meta.json +1 -1
- package/docs/zh/guides/topic-detail/generator/_meta.json +1 -1
- package/docs/zh/guides/topic-detail/generator/create/_meta.json +1 -5
- package/docs/zh/guides/topic-detail/generator/new/_meta.json +1 -5
- package/docs/zh/guides/topic-detail/generator/plugin/_meta.json +1 -1
- package/docs/zh/guides/troubleshooting/_meta.json +1 -6
- package/package.json +4 -4
- package/rspress.config.ts +2 -2
- package/src/components/ContentCard/index.tsx +1 -1
- package/src/components/FeatureLayout/index.tsx +1 -1
- package/src/components/Footer/index.tsx +2 -2
- package/src/components/ListCard/index.tsx +1 -1
- package/src/components/Sandpack/index.tsx +5 -3
- package/src/components/SecondaryTitle/index.tsx +1 -1
- package/src/components/ShowcaseList/index.tsx +1 -1
- package/src/i18n/index.ts +1 -1
- package/src/i18n/zhCN.ts +1 -1
- package/src/index.ts +0 -1
- package/src/pages/index.tsx +15 -7
- package/src/.eslintrc.json +0 -3
@@ -42,7 +42,7 @@ When there is an `icon.*` file in the `config` directory at the root of the proj
|
|
42
42
|
After the build is completed, you can see the following tags automatically generated in HTML:
|
43
43
|
|
44
44
|
```html
|
45
|
-
<link rel="apple-touch-icon" sizes="
|
45
|
+
<link rel="apple-touch-icon" sizes="180x180" href="/static/image/icon.png" />
|
46
46
|
```
|
47
47
|
|
48
48
|
### Order
|
@@ -92,7 +92,7 @@ import {
|
|
92
92
|
} from '@modern-js/runtime/server';
|
93
93
|
import type { Vars } from '../shared/index';
|
94
94
|
|
95
|
-
const setPayload:
|
95
|
+
const setPayload: UnstableMiddlewaree<Vars> = async (
|
96
96
|
c: UnstableMiddlewareContext<Vars>,
|
97
97
|
next,
|
98
98
|
) => {
|
@@ -101,7 +101,7 @@ const setPayload: UnstableMiddleware = async (
|
|
101
101
|
await next();
|
102
102
|
};
|
103
103
|
|
104
|
-
export const unstableMiddleware: UnstableMiddleware[] = [setPayload];
|
104
|
+
export const unstableMiddleware: UnstableMiddleware<Vars>[] = [setPayload];
|
105
105
|
```
|
106
106
|
|
107
107
|
```ts title="src/routes/page.data.ts"
|
@@ -1 +1 @@
|
|
1
|
-
After local
|
1
|
+
After local develop, you can refer to the [Deployment](/guides/basic-features/deploy.html) section to deploy the project to the server.
|
@@ -41,4 +41,4 @@ export default defineConfig({
|
|
41
41
|
});
|
42
42
|
```
|
43
43
|
|
44
|
-
For config details, please refer to [
|
44
|
+
For config details, please refer to [Esbuild Plugin Configuration](/guides/rsbuild-plugins/plugin-esbuild.html#config).
|
@@ -19,7 +19,7 @@ When using Rspack as the bundler, SWC will be used for transpiling and compressi
|
|
19
19
|
|
20
20
|
## Used in Rspack mode
|
21
21
|
|
22
|
-
You can set the options of [builtin:swc-loader](https://
|
22
|
+
You can set the options of [builtin:swc-loader](https://rspack.dev/guide/features/builtin-swc-loader) through `tools.swc`.
|
23
23
|
|
24
24
|
```
|
25
25
|
import { defineConfig } from '@modern-js/app-tools';
|
@@ -77,4 +77,4 @@ export default defineConfig({
|
|
77
77
|
});
|
78
78
|
```
|
79
79
|
|
80
|
-
For config details, please refer to [
|
80
|
+
For config details, please refer to [SWC Plugin Configuration](/guides/rsbuild-plugins/plugin-swc.html#config).
|
@@ -22,13 +22,6 @@ import InitRspackApp from '@site-docs-en/components/init-rspack-app';
|
|
22
22
|
|
23
23
|
After the project is created, you can experience the project by running `pnpm run dev`. For more project information, please refer to [Quick Start](/guides/get-started/quick-start.html).
|
24
24
|
|
25
|
-
:::tip
|
26
|
-
When using Rspack as the bundler, the following Features are temporarily unavailable as some of the capabilities are still under development and we will provide support in the future.
|
27
|
-
|
28
|
-
- The usage of [useLoader](/guides/basic-features/data/data-fetch.html) in Client Side Rendering
|
29
|
-
|
30
|
-
:::
|
31
|
-
|
32
25
|
## Enable Rspack build
|
33
26
|
|
34
27
|
Since Modern.js MAJOR_VERSION.46.0, you can enable Rspack build by add the following configuration in `modern.config.ts`:
|
@@ -85,7 +78,7 @@ For unsupported configurations, we have marked `Bundler: only support webpack` o
|
|
85
78
|
|
86
79
|
## Modify transpile configuration
|
87
80
|
|
88
|
-
Modern.js uses Rspack [builtin:swc-loader](https://
|
81
|
+
Modern.js uses Rspack [builtin:swc-loader](https://rspack.dev/guide/features/builtin-swc-loader) for code translation in Rspack mode.
|
89
82
|
|
90
83
|
Modern.js has provided a more convenient configuration for the common configuration of `builtin:swc-loader`, such as: configuring the component library to import it on demand through [source.transformImport](/configure/app/source/transform-import).
|
91
84
|
|
@@ -481,7 +481,7 @@ The Data Loader in our SSR project is designed to only run on the server side. T
|
|
481
481
|
**`useLoader`** is a legacy API in Modern.js v1. This API is a React Hook designed specifically for SSR applications, allowing developers to fetch data in components in isomorphic development.
|
482
482
|
|
483
483
|
:::tip
|
484
|
-
It is not necessary to use `useLoader` to fetch data in CSR projects.
|
484
|
+
It is not necessary to use `useLoader` to fetch data in CSR projects, and `useLoader` is not supported when using Rspack as the bundler.
|
485
485
|
:::
|
486
486
|
|
487
487
|
Here is the simplest example:
|
@@ -380,7 +380,7 @@ export default () => {
|
|
380
380
|
|
381
381
|
In each directory under `routes/`, developers can also define an `error.tsx` file that exports an `<ErrorBoundary>` component by default.
|
382
382
|
|
383
|
-
When this component exists in the routes directory, any rendering errors will be caught by the `ErrorBoundary` component.
|
383
|
+
When this component exists in the routes directory, any rendering errors will be caught by the `ErrorBoundary` component.
|
384
384
|
|
385
385
|
`<ErrorBoundary>` can return the UI view when an error occurs. When the `<ErrorBoundary>` component is not declared in the current level, the error will bubble up to a higher-level component until it is caught or thrown. At the same time, when a component has an error, it will only affect the route component and its child components that catch the error. The status and view of other components are not affected and can continue to interact.
|
386
386
|
|
@@ -16,7 +16,7 @@ Many configuration options provided by Modern.js are divided by entry, such as p
|
|
16
16
|
|
17
17
|
## Single Entry and Multiple Entries
|
18
18
|
|
19
|
-
The project initialized by Modern.js is a single entry
|
19
|
+
The project initialized by Modern.js is a single entry project, with the following structure:
|
20
20
|
|
21
21
|
```
|
22
22
|
.
|
@@ -56,13 +56,12 @@ After running the command, Modern.js will automatically generate a new entry dir
|
|
56
56
|
|
57
57
|
The original entry code has been moved to a directory with the same name as the `name` field in `package.json`, and a `new-entry` entry directory has been created.
|
58
58
|
|
59
|
-
You can run `pnpm run dev` to start the development server. At this point, you will see a new route named `/new-entry` added, and the existing page routes remain unchanged.
|
60
|
-
|
61
|
-
:::tip
|
62
59
|
Modern.js will use the entry with the same name as the `name` field in `package.json` as the main entry. The route of the main entry is `/`, and the route of other entries is `/{entryName}`.
|
63
60
|
|
64
|
-
|
61
|
+
You can run `pnpm run dev` to start the development server. At this point, you will see a new route named `/new-entry` added, and the existing page routes remain unchanged.
|
65
62
|
|
63
|
+
:::note
|
64
|
+
The concepts of **single entry/multiple entry** and **SPA/MPA** are not equivalent. The former pertains to how to configure and package the application, while the latter is about the patterns for organizing front-end applications. Each entry point can be either an SPA or a non-SPA.
|
66
65
|
:::
|
67
66
|
|
68
67
|
## Entry Types
|
@@ -82,30 +81,26 @@ By default, Modern.js scans the files under `src/` before starting the project,
|
|
82
81
|
|
83
82
|
:::
|
84
83
|
|
85
|
-
|
84
|
+
The entry directory must meet one of the following three conditions:
|
86
85
|
|
87
86
|
1. Has a `routes/` directory.
|
88
87
|
2. Has an `App.[jt]sx?` file.
|
89
|
-
|
90
|
-
4. Has a `pages/` directory (compatible with Modern.js 1.0).
|
91
|
-
5. Has an `entry.[jt]sx?` file.
|
88
|
+
5. Has an `entry.[jt]sx?` file.(Requires [source.enableCustomEntry](/configure/app/source/enable-custom-entry) to be enabled)
|
92
89
|
|
93
|
-
When the `src/` directory meets the entry
|
90
|
+
When the `src/` directory meets the conditions of an entry, Modern.js will consider the current application to be a single-entry application. Otherwise, Modern.js will scan the first-level directories under `src/` and further determine if they are entries. In this case, the application is typically a multi-entry application.
|
94
91
|
|
95
92
|
:::tip
|
96
93
|
In a single entry application, the default entry name is `main`.
|
97
94
|
|
98
95
|
:::
|
99
96
|
|
100
|
-
When the project is not a single entry application, Modern.js will further look at the top-level directories under `src/`.
|
101
|
-
|
102
97
|
### Framework Mode Entry
|
103
98
|
|
104
|
-
|
99
|
+
Framework mode refers to using Modern.js's framework capabilities, such as convention routing, SSR (Server-Side Rendering), and integrated calls. Under this type of entry convention, the entries in the application are not the actual compilation entries. Modern.js will generate a wrapped entry during startup, which you can find in `node_modules/.modern/[entryName]/index.js`.
|
105
100
|
|
106
101
|
#### Conventional Routing
|
107
102
|
|
108
|
-
If there is a `routes/` directory
|
103
|
+
If there is a `routes/` directory within the entry, we refer to this entry as a convention-based route. Modern.js will scan the files under `routes/` during startup and automatically generate client-side routes (react-router) based on file conventions. For example:
|
109
104
|
|
110
105
|
```bash
|
111
106
|
.
|
@@ -121,7 +116,15 @@ For more information, please refer to [Conventional Routing](/guides/basic-featu
|
|
121
116
|
|
122
117
|
#### Self-controlled Routing
|
123
118
|
|
124
|
-
If there is an `App.[jt]sx?` file
|
119
|
+
If there is an `App.[jt]sx?` file within the entry, we refer to this entry as a self-controlled route. For example:
|
120
|
+
|
121
|
+
```bash
|
122
|
+
.
|
123
|
+
├── src
|
124
|
+
│ └── App.tsx
|
125
|
+
```
|
126
|
+
|
127
|
+
In this file, you can define client-side routes or not to set any client-side routes.
|
125
128
|
|
126
129
|
```tsx
|
127
130
|
import { BrowserRouter, Route, Routes } from '@modern-js/runtime/router';
|
@@ -142,12 +145,14 @@ For more information, please refer to [Self-controlled Routing](/guides/basic-fe
|
|
142
145
|
|
143
146
|
#### Custom Entry
|
144
147
|
|
145
|
-
If there is an `entry.[jt]sx` file in the entry, developers need to call the `createRoot` and `render` functions in the `entry.[jt]sx` file to complete the project's entry logic.
|
146
|
-
|
147
148
|
:::info
|
148
|
-
Using this
|
149
|
+
Using this features requires enabling [source.enableCustomEntry](/configure/app/source/enable-custom-entry).
|
149
150
|
:::
|
150
151
|
|
152
|
+
By default, whether you use convention routing or self-controlled routing, Modern.js will automatically handle rendering. If you wish to customize this behavior, you can creating a custom entry file.
|
153
|
+
|
154
|
+
If there is an `entry.[jt]sx` file within the entry, Modern.js will no longer control the application's rendering process. You can call the `createRoot` and `render` functions within the `entry.[jt]sx` file to complete the entry logic for your application.
|
155
|
+
|
151
156
|
```tsx
|
152
157
|
import { createRoot } from '@modern-js/runtime/react';
|
153
158
|
import { render } from '@modern-js/runtime/browser';
|
@@ -157,7 +162,9 @@ const ModernRoot = createRoot();
|
|
157
162
|
render(<ModernRoot />);
|
158
163
|
```
|
159
164
|
|
160
|
-
|
165
|
+
In the code above, the component returned by the `createRoot` function is either the component generated from the `routes/` directory or the component exported by `App.tsx`.
|
166
|
+
|
167
|
+
The `render` function is used to handle rendering and mounting of the component. For example, if you want to execute some asynchronous tasks before rendering, you can achieve it like this:
|
161
168
|
|
162
169
|
```tsx
|
163
170
|
import { createRoot } from '@modern-js/runtime/react';
|
@@ -166,7 +173,7 @@ import { render } from '@modern-js/runtime/browser';
|
|
166
173
|
const ModernRoot = createRoot();
|
167
174
|
|
168
175
|
async function beforeRender() {
|
169
|
-
|
176
|
+
// some async request
|
170
177
|
}
|
171
178
|
|
172
179
|
beforeRender().then(() => {
|
@@ -174,34 +181,15 @@ beforeRender().then(() => {
|
|
174
181
|
});
|
175
182
|
```
|
176
183
|
|
177
|
-
#### Custom Bootstrap
|
178
|
-
|
179
|
-
:::warning
|
180
|
-
Soon to be deprecated, it is recommended to use a custom entry.
|
181
|
-
:::
|
182
|
-
|
183
|
-
If there is an `index.[jt]sx` file in the entry, and the file exports a function by default, Modern.js will pass the default `bootstrap` function as a parameter and use the exported function to replace the default `bootstrap`. This way, developers can customize how components are mounted to DOM nodes or add custom behavior before mounting. For example:
|
184
|
-
|
185
|
-
```tsx
|
186
|
-
export default (App: React.ComponentType, bootstrap: () => void) => {
|
187
|
-
// do something before bootstrap...
|
188
|
-
initSomething().then(() => {
|
189
|
-
bootstrap();
|
190
|
-
});
|
191
|
-
};
|
192
|
-
```
|
193
|
-
|
194
184
|
### Build Mode Entry
|
195
185
|
|
196
|
-
Build mode refers to not using the Runtime capabilities provided by Modern.js, but rather having the developer define the entry of the page completely on their own.
|
197
|
-
|
198
|
-
When the entry directory contains `index.[jt]sx` (soon to be deprecated) and does not export a function via `export default`, or when there is an `entry.[jt]sx` file in the entry directory and the `@modern-js/runtime` dependency is not installed, the corresponding file will be identified as the entry module of webpack or Rspack.
|
199
|
-
|
200
186
|
:::info
|
201
|
-
Using
|
187
|
+
Using this features requires enabling [source.enableCustomEntry](/configure/app/source/enable-custom-entry).
|
202
188
|
:::
|
203
189
|
|
204
|
-
|
190
|
+
Build mode refers to the development mode that does not use Modern.js's runtime capabilities and only utilizes Modern.js's build capabilities. When the `@modern-js/runtime` dependency is not installed in the application, Modern.js will treat all entries as build mode entries.
|
191
|
+
|
192
|
+
In this case, if there is an `entry.[jt]sx` file within the entry, this file will be recognized as the build entry for webpack or Rspack. Modern.js will not automatically generate entry code at this time, and you need to mount the component to the DOM node yourself. For example:
|
205
193
|
|
206
194
|
```js title=src/entry.tsx
|
207
195
|
import React from 'react';
|
@@ -211,9 +199,13 @@ import App from './App';
|
|
211
199
|
ReactDOM.render(<App />, document.getElementById('root'));
|
212
200
|
```
|
213
201
|
|
214
|
-
|
202
|
+
In build mode, the application **will not be able to use Modern.js's runtime capabilities**, such as:
|
203
|
+
- Convention routing, the routing based on the files under `src/routes`
|
204
|
+
- Server-Side Rendering (SSR)
|
205
|
+
- The `runtime` configuration in the `modern.config.js` file will no longer take effect
|
215
206
|
|
216
|
-
|
207
|
+
|
208
|
+
## Specify entry in the configuration file
|
217
209
|
|
218
210
|
In some cases, you may need to customize the entry configuration instead of using the entry conventions provided by Modern.js.
|
219
211
|
|
@@ -246,4 +238,36 @@ export default defineConfig({
|
|
246
238
|
});
|
247
239
|
```
|
248
240
|
|
249
|
-
|
241
|
+
It is worth noting that, by default, Modern.js considers entries specified through the configuration as **framework mode entries** and will automatically generate the actual compilation entry.
|
242
|
+
|
243
|
+
If your application is migrating from build tools like Webpack or Vite to the Modern.js framework, you typically need to enable the `disableMount` option in the entry configuration. In this case, Modern.js will treat the entry as a **build mode entry**.
|
244
|
+
|
245
|
+
## Deprecated
|
246
|
+
|
247
|
+
Currently, if the entry directory meets the following conditions, it will also be considered an application entry:
|
248
|
+
|
249
|
+
1. Has an `index.[jt]sx?` file.
|
250
|
+
2. Has a `pages/` directory.
|
251
|
+
|
252
|
+
The `pages/` directory was the convention routing in older versions of Modern.js. Now, it is recommended to use the `routes/` directory.
|
253
|
+
|
254
|
+
The `index.[jt]sx?` file supported **Custom Bootstrap** and **Build Mode Entry** in older versions. Now, it is recommended to use `entry.[jt]sx?` instead.
|
255
|
+
|
256
|
+
### Custom Bootstrap
|
257
|
+
|
258
|
+
When there is an `index.[jt]sx` file in the entry, and the file's default export is a function, Modern.js will pass the default `bootstrap` function as an argument and use the exported function to replace the default `bootstrap`.
|
259
|
+
|
260
|
+
This allows developers to customize mounting components to DOM or add custom behaviors before mounting. For example:
|
261
|
+
|
262
|
+
```tsx
|
263
|
+
export default (App: React.ComponentType, bootstrap: () => void) => {
|
264
|
+
// do something before bootstrap...
|
265
|
+
initSomething().then(() => {
|
266
|
+
bootstrap();
|
267
|
+
});
|
268
|
+
};
|
269
|
+
```
|
270
|
+
|
271
|
+
### Build Mode Entry
|
272
|
+
|
273
|
+
When an `index.[jt]sx` file exists in the entry directory and does not export a function via export default, this entry will also be considered a build mode entry.
|
@@ -55,7 +55,7 @@ It mainly includes the following features:
|
|
55
55
|
- 🏠 **Integration**: Development and production environment web server are unique, CSR and SSR are isomorphic development, and API service calls are functions as interfaces.
|
56
56
|
- 📦 **Out Of The Box**: Default TS support, built-in build, ESLint, debugging tools, fully functional and testable.
|
57
57
|
- 🌏 **Ecology**: Self-developed state management, micro-frontend, module packaging, and other peripheral needs.
|
58
|
-
- 🕸 **Routing
|
58
|
+
- 🕸 **Convention Routing**: Using file-based routing helps developers quickly set up applications.
|
59
59
|
|
60
60
|
## Comparison with Others
|
61
61
|
|
@@ -72,7 +72,6 @@ In a newly created project, the `@modern-js/app-tools` npm package is installed
|
|
72
72
|
- It integrates Modern.js Core, providing capabilities for configuration parsing, plugin loading, and more.
|
73
73
|
- It integrates Modern.js Builder, providing build capabilities.
|
74
74
|
- It integrates Modern.js Server, providing capabilities for development and production servers.
|
75
|
-
- It integrates some commonly used plugins, such as `plugin-lint`, `plugin-data-loader`, and more.
|
76
75
|
|
77
76
|
`@modern-js/app-tools` is implemented based on the plugin system of Modern.js. Essentially, it is a plugin. Therefore, you need to register `appTools` in the `plugins` field of the configuration file:
|
78
77
|
|
@@ -30,8 +30,6 @@ We are also working with Zack Jackson, the author of [Module Federation](https:/
|
|
30
30
|
|
31
31
|
Modern.js can be used with any community state management library, such as [Redux](https://redux.js.org/), [Jotai](https://jotai.org/), [Zustand](https://docs.pmnd.rs/zustand), [Valtio](https://valtio.pmnd.rs/), and more.
|
32
32
|
|
33
|
-
Modern.js also provides a wrapper around Redux called Reduck for state management. You can refer to ["Reduck State Management"](/en/guides/topic-detail/model/quick-start) for usage.
|
34
|
-
|
35
33
|
## Package Manager
|
36
34
|
|
37
35
|
Modern.js can be used with any community package manager, such as [npm](https://www.npmjs.com/package/npm), [yarn](https://classic.yarnpkg.com/lang/en/), [pnpm](https://pnpm.io/), or [Bun](https://bun.sh/).
|
@@ -37,10 +37,24 @@ import ReleaseNote from '@site-docs-en/components/release-note';
|
|
37
37
|
<ReleaseNote />
|
38
38
|
|
39
39
|
:::tip
|
40
|
-
When upgrading, you need to upgrade all packages provided by Modern.js,
|
41
|
-
|
40
|
+
When upgrading, you need to upgrade all packages provided by Modern.js, rather than upgrading some dependencies.
|
42
41
|
:::
|
43
42
|
|
43
|
+
## Version Management Strategy
|
44
|
+
|
45
|
+
In Modern.js projects, we recommend that all officially provided dependencies use fixed version, and avoid using `^` or `~` for range declarations. For example:
|
46
|
+
|
47
|
+
```json
|
48
|
+
{
|
49
|
+
"dependencies": {
|
50
|
+
"@modern-js/app-tools": "x.y.z"
|
51
|
+
}
|
52
|
+
}
|
53
|
+
```
|
54
|
+
|
55
|
+
This ensures that the versions of dependencies are fully determined, thereby guaranteeing build consistency and predictability.
|
56
|
+
|
57
|
+
|
44
58
|
## Lock nested dependency
|
45
59
|
|
46
60
|
When a nested dependency of the project has a problem and Modern.js cannot be updated immediately, you can use the package manager to lock the version of the nested dependency.
|
@@ -0,0 +1,205 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 3
|
3
|
+
---
|
4
|
+
|
5
|
+
# Esbuild Plugin
|
6
|
+
|
7
|
+
:::warning
|
8
|
+
**The esbuild feature in the current document is no longer maintained**, we recommend using the Rspack + SWC solution, because Rspack + SWC provide better build performance, richer features, and better compatibility.
|
9
|
+
|
10
|
+
Please refer to [「Use Rspack」](guides/advanced-features/rspack-start) for more information.
|
11
|
+
|
12
|
+
:::
|
13
|
+
|
14
|
+
import Esbuild from '@modern-js/builder-doc/docs/en/shared/esbuild.md';
|
15
|
+
|
16
|
+
<Esbuild />
|
17
|
+
|
18
|
+
## Quick Start
|
19
|
+
|
20
|
+
### Used in Modern.js framework
|
21
|
+
|
22
|
+
The Modern.js framework integrates the Builder's esbuild plugin by default, so you don't need to manually install and register the plugin, just use the [tools.esbuild](https://modernjs.dev/en/configure/app/tools/esbuild.html) configuration:
|
23
|
+
|
24
|
+
```js
|
25
|
+
export default defineConfig({
|
26
|
+
tools: {
|
27
|
+
esbuild: {
|
28
|
+
loader: {
|
29
|
+
target: 'chrome61',
|
30
|
+
},
|
31
|
+
minimize: {
|
32
|
+
target: 'chrome61',
|
33
|
+
},
|
34
|
+
},
|
35
|
+
},
|
36
|
+
});
|
37
|
+
```
|
38
|
+
|
39
|
+
## Config
|
40
|
+
|
41
|
+
The plugin will enable code transformation and minification by default. You can also customize the behavior of the plugin through configuration.
|
42
|
+
|
43
|
+
### loader
|
44
|
+
|
45
|
+
- **Type:**
|
46
|
+
|
47
|
+
```ts
|
48
|
+
type LoaderOptions = EsbuildLoaderOptions | false | undefined;
|
49
|
+
```
|
50
|
+
|
51
|
+
- **Default:**
|
52
|
+
|
53
|
+
```ts
|
54
|
+
const defaultOptions = {
|
55
|
+
target: 'es2015',
|
56
|
+
charset: builderConfig.output.charset,
|
57
|
+
};
|
58
|
+
```
|
59
|
+
|
60
|
+
This config is used to enable JavaScript/TypeScript transformation, which will replace babel-loader and ts-loader with esbuild-loader.
|
61
|
+
|
62
|
+
If you want to modify the options, you can check the [esbuild-loader documentation](https://github.com/privatenumber/esbuild-loader#loader).
|
63
|
+
|
64
|
+
#### Set JSX Format
|
65
|
+
|
66
|
+
When using esbuild for transformation, esbuild will read the `compilerOptions.jsx` field in `tsconfig.json` to determine which JSX syntax to use.
|
67
|
+
|
68
|
+
Therefore, you need to set the correct JSX syntax in `tsconfig.json`.
|
69
|
+
|
70
|
+
For example, React projects need to set `compilerOptions.jsx` to `react-jsx`:
|
71
|
+
|
72
|
+
```json
|
73
|
+
{
|
74
|
+
"compilerOptions": {
|
75
|
+
"jsx": "react-jsx"
|
76
|
+
}
|
77
|
+
}
|
78
|
+
```
|
79
|
+
|
80
|
+
#### Set the target environment
|
81
|
+
|
82
|
+
Use the `target` option to set the target environment for transformation. `target` can be set directly to the JavaScript language version, such as `es6`, `es2020`; it can also be set to several target environments, each target environment is an environment name followed by a version number, such as `['chrome58', 'edge16' ,'firefox57']`. For a detailed introduction of the `target` option, please refer to [esbuild - target](https://esbuild.github.io/api/#target).
|
83
|
+
|
84
|
+
target supports setting to the following environments:
|
85
|
+
|
86
|
+
- chrome
|
87
|
+
- edge
|
88
|
+
- firefox
|
89
|
+
- ie
|
90
|
+
- ios
|
91
|
+
- node
|
92
|
+
- opera
|
93
|
+
- safari
|
94
|
+
|
95
|
+
```ts
|
96
|
+
builderPluginEsbuild({
|
97
|
+
loader: {
|
98
|
+
target: 'chrome61',
|
99
|
+
},
|
100
|
+
});
|
101
|
+
```
|
102
|
+
|
103
|
+
#### Disable transformation
|
104
|
+
|
105
|
+
Set `loader` to `false` to disable esbuild transformation, and Builder will continue to use Babel to transform the code.
|
106
|
+
|
107
|
+
```ts
|
108
|
+
builderPluginEsbuild({
|
109
|
+
loader: false,
|
110
|
+
});
|
111
|
+
```
|
112
|
+
|
113
|
+
### minimize
|
114
|
+
|
115
|
+
- **Type:**
|
116
|
+
|
117
|
+
```ts
|
118
|
+
type MinimizeOptions = EsbuildMinifyOptions | false | undefined;
|
119
|
+
```
|
120
|
+
|
121
|
+
- **Default:**
|
122
|
+
|
123
|
+
```ts
|
124
|
+
const defaultOptions = {
|
125
|
+
css: true,
|
126
|
+
target: 'es2015',
|
127
|
+
format: builderTarget === 'web' ? 'iife' : undefined,
|
128
|
+
};
|
129
|
+
```
|
130
|
+
|
131
|
+
This option is used to enable minification for JavaScript and CSS.
|
132
|
+
|
133
|
+
If you want to modify the options, you can check the [esbuild-loader documentation](https://github.com/privatenumber/esbuild-loader#minifyplugin).
|
134
|
+
|
135
|
+
#### Set the target environment
|
136
|
+
|
137
|
+
Use the `target` option to set the target environment for minification.
|
138
|
+
|
139
|
+
```ts
|
140
|
+
builderPluginEsbuild({
|
141
|
+
minimize: {
|
142
|
+
target: 'chrome61',
|
143
|
+
},
|
144
|
+
});
|
145
|
+
```
|
146
|
+
|
147
|
+
#### Disable minification
|
148
|
+
|
149
|
+
Set `minimize` to `false` to disable esbuild minification, and Builder will continue to use Terser to minify the code.
|
150
|
+
|
151
|
+
```ts
|
152
|
+
builderPluginEsbuild({
|
153
|
+
minimize: false,
|
154
|
+
});
|
155
|
+
```
|
156
|
+
|
157
|
+
## Limitations
|
158
|
+
|
159
|
+
Although esbuild can significantly improve the build performance of existing webpack projects, it still has certain limitations that require special attention.
|
160
|
+
|
161
|
+
### Compatibility
|
162
|
+
|
163
|
+
As a compiler (i.e. `loader` capability), esbuild usually supports at least ES2015 (that is, ES6) syntax, and does not have the ability to automatically inject Polyfill.. If the production environment needs to downgrade to ES5 and below syntax, it is recommended to use SWC compilation.
|
164
|
+
|
165
|
+
You can specify the target syntax version by following config:
|
166
|
+
|
167
|
+
```ts
|
168
|
+
builderPluginEsbuild({
|
169
|
+
loader: {
|
170
|
+
target: 'es2015',
|
171
|
+
},
|
172
|
+
});
|
173
|
+
```
|
174
|
+
|
175
|
+
As a code minify tool (i.e. `minimize` capability), esbuild can minify the code in production environment, and usually supports ES2015 syntax at least.
|
176
|
+
|
177
|
+
If you set the compressed `target` to `es5`, you need to ensure that all codes have been compiled to ES5 codes, otherwise it will cause esbuild compilation error: `Transforming 'xxx' to the configured target environment ("es5") is not supported yet `.
|
178
|
+
|
179
|
+
Therefore, for projects that need to be compatible with ES5 and below syntax in the production environment, please be careful to enable the minimize capability, and it is recommended to use SWC compression.
|
180
|
+
|
181
|
+
You can specify the target syntax version by following config:
|
182
|
+
|
183
|
+
```js
|
184
|
+
builderPluginEsbuild({
|
185
|
+
minimize: {
|
186
|
+
target: 'es2015',
|
187
|
+
},
|
188
|
+
});
|
189
|
+
```
|
190
|
+
|
191
|
+
:::danger Caution
|
192
|
+
Projects that need to be compatible with ES5 and below syntax in the production environment need to be careful to turn on the minimize config.
|
193
|
+
:::
|
194
|
+
|
195
|
+
### Not support Babel plugins
|
196
|
+
|
197
|
+
As a compiler, the syntax transformation function of the original Babel plugins such as `babel-plugin-import` is not available after esbuild is turned on. And since the bottom layer of the plugin uses esbuild's `Transform API`, it does not support esbuild plugins to customize the compilation process.
|
198
|
+
|
199
|
+
If you have requirements related to Babel plugins such as `babel-plugin-import`, you can use the SWC plugin.
|
200
|
+
|
201
|
+
### Bundle Size
|
202
|
+
|
203
|
+
Although the compression speed of esbuild is faster, the compression ratio of esbuild is lower than that of terser, so the bundle size will increase, please use it according to the scenes. Generally speaking, esbuild is more suitable for scenes that are not sensitive to bundle size.
|
204
|
+
|
205
|
+
You can refer to [minification-benchmarks](https://github.com/privatenumber/minification-benchmarks) for a detailed comparison between minimizers.
|