@modern-js/module-tools-docs 2.8.0 → 2.9.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/CHANGELOG.md +9 -0
- package/docs/en/guide/advance/asset.mdx +2 -65
- package/docs/en/guide/advance/external-dependency.mdx +5 -1
- package/docs/en/guide/advance/in-depth-about-build.md +6 -2
- package/docs/en/guide/best-practices/components.mdx +8 -8
- package/docs/en/guide/intro/getting-started.md +4 -4
- package/docs/zh/guide/advance/asset.mdx +8 -69
- package/docs/zh/guide/advance/external-dependency.mdx +6 -3
- package/docs/zh/guide/advance/in-depth-about-build.md +8 -4
- package/docs/zh/guide/best-practices/components.mdx +8 -8
- package/docs/zh/guide/intro/getting-started.md +4 -4
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# @modern-js/module-tools-docs
|
|
2
2
|
|
|
3
|
+
## 2.9.0
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 540205e909: docs(module-tools-doc): format content
|
|
8
|
+
docs(module-tools-doc): 格式化内容
|
|
9
|
+
- @modern-js/doc-tools@2.9.0
|
|
10
|
+
- @modern-js/doc-plugin-auto-sidebar@2.9.0
|
|
11
|
+
|
|
3
12
|
## 2.8.0
|
|
4
13
|
|
|
5
14
|
### Patch Changes
|
|
@@ -32,10 +32,6 @@ import img from './bg.png';
|
|
|
32
32
|
|
|
33
33
|
Project source code.
|
|
34
34
|
|
|
35
|
-
```ts ./src/asset.ts
|
|
36
|
-
import img from './bg.png';
|
|
37
|
-
//...
|
|
38
|
-
```
|
|
39
35
|
|
|
40
36
|
---
|
|
41
37
|
|
|
@@ -71,8 +67,8 @@ If the size of `bg.png` is larger than 10 kb, then the product directory structu
|
|
|
71
67
|
---
|
|
72
68
|
|
|
73
69
|
```js ./dist/asset.js
|
|
74
|
-
|
|
75
|
-
console.info(
|
|
70
|
+
import img from './assets/bg.13e2aba2.png';
|
|
71
|
+
console.info(img);
|
|
76
72
|
```
|
|
77
73
|
|
|
78
74
|
</CH.Code>
|
|
@@ -84,62 +80,3 @@ When wanting to modify the default behavior, the following API can be used:
|
|
|
84
80
|
- `asset.path`: modify the output path of the static resource file.
|
|
85
81
|
- `asset.limit`: modify the threshold value for inline static resource files.
|
|
86
82
|
|
|
87
|
-
## Setting CDN Prefix
|
|
88
|
-
|
|
89
|
-
If the project is running in a browser environment, we may need to host static resources to a CDN and then use them in the production environment.
|
|
90
|
-
|
|
91
|
-
Then we need to add the CDN base path in front of the path to the static resource file in the default generated build product. Again, see an example:
|
|
92
|
-
|
|
93
|
-
<CH.Spotlight>
|
|
94
|
-
|
|
95
|
-
```ts ./src/asset.ts
|
|
96
|
-
import img from './bg.png';
|
|
97
|
-
//...
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
---
|
|
101
|
-
|
|
102
|
-
Project source code.
|
|
103
|
-
|
|
104
|
-
```ts ./src/asset.ts
|
|
105
|
-
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
---
|
|
109
|
-
|
|
110
|
-
`modern.config` config file。
|
|
111
|
-
|
|
112
|
-
```ts ./modern.config.ts
|
|
113
|
-
export default defineConfig({
|
|
114
|
-
buildConfig: {
|
|
115
|
-
format: 'umd',
|
|
116
|
-
asset: {
|
|
117
|
-
publicPath: 'https://cdn/',
|
|
118
|
-
},
|
|
119
|
-
},
|
|
120
|
-
});
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
---
|
|
124
|
-
|
|
125
|
-
If the size of `bg.png` is larger than 10 kb, then the product content will be.
|
|
126
|
-
|
|
127
|
-
```js ./dist/asset.js focus=7:7
|
|
128
|
-
(function (global, factory) {
|
|
129
|
-
if (typeof module === 'object' && typeof module.exports === 'object')
|
|
130
|
-
factory();
|
|
131
|
-
else if (typeof define === 'function' && define.amd) define([], factory);
|
|
132
|
-
else if (
|
|
133
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self)
|
|
134
|
-
)
|
|
135
|
-
factory();
|
|
136
|
-
})(this, function () {
|
|
137
|
-
'use strict';
|
|
138
|
-
var bg_default = 'http://cdn/assets/bg.13e2aba2.png';
|
|
139
|
-
// src/asset.tsx
|
|
140
|
-
console.info(bg_default);
|
|
141
|
-
});
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
</CH.Spotlight>
|
|
145
|
-
|
|
@@ -19,7 +19,11 @@ In addition to `"dependencies"`, [`"peerDependencies"`](/en/guide/basic/before-g
|
|
|
19
19
|
|
|
20
20
|
## Default handling of third-party dependencies
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
In module projects, **by default, third-party dependencies under `"dependencies"` and `"peerDependencies"` are not packaged**. This is because when the npm package is released, the `"dependencies"` for the npm package will be installed when the npm package is installed.
|
|
23
|
+
|
|
24
|
+
- By not packaging `"dependencies"`, you can reduce the size of the package product.
|
|
25
|
+
- If you need to package, move `"dependencies"` to `"peerDependencies"`, which is equivalent to a **prebundle** of dependencies to reduce the size of the dependency installation.
|
|
26
|
+
|
|
23
27
|
|
|
24
28
|
<CH.Spotlight>
|
|
25
29
|
|
|
@@ -25,15 +25,19 @@ A Bundle is a package of build products, which may be a single file or multiple
|
|
|
25
25
|
|
|
26
26
|
Bundleless, on the other hand, means that each source file is compiled and built separately, but not packaged together. Each product file can be found with its corresponding source code file. The process of **Bundleless build can also be understood as the process of code conversion of source files only**.
|
|
27
27
|
|
|
28
|
+
They have their own benefits.
|
|
29
|
+
|
|
30
|
+
- Bundle can reduce the size of build products and also pre-package dependencies to reduce the size of installed dependencies. Packaging libraries in advance can speed up application project builds.
|
|
31
|
+
- Bundleless maintains the original file structure and is more conducive to debugging and tree shaking.
|
|
32
|
+
|
|
28
33
|
In `buildConfig` you can specify whether the current build task is Bundle or Bundleless by using [`buildConfig.buildType`](/en/api/config/build-config#buildtype).
|
|
29
34
|
|
|
30
35
|
### Relationship between `input` and `sourceDir`
|
|
31
36
|
|
|
32
37
|
[`buildConfig.input`](/en/api/config/build-config#input) is used to specify the file path or directory path where the source code is read, and its default value differs between Bundle and Bundleless builds.
|
|
33
38
|
|
|
34
|
-
- When `buildType: 'bundle'`, `input` defaults to `
|
|
39
|
+
- When `buildType: 'bundle'`, `input` defaults to `src/index.(j|t)sx?`
|
|
35
40
|
- When `buildType: 'bundleless'`, `input` defaults to `['src']`
|
|
36
|
-
> In fact, at `buildType: 'bundle'`, the build tool detects the existence of a file matching the name rule `src/index.(j|t)sx?` and uses it as an entry file.
|
|
37
41
|
|
|
38
42
|
:::warning{title=notes}
|
|
39
43
|
It is recommended that you do not specify multiple source file directories during a Bundleless build, as unintended results may occur. Bundleless builds with multiple source directories are currently in an unstable stage.
|
|
@@ -9,10 +9,10 @@ This chapter will describe how to develop component projects using the module en
|
|
|
9
9
|
```bash Interactive questions
|
|
10
10
|
npx @modern-js/create components-project
|
|
11
11
|
|
|
12
|
-
? Please select the solution you want to create Module
|
|
13
|
-
? Package Name components-demo
|
|
14
|
-
? Development Language TS
|
|
15
|
-
? Package
|
|
12
|
+
? Please select the solution you want to create: Npm Module
|
|
13
|
+
? Package Name: components-demo
|
|
14
|
+
? Development Language: TS
|
|
15
|
+
? Package Manager: pnpm
|
|
16
16
|
```
|
|
17
17
|
|
|
18
18
|
---
|
|
@@ -22,10 +22,10 @@ It is recommended to use the `@modern-js/create` command to initialize an npm pr
|
|
|
22
22
|
```bash Interactive questions
|
|
23
23
|
npx @modern-js/create components-project
|
|
24
24
|
|
|
25
|
-
? Please select the solution you want to create Module
|
|
26
|
-
? Package Name components-demo
|
|
27
|
-
? Development Language TS
|
|
28
|
-
? Package
|
|
25
|
+
? Please select the solution you want to create: Npm Module
|
|
26
|
+
? Package Name: components-demo
|
|
27
|
+
? Development Language: TS
|
|
28
|
+
? Package Manager: pnpm
|
|
29
29
|
```
|
|
30
30
|
|
|
31
31
|
---
|
|
@@ -75,10 +75,10 @@ Execute `npx @modern-js/create -h` for more command line arguments
|
|
|
75
75
|
Next, in the issue interaction, follow the options below.
|
|
76
76
|
|
|
77
77
|
```bash
|
|
78
|
-
? Please select the type of project you want to create Module
|
|
79
|
-
? Please fill in the project name library
|
|
80
|
-
? Please select the development language TS
|
|
81
|
-
? Please select the package
|
|
78
|
+
? Please select the type of project you want to create: Npm Module
|
|
79
|
+
? Please fill in the project name: library
|
|
80
|
+
? Please select the development language: TS
|
|
81
|
+
? Please select the package manager: pnpm
|
|
82
82
|
```
|
|
83
83
|
|
|
84
84
|
> The project name is the value of the `"name"` field in `package.json`.
|
|
@@ -14,7 +14,7 @@ sidebar_position: 7
|
|
|
14
14
|
- `'.ttf'`、`'.otf'`、`'.woff'`、`'.woff2'`、`'.eot'`
|
|
15
15
|
- `'.mp3'`、`'.mp4'`、`'.webm'`、`'.ogg'`、`'.wav'`、`'.flac'`、`'.aac'`、`'.mov'`
|
|
16
16
|
|
|
17
|
-
|
|
17
|
+
对于静态文件的处理,模块工程目前默认支持的功能有:
|
|
18
18
|
|
|
19
19
|
- 设置静态资源路径为 `./assets`。
|
|
20
20
|
- 对于超过 **10kb** 的文件会内联到代码中。
|
|
@@ -25,18 +25,13 @@ sidebar_position: 7
|
|
|
25
25
|
|
|
26
26
|
```ts ./src/asset.ts
|
|
27
27
|
import img from './bg.png';
|
|
28
|
-
|
|
28
|
+
console.log(img);
|
|
29
29
|
```
|
|
30
30
|
|
|
31
31
|
---
|
|
32
32
|
|
|
33
33
|
项目源代码。
|
|
34
34
|
|
|
35
|
-
```ts ./src/asset.ts
|
|
36
|
-
import img from './bg.png';
|
|
37
|
-
//...
|
|
38
|
-
```
|
|
39
|
-
|
|
40
35
|
---
|
|
41
36
|
|
|
42
37
|
如果 `bg.png` 的大小小于 10 kb,则此时产物目录结构和产物内容为。
|
|
@@ -51,12 +46,14 @@ import img from './bg.png';
|
|
|
51
46
|
---
|
|
52
47
|
|
|
53
48
|
```js ./dist/asset.js
|
|
54
|
-
var
|
|
55
|
-
console.info(
|
|
49
|
+
var img_default = 'data:image/png;base64,';
|
|
50
|
+
console.info(img_default);
|
|
56
51
|
```
|
|
57
52
|
|
|
58
53
|
</CH.Code>
|
|
59
54
|
|
|
55
|
+
---
|
|
56
|
+
|
|
60
57
|
如果 `bg.png` 的大小大于 10 kb,则此时产物目录结构和产物内容为。
|
|
61
58
|
|
|
62
59
|
<CH.Code>
|
|
@@ -71,8 +68,8 @@ console.info(bg_default);
|
|
|
71
68
|
---
|
|
72
69
|
|
|
73
70
|
```js ./dist/asset.js
|
|
74
|
-
|
|
75
|
-
console.info(
|
|
71
|
+
import img from './assets/bg.13e2aba2.png';
|
|
72
|
+
console.info(img);
|
|
76
73
|
```
|
|
77
74
|
|
|
78
75
|
</CH.Code>
|
|
@@ -84,61 +81,3 @@ console.info(bg_default);
|
|
|
84
81
|
- `asset.path`:修改静态资源文件输出路径。
|
|
85
82
|
- `asset.limit`:修改内联静态资源文件的阈值。
|
|
86
83
|
|
|
87
|
-
## 设置 CDN 前缀
|
|
88
|
-
|
|
89
|
-
如果项目运行的环境是在浏览器下,我们有可能需要静态资源托管至 CDN,然后在生产环境使用。
|
|
90
|
-
|
|
91
|
-
那么此时默认生成的构建产物中,我们需要在静态资源文件的路径前面增加 CDN 的基础路径。还是看一个例子:
|
|
92
|
-
|
|
93
|
-
<CH.Spotlight>
|
|
94
|
-
|
|
95
|
-
```ts ./src/asset.ts
|
|
96
|
-
import img from './bg.png';
|
|
97
|
-
//...
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
---
|
|
101
|
-
|
|
102
|
-
项目源代码。
|
|
103
|
-
|
|
104
|
-
```ts ./src/asset.ts
|
|
105
|
-
|
|
106
|
-
```
|
|
107
|
-
|
|
108
|
-
---
|
|
109
|
-
|
|
110
|
-
`modern.config` 配置为。
|
|
111
|
-
|
|
112
|
-
```ts ./modern.config.ts
|
|
113
|
-
export default defineConfig({
|
|
114
|
-
buildConfig: {
|
|
115
|
-
format: 'umd',
|
|
116
|
-
asset: {
|
|
117
|
-
publicPath: 'https://cdn/',
|
|
118
|
-
},
|
|
119
|
-
},
|
|
120
|
-
});
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
---
|
|
124
|
-
|
|
125
|
-
如果 `bg.png` 的大小大于 10 kb,则此时产物内容为。
|
|
126
|
-
|
|
127
|
-
```js ./dist/asset.js focus=7:7
|
|
128
|
-
(function (global, factory) {
|
|
129
|
-
if (typeof module === 'object' && typeof module.exports === 'object')
|
|
130
|
-
factory();
|
|
131
|
-
else if (typeof define === 'function' && define.amd) define([], factory);
|
|
132
|
-
else if (
|
|
133
|
-
(global = typeof globalThis !== 'undefined' ? globalThis : global || self)
|
|
134
|
-
)
|
|
135
|
-
factory();
|
|
136
|
-
})(this, function () {
|
|
137
|
-
'use strict';
|
|
138
|
-
var bg_default = 'http://cdn/assets/bg.13e2aba2.png';
|
|
139
|
-
// src/asset.tsx
|
|
140
|
-
console.info(bg_default);
|
|
141
|
-
});
|
|
142
|
-
```
|
|
143
|
-
|
|
144
|
-
</CH.Spotlight>
|
|
@@ -13,13 +13,16 @@ sidebar_position: 4
|
|
|
13
13
|
}
|
|
14
14
|
```
|
|
15
15
|
|
|
16
|
-
|
|
16
|
+
`"dependencies"` 下的依赖通常来说是这个包运行所需的依赖, `"devDependencies"` 则代表着开发依赖。
|
|
17
17
|
|
|
18
|
-
除了 `"dependencies"` 以外,[`"peerDependencies"`](/guide/basic/before-getting-started#peerdependencies)
|
|
18
|
+
除了 `"dependencies"` 以外,[`"peerDependencies"`](/guide/basic/before-getting-started#peerdependencies) 也可以声明在生产环境下运行所需要的依赖,此时会和它的宿主共享一份依赖。
|
|
19
19
|
|
|
20
20
|
## 第三方依赖的默认处理
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
在模块工程里,**默认情况下不会对 `"dependencies"` 以及 `"peerDependencies"` 下的第三方依赖进行打包处理**。这是因为 npm 包发布出去以后,安装 npm 包时,npm 包的 `"dependencies"` 也会被安装。
|
|
23
|
+
|
|
24
|
+
- 不打包 `"dependencies"`,可以减小包产物的体积。
|
|
25
|
+
- 如果需要打包,请将 `"dependencies"` 挪到 `"peerDependencies"` ,相当于对依赖进行 **prebundle** ,减小依赖安装的体积。
|
|
23
26
|
|
|
24
27
|
<CH.Spotlight>
|
|
25
28
|
|
|
@@ -25,18 +25,22 @@ sidebar_position: 1
|
|
|
25
25
|
|
|
26
26
|
而 Bundleless 则是指对每个源文件单独进行编译构建,但是并不将它们打包在一起。每一个产物文件都可以找到与之相对应的源码文件。**Bundleless 构建的过程,也可以理解为仅对源文件进行代码转换的过程**。
|
|
27
27
|
|
|
28
|
+
它们有各自的好处:
|
|
29
|
+
|
|
30
|
+
- Bundle 可以减少构建产物的体积,也可以对依赖预打包,减小安装依赖的体积。提前对库进行打包,可以加快应用项目构建的速度。
|
|
31
|
+
- Bundleless 则是可以保持原有的文件结构,更有利于调试和 tree shaking。
|
|
32
|
+
|
|
28
33
|
在 `buildConfig` 中可以通过 [`buildConfig.buildType`](/api/config/build-config#buildtype) 来指定当前构建任务是 Bundle 还是 Bundleless。
|
|
29
34
|
|
|
30
35
|
### `input` 与 `sourceDir` 的关系
|
|
31
36
|
|
|
32
37
|
[`buildConfig.input`](/api/config/build-config#input) 用于指定读取源码的文件路径或者目录路径,其默认值在 Bundle 和 Bundleless 构建过程中有所不同:
|
|
33
38
|
|
|
34
|
-
- 当 `buildType: 'bundle'` 的时候,`input` 默认值为 `
|
|
39
|
+
- 当 `buildType: 'bundle'` 的时候,`input` 默认值为 `src/index.(j|t)sx?`
|
|
35
40
|
- 当 `buildType: 'bundleless'` 的时候,`input` 默认值为 `['src']`
|
|
36
|
-
> 实际上,在 `buildType: 'bundle'` 的时候,构建工具会检测是否存在符合 `src/index.(j|t)sx?` 这个名称规则的文件,并将其作为入口文件。
|
|
37
41
|
|
|
38
42
|
:::warning {title=注意}
|
|
39
|
-
建议不要在 Bundleless
|
|
43
|
+
建议不要在 Bundleless 构建过程中指定多个源码文件目录,这可能会导致产物里的相对路径不正确。
|
|
40
44
|
:::
|
|
41
45
|
|
|
42
46
|
从默认值上我们可以知道:**Bundle 构建一般可以指定文件路径作为构建的入口,而 Bundleless 构建则更期望使用目录路径寻找源文件**。
|
|
@@ -70,7 +74,7 @@ export default defineConfig({
|
|
|
70
74
|
一般来说:
|
|
71
75
|
|
|
72
76
|
- **在 Bundleless 构建过程中,`sourceDir` 与 `input` 的值要保持一致,它们的默认值都是 `src`**。
|
|
73
|
-
- 在 Bundle
|
|
77
|
+
- 在 Bundle 构建过程中,无需使用 `sourceDir`。
|
|
74
78
|
|
|
75
79
|
### 类型文件
|
|
76
80
|
|
|
@@ -13,10 +13,10 @@ sidebar_position: 1
|
|
|
13
13
|
```bash 交互式问题
|
|
14
14
|
npx @modern-js/create components-project
|
|
15
15
|
|
|
16
|
-
?
|
|
17
|
-
?
|
|
18
|
-
?
|
|
19
|
-
?
|
|
16
|
+
? 请选择你想创建的工程类型:Npm 模块
|
|
17
|
+
? 请填写项目名称:components-demo
|
|
18
|
+
? 请选择开发语言:TS
|
|
19
|
+
? 请选择包管理工具:pnpm
|
|
20
20
|
```
|
|
21
21
|
|
|
22
22
|
---
|
|
@@ -26,10 +26,10 @@ npx @modern-js/create components-project
|
|
|
26
26
|
```bash 交互式问题
|
|
27
27
|
npx @modern-js/create components-project
|
|
28
28
|
|
|
29
|
-
?
|
|
30
|
-
?
|
|
31
|
-
?
|
|
32
|
-
?
|
|
29
|
+
? 请选择你想创建的工程类型:Npm 模块
|
|
30
|
+
? 请填写项目名称:components-demo
|
|
31
|
+
? 请选择开发语言:TS
|
|
32
|
+
? 请选择包管理工具:pnpm
|
|
33
33
|
```
|
|
34
34
|
|
|
35
35
|
---
|
|
@@ -73,10 +73,10 @@ npx @modern-js/create your-project-dir-name
|
|
|
73
73
|
接着在问题交互中,按照如下选择:
|
|
74
74
|
|
|
75
75
|
```bash
|
|
76
|
-
?
|
|
77
|
-
?
|
|
78
|
-
?
|
|
79
|
-
?
|
|
76
|
+
? 请选择你想创建的工程类型:Npm 模块
|
|
77
|
+
? 请填写项目名称:library
|
|
78
|
+
? 请选择开发语言:TS
|
|
79
|
+
? 请选择包管理工具:pnpm
|
|
80
80
|
```
|
|
81
81
|
|
|
82
82
|
> 项目名称为 `package.json` 中的 `"name"` 字段值。
|
package/package.json
CHANGED
|
@@ -5,15 +5,15 @@
|
|
|
5
5
|
"bugs": "https://github.com/web-infra-dev/modern.js/issues",
|
|
6
6
|
"repository": "web-infra-dev/modern.js",
|
|
7
7
|
"license": "MIT",
|
|
8
|
-
"version": "2.
|
|
8
|
+
"version": "2.9.0",
|
|
9
9
|
"main": "index.js",
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"@code-hike/mdx": "^0.7.4",
|
|
12
12
|
"react": "^18.2.0",
|
|
13
13
|
"react-dom": "^18.2.0",
|
|
14
14
|
"shiki": "^0.11.1",
|
|
15
|
-
"@modern-js/doc-plugin-auto-sidebar": "2.
|
|
16
|
-
"@modern-js/doc-tools": "2.
|
|
15
|
+
"@modern-js/doc-plugin-auto-sidebar": "2.9.0",
|
|
16
|
+
"@modern-js/doc-tools": "2.9.0"
|
|
17
17
|
},
|
|
18
18
|
"scripts": {
|
|
19
19
|
"dev": "modern dev",
|