@modern-js/main-doc 2.42.1 → 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,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
|
|
@@ -82,13 +82,14 @@ export default {
|
|
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
|
|
@@ -4,9 +4,87 @@ sidebar_position: 4
|
|
4
4
|
|
5
5
|
# 路径别名
|
6
6
|
|
7
|
-
|
7
|
+
路径别名(alias)允许开发者为模块定义别名,以便于在代码中更方便的引用它们。当你想要使用一个简短、易于记忆的名称来代替冗长复杂的路径时,这将非常有用。
|
8
8
|
|
9
|
-
|
9
|
+
例如,假如你在项目中经常引用 `src/common/request.ts` 模块,你可以为它定义一个别名 `@request`,然后在代码中通过 `import request from '@request'` 来引用它,而不需要每次都写出完整的相对路径。同时,这也允许你将模块移动到不同的位置,而不需要更新代码中的所有 import 语法。
|
10
|
+
|
11
|
+
在 Modern.js 中,你有两种方式可以设置路径别名:
|
12
|
+
|
13
|
+
- 通过 `tsconfig.json` 中的 `paths` 配置。
|
14
|
+
- 通过 [source.alias](/configure/app/source/alias) 配置。
|
15
|
+
|
16
|
+
## 通过 `tsconfig.json` 的 `paths` 配置
|
17
|
+
|
18
|
+
你可以通过 `tsconfig.json` 中的 `paths` 来配置别名,这是我们在 TypeScript 项目中推荐使用的方式,因为它可以解决路径别名的 TS 类型问题。
|
19
|
+
|
20
|
+
比如:
|
21
|
+
|
22
|
+
```json title="tsconfig.json"
|
23
|
+
{
|
24
|
+
"compilerOptions": {
|
25
|
+
"paths": {
|
26
|
+
"@common/*": ["./src/common/*"]
|
27
|
+
}
|
28
|
+
}
|
29
|
+
}
|
30
|
+
```
|
31
|
+
|
32
|
+
以上配置完成后,如果你在代码中引用 `@common/Foo.tsx`, 则会映射到 `<project>/src/common/Foo.tsx` 路径上。
|
33
|
+
|
34
|
+
:::tip
|
35
|
+
你可以阅读 [TypeScript - paths](https://www.typescriptlang.org/tsconfig#paths) 文档来了解更多用法。
|
36
|
+
:::
|
37
|
+
|
38
|
+
## 通过 `source.alias` 配置
|
39
|
+
|
40
|
+
Modern.js 提供了 [source.alias](/configure/app/source/alias) 配置项,对应 webpack / Rspack 原生的 [resolve.alias](https://webpack.js.org/configuration/resolve/#resolvealias) 配置,你可以通过对象或者函数的方式来配置这个选项。
|
41
|
+
|
42
|
+
### 使用场景
|
43
|
+
|
44
|
+
由于 `tsconfig.json` 的 `paths` 配置是写在静态 JSON 文件里的,因此它不具备动态性。
|
45
|
+
|
46
|
+
而 `source.alias` 则可以弥补这一不足,你可以通过 JavaScript 代码来动态设置 `source.alias`(比如基于环境变量来设置)。
|
47
|
+
|
48
|
+
### 对象用法
|
49
|
+
|
50
|
+
你可以通过对象的方式来配置 `source.alias`,其中的相对路径会被自动补全为绝对路径。
|
51
|
+
|
52
|
+
比如:
|
53
|
+
|
54
|
+
```js
|
55
|
+
export default {
|
56
|
+
source: {
|
57
|
+
alias: {
|
58
|
+
'@common': './src/common',
|
59
|
+
},
|
60
|
+
},
|
61
|
+
};
|
62
|
+
```
|
63
|
+
|
64
|
+
以上配置完成后,如果你在代码中引用 `@common/Foo.tsx`, 则会映射到 `<project>/src/common/Foo.tsx` 路径上。
|
65
|
+
|
66
|
+
### 函数用法
|
67
|
+
|
68
|
+
你也可以将 `source.alias` 配置为一个函数,拿到内置的 `alias` 对象,对其进行修改。
|
69
|
+
|
70
|
+
比如:
|
71
|
+
|
72
|
+
```js
|
73
|
+
export default {
|
74
|
+
source: {
|
75
|
+
alias: alias => {
|
76
|
+
alias['@common'] = './src/common';
|
77
|
+
return alias;
|
78
|
+
},
|
79
|
+
},
|
80
|
+
};
|
81
|
+
```
|
82
|
+
|
83
|
+
### 优先级
|
84
|
+
|
85
|
+
`tsconfig.json` 的 `paths` 配置的优先级高于 `source.alias`,当一个路径同时匹配到这两者定义的规则时,会优先使用 `tsconfig.json` 的 `paths` 定义的值。
|
86
|
+
|
87
|
+
你可以通过 [source.aliasStrategy](/configure/app/source/alias-strategy) 来调整这两个选项的优先级。
|
10
88
|
|
11
89
|
## 默认别名
|
12
90
|
|
@@ -0,0 +1,229 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 14
|
3
|
+
---
|
4
|
+
|
5
|
+
# 使用 CSS Modules
|
6
|
+
|
7
|
+
[CSS Modules](https://github.com/css-modules/css-modules) 让我们能以模块化的方式编写 CSS 代码,并且可以在 JavaScript 文件中导入和使用这些样式。使用 CSS Modules 可以自动生成唯一的类名,隔离不同模块之间的样式,避免类名冲突。
|
8
|
+
|
9
|
+
Builder 默认支持使用 CSS Modules,无需添加额外的配置。我们约定使用 `[name].module.css` 文件名来启用 CSS Modules。
|
10
|
+
|
11
|
+
以下样式文件会被视为 CSS Modules:
|
12
|
+
|
13
|
+
- `*.module.scss`
|
14
|
+
- `*.module.less`
|
15
|
+
- `*.module.css`
|
16
|
+
|
17
|
+
## 示例
|
18
|
+
|
19
|
+
- 编写样式:
|
20
|
+
|
21
|
+
```css
|
22
|
+
/* button.module.css */
|
23
|
+
.error {
|
24
|
+
background: red;
|
25
|
+
}
|
26
|
+
```
|
27
|
+
|
28
|
+
- 使用样式:
|
29
|
+
|
30
|
+
```tsx
|
31
|
+
// Button.tsx
|
32
|
+
import React, { Component } from 'react';
|
33
|
+
// 引入样式文件
|
34
|
+
import styles from './button.module.css';
|
35
|
+
|
36
|
+
export default () => {
|
37
|
+
return <button className={styles.error}>Error Button</button>;
|
38
|
+
};
|
39
|
+
```
|
40
|
+
|
41
|
+
## 为所有样式文件启用 CSS Modules
|
42
|
+
|
43
|
+
在默认情况下,只有 `*.module.css` 结尾的文件才被视为 CSS Modules 模块。
|
44
|
+
|
45
|
+
如果你想将源码目录下的所有 CSS 文件当做 CSS Modules 模块进行处理,可以通过开启 [output.disableCssModuleExtension](/configure/app/output/disable-css-module-extension) 来实现,比如:
|
46
|
+
|
47
|
+
```ts
|
48
|
+
export default {
|
49
|
+
output: {
|
50
|
+
disableCssModuleExtension: true,
|
51
|
+
},
|
52
|
+
};
|
53
|
+
```
|
54
|
+
|
55
|
+
设置后,以下两个文件都会被视为 CSS Modules:
|
56
|
+
|
57
|
+
```ts
|
58
|
+
import styles1 from './foo.module.css';
|
59
|
+
import styles2 from './bar.css';
|
60
|
+
```
|
61
|
+
|
62
|
+
:::tip
|
63
|
+
我们不推荐开启此配置项,因为开启 `disableCssModuleExtension` 后,CSS Modules 文件和普通 CSS 文件无法得到明确的区分,不利于长期维护。
|
64
|
+
:::
|
65
|
+
|
66
|
+
## 为指定的样式文件启用 CSS Modules
|
67
|
+
|
68
|
+
在默认情况下,只有 `*.module.css` 结尾的文件才被视为 CSS Modules 模块。
|
69
|
+
|
70
|
+
如果你想只为一些指定的样式文件启用 CSS Modules,可以通过配置 [output.cssModules](/configure/app/output/css-modules) 来实现,比如:
|
71
|
+
|
72
|
+
```ts
|
73
|
+
export default {
|
74
|
+
output: {
|
75
|
+
cssModules: {
|
76
|
+
auto: resource => {
|
77
|
+
return resource.includes('.module.') || resource.includes('shared/');
|
78
|
+
},
|
79
|
+
},
|
80
|
+
},
|
81
|
+
};
|
82
|
+
```
|
83
|
+
|
84
|
+
## 自定义类名
|
85
|
+
|
86
|
+
自定义 CSS Modules 生成的类名也是我们比较常用的功能,你可以使用 [output.cssModuleLocalIdentName](/configure/app/output/css-modules.html#cssmodulesexportlocalsconvention) 来进行配置。
|
87
|
+
|
88
|
+
```ts
|
89
|
+
export default {
|
90
|
+
output: {
|
91
|
+
cssModuleLocalIdentName: '[hash:base64:4]',
|
92
|
+
},
|
93
|
+
};
|
94
|
+
```
|
95
|
+
|
96
|
+
如果你需要自定义 CSS Modules 的其他配置,可以通过 [tools.cssLoader](/configure/app/tools/css-loader) 进行设置。
|
97
|
+
|
98
|
+
## 添加类型声明
|
99
|
+
|
100
|
+
当你在 TypeScript 代码中引用 CSS Modules 时,TypeScript 可能会提示该模块缺少类型定义:
|
101
|
+
|
102
|
+
```
|
103
|
+
TS2307: Cannot find module './index.module.css' or its corresponding type declarations.
|
104
|
+
```
|
105
|
+
|
106
|
+
此时你需要为 CSS Modules 添加类型声明文件,请在项目中创建 `src/global.d.ts` 文件,并添加相应的类型声明:
|
107
|
+
|
108
|
+
```ts title="src/global.d.ts"
|
109
|
+
declare module '*.module.css' {
|
110
|
+
const classes: { readonly [key: string]: string };
|
111
|
+
export default classes;
|
112
|
+
}
|
113
|
+
|
114
|
+
declare module '*.module.scss' {
|
115
|
+
const classes: { readonly [key: string]: string };
|
116
|
+
export default classes;
|
117
|
+
}
|
118
|
+
|
119
|
+
declare module '*.module.sass' {
|
120
|
+
const classes: { readonly [key: string]: string };
|
121
|
+
export default classes;
|
122
|
+
}
|
123
|
+
|
124
|
+
declare module '*.module.less' {
|
125
|
+
const classes: { readonly [key: string]: string };
|
126
|
+
export default classes;
|
127
|
+
}
|
128
|
+
|
129
|
+
declare module '*.module.styl' {
|
130
|
+
const classes: { readonly [key: string]: string };
|
131
|
+
export default classes;
|
132
|
+
}
|
133
|
+
```
|
134
|
+
|
135
|
+
如果你开启了 `disableCssModuleExtension` 配置值,还需要添加以下类型:
|
136
|
+
|
137
|
+
```ts title="src/global.d.ts"
|
138
|
+
declare module '*.css' {
|
139
|
+
const classes: { readonly [key: string]: string };
|
140
|
+
export default classes;
|
141
|
+
}
|
142
|
+
|
143
|
+
declare module '*.scss' {
|
144
|
+
const classes: { readonly [key: string]: string };
|
145
|
+
export default classes;
|
146
|
+
}
|
147
|
+
|
148
|
+
declare module '*.sass' {
|
149
|
+
const classes: { readonly [key: string]: string };
|
150
|
+
export default classes;
|
151
|
+
}
|
152
|
+
|
153
|
+
declare module '*.less' {
|
154
|
+
const classes: { readonly [key: string]: string };
|
155
|
+
export default classes;
|
156
|
+
}
|
157
|
+
|
158
|
+
declare module '*.styl' {
|
159
|
+
const classes: { readonly [key: string]: string };
|
160
|
+
export default classes;
|
161
|
+
}
|
162
|
+
```
|
163
|
+
|
164
|
+
添加类型声明后,如果依然存在上述错误提示,请尝试重启当前 IDE,或者调整 `global.d.ts` 所在的目录,使 TypeScript 能够正确识别类型定义。
|
165
|
+
|
166
|
+
## 生成准确的类型定义
|
167
|
+
|
168
|
+
上述方法虽然可以解决 CSS Modules 在 TypeScript 中的类型问题,但是无法准确地提示出某个 CSS 文件导出了哪些类名。
|
169
|
+
|
170
|
+
Builder 支持为 CSS Modules 生成准确的类型声明,你只需要开启 [output.enableCssModuleTSDeclaration](/configure/app/output/enable-css-module-tsdeclaration) 配置项,再执行构建命令,Builder 就会为项目中所有的 CSS Modules 文件生成相应的类型声明文件。
|
171
|
+
|
172
|
+
```ts
|
173
|
+
export default {
|
174
|
+
output: {
|
175
|
+
enableCssModuleTSDeclaration: true,
|
176
|
+
},
|
177
|
+
};
|
178
|
+
```
|
179
|
+
|
180
|
+
### 示例
|
181
|
+
|
182
|
+
例如某个文件夹下面有 `src/index.ts` 和 `src/index.module.scss` 两个文件:
|
183
|
+
|
184
|
+
```tsx title="src/index.ts"
|
185
|
+
import styles from './index.module.scss';
|
186
|
+
|
187
|
+
export default () => {
|
188
|
+
<div>
|
189
|
+
<div className={styles.pageHeader}>Page Header</div>
|
190
|
+
</div>;
|
191
|
+
};
|
192
|
+
```
|
193
|
+
|
194
|
+
```scss
|
195
|
+
// index.module.scss
|
196
|
+
.page-header {
|
197
|
+
color: black;
|
198
|
+
}
|
199
|
+
```
|
200
|
+
|
201
|
+
执行构建命令后,会自动生成 `src/index.module.scss.d.ts` 类型声明文件:
|
202
|
+
|
203
|
+
```ts title="src/index.module.scss.d.ts"
|
204
|
+
// This file is automatically generated.
|
205
|
+
// Please do not change this file!
|
206
|
+
interface CssExports {
|
207
|
+
'page-header': string;
|
208
|
+
pageHeader: string;
|
209
|
+
}
|
210
|
+
export const cssExports: CssExports;
|
211
|
+
export default cssExports;
|
212
|
+
```
|
213
|
+
|
214
|
+
此时再打开 `src/index.ts` 文件,可以看到 `styles` 对象已经具备了准确的类型。
|
215
|
+
|
216
|
+
### 相关配置
|
217
|
+
|
218
|
+
在上述例子中,`src/index.module.scss.d.ts` 是编译生成的代码,你可以选择将它们提交到 Git 仓库里,也可以选择在 `.gitignore` 文件中忽略它们:
|
219
|
+
|
220
|
+
```bash
|
221
|
+
# Ignore auto generated CSS declarations
|
222
|
+
*.module.css.d.ts
|
223
|
+
*.module.sass.d.ts
|
224
|
+
*.module.scss.d.ts
|
225
|
+
*.module.less.d.ts
|
226
|
+
*.module.styl.d.ts
|
227
|
+
```
|
228
|
+
|
229
|
+
此外,如果生成的代码导致了 ESLint 报错,你也可以将上述配置添加到 `.eslintignore` 文件里。
|
@@ -0,0 +1,106 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 12
|
3
|
+
---
|
4
|
+
|
5
|
+
# 引用 JSON 文件
|
6
|
+
|
7
|
+
Modern.js 支持在代码中引用 JSON 文件,也支持引用 [YAML](https://yaml.org/) 和 [TOML](https://toml.io/en/) 文件并将其转换为 JSON 格式。
|
8
|
+
|
9
|
+
## JSON 文件
|
10
|
+
|
11
|
+
你可以直接在 JavaScript 文件中引用 JSON 文件。
|
12
|
+
|
13
|
+
### 示例
|
14
|
+
|
15
|
+
```json title="example.json"
|
16
|
+
{
|
17
|
+
"name": "foo",
|
18
|
+
"items": [1, 2]
|
19
|
+
}
|
20
|
+
```
|
21
|
+
|
22
|
+
```js title="index.js"
|
23
|
+
import example from './example.json';
|
24
|
+
|
25
|
+
console.log(example.name); // 'foo';
|
26
|
+
console.log(example.items); // [1, 2];
|
27
|
+
```
|
28
|
+
|
29
|
+
### 具名引用
|
30
|
+
|
31
|
+
Modern.js 暂不支持通过 named import 来引用 JSON 文件:
|
32
|
+
|
33
|
+
```js
|
34
|
+
import { name } from './example.json';
|
35
|
+
```
|
36
|
+
|
37
|
+
## YAML 文件
|
38
|
+
|
39
|
+
YAML 是一种数据序列化语言,通常用于编写配置文件。
|
40
|
+
|
41
|
+
你可以直接在 JavaScript 中引用 `.yaml` 或 `.yml` 文件,它们会被自动转换为 JSON 格式。
|
42
|
+
|
43
|
+
### 示例
|
44
|
+
|
45
|
+
```yaml title="example.yaml"
|
46
|
+
---
|
47
|
+
hello: world
|
48
|
+
foo:
|
49
|
+
bar: baz
|
50
|
+
```
|
51
|
+
|
52
|
+
```js
|
53
|
+
import example from './example.yaml';
|
54
|
+
|
55
|
+
console.log(example.hello); // 'world';
|
56
|
+
console.log(example.foo); // { bar: 'baz' };
|
57
|
+
```
|
58
|
+
|
59
|
+
### 添加类型声明
|
60
|
+
|
61
|
+
当你在 TypeScript 代码中引用 YAML 文件时,请在项目中创建 `src/global.d.ts` 文件,并添加相应的类型声明:
|
62
|
+
|
63
|
+
```ts title="src/global.d.ts"
|
64
|
+
declare module '*.yaml' {
|
65
|
+
const content: Record<string, any>;
|
66
|
+
export default content;
|
67
|
+
}
|
68
|
+
|
69
|
+
declare module '*.yml' {
|
70
|
+
const content: Record<string, any>;
|
71
|
+
export default content;
|
72
|
+
}
|
73
|
+
```
|
74
|
+
|
75
|
+
## TOML 文件
|
76
|
+
|
77
|
+
TOML 是一种语义明显、易于阅读的配置文件格式。
|
78
|
+
|
79
|
+
你可以直接在 JavaScript 中引用 `.toml` 文件,它会被自动转换为 JSON 格式。
|
80
|
+
|
81
|
+
### 示例
|
82
|
+
|
83
|
+
```toml title="example.toml"
|
84
|
+
hello = "world"
|
85
|
+
|
86
|
+
[foo]
|
87
|
+
bar = "baz"
|
88
|
+
```
|
89
|
+
|
90
|
+
```js
|
91
|
+
import example from './example.toml';
|
92
|
+
|
93
|
+
console.log(example.hello); // 'world';
|
94
|
+
console.log(example.foo); // { bar: 'baz' };
|
95
|
+
```
|
96
|
+
|
97
|
+
### 添加类型声明
|
98
|
+
|
99
|
+
当你在 TypeScript 代码中引用 TOML 文件时,请在项目中创建 `src/global.d.ts` 文件,并添加相应的类型声明:
|
100
|
+
|
101
|
+
```ts title="src/global.d.ts"
|
102
|
+
declare module '*.toml' {
|
103
|
+
const content: Record<string, any>;
|
104
|
+
export default content;
|
105
|
+
}
|
106
|
+
```
|