@modern-js/main-doc 2.42.0 → 2.42.2
Sign up to get free protection for your applications and to get access to all the features.
- package/docs/en/guides/advanced-features/inline-assets.mdx +161 -0
- package/docs/en/guides/advanced-features/optimize-bundle.mdx +7 -6
- 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/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/zh/guides/advanced-features/inline-assets.mdx +162 -0
- package/docs/zh/guides/advanced-features/optimize-bundle.mdx +7 -6
- 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/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/package.json +7 -7
@@ -0,0 +1,173 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 10
|
3
|
+
---
|
4
|
+
|
5
|
+
# 构建产物目录
|
6
|
+
|
7
|
+
本章节主要介绍构建产物的目录结构,以及如何控制不同类型产物的输出目录。
|
8
|
+
|
9
|
+
## 默认产物目录
|
10
|
+
|
11
|
+
下面是最基础的产物目录结构,默认情况下,构建产物会生成在当前项目的 `dist` 目录下。
|
12
|
+
|
13
|
+
```bash
|
14
|
+
dist
|
15
|
+
├── static
|
16
|
+
│ ├── css
|
17
|
+
│ │ ├── [name].[hash].css
|
18
|
+
│ │ └── [name].[hash].css.map
|
19
|
+
│ │
|
20
|
+
│ └── js
|
21
|
+
│ ├── [name].[hash].js
|
22
|
+
│ ├── [name].[hash].js.LICENSE.txt
|
23
|
+
│ └── [name].[hash].js.map
|
24
|
+
│
|
25
|
+
└── html
|
26
|
+
└── [name]
|
27
|
+
└── index.html
|
28
|
+
```
|
29
|
+
|
30
|
+
最常见的产物是 HTML 文件、JS 文件和 CSS 文件:
|
31
|
+
|
32
|
+
- HTML 文件:默认输出到 `html` 目录。
|
33
|
+
- JS 文件:默认输出到 `static/js` 目录,
|
34
|
+
- CSS 文件:默认输出到 `static/css` 目录。
|
35
|
+
|
36
|
+
此外,JS 文件和 CSS 文件有时候会生成一些相关产物:
|
37
|
+
|
38
|
+
- License 文件:包含开源许可证信息,默认输出到 JS 文件的同级目录,并添加 `.LICENSE.txt` 后缀。
|
39
|
+
- Source Map 文件:包含保存源代码映射关系,默认输出到 JS 文件和 CSS 文件的同级目录,并添加 `.map` 后缀。
|
40
|
+
|
41
|
+
在产物的文件名称中,`[name]` 表示这个文件对应的入口名称,比如 `index`, `main`。`[hash]` 则是基于该文件的内容生成的哈希值。
|
42
|
+
|
43
|
+
## 修改产物目录
|
44
|
+
|
45
|
+
Builder 提供了多个配置项来修改产物目录和产物名称,你可以:
|
46
|
+
|
47
|
+
- 通过 [output.filename](/configure/app/output/filename) 来修改产物的文件名。
|
48
|
+
- 通过 [output.distPath](/configure/app/output/dist-path) 来修改产物的输出路径。
|
49
|
+
- 通过 [output.legalComments](/configure/app/output/legal-comments) 来修改 License 文件的生成方式。
|
50
|
+
- 通过 [output.disableSourceMap](/configure/app/output/disable-source-map) 来移除 Source Map 文件。
|
51
|
+
- 通过 [html.disableHtmlFolder](/configure/app/html/disable-html-folder) 移除 HTML 产物对应的文件夹。
|
52
|
+
|
53
|
+
## 静态资源
|
54
|
+
|
55
|
+
当你在代码中引用图片、SVG、字体、媒体等类型的静态资源时,它们也会被输出到 `dist/static` 目录下,并根据静态资源类型来自动分配到对应的子目录:
|
56
|
+
|
57
|
+
```bash
|
58
|
+
dist
|
59
|
+
└── static
|
60
|
+
├── image
|
61
|
+
│ └── foo.[hash].png
|
62
|
+
│
|
63
|
+
├── svg
|
64
|
+
│ └── bar.[hash].svg
|
65
|
+
│
|
66
|
+
├── font
|
67
|
+
│ └── baz.[hash].woff2
|
68
|
+
│
|
69
|
+
└── media
|
70
|
+
└── qux.[hash].mp4
|
71
|
+
```
|
72
|
+
|
73
|
+
你可以通过 [output.distPath](/configure/app/output/dist-path) 配置项将这些静态资源统一输入到单个目录下,比如输出到 `assets` 目录:
|
74
|
+
|
75
|
+
```ts
|
76
|
+
export default {
|
77
|
+
output: {
|
78
|
+
distPath: {
|
79
|
+
image: 'assets',
|
80
|
+
svg: 'assets',
|
81
|
+
font: 'assets',
|
82
|
+
media: 'assets',
|
83
|
+
},
|
84
|
+
},
|
85
|
+
};
|
86
|
+
```
|
87
|
+
|
88
|
+
上方的配置会生成以下目录结构:
|
89
|
+
|
90
|
+
```bash
|
91
|
+
dist
|
92
|
+
└── assets
|
93
|
+
├── foo.[hash].png
|
94
|
+
├── bar.[hash].svg
|
95
|
+
├── baz.[hash].woff2
|
96
|
+
└── qux.[hash].mp4
|
97
|
+
```
|
98
|
+
|
99
|
+
## Node.js 产物目录
|
100
|
+
|
101
|
+
当你在 Modern.js 中开启了 SSR 或 SSG 等服务端功能时,Modern.js 会在构建后生成一份 Node.js 产物,并输出到 `bundles` 目录下:
|
102
|
+
|
103
|
+
```bash
|
104
|
+
dist
|
105
|
+
├── bundles
|
106
|
+
│ └── [name].js
|
107
|
+
├── static
|
108
|
+
└── html
|
109
|
+
```
|
110
|
+
|
111
|
+
Node.js 产物通常只包含 JS 文件,不包含 HTML、CSS 等文件。此外,Node 产物的 JS 文件名称也不会自动生成哈希值。
|
112
|
+
|
113
|
+
你可以通过 [output.distPath.server](/configure/app/output/dist-path) 配置项来修改 Node 产物的输出路径。
|
114
|
+
|
115
|
+
比如,将 Node.js 产物输出到 `server` 目录:
|
116
|
+
|
117
|
+
```ts
|
118
|
+
export default {
|
119
|
+
output: {
|
120
|
+
distPath: {
|
121
|
+
server: 'server',
|
122
|
+
},
|
123
|
+
},
|
124
|
+
};
|
125
|
+
```
|
126
|
+
|
127
|
+
## 扁平化产物目录
|
128
|
+
|
129
|
+
有时候你不想产物目录有太多层级,可以将目录设置为空字符串,使生成的产物目录扁平化。
|
130
|
+
|
131
|
+
参考下方的例子:
|
132
|
+
|
133
|
+
```ts
|
134
|
+
export default {
|
135
|
+
output: {
|
136
|
+
distPath: {
|
137
|
+
js: '',
|
138
|
+
css: '',
|
139
|
+
html: '',
|
140
|
+
},
|
141
|
+
},
|
142
|
+
html: {
|
143
|
+
disableHtmlFolder: true,
|
144
|
+
},
|
145
|
+
};
|
146
|
+
```
|
147
|
+
|
148
|
+
上方的配置会生成以下目录结构:
|
149
|
+
|
150
|
+
```bash
|
151
|
+
dist
|
152
|
+
├── [name].[hash].css
|
153
|
+
├── [name].[hash].css.map
|
154
|
+
├── [name].[hash].js
|
155
|
+
├── [name].[hash].js.map
|
156
|
+
└── [name].html
|
157
|
+
```
|
158
|
+
|
159
|
+
## 产物不写入磁盘
|
160
|
+
|
161
|
+
默认情况下,Builder 会将构建产物写入磁盘,以方便开发者查看产物的内容,或是配置静态资源的代理规则。
|
162
|
+
|
163
|
+
在开发环境,你可以选择将构建产物保存在 Dev Server 的内存中,从而减少文件操作产生的开销。
|
164
|
+
|
165
|
+
将 `dev.writeToDisk` 配置项设置为 `false` 即可:
|
166
|
+
|
167
|
+
```ts
|
168
|
+
export default {
|
169
|
+
dev: {
|
170
|
+
writeToDisk: false,
|
171
|
+
},
|
172
|
+
};
|
173
|
+
```
|
@@ -0,0 +1,165 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 10
|
3
|
+
---
|
4
|
+
|
5
|
+
# 引用静态资源
|
6
|
+
|
7
|
+
Modern.js 支持在代码中引用图片、字体、媒体类型的静态资源。
|
8
|
+
|
9
|
+
:::tip 什么是静态资源
|
10
|
+
静态资源是指 Web 应用中不会发生变化的文件。常见的静态资源包括图片、字体、视频、样式表和 JavaScript 文件。这些资源通常存储在服务器或 CDN 上,当用户访问 Web 应用时会被传送到用户的浏览器。由于它们不会发生变化,静态资源可以被浏览器缓存,从而提高 Web 应用的性能。
|
11
|
+
:::
|
12
|
+
|
13
|
+
## 静态资源格式
|
14
|
+
|
15
|
+
以下是 Modern.js 默认支持的静态资源格式:
|
16
|
+
|
17
|
+
- **图片**:png、jpg、jpeg、gif、svg、bmp、webp、ico、apng、avif、tiff。
|
18
|
+
- **字体**:woff、woff2、eot、ttf、otf、ttc。
|
19
|
+
- **媒体**:mp4、webm、ogg、mp3、wav、flac、aac、mov。
|
20
|
+
|
21
|
+
如果你需要引用其他格式的静态资源,请参考 [扩展静态资源类型](#扩展静态资源类型)。
|
22
|
+
|
23
|
+
:::tip SVG 图片
|
24
|
+
SVG 图片是一种特殊情况,Modern.js 提供了 SVG 转 React 组件的能力,对 SVG 进行单独处理,详见 [引用 SVG 资源](/guides/basic-features/svg-assets.html)。
|
25
|
+
:::
|
26
|
+
|
27
|
+
## 在 JS 文件中引用
|
28
|
+
|
29
|
+
在 JS 文件中,可以直接通过 `import` 的方式引用相对路径下的静态资源:
|
30
|
+
|
31
|
+
```tsx
|
32
|
+
// 引用 static 目录下的 logo.png 图片
|
33
|
+
import logo from './static/logo.png';
|
34
|
+
|
35
|
+
export default = () => <img src={logo} />;
|
36
|
+
```
|
37
|
+
|
38
|
+
也支持使用[路径别名](/guides/basic-features/alias.html)来引用:
|
39
|
+
|
40
|
+
```tsx
|
41
|
+
import logo from '@/static/logo.png';
|
42
|
+
|
43
|
+
export default = () => <img src={logo} />;
|
44
|
+
```
|
45
|
+
|
46
|
+
## 在 CSS 文件中引用
|
47
|
+
|
48
|
+
在 CSS 文件中,可以引用相对路径下的静态资源:
|
49
|
+
|
50
|
+
```css
|
51
|
+
.logo {
|
52
|
+
background-image: url('../static/logo.png');
|
53
|
+
}
|
54
|
+
```
|
55
|
+
|
56
|
+
也支持使用[路径别名](/guides/basic-features/alias.html)来引用:
|
57
|
+
|
58
|
+
```css
|
59
|
+
.logo {
|
60
|
+
background-image: url('@/static/logo.png');
|
61
|
+
}
|
62
|
+
```
|
63
|
+
|
64
|
+
## 引用结果
|
65
|
+
|
66
|
+
引用静态资源的结果取决于文件体积:
|
67
|
+
|
68
|
+
- 当文件体积大于 10KB 时,会返回一个 URL,同时文件会被输出到构建产物目录下。
|
69
|
+
- 当文件体积小于 10KB 时,会自动被内联为 Base64 格式。
|
70
|
+
|
71
|
+
```js
|
72
|
+
import largeImage from './static/largeImage.png';
|
73
|
+
import smallImage from './static/smallImage.png';
|
74
|
+
|
75
|
+
console.log(largeImage); // "/static/largeImage.6c12aba3.png"
|
76
|
+
console.log(smallImage); // "data:image/png;base64,iVBORw0KGgo..."
|
77
|
+
```
|
78
|
+
|
79
|
+
关于资源内联的更详细介绍,请参考 [静态资源内联](/guides/advanced-features/inline-assets) 章节。
|
80
|
+
|
81
|
+
## 构建产物
|
82
|
+
|
83
|
+
当静态资源被引用后,会自动被输出到构建产物的目录下,你可以:
|
84
|
+
|
85
|
+
- 通过 [output.filename](/configure/app/output/filename) 来修改产物的文件名。
|
86
|
+
- 通过 [output.distPath](/configure/app/output/dist-path) 来修改产物的输出路径。
|
87
|
+
|
88
|
+
请阅读 [构建产物目录](/guides/basic-features/output-files) 来了解更多细节。
|
89
|
+
|
90
|
+
## URL 前缀
|
91
|
+
|
92
|
+
引用静态资源后返回的 URL 中会自动包含路径前缀:
|
93
|
+
|
94
|
+
- 在开发环境下,通过 [dev.assetPrefix](/configure/app/dev/asset-prefix) 设置路径前缀。
|
95
|
+
- 在生产环境下,通过 [output.assetPrefix](/configure/app/output/asset-prefix) 设置路径前缀。
|
96
|
+
|
97
|
+
比如将 `output.assetPrefix` 设置为 `https://modern.com`:
|
98
|
+
|
99
|
+
```js
|
100
|
+
import logo from './static/logo.png';
|
101
|
+
|
102
|
+
console.log(logo); // "https://modern.com/static/logo.6c12aba3.png"
|
103
|
+
```
|
104
|
+
|
105
|
+
## 添加类型声明
|
106
|
+
|
107
|
+
当你在 TypeScript 代码中引用静态资源时,TypeScript 可能会提示该模块缺少类型定义:
|
108
|
+
|
109
|
+
```
|
110
|
+
TS2307: Cannot find module './logo.png' or its corresponding type declarations.
|
111
|
+
```
|
112
|
+
|
113
|
+
此时你需要为静态资源添加类型声明文件,请在项目中创建 `src/global.d.ts` 文件,并添加相应的类型声明。以 png 图片为例,需要添加以下声明:
|
114
|
+
|
115
|
+
```ts title="src/global.d.ts"
|
116
|
+
declare module '*.png' {
|
117
|
+
const content: string;
|
118
|
+
export default content;
|
119
|
+
}
|
120
|
+
```
|
121
|
+
|
122
|
+
添加类型声明后,如果依然存在上述错误提示,请尝试重启当前 IDE,或者调整 `global.d.ts` 所在的目录,使 TypeScript 能够正确识别类型定义。
|
123
|
+
|
124
|
+
## 扩展静态资源类型
|
125
|
+
|
126
|
+
如果 Modern.js 内置的静态资源类型不能满足你的需求,那么你可以通过 [tools.bundlerChain](/configure/app/tools/bundler-chain) 来修改内置的 webpack / Rspack 配置,并扩展你需要的静态资源类型。
|
127
|
+
|
128
|
+
比如,你需要把 `*.pdf` 文件当做静态资源直接输出到产物目录,可以添加以下配置:
|
129
|
+
|
130
|
+
```ts
|
131
|
+
export default {
|
132
|
+
tools: {
|
133
|
+
bundlerChain(chain) {
|
134
|
+
chain.module
|
135
|
+
.rule('pdf')
|
136
|
+
.test(/\.pdf$/)
|
137
|
+
.type('asset/resource');
|
138
|
+
},
|
139
|
+
},
|
140
|
+
};
|
141
|
+
```
|
142
|
+
|
143
|
+
添加以上配置后,你就可以在代码里引用 `*.pdf` 文件了,比如:
|
144
|
+
|
145
|
+
```js
|
146
|
+
import myFile from './static/myFile.pdf';
|
147
|
+
|
148
|
+
console.log(myFile); // "/static/myFile.6c12aba3.pdf"
|
149
|
+
```
|
150
|
+
|
151
|
+
关于以上配置的更多介绍,请参考:
|
152
|
+
|
153
|
+
- [Rspack 文档 - Asset modules](https://www.rspack.dev/guide/asset-module.html#asset-modules)
|
154
|
+
- [webpack 文档 - Asset modules](https://webpack.js.org/guides/asset-modules/)
|
155
|
+
|
156
|
+
## 图片格式
|
157
|
+
|
158
|
+
在使用图片资源时,你可以根据下方表格中图片的优缺点以及适用场景,来选择合适的图片格式。
|
159
|
+
|
160
|
+
| 格式 | 优点 | 缺点 | 适用场景 |
|
161
|
+
| ---- | ---------------------------------------------------------------- | -------------------------------------- | ---------------------------------------------------------------------------- |
|
162
|
+
| PNG | 无损压缩,不会丢失图片细节,不失真,支持半透明 | 不适合色表复杂的图片 | 适合颜色数量少,边界层次分明的图片,适合用在 logo、icon、透明图等场景 |
|
163
|
+
| JPG | 颜色丰富 | 有损压缩,会导致图片失真,不支持透明度 | 适合颜色数量多,颜色带有渐变、过度复杂的图片,适合用在人像照片、风景图等场景 |
|
164
|
+
| WebP | 同时支持有损压缩与无损压缩,支持透明度,体积比 PNG 和 JPG 小很多 | iOS 兼容性不好 | 几乎任何场景的像素图片,支持 WebP 的宿主环境,都应该首选 WebP 图片格式 |
|
165
|
+
| SVG | 无损格式,不失真,支持透明度 | 不适合复杂图形 | 适合矢量图,适合用于 icon |
|
@@ -0,0 +1,157 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 11
|
3
|
+
---
|
4
|
+
|
5
|
+
# 引用 SVG 资源
|
6
|
+
|
7
|
+
Modern.js 支持在代码中引用 SVG 资源,并将 SVG 图片转换为 React 组件或 URL。
|
8
|
+
|
9
|
+
:::tip 什么是 SVG
|
10
|
+
SVG 是 Scalable Vector Graphics 的缩写,意为可伸缩矢量图形。SVG 是一种用来描述二维矢量图形的 XML-based 格式,可以用来创建可以无限放大或缩小而不失真的图像。因为 SVG 图形是由矢量图形元素组成的,所以可以轻松地在各种尺寸和分辨率下渲染。
|
11
|
+
:::
|
12
|
+
|
13
|
+
## 在 JS 文件中引用
|
14
|
+
|
15
|
+
在 JS 文件中引用 SVG 资源时,如果你具名导入 `ReactComponent` 对象,Modern.js 会调用 [SVGR](https://react-svgr.com/),将 SVG 图片转换为一个 React 组件。
|
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
|
+
如果你使用默认导入,那么 SVG 会被当做普通的静态资源来处理,你会得到一个 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
|
+
## 修改默认导出
|
32
|
+
|
33
|
+
你可以通过 [output.svgDefaultExport](/configure/app/output/svg-default-export) 配置项来修改 SVG 文件默认导出的内容,比如把默认导出的内容设置为 React 组件:
|
34
|
+
|
35
|
+
```ts
|
36
|
+
export default {
|
37
|
+
output: {
|
38
|
+
svgDefaultExport: 'component',
|
39
|
+
},
|
40
|
+
};
|
41
|
+
```
|
42
|
+
|
43
|
+
此时再使用默认导入,你会得到一个 React 组件,而不是 URL:
|
44
|
+
|
45
|
+
```tsx title="src/component/Logo.tsx"
|
46
|
+
import Logo from './static/logo.svg';
|
47
|
+
|
48
|
+
export default () => <Logo />;
|
49
|
+
```
|
50
|
+
|
51
|
+
## 在 CSS 文件中引用
|
52
|
+
|
53
|
+
在 CSS 文件中引用 SVG 资源时,SVG 会被当做一个普通的静态资源来处理,你会得到一个 URL:
|
54
|
+
|
55
|
+
```css
|
56
|
+
.logo {
|
57
|
+
background-image: url('../static/logo.svg');
|
58
|
+
}
|
59
|
+
```
|
60
|
+
|
61
|
+
## 静态资源处理
|
62
|
+
|
63
|
+
当 SVG 不是作为 React 组件,而是作为一个普通的静态资源被引用时,它的处理逻辑和其他静态资源完全一致,也会受到静态资源内联、URL 前缀等规则的影响。
|
64
|
+
|
65
|
+
请阅读 [引用静态资源](/guides/basic-features/static-assets) 章节来了解静态资源的处理规则。
|
66
|
+
|
67
|
+
## 禁用 SVGR 处理
|
68
|
+
|
69
|
+
默认情况下,在 JS 文件中引用 SVG 资源时,Modern.js 会调用 SVGR,将 SVG 图片转换为一个 React 组件。
|
70
|
+
|
71
|
+
如果你确定项目内的所有 SVG 资源都没有当成 React 组件使用时,可以通过将 [disableSvgr](/configure/app/output/disable-svgr) 设置为 true 来关闭此项转换,以提升构建性能。
|
72
|
+
|
73
|
+
```js
|
74
|
+
export default {
|
75
|
+
output: {
|
76
|
+
disableSvgr: true,
|
77
|
+
},
|
78
|
+
};
|
79
|
+
```
|
80
|
+
|
81
|
+
## 添加类型声明
|
82
|
+
|
83
|
+
当你在 TypeScript 代码中引用 SVG 资源时,TypeScript 可能会提示该模块缺少类型定义:
|
84
|
+
|
85
|
+
```
|
86
|
+
TS2307: Cannot find module './logo.svg' or its corresponding type declarations.
|
87
|
+
```
|
88
|
+
|
89
|
+
此时你需要为 SVG 资源添加类型声明文件,请在项目中创建 `src/global.d.ts` 文件,并添加相应的类型声明:
|
90
|
+
|
91
|
+
```ts
|
92
|
+
declare module '*.svg' {
|
93
|
+
export const ReactComponent: React.FunctionComponent<
|
94
|
+
React.SVGProps<SVGSVGElement>
|
95
|
+
>;
|
96
|
+
|
97
|
+
const content: string;
|
98
|
+
export default content;
|
99
|
+
}
|
100
|
+
```
|
101
|
+
|
102
|
+
如果你将 `svgDefaultExport` 设置为 `'component'`,则将类型声明修改为:
|
103
|
+
|
104
|
+
```ts
|
105
|
+
declare module '*.svg' {
|
106
|
+
export const ReactComponent: React.FunctionComponent<
|
107
|
+
React.SVGProps<SVGSVGElement>
|
108
|
+
>;
|
109
|
+
|
110
|
+
export default ReactComponent;
|
111
|
+
}
|
112
|
+
```
|
113
|
+
|
114
|
+
添加类型声明后,如果依然存在上述错误提示,请尝试重启当前 IDE,或者调整 `global.d.ts` 所在的目录,使 TypeScript 能够正确识别类型定义。
|
115
|
+
|
116
|
+
## 修改 SVGR 配置
|
117
|
+
|
118
|
+
当启用 SVGR 时,其默认配置如下:
|
119
|
+
|
120
|
+
```js
|
121
|
+
{
|
122
|
+
svgo: true,
|
123
|
+
svgoConfig: {
|
124
|
+
plugins: [
|
125
|
+
{
|
126
|
+
name: 'preset-default',
|
127
|
+
params: {
|
128
|
+
overrides: {
|
129
|
+
removeViewBox: false,
|
130
|
+
},
|
131
|
+
},
|
132
|
+
},
|
133
|
+
'prefixIds',
|
134
|
+
],
|
135
|
+
},
|
136
|
+
}
|
137
|
+
```
|
138
|
+
|
139
|
+
如果需要修改 SVGR 配置,可通过如下方式:
|
140
|
+
|
141
|
+
```js
|
142
|
+
export default {
|
143
|
+
tools: {
|
144
|
+
bundlerChain: (chain, { CHAIN_ID }) => {
|
145
|
+
chain.module
|
146
|
+
.rule(CHAIN_ID.RULE.SVG)
|
147
|
+
.oneOf(CHAIN_ID.ONE_OF.SVG)
|
148
|
+
.use(CHAIN_ID.USE.SVGR)
|
149
|
+
.tap(options => {
|
150
|
+
// modify svgoConfig
|
151
|
+
options.svgoConfig.plugins[0].params.overrides.removeUselessDefs = false;
|
152
|
+
return options;
|
153
|
+
});
|
154
|
+
},
|
155
|
+
},
|
156
|
+
};
|
157
|
+
```
|
@@ -0,0 +1,66 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 13
|
3
|
+
---
|
4
|
+
|
5
|
+
# 引用 Wasm 资源
|
6
|
+
|
7
|
+
Modern.js 支持在代码引用 WebAssembly 资源。
|
8
|
+
|
9
|
+
:::tip 什么是 WebAssembly
|
10
|
+
WebAssembly(缩写为 wasm)是一种可移植、高性能的字节码格式,被设计用来在现代 Web 浏览器中执行 CPU 密集型计算任务,为 Web 平台带来了接近本地编译代码的性能和可靠性。
|
11
|
+
:::
|
12
|
+
|
13
|
+
## 引用方式
|
14
|
+
|
15
|
+
你可以直接在 JavaScript 文件中引用一个 WebAssembly 模块:
|
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
|
+
也可以通过 dynamic import 来引用 WebAssembly 模块:
|
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
|
+
还可以通过 `new URL` 语法来获取 WebAssembly 模块的路径:
|
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
|
+
## 输出目录
|
43
|
+
|
44
|
+
当 `.wasm` 资源被引用后,默认会被 Modern.js 输出到 `dist/static/wasm` 目录下。
|
45
|
+
|
46
|
+
你可以通过 [output.distPath](/configure/app/output/dist-path) 配置项来修改 `.wasm` 产物的输出目录。
|
47
|
+
|
48
|
+
```ts
|
49
|
+
export default {
|
50
|
+
output: {
|
51
|
+
distPath: {
|
52
|
+
wasm: 'resource/wasm',
|
53
|
+
},
|
54
|
+
},
|
55
|
+
};
|
56
|
+
```
|
57
|
+
|
58
|
+
## 添加类型声明
|
59
|
+
|
60
|
+
当你在 TypeScript 代码中引用 Wasm 文件时,通常需要添加相应的类型声明。
|
61
|
+
|
62
|
+
比如 `add.wasm` 文件导出了 `add()` 方法,那么你可以在同级目录下创建一个 `add.wasm.d.ts` 文件,并添加相应的类型声明:
|
63
|
+
|
64
|
+
```ts title="add.wasm.d.ts"
|
65
|
+
export const add = (num1: number, num2: number) => number;
|
66
|
+
```
|
package/package.json
CHANGED
@@ -15,17 +15,17 @@
|
|
15
15
|
"modern",
|
16
16
|
"modern.js"
|
17
17
|
],
|
18
|
-
"version": "2.42.
|
18
|
+
"version": "2.42.2",
|
19
19
|
"publishConfig": {
|
20
20
|
"registry": "https://registry.npmjs.org/",
|
21
21
|
"access": "public",
|
22
22
|
"provenance": true
|
23
23
|
},
|
24
24
|
"dependencies": {
|
25
|
-
"@modern-js/sandpack-react": "2.42.
|
25
|
+
"@modern-js/sandpack-react": "2.42.2"
|
26
26
|
},
|
27
27
|
"peerDependencies": {
|
28
|
-
"@modern-js/builder-doc": "^2.42.
|
28
|
+
"@modern-js/builder-doc": "^2.42.2"
|
29
29
|
},
|
30
30
|
"devDependencies": {
|
31
31
|
"classnames": "^2",
|
@@ -35,12 +35,12 @@
|
|
35
35
|
"ts-node": "^10.9.1",
|
36
36
|
"typescript": "^5",
|
37
37
|
"fs-extra": "^10",
|
38
|
-
"rspress": "1.
|
39
|
-
"@rspress/shared": "1.
|
38
|
+
"rspress": "1.8.2",
|
39
|
+
"@rspress/shared": "1.8.2",
|
40
40
|
"@types/node": "^16",
|
41
41
|
"@types/fs-extra": "9.0.13",
|
42
|
-
"@modern-js/doc-plugin-auto-sidebar": "2.42.
|
43
|
-
"@modern-js/builder-doc": "2.42.
|
42
|
+
"@modern-js/doc-plugin-auto-sidebar": "2.42.2",
|
43
|
+
"@modern-js/builder-doc": "2.42.2"
|
44
44
|
},
|
45
45
|
"scripts": {
|
46
46
|
"dev": "rspress dev",
|