@modern-js/main-doc 2.42.1 → 2.43.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/docs/en/apis/app/runtime/web-server/middleware.mdx +3 -3
- package/docs/en/configure/app/server/ssr.mdx +2 -0
- package/docs/en/guides/advanced-features/inline-assets.mdx +161 -0
- package/docs/en/guides/advanced-features/optimize-bundle.mdx +8 -7
- package/docs/en/guides/advanced-features/ssr/_category_.json +8 -0
- package/docs/en/guides/advanced-features/ssr/cache.mdx +186 -0
- package/docs/en/guides/advanced-features/ssr/index.mdx +22 -0
- package/docs/en/guides/advanced-features/ssr/stream.mdx +236 -0
- package/docs/en/guides/advanced-features/ssr/usage.mdx +341 -0
- package/docs/en/guides/basic-features/alias.mdx +80 -2
- package/docs/en/guides/basic-features/css-modules.mdx +228 -0
- package/docs/en/guides/basic-features/css.mdx +2 -13
- package/docs/en/guides/basic-features/json-files.mdx +106 -0
- package/docs/en/guides/basic-features/output-files.mdx +173 -0
- package/docs/en/guides/basic-features/static-assets.mdx +165 -0
- package/docs/en/guides/basic-features/svg-assets.mdx +155 -0
- package/docs/en/guides/basic-features/wasm-assets.mdx +66 -0
- package/docs/en/guides/get-started/quick-start.mdx +1 -1
- package/docs/en/guides/get-started/tech-stack.mdx +1 -1
- package/docs/en/guides/topic-detail/framework-plugin/introduction.mdx +63 -16
- package/docs/zh/apis/app/runtime/web-server/middleware.mdx +4 -4
- package/docs/zh/configure/app/server/ssr.mdx +2 -0
- package/docs/zh/guides/advanced-features/inline-assets.mdx +162 -0
- package/docs/zh/guides/advanced-features/optimize-bundle.mdx +8 -7
- package/docs/zh/guides/advanced-features/ssr/_category_.json +8 -0
- package/docs/zh/guides/advanced-features/ssr/cache.mdx +189 -0
- package/docs/zh/guides/advanced-features/ssr/index.mdx +22 -0
- package/docs/zh/guides/advanced-features/ssr/stream.mdx +240 -0
- package/docs/zh/guides/advanced-features/{ssr.mdx → ssr/usage.mdx} +7 -225
- package/docs/zh/guides/basic-features/alias.mdx +80 -2
- package/docs/zh/guides/basic-features/css-modules.mdx +229 -0
- package/docs/zh/guides/basic-features/css.mdx +2 -13
- package/docs/zh/guides/basic-features/data/data-write.mdx +1 -1
- package/docs/zh/guides/basic-features/json-files.mdx +106 -0
- package/docs/zh/guides/basic-features/output-files.mdx +173 -0
- package/docs/zh/guides/basic-features/static-assets.mdx +165 -0
- package/docs/zh/guides/basic-features/svg-assets.mdx +157 -0
- package/docs/zh/guides/basic-features/wasm-assets.mdx +66 -0
- package/docs/zh/guides/get-started/quick-start.mdx +1 -1
- package/docs/zh/guides/get-started/tech-stack.mdx +1 -1
- package/docs/zh/guides/topic-detail/framework-plugin/introduction.mdx +61 -16
- package/package.json +7 -7
@@ -0,0 +1,155 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 11
|
3
|
+
---
|
4
|
+
|
5
|
+
# Import SVG Assets
|
6
|
+
|
7
|
+
Modern.js supports import SVG assets and transform SVG into React components or URLs.
|
8
|
+
|
9
|
+
:::tip What is SVG
|
10
|
+
SVG stands for Scalable Vector Graphics. It is a type of image format that uses vector graphics to represent images. Vector graphics are different from raster graphics, which are made up of pixels. Instead, vector graphics use geometric shapes, lines, and curves to represent images. Because vector graphics are not made up of pixels, they can be scaled to any size without losing resolution or quality.
|
11
|
+
:::
|
12
|
+
|
13
|
+
## Import SVG in JS file
|
14
|
+
|
15
|
+
When import an SVG in a JS file, if you import `ReactComponent`, Modern.js will call [SVGR](https://react-svgr.com/) to convert the SVG into a React component.
|
16
|
+
|
17
|
+
```tsx title="src/component/Logo.tsx"
|
18
|
+
import { ReactComponent as Logo } from './static/logo.svg';
|
19
|
+
|
20
|
+
export default () => <Logo />;
|
21
|
+
```
|
22
|
+
|
23
|
+
If you use the default import, then the SVG will be treated as a normal static asset and you will get a URL:
|
24
|
+
|
25
|
+
```tsx title="src/component/Logo.tsx"
|
26
|
+
import logoURL from './static/logo.svg';
|
27
|
+
|
28
|
+
console.log(logoURL); // => "/static/logo.6c12aba3.png"
|
29
|
+
```
|
30
|
+
|
31
|
+
## Modify the Default Export
|
32
|
+
|
33
|
+
You can modify the default export of SVG files through the [output.svgDefaultExport](/configure/app/output/svg-default-export) config. For example, set the default export as a React component:
|
34
|
+
|
35
|
+
```ts
|
36
|
+
export default {
|
37
|
+
output: {
|
38
|
+
svgDefaultExport: 'component',
|
39
|
+
},
|
40
|
+
};
|
41
|
+
```
|
42
|
+
|
43
|
+
Then import the SVG, you'll get a React component instead of a URL:
|
44
|
+
|
45
|
+
```tsx title="src/component/Logo.tsx"
|
46
|
+
import Logo from './static/logo.svg';
|
47
|
+
|
48
|
+
export default () => <Logo />;
|
49
|
+
```
|
50
|
+
|
51
|
+
## Import SVG in CSS file
|
52
|
+
|
53
|
+
When import an SVG in a CSS file, the SVG is treated as a normal static asset and you will get a URL:
|
54
|
+
|
55
|
+
```css
|
56
|
+
.logo {
|
57
|
+
background-image: url('../static/logo.svg');
|
58
|
+
}
|
59
|
+
```
|
60
|
+
|
61
|
+
## Assets Processing
|
62
|
+
|
63
|
+
When SVG is imported not as a React component but as a normal static asset, it is processed exactly the same as other static assets, and it is also affected by rules such as assets inlining and URL prefixing.
|
64
|
+
|
65
|
+
Please read the [Import Static Assets](/guides/basic-features/static-assets) section to understand the processing rules for static assets.
|
66
|
+
|
67
|
+
## Disable SVGR Processing
|
68
|
+
|
69
|
+
By default, when an SVG resource is referenced in a JS file, Modern.js will call SVGR to convert the SVG into a React component. If you are sure that all SVG resources in your project are not being used as React components, you can turn off this conversion by setting [disableSvgr](/configure/app/output/disable-svgr) to true to improve build performance.
|
70
|
+
|
71
|
+
```js
|
72
|
+
export default {
|
73
|
+
output: {
|
74
|
+
disableSvgr: true,
|
75
|
+
},
|
76
|
+
};
|
77
|
+
```
|
78
|
+
|
79
|
+
## Add type declaration
|
80
|
+
|
81
|
+
When you reference an SVG asset in TypeScript code, TypeScript may prompt that the module is missing a type definition:
|
82
|
+
|
83
|
+
```
|
84
|
+
TS2307: Cannot find module './logo.svg' or its corresponding type declarations.
|
85
|
+
```
|
86
|
+
|
87
|
+
To fix this, you need to add a type declaration file for the SVG asset, please create a `src/global.d.ts` file, and add the following type declaration:
|
88
|
+
|
89
|
+
```ts
|
90
|
+
declare module '*.svg' {
|
91
|
+
export const ReactComponent: React.FunctionComponent<
|
92
|
+
React.SVGProps<SVGSVGElement>
|
93
|
+
>;
|
94
|
+
|
95
|
+
const content: string;
|
96
|
+
export default content;
|
97
|
+
}
|
98
|
+
```
|
99
|
+
|
100
|
+
If you set `svgDefaultExport` to `'component'`, then change the type declaration to:
|
101
|
+
|
102
|
+
```ts
|
103
|
+
declare module '*.svg' {
|
104
|
+
export const ReactComponent: React.FunctionComponent<
|
105
|
+
React.SVGProps<SVGSVGElement>
|
106
|
+
>;
|
107
|
+
|
108
|
+
export default ReactComponent;
|
109
|
+
}
|
110
|
+
```
|
111
|
+
|
112
|
+
After adding the type declaration, if the type error still exists, you can try to restart the current IDE, or adjust the directory where `global.d.ts` is located, making sure the TypeScript can correctly identify the type definition.
|
113
|
+
|
114
|
+
## Modify the SVGR configuration
|
115
|
+
|
116
|
+
When SVGR is enabled, its default configuration is as follows:
|
117
|
+
|
118
|
+
```js
|
119
|
+
{
|
120
|
+
svgo: true,
|
121
|
+
svgoConfig: {
|
122
|
+
plugins: [
|
123
|
+
{
|
124
|
+
name: 'preset-default',
|
125
|
+
params: {
|
126
|
+
overrides: {
|
127
|
+
removeViewBox: false,
|
128
|
+
},
|
129
|
+
},
|
130
|
+
},
|
131
|
+
'prefixIds',
|
132
|
+
],
|
133
|
+
},
|
134
|
+
}
|
135
|
+
```
|
136
|
+
|
137
|
+
If you need to modify the SVGR configuration, you can do the following:
|
138
|
+
|
139
|
+
```js
|
140
|
+
export default {
|
141
|
+
tools: {
|
142
|
+
bundlerChain: (chain, { CHAIN_ID }) => {
|
143
|
+
chain.module
|
144
|
+
.rule(CHAIN_ID.RULE.SVG)
|
145
|
+
.oneOf(CHAIN_ID.ONE_OF.SVG)
|
146
|
+
.use(CHAIN_ID.USE.SVGR)
|
147
|
+
.tap(options => {
|
148
|
+
// modify svgoConfig
|
149
|
+
options.svgoConfig.plugins[0].params.overrides.removeUselessDefs = false;
|
150
|
+
return options;
|
151
|
+
});
|
152
|
+
},
|
153
|
+
},
|
154
|
+
};
|
155
|
+
```
|
@@ -0,0 +1,66 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 13
|
3
|
+
---
|
4
|
+
|
5
|
+
# Import Wasm Assets
|
6
|
+
|
7
|
+
Modern.js supports import WebAssembly assets in code.
|
8
|
+
|
9
|
+
:::tip What is WebAssembly
|
10
|
+
WebAssembly (Wasm) is a portable, high-performance binary format designed to execute CPU-intensive computing tasks in modern web browsers, bringing performance and reliability similar to native compiled code to the web platform.
|
11
|
+
:::
|
12
|
+
|
13
|
+
## Import
|
14
|
+
|
15
|
+
You can import a WebAssembly module directly in a JavaScript file:
|
16
|
+
|
17
|
+
```js title="index.js"
|
18
|
+
import { add } from './add.wasm';
|
19
|
+
|
20
|
+
console.log(add); // [native code]
|
21
|
+
console.log(add(1, 2)); // 3
|
22
|
+
```
|
23
|
+
|
24
|
+
WebAssembly modules can also be imported via dynamic import:
|
25
|
+
|
26
|
+
```js title="index.js"
|
27
|
+
import('./add.wasm').then(({ add }) => {
|
28
|
+
console.log('---- Async Wasm Module');
|
29
|
+
console.log(add); // [native code]
|
30
|
+
console.log(add(1, 2)); // 3
|
31
|
+
});
|
32
|
+
```
|
33
|
+
|
34
|
+
You can also get the path of a WebAssembly module using the `new URL` syntax:
|
35
|
+
|
36
|
+
```js title="index.js"
|
37
|
+
const wasmURL = new URL('./add.wasm', import.meta.url);
|
38
|
+
|
39
|
+
console.log(wasmURL).pathname; // "/static/wasm/[hash].module.wasm"
|
40
|
+
```
|
41
|
+
|
42
|
+
## Output Directory
|
43
|
+
|
44
|
+
When a `.wasm` asset is imported, it will be output by Modern.js to the `dist/static/wasm` directory by default.
|
45
|
+
|
46
|
+
You can change the output directory of `.wasm` files via [output.distPath](/configure/app/output/dist-path) config.
|
47
|
+
|
48
|
+
```ts
|
49
|
+
export default {
|
50
|
+
output: {
|
51
|
+
distPath: {
|
52
|
+
wasm: 'resource/wasm',
|
53
|
+
},
|
54
|
+
},
|
55
|
+
};
|
56
|
+
```
|
57
|
+
|
58
|
+
## Add Type Declaration
|
59
|
+
|
60
|
+
When you import a Wasm file in TypeScript code, you usually need to add the corresponding type declaration.
|
61
|
+
|
62
|
+
For example, the `add.wasm` file exports an `add()` method, then you can create an `add.wasm.d.ts` file in the same directory and add the corresponding type declaration:
|
63
|
+
|
64
|
+
```ts title="add.wasm.d.ts"
|
65
|
+
export const add = (num1: number, num2: number) => number;
|
66
|
+
```
|
@@ -116,7 +116,7 @@ dist
|
|
116
116
|
└── js
|
117
117
|
```
|
118
118
|
|
119
|
-
> If you need to customize the directory of the build artifacts, please refer to [Output files](
|
119
|
+
> If you need to customize the directory of the build artifacts, please refer to [Output files](/guides/basic-features/output-files).
|
120
120
|
|
121
121
|
## Verify
|
122
122
|
|
@@ -97,7 +97,7 @@ Modern.js supports three CSS preprocessors: [Sass](https://sass-lang.com/), [Les
|
|
97
97
|
|
98
98
|
Modern.js provides out-of-the-box support for [CSS Modules](https://github.com/css-modules/css-modules), which is implemented internally based on [css-loader](https://www.npmjs.com/package/css-loader).
|
99
99
|
|
100
|
-
Please refer to ["Use CSS Modules"](
|
100
|
+
Please refer to ["Use CSS Modules"](/guides/basic-features/css-modules) for usage instructions.
|
101
101
|
|
102
102
|
---
|
103
103
|
|
@@ -6,22 +6,69 @@ sidebar_position: 1
|
|
6
6
|
|
7
7
|
## Modern.js Plugin System
|
8
8
|
|
9
|
-
Modern.js
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
9
|
+
Modern.js offers a comprehensive plugin system with a complete lifecycle. Plugins can be used to extend different stages of project operation, request handling, rendering, and more.
|
10
|
+
|
11
|
+
## Usage
|
12
|
+
|
13
|
+
Plugins must be explicitly registered in the configuration file to be effective. When you need to add plugins to Modern.js, you can configure them in the `[plugin](/configure/app/plugins.html)` field:
|
14
|
+
|
15
|
+
```ts title="edenx.config.ts"
|
16
|
+
// an example for bff
|
17
|
+
import { appTools, defineConfig } from '@modern-js/app-tools';
|
18
|
+
import { bffPlugin } from '@modern-js/plugin-bff';
|
19
|
+
|
20
|
+
export default defineConfig({
|
21
|
+
plugins: [appTools(), bffPlugin()],
|
22
|
+
});
|
23
|
+
```
|
24
|
+
|
25
|
+
:::note
|
26
|
+
Note that this configuration only supports adding Modern.js plugins and does not support adding Webpack plugins.
|
27
|
+
:::
|
28
|
+
|
29
|
+
## Official Plugins
|
30
|
+
|
31
|
+
Modern.js offers a range of official plugins, which are integrated with the Modern.js generator. All the functionalities of the official plugins can be enabled by executing the `new` command. For instance, to enable the BFF (Backend For Frontend) feature:
|
32
|
+
|
33
|
+
```bash
|
34
|
+
$ npx modern new
|
35
|
+
? Please select the operation you want: Enable Features
|
36
|
+
? Please select the feature name: (Use arrow keys)
|
37
|
+
Enable Tailwind CSS
|
38
|
+
❯ Enable BFF
|
39
|
+
Enable SSG
|
40
|
+
Enable Micro Frontend
|
41
|
+
Enable Unit Test / Integration Test
|
42
|
+
Enable Visual Testing (Storybook)
|
43
|
+
```
|
44
|
+
|
45
|
+
After the selection is completed, the Modern.js generator will automatically install the corresponding plugins and third-party dependencies. Upon completion of the installation, you will see:
|
46
|
+
|
47
|
+
```bash
|
48
|
+
[INFO] Dependency automatic installation succeeded
|
49
|
+
|
50
|
+
[INFO] install plugin dependencies success!add follow code to modern.config.ts :
|
51
|
+
|
52
|
+
import { bffPlugin } from '@modern-js/plugin-bff';
|
53
|
+
import { expressPlugin } from '@modern-js/plugin-express';
|
54
|
+
|
55
|
+
export default defineConfig({
|
56
|
+
...,
|
57
|
+
plugins: [..., bffPlugin(), expressPlugin()],
|
58
|
+
});
|
59
|
+
```
|
60
|
+
|
61
|
+
At this point, you can add the plugin to the configuration file according to the output in the console.
|
62
|
+
|
63
|
+
## Composition
|
64
|
+
|
65
|
+
The Modern.js plugin system is mainly divided into three parts: Hook model, Manager, and Context Sharing Mechanism.
|
66
|
+
|
67
|
+
- The Hook model is used to determine the execution logic of the current Hook.
|
68
|
+
- The Manager controls the execution and scheduling of Hooks.
|
69
|
+
- The Context Sharing Mechanism is used to pass information between different Hooks.
|
70
|
+
|
71
|
+
Currently, Modern.js offers several different Hook models: **Pipeline, Waterfall, Workflow**.
|
25
72
|
|
26
73
|
:::note
|
27
74
|
Subsequent chapters will introduce the execution methods of each model in detail.
|
@@ -91,7 +91,7 @@ type MiddlewareContext = {
|
|
91
91
|
```ts
|
92
92
|
export const Middleware = () => async (ctx, next) => {
|
93
93
|
const start = Date.now();
|
94
|
-
ctx.res.once('finish', () => {
|
94
|
+
ctx.source.res.once('finish', () => {
|
95
95
|
console.log(Date.now() - start);
|
96
96
|
});
|
97
97
|
};
|
@@ -99,12 +99,12 @@ export const Middleware = () => async (ctx, next) => {
|
|
99
99
|
|
100
100
|
### 注入服务端工具 & 数据
|
101
101
|
|
102
|
-
Modern.js 提供了 `
|
102
|
+
Modern.js 提供了 `response.locals` 属性用来存放当前请求的局部变量。
|
103
103
|
|
104
104
|
```ts
|
105
105
|
export const Middleware = () => async (ctx, next) => {
|
106
|
-
ctx.
|
107
|
-
ctx.
|
106
|
+
ctx.response.locals.id = 'Modern.js';
|
107
|
+
ctx.response.locals.rpc = createRpcInstance();
|
108
108
|
};
|
109
109
|
```
|
110
110
|
|
@@ -30,6 +30,7 @@ export default defineConfig({
|
|
30
30
|
- `inlineScript`:`boolean = true`,默认情况下,SSR 的数据会以内联脚本的方式注入到 HTML 中,并且直接赋值给全局变量。配置为 `false` 后,会下发 JSON,而不是赋值给全局变量。
|
31
31
|
- `disablePrerender`: `boolean = fasle`, 为了兼容旧数据请求方式 - `useLoader`, 默认情况下 Modern.js 会对组件进行一次预渲染即有两次渲染。
|
32
32
|
开发者在保证项目中没有使用 useLoader Api 情况下, 可通过配置 `disablePrerender=true`来减少一次渲染。
|
33
|
+
- `unsafeHeaders`: `string[] = []`, 为了安全考虑,Modern.js 不会往 SSR_DATA 添加过多的内容。开发者可以通过该配置,对需要注入的 headers 进行配置。
|
33
34
|
|
34
35
|
```ts title="modern.config.ts"
|
35
36
|
export default defineConfig({
|
@@ -39,6 +40,7 @@ export default defineConfig({
|
|
39
40
|
mode: 'stream',
|
40
41
|
inlineScript: false,
|
41
42
|
disablePrerender: true,
|
43
|
+
unsafeHeaders: ['User-Agent'],
|
42
44
|
},
|
43
45
|
},
|
44
46
|
});
|
@@ -0,0 +1,162 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 13
|
3
|
+
---
|
4
|
+
|
5
|
+
# 静态资源内联
|
6
|
+
|
7
|
+
静态资源内联是一种优化网页性能的方法,它指的是将静态资源直接内联到 HTML 或 JS 文件中,而不是使用外部文件引用的方式。这样做的好处是减少了浏览器发起的请求数,从而提高页面的加载速度。
|
8
|
+
|
9
|
+
不过,静态资源内联也有一些缺点,比如增加了单个文件的体积,可能会导致加载变慢。所以在实际应用中,需要根据具体情况来决定是否使用静态资源内联。
|
10
|
+
|
11
|
+
Modern.js 默认会自动内联体积小于 10KB 的静态资源,但有时候你可能需要手动控制某些特殊资源,让其强制内联或者强制不内联,这篇文档阐述了如何进行精确地控制静态资源内联行为。
|
12
|
+
|
13
|
+
## 自动内联
|
14
|
+
|
15
|
+
默认情况下,当图片、字体、媒体等类型的文件体积小于阈值(默认为 10KB)时,Modern.js 会将资源进行内联处理,资源内联后,会被转换成一个 Base64 编码的字符串,不再会发送独立的 HTTP 请求。当文件体积大于或等于该阈值时,则会被作为单独的资源文件,通过独立的 HTTP 请求来加载。
|
16
|
+
|
17
|
+
自动内联的体积阈值可以通过 [output.dataUriLimit](/configure/app/output/data-uri-limit) 配置项修改。例如,修改图片资源的阈值为 5000 字节,设置视频资源不内联:
|
18
|
+
|
19
|
+
```ts
|
20
|
+
export default {
|
21
|
+
output: {
|
22
|
+
dataUriLimit: {
|
23
|
+
image: 5000,
|
24
|
+
media: 0,
|
25
|
+
},
|
26
|
+
},
|
27
|
+
};
|
28
|
+
```
|
29
|
+
|
30
|
+
## 强制内联
|
31
|
+
|
32
|
+
你可以通过在引入资源时添加 `inline` URL 参数来强制内联该资源,无论该资源的体积是否小于阈值。
|
33
|
+
|
34
|
+
```tsx
|
35
|
+
import React from 'react';
|
36
|
+
import img from './foo.png?inline';
|
37
|
+
|
38
|
+
export default function Foo() {
|
39
|
+
return <img src={img} />;
|
40
|
+
}
|
41
|
+
```
|
42
|
+
|
43
|
+
在上面这个例子中,`foo.png` 图片将始终被内联,无论该图片的大小是否大于阈值。
|
44
|
+
|
45
|
+
除了 `inline` 参数以外,你也可以使用 `__inline` 参数来强制内联该资源:
|
46
|
+
|
47
|
+
```tsx
|
48
|
+
import img from './foo.png?__inline';
|
49
|
+
```
|
50
|
+
|
51
|
+
### 从 CSS 文件中引用
|
52
|
+
|
53
|
+
当你在 CSS 文件中引用静态资源时,同样可以通过 `inline` 或 `__inline` 参数来强制内联资源。
|
54
|
+
|
55
|
+
```css
|
56
|
+
.foo {
|
57
|
+
background-image: url('./icon.png?inline');
|
58
|
+
}
|
59
|
+
```
|
60
|
+
|
61
|
+
:::tip 你真的需要强制内联吗?
|
62
|
+
内联体积过大的资源时,会显著增加页面的白屏时间或首次可交互时间,这会损害用户体验。并且当你将一个静态资源多次内联到 CSS 文件中时,base64 内容会重复注入,导致产物体积增大。因此,请酌情使用强制内联。
|
63
|
+
:::
|
64
|
+
|
65
|
+
## 强制不内联
|
66
|
+
|
67
|
+
当你想把一些资源始终作为单独的资源文件来处理,无论该资源的体积多小时,你可以添加 `url` URL 参数来强制不内联该资源。
|
68
|
+
|
69
|
+
```tsx
|
70
|
+
import React from 'react';
|
71
|
+
import img from './foo.png?url';
|
72
|
+
|
73
|
+
export default function Foo() {
|
74
|
+
return <img src={img} />;
|
75
|
+
}
|
76
|
+
```
|
77
|
+
|
78
|
+
在上面这个例子中,`foo.png` 图片将始终通过单独的资源文件加载,无论该图片的大小是否小于阈值。
|
79
|
+
|
80
|
+
除了 `url` 参数以外,你也可以使用 `__inline=false` 参数来强制不内联该资源:
|
81
|
+
|
82
|
+
```tsx
|
83
|
+
import img from './foo.png?__inline=false';
|
84
|
+
```
|
85
|
+
|
86
|
+
### 从 CSS 文件中引用
|
87
|
+
|
88
|
+
当你在 CSS 文件中引用静态资源时,同样可以通过 `url` 或 `__inline=false` 参数来强制不内联资源。
|
89
|
+
|
90
|
+
```css
|
91
|
+
.foo {
|
92
|
+
background-image: url('./icon.png?url');
|
93
|
+
}
|
94
|
+
```
|
95
|
+
|
96
|
+
:::tip 你真的需要把资源排除内联吗?
|
97
|
+
将资源排除内联将增加 Web App 需要加载的资源数量,这对于弱网环境,或是未开启 HTTP2 的场景下,将会降低资源加载效率,因此,请酌情使用强制不内联。
|
98
|
+
:::
|
99
|
+
|
100
|
+
## 内联 JS 文件
|
101
|
+
|
102
|
+
除了将静态资源文件内联到 JS 文件里,Modern.js 也支持将 JS 文件内联到 HTML 文件中。
|
103
|
+
|
104
|
+
只需要开启 [output.enableInlineScripts](/configure/app/output/enable-inline-scripts) 配置项,构建生成的 JS 文件将不会被写入产物目录下,而是会直接内联到对应的 HTML 文件中。
|
105
|
+
|
106
|
+
```ts
|
107
|
+
export default {
|
108
|
+
output: {
|
109
|
+
enableInlineScripts: true,
|
110
|
+
},
|
111
|
+
};
|
112
|
+
```
|
113
|
+
|
114
|
+
:::tip
|
115
|
+
内联 JS 文件可能会导致 HTML 单文件体积过大,并且不利于静态资源缓存,请酌情使用。
|
116
|
+
:::
|
117
|
+
|
118
|
+
## 内联 CSS 文件
|
119
|
+
|
120
|
+
你也可以将 CSS 文件内联到 HTML 文件中。
|
121
|
+
|
122
|
+
只需要开启 [output.enableInlineStyles](/configure/app/output/enable-inline-styles) 配置项,构建生成的 CSS 文件将不会被写入产物目录下,而是会直接内联到对应的 HTML 文件中。
|
123
|
+
|
124
|
+
```ts
|
125
|
+
export default {
|
126
|
+
output: {
|
127
|
+
enableInlineStyles: true,
|
128
|
+
},
|
129
|
+
};
|
130
|
+
```
|
131
|
+
|
132
|
+
## 添加类型声明
|
133
|
+
|
134
|
+
当你在 TypeScript 代码中使用 `?inline` 和 `?url` 等 URL 参数时,TypeScript 可能会提示该模块缺少类型定义:
|
135
|
+
|
136
|
+
```
|
137
|
+
TS2307: Cannot find module './logo.png?inline' or its corresponding type declarations.
|
138
|
+
```
|
139
|
+
|
140
|
+
此时你需要为这些 URL 参数添加类型声明,请在项目中创建 `src/global.d.ts` 文件,并添加以下类型声明:
|
141
|
+
|
142
|
+
```ts
|
143
|
+
declare module '*?inline' {
|
144
|
+
const content: string;
|
145
|
+
export default content;
|
146
|
+
}
|
147
|
+
|
148
|
+
declare module '*?inline' {
|
149
|
+
const content: string;
|
150
|
+
export default content;
|
151
|
+
}
|
152
|
+
|
153
|
+
declare module '*?__inline' {
|
154
|
+
const content: string;
|
155
|
+
export default content;
|
156
|
+
}
|
157
|
+
|
158
|
+
declare module '*?inline=false' {
|
159
|
+
const content: string;
|
160
|
+
export default content;
|
161
|
+
}
|
162
|
+
```
|
@@ -4,7 +4,7 @@ sidebar_position: 13
|
|
4
4
|
|
5
5
|
# 产物体积优化
|
6
6
|
|
7
|
-
产物体积的优化在生产环境中是非常重要的,因为它直接影响到了线上的用户体验。在这篇文档中,我们将介绍在
|
7
|
+
产物体积的优化在生产环境中是非常重要的,因为它直接影响到了线上的用户体验。在这篇文档中,我们将介绍在 Modern.js 中一些常见的产物体积优化方式。
|
8
8
|
|
9
9
|
## 减少重复依赖
|
10
10
|
|
@@ -77,18 +77,19 @@ export default {
|
|
77
77
|
```
|
78
78
|
|
79
79
|
:::tip
|
80
|
-
请阅读 [浏览器兼容性](
|
80
|
+
请阅读 [浏览器兼容性](/guides/advanced-features/compatibility) 章节来了解更多关于 polyfill 的用法。
|
81
81
|
:::
|
82
82
|
|
83
83
|
## 使用图片压缩
|
84
84
|
|
85
|
-
在一般的前端项目中,图片资源的体积往往是项目产物体积的大头,因此如果能尽可能精简图片的体积,那么将会对项目的打包产物体积起到明显的优化效果。你可以在
|
85
|
+
在一般的前端项目中,图片资源的体积往往是项目产物体积的大头,因此如果能尽可能精简图片的体积,那么将会对项目的打包产物体积起到明显的优化效果。你可以在 Modern.js 中注册插件来启用图片压缩功能:
|
86
86
|
|
87
|
-
```js
|
87
|
+
```js title="modern.config.ts"
|
88
88
|
import { builderPluginImageCompress } from '@modern-js/builder-plugin-image-compress';
|
89
89
|
|
90
|
-
|
91
|
-
|
90
|
+
export default {
|
91
|
+
builderPlugins: [builderPluginImageCompress()],
|
92
|
+
};
|
92
93
|
```
|
93
94
|
|
94
95
|
详见 [Image Compress 插件](https://modernjs.dev/builder/plugins/plugin-image-compress.html)。
|
@@ -97,7 +98,7 @@ builder.addPlugins([builderPluginImageCompress()]);
|
|
97
98
|
|
98
99
|
良好的拆包策略对于提升应用的加载性能是十分重要的,可以充分利用浏览器的缓存机制,减少请求数量,加快页面加载速度。
|
99
100
|
|
100
|
-
在
|
101
|
+
在 Modern.js 中内置了[多种拆包策略](https://modernjs.dev/builder/guide/optimization/split-chunk),可以满足大部分应用的需求,你也可以根据自己的业务场景,自定义拆包配置。
|
101
102
|
|
102
103
|
比如将 node_modules 下的 `axios` 库拆分到 `axios.js` 中:
|
103
104
|
|