@modern-js/main-doc 2.0.0-beta.1 → 2.0.0-beta.2
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/.turbo/turbo-build.log +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/commands/build.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/commands/dev.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/commands/lint.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/commands/new.md +1 -2
- package/en/docusaurus-plugin-content-docs/current/apis/app/commands/start.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/commands/test.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/overview.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/auto-actions.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/connect.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/create-app.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/create-store.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/handle-effect.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/model_.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/use-local-model.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/use-model.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/use-static-model.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/model/use-store.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/components/command-tip.md +56 -0
- package/en/docusaurus-plugin-content-docs/current/components/reduck-tip.md +5 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/output/disable-css-extract.md +12 -0
- package/en/docusaurus-plugin-content-docs/current/configure/app/runtime/router.md +1 -1
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/api/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/api/app.md +152 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/api/ejs.md +56 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/api/fs.md +54 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/api/git.md +49 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/api/handlebars.md +58 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/api/json.md +59 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/api/npm.md +49 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/develop.md +52 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/introduce.md +46 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/run-in-js.md +48 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/codesmith/structure.md +87 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/common.md +106 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/module.md +39 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/monorepo.md +28 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/config/mwa.md +89 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/abstract.md +26 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/file/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/file/addFile.md +52 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/file/addHelper.md +26 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/file/addManyFile.md +58 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/file/addPartial.md +26 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/file/introduce.md +38 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/file/rmDir.md +24 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/file/rmFile.md +24 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/file/updateJSONFile.md +54 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/file/updateModernConfig.md +26 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/file/updateTextRawFile.md +33 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/git/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/git/gitAddAndCommit.md +20 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/git/initGitRepo.md +16 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/git/isInGitRepo.md +16 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/hook/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/hook/afterForged.md +35 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/hook/onForged.md +36 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/info/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/info/isFileExit.md +22 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/info/locale.md +17 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/info/readDir.md +22 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/input/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/input/addInputAfter.md +57 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/input/addInputBefore.md +56 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/input/setInput.md +42 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/input/setInputValue.md +30 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/input/type.md +65 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/introduce.md +91 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/new/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/new/createElement.md +33 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/new/createSubProject.md +35 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/new/enableFunc.md +44 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/new/introduce.md +15 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/npm/_category_.json +4 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/api/npm/install.md +18 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/develop.md +125 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/plugin/use.md +61 -0
- package/en/docusaurus-plugin-content-docs/current/guides/topic-detail/generator/project.md +118 -0
- package/en/docusaurus-plugin-content-docs/current.json +39 -3
- package/package.json +3 -3
- package/scripts/summary.en.json +1 -1
- package/scripts/summary.zh.json +1 -1
- package/zh/apis/app/commands/new.md +0 -1
- package/zh/apis/app/hooks/server/test.md +2 -2
- package/zh/apis/app/runtime/web-server/hook.md +4 -4
- package/zh/apis/app/runtime/web-server/middleware.md +4 -4
- package/zh/configure/app/output/disable-css-extract.md +12 -0
- package/zh/guides/advanced-features/bff/_category_.json +5 -1
- package/zh/guides/advanced-features/bff/bff-proxy.md +5 -3
- package/zh/guides/advanced-features/bff/frameworks.md +72 -51
- package/zh/guides/advanced-features/bff/function.md +16 -30
- package/zh/guides/advanced-features/bff/index.md +20 -0
- package/zh/guides/advanced-features/bff/type.md +43 -0
- package/zh/guides/advanced-features/custom-app.md +63 -1
- package/zh/guides/advanced-features/web-server.md +21 -110
- package/zh/guides/basic-features/data-fetch.md +61 -0
- package/zh/guides/basic-features/env-vars.md +88 -41
- package/zh/guides/basic-features/routes.md +267 -0
- package/zh/guides/concept/entries.md +112 -0
- package/zh/guides/concept/lifecycle.md +10 -0
- package/zh/guides/get-started/quick-start.md +199 -1
- package/zh/guides/get-started/upgrade.md +75 -1
- package/zh/guides/topic-detail/generator/codesmith/api/_category_.json +4 -0
- package/zh/guides/topic-detail/generator/codesmith/api/app.md +152 -0
- package/zh/guides/topic-detail/generator/codesmith/api/ejs.md +56 -0
- package/zh/guides/topic-detail/generator/codesmith/api/fs.md +54 -0
- package/zh/guides/topic-detail/generator/codesmith/api/git.md +48 -0
- package/zh/guides/topic-detail/generator/codesmith/api/handlebars.md +56 -0
- package/zh/guides/topic-detail/generator/codesmith/api/json.md +58 -0
- package/zh/guides/topic-detail/generator/codesmith/api/npm.md +45 -0
- package/zh/guides/topic-detail/generator/codesmith/develop.md +52 -0
- package/zh/guides/topic-detail/generator/codesmith/introduce.md +42 -1
- package/zh/guides/topic-detail/generator/codesmith/run-in-js.md +48 -0
- package/zh/guides/topic-detail/generator/codesmith/structure.md +86 -0
- package/zh/guides/topic-detail/generator/config/mwa.md +4 -8
- package/zh/guides/topic-detail/generator/plugin/api/file/addFile.md +1 -1
- package/zh/guides/topic-detail/generator/plugin/api/file/addHelper.md +1 -1
- package/zh/guides/topic-detail/generator/plugin/api/file/addPartial.md +2 -2
- package/zh/guides/topic-detail/generator/plugin/api/file/introduce.md +1 -1
- package/zh/guides/topic-detail/generator/plugin/api/file/rmDir.md +1 -1
- package/zh/guides/topic-detail/generator/plugin/api/file/rmFile.md +1 -1
- package/zh/guides/topic-detail/generator/plugin/api/file/updateJSONFile.md +3 -2
- package/zh/guides/topic-detail/generator/plugin/api/file/updateModernConfig.md +2 -2
- package/zh/guides/topic-detail/generator/plugin/api/file/updateTextRawFile.md +1 -1
- package/zh/guides/topic-detail/generator/plugin/api/hook/afterForged.md +3 -3
- package/zh/guides/topic-detail/generator/plugin/api/hook/onForged.md +3 -3
- package/zh/guides/topic-detail/generator/plugin/api/input/addInputAfter.md +1 -1
- package/zh/guides/topic-detail/generator/plugin/api/input/addInputBefore.md +1 -1
- package/zh/guides/topic-detail/generator/plugin/api/input/type.md +1 -1
- package/zh/guides/topic-detail/generator/plugin/api/new/createElement.md +1 -1
- package/zh/guides/topic-detail/generator/plugin/api/new/createSubProject.md +1 -1
- package/zh/guides/topic-detail/generator/plugin/api/new/enableFunc.md +1 -1
- package/zh/guides/topic-detail/generator/plugin/api/npm/install.md +1 -1
- package/zh/guides/topic-detail/generator/plugin/develop.md +11 -7
- package/zh/guides/topic-detail/monorepo/sub-project-interface.md +0 -2
- package/zh/tutorials/first-app/c01-getting-started/1.1-prerequisites.md +2 -2
- package/zh/tutorials/first-app/c02-generator-and-studio/2.1-generator.md +0 -8
- package/zh/tutorials/first-app/c02-generator-and-studio/2.2-boilerplates.md +15 -7
- package/zh/tutorials/first-app/c07-app-entry/7.1-intro.md +1 -1
- package/zh/tutorials/first-app/c07-app-entry/7.2-add-entry-in-cli.md +1 -5
- package/zh/tutorials/first-app/c08-client-side-routing/8.2-file-based-routing.md +0 -8
- package/en/docusaurus-plugin-content-docs/current/apis/app/runtime/env.md +0 -72
- package/zh/apis/app/runtime/env.md +0 -70
- package/zh/components/deploy.md +0 -60
- package/zh/components/dev-ide.md +0 -1
- package/zh/components/entry-name.md +0 -15
- package/zh/components/env-prepare.md +0 -1
- package/zh/components/launch-bff-choices.md +0 -6
- package/zh/guides/advanced-features/bff/bff-server.md +0 -142
- package/zh/guides/advanced-features/typescript.md +0 -4
- package/zh/guides/concept/integrated.md +0 -4
- package/zh/guides/concept/solutions.md +0 -4
@@ -0,0 +1,86 @@
|
|
1
|
+
---
|
2
|
+
sidebar_position: 2
|
3
|
+
---
|
4
|
+
|
5
|
+
# 微生成器的组成
|
6
|
+
|
7
|
+
一个微生成器项目为一个模块项目或者一个 Npm 包,包中包含几个基本部分:
|
8
|
+
|
9
|
+
## package.json
|
10
|
+
|
11
|
+
npm 包描述信息,包含 name、version、main 等字段。
|
12
|
+
|
13
|
+
## 入口文件
|
14
|
+
|
15
|
+
上述 main 字段对应的 js 文件。文件为默认导出一个生成器函数,函数格式如下:
|
16
|
+
|
17
|
+
```typescript
|
18
|
+
export default async (context: GeneratorContext, generator: GeneratorCore) => {
|
19
|
+
|
20
|
+
};
|
21
|
+
```
|
22
|
+
|
23
|
+
该函数参数为 context 和 generator:
|
24
|
+
|
25
|
+
### context
|
26
|
+
|
27
|
+
context 提供 codesmith 运行时维护的上下文信息。其类型定义为:
|
28
|
+
|
29
|
+
```typescript
|
30
|
+
interface GeneratorContext {
|
31
|
+
materials: Record<string, FsMaterial>; // 资源
|
32
|
+
config: Record<string, any>; // 用户 config 配置
|
33
|
+
data?: Record<string, any>; // 用户 data 配置
|
34
|
+
current: { material: FsMaterial; } | null; // 当前生成器运行可获取的文件资源
|
35
|
+
[key: string]: any; // 其他补充字段
|
36
|
+
}
|
37
|
+
```
|
38
|
+
|
39
|
+
materials 是 codesmith 的抽象文件系统,维护这生成器名称和可操作文件的映射关系。
|
40
|
+
|
41
|
+
materials 中还维护这一个 default 的映射关系,用于维护当前生成器执行目录的资源信息,通过该字段可以对目标项目资源进行操作。
|
42
|
+
|
43
|
+
例如当需要修改当前项目的 `package.json` 中的字段:
|
44
|
+
|
45
|
+
```typescript
|
46
|
+
const resource = context.materials.default.get(path.join(appDir, 'package.json'))
|
47
|
+
```
|
48
|
+
|
49
|
+
将获取到的 resource 传递给 codesmith 提供的 JSON API 即可实现 json 操作。
|
50
|
+
|
51
|
+
GeneratorContext 的 current 字段上维护这当前运行的生成器文件资源,可以通过 `current.material` 获取到当前的 `FsMaterial`.
|
52
|
+
|
53
|
+
FsMaterial 中提供了 get 方法用于获取资源文件并将资源文件传给生成器支持的 API 即可对模板文件进行处理。
|
54
|
+
|
55
|
+
例如:
|
56
|
+
|
57
|
+
```typescript
|
58
|
+
const resourceMap = await material.find('templates/**/*');
|
59
|
+
```
|
60
|
+
|
61
|
+
通过上述方式可以获取到生成器中符合 `templates/**/*` 规则的所有模板文件,遍历该对象即可对资源文件进行操作。
|
62
|
+
|
63
|
+
### generator
|
64
|
+
|
65
|
+
generator 提供 codesmith 运行时的函数方法。其类型定义为:
|
66
|
+
|
67
|
+
```typescript
|
68
|
+
interface GeneratorCore {
|
69
|
+
logger: Logger; // log 函数,支持 info、warning、debug、error、verbose、stream
|
70
|
+
outputPath: string; // 输出文件路径
|
71
|
+
output: {
|
72
|
+
fs: (file: string | number, data: any, options?: string | fs.WriteFileOptions | undefined) => Promise<void>
|
73
|
+
}; // 写文件方法
|
74
|
+
runSubGenerator: (subGenerator: string, relativePwdPath?: string | undefined, config?: Record<string, any> | undefined): Promise<void> // 运行子生成器
|
75
|
+
}
|
76
|
+
```
|
77
|
+
|
78
|
+
其中 outputPath 和 context 中的 `materials.default` 指向相同的目录,outputPath 用于直接计算目标路径并操作,`materials.default` 用于获取资源文件。
|
79
|
+
|
80
|
+
runSubGenerator 提供了在一个微生成器中去运行其他微生成器的方法,该方法将自动更新和维护新的 context 上下文信息。
|
81
|
+
|
82
|
+
## Template 文件
|
83
|
+
|
84
|
+
生成器模板文件。
|
85
|
+
|
86
|
+
推荐在项目根目录创建一个 templates 目录,用于维护该微生成器的模板文件,微生成器执行过程中只能使用自身的模板文件,对于需要共用的模板文件,建议通过共用子生成器实现。
|
@@ -32,7 +32,7 @@ New 命令的操作类型(actionType),类型支持两种:
|
|
32
32
|
|
33
33
|
- 创建应用入口(entry)
|
34
34
|
|
35
|
-
-
|
35
|
+
- 新建自定义 Web Serve 源码目录(server)
|
36
36
|
|
37
37
|
这两个选项还需要配置具体的配合使用,介绍如下:
|
38
38
|
|
@@ -40,11 +40,9 @@ New 命令的操作类型(actionType),类型支持两种:
|
|
40
40
|
|
41
41
|
入口名称(name), 字符串类型。
|
42
42
|
|
43
|
-
创建入口除了`name`之外,还需要 `needModifyMWAConfig`、`clientRoute` 字段。
|
44
|
-
|
45
43
|
#### framework
|
46
44
|
|
47
|
-
自定义
|
45
|
+
自定义 Web Serve 运行时框架(framework),支持两个选项:
|
48
46
|
|
49
47
|
- Express(express)
|
50
48
|
|
@@ -66,13 +64,11 @@ New 命令的操作类型(actionType),类型支持两种:
|
|
66
64
|
|
67
65
|
- Storybook(mwa_storybook)
|
68
66
|
|
69
|
-
- 部署(deploy)
|
70
|
-
|
71
67
|
- 启用「基于 UA 的 Polyfill」功能(polyfill)
|
72
68
|
|
73
69
|
- 启用「全局代理」(proxy)
|
74
70
|
|
75
|
-
`bff`
|
71
|
+
`bff` 和还需要配置具体的配合使用,介绍如下:
|
76
72
|
|
77
73
|
#### BFF 相关参数
|
78
74
|
|
@@ -86,7 +82,7 @@ BFF 类型(bffType),支持两个选项:
|
|
86
82
|
|
87
83
|
##### framework
|
88
84
|
|
89
|
-
|
85
|
+
BFF 运行时框架(framework),支持两个选项:
|
90
86
|
|
91
87
|
- Express(express)
|
92
88
|
|
@@ -6,7 +6,7 @@ sidebar_position: 3
|
|
6
6
|
|
7
7
|
针对于文本类型文件,增加 Handlebars 的自定义 Partial 片段,具体可查看文档[Partials](https://handlebarsjs.com/guide/#partials)。
|
8
8
|
|
9
|
-
该方法可在 onForged 生命周期的 api 参数上获取。
|
9
|
+
该方法可在 `onForged` 生命周期的 api 参数上获取。
|
10
10
|
|
11
11
|
其类型定义为:
|
12
12
|
|
@@ -19,7 +19,7 @@ export type ForgedAPI = {
|
|
19
19
|
|
20
20
|
## name
|
21
21
|
|
22
|
-
partial名称。
|
22
|
+
partial 名称。
|
23
23
|
|
24
24
|
## str
|
25
25
|
|
@@ -8,7 +8,7 @@ sidebar_position: 6
|
|
8
8
|
|
9
9
|
该方法适用于 JSON 文件类型,可批量更新 JSON 文件中字段值。
|
10
10
|
|
11
|
-
该方法可在 onForged 生命周期的 api 参数上获取。
|
11
|
+
该方法可在 `onForged` 生命周期的 api 参数上获取。
|
12
12
|
|
13
13
|
其类型定义为:
|
14
14
|
|
@@ -29,7 +29,8 @@ JSON 文件的文件名或者文件路径。
|
|
29
29
|
## updateInfo
|
30
30
|
|
31
31
|
字段更新信息。
|
32
|
-
|
32
|
+
|
33
|
+
该信息使用 `Record` 形式表示。
|
33
34
|
|
34
35
|
例如需要更新 name 字段:
|
35
36
|
|
@@ -6,7 +6,7 @@ sidebar_position: 8
|
|
6
6
|
|
7
7
|
更新 `package.json` 中 `modernConfig` 字段。
|
8
8
|
|
9
|
-
该方法可在 onForged 生命周期的 api 参数上获取。
|
9
|
+
该方法可在 `onForged` 生命周期的 api 参数上获取。
|
10
10
|
|
11
11
|
其类型定义为:
|
12
12
|
|
@@ -22,5 +22,5 @@ export type ForgedAPI = {
|
|
22
22
|
字段更新信息。
|
23
23
|
|
24
24
|
:::info
|
25
|
-
该函数为 updateJSONFile 的封装,将自动更新 `package.json` 的 `modernConfig` 字段 updateInfo 中只需填写相对于 `modernConfig` 的更新信息即可。
|
25
|
+
该函数为 updateJSONFile 的封装,将自动更新 `package.json` 的 `modernConfig` 字段 `updateInfo` 中只需填写相对于 `modernConfig` 的更新信息即可。
|
26
26
|
:::
|
@@ -4,7 +4,7 @@ sidebar_position: 2
|
|
4
4
|
|
5
5
|
# AfterForged
|
6
6
|
|
7
|
-
afterForged 函数为生成器插件生命周期函数,其通常用于定义安装依赖、Git
|
7
|
+
`afterForged` 函数为生成器插件生命周期函数,其通常用于定义安装依赖、Git 等操作,这些操作将会在 `onForged` 函数执行完成后进行。
|
8
8
|
|
9
9
|
该方法可直接在 context 上获取。
|
10
10
|
|
@@ -24,11 +24,11 @@ export interface IPluginContext {
|
|
24
24
|
|
25
25
|
## func
|
26
26
|
|
27
|
-
afterForged 参数是一个回调函数,函数参数为 api 和 inputData
|
27
|
+
`afterForged` 参数是一个回调函数,函数参数为 `api` 和 `inputData。`
|
28
28
|
|
29
29
|
### api
|
30
30
|
|
31
|
-
在 afterForged 生命周期中支持的函数列表,具体可查看 [Git API](/docs/guides/topic-detail/generator/plugin/api/git/isInGitRepo) 和 [NPM API](/docs/guides/topic-detail/generator/plugin/api/npm/install)。
|
31
|
+
在 `afterForged` 生命周期中支持的函数列表,具体可查看 [Git API](/docs/guides/topic-detail/generator/plugin/api/git/isInGitRepo) 和 [NPM API](/docs/guides/topic-detail/generator/plugin/api/npm/install)。
|
32
32
|
|
33
33
|
### inputData
|
34
34
|
|
@@ -4,7 +4,7 @@ sidebar_position: 1
|
|
4
4
|
|
5
5
|
# OnForged
|
6
6
|
|
7
|
-
onForged 函数为生成器插件生命周期函数,其通常用于定义文件类型操作,这些操作将在 base 的工程方案文件操作完成后进行。
|
7
|
+
`onForged` 函数为生成器插件生命周期函数,其通常用于定义文件类型操作,这些操作将在 base 的工程方案文件操作完成后进行。
|
8
8
|
|
9
9
|
该方法可直接在 context 上获取。
|
10
10
|
|
@@ -24,11 +24,11 @@ export interface IPluginContext {
|
|
24
24
|
|
25
25
|
## func
|
26
26
|
|
27
|
-
onForged 参数是一个回调函数,函数参数为 api 和 inputData。
|
27
|
+
`onForged` 参数是一个回调函数,函数参数为 api 和 inputData。
|
28
28
|
|
29
29
|
### api
|
30
30
|
|
31
|
-
在 onForged 生命周期中支持的函数列表,具体可查看[文件操作 API](/docs/guides/topic-detail/generator/plugin/api/file/introduce) 和[启用功能 API](/docs/guides/topic-detail/generator/plugin/api/new/introduce)。
|
31
|
+
在 `onForged` 生命周期中支持的函数列表,具体可查看[文件操作 API](/docs/guides/topic-detail/generator/plugin/api/file/introduce) 和[启用功能 API](/docs/guides/topic-detail/generator/plugin/api/new/introduce)。
|
32
32
|
|
33
33
|
### inputData
|
34
34
|
|
@@ -61,11 +61,15 @@ npx @modern-js/create plugin --plugin @modern-js/generator-plugin-plugin
|
|
61
61
|
在默认的输入前添加输入,比如在`选择包管理工具`问题前添加问题,具体 Modern.js 每个问题的配置可查看[工程方案配置](/docs/guides/topic-detail/generator/config/common)。
|
62
62
|
|
63
63
|
```ts
|
64
|
-
context.addInputBefore('
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
64
|
+
context.addInputBefore('input2', {
|
65
|
+
type: 'object',
|
66
|
+
properties: {
|
67
|
+
'input1.2': {
|
68
|
+
type: 'string',
|
69
|
+
title: 'input1.2',
|
70
|
+
},
|
71
|
+
},
|
72
|
+
});
|
69
73
|
```
|
70
74
|
|
71
75
|
- setInput
|
@@ -73,8 +77,8 @@ context.addInputBefore('packageManager', {
|
|
73
77
|
设置已经存在问题的属性
|
74
78
|
|
75
79
|
```ts
|
76
|
-
context.setInput('test', 'type', '
|
77
|
-
context.setInput('test', '
|
80
|
+
context.setInput('test', 'type', 'number')
|
81
|
+
context.setInput('test', 'enum', [{ value: 'option1', label: '选项一' }])
|
78
82
|
```
|
79
83
|
|
80
84
|
### onForged
|
@@ -10,11 +10,11 @@ title: 环境准备
|
|
10
10
|
|
11
11
|
## 1. Node.js
|
12
12
|
|
13
|
-
需要 [Node.js LTS](https://github.com/nodejs/Release),并确保 Node 版本大于等于 14.
|
13
|
+
需要 [Node.js LTS](https://github.com/nodejs/Release),并确保 Node 版本大于等于 14.19.0, 推荐使用 16.x 版本。
|
14
14
|
|
15
15
|
Modern.js 推荐在开发环境里先安装 [nvm](https://github.com/nvm-sh/nvm#install--update-script),在 shell 里集成[自动切换 node 版本的脚本](https://github.com/nvm-sh/nvm#deeper-shell-integration)。
|
16
16
|
|
17
|
-
然后只要仓库根目录下有内容为 `lts/fermium` 或 `lts/
|
17
|
+
然后只要仓库根目录下有内容为 `lts/fermium` 或 `lts/gallium` 的 `.nvmrc` 文件,进入这个仓库时就会自动安装或切换到正确的 Node.js 版本。
|
18
18
|
|
19
19
|
## 2. pnpm
|
20
20
|
|
@@ -6,18 +6,26 @@ title: 样板文件
|
|
6
6
|
|
7
7
|
```md
|
8
8
|
.
|
9
|
-
├── .
|
10
|
-
├── src/
|
11
|
-
│ ├── .eslintrc.json
|
12
|
-
│ ├── App.css
|
13
|
-
│ └── App.jsx
|
14
|
-
├── .editorconfig
|
9
|
+
├── .eslintrc.js
|
15
10
|
├── .gitignore
|
11
|
+
├── .husky
|
12
|
+
├── .idea
|
16
13
|
├── .npmrc
|
17
14
|
├── .nvmrc
|
15
|
+
├── .prettierrc
|
16
|
+
├── .vscode
|
18
17
|
├── README.md
|
18
|
+
├── modern.config.ts
|
19
19
|
├── package.json
|
20
|
-
|
20
|
+
├── pnpm-lock.yaml
|
21
|
+
├── src
|
22
|
+
│ ├── .eslintrc.js
|
23
|
+
│ ├── modern-app-env.d.ts
|
24
|
+
│ └── routes
|
25
|
+
│ ├── index.css
|
26
|
+
│ ├── layout.tsx
|
27
|
+
│ └── page.tsx
|
28
|
+
└── tsconfig.json
|
21
29
|
```
|
22
30
|
|
23
31
|
和上一章节手动创建出来的项目一样,仍然是零配置、极简和轻量的,但功能更完整,最佳实践开箱即用,比如:
|
@@ -1,72 +0,0 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 1
|
3
|
-
title: Environment Variables
|
4
|
-
---
|
5
|
-
|
6
|
-
Modern.js has built-in support for environment variables, including built-in environment variables and custom environment variables.
|
7
|
-
|
8
|
-
## Built-in Environment Variables
|
9
|
-
|
10
|
-
### NODE_ENV
|
11
|
-
|
12
|
-
A **read-only** environment variable, whose have different values under different execution commands:
|
13
|
-
|
14
|
-
- `production`: default value when executing `modern build`、`modern preview` commands.
|
15
|
-
- `test`: default value when executing `modern test` commands.
|
16
|
-
- `development`: default value when executing `modern dev` commands, also the default value in all other cases.
|
17
|
-
|
18
|
-
### MODERN_ENV
|
19
|
-
|
20
|
-
Used to set the current execution environment.In addition to the values corresponding to the above NODE_ENV, custom environment names are supported here, such as `staging`, `boe`.
|
21
|
-
|
22
|
-
Modern.js loads the environment variables values set in the `.env.{process.env.MODERN_ENV}` file for that environment variable.
|
23
|
-
|
24
|
-
When you need to use different configurations through the environment name, you can define environment variables in the `.env` file of the corresponding environment name.
|
25
|
-
|
26
|
-
MODERN_ENV priority is higher than NODE_ENV.
|
27
|
-
|
28
|
-
## Custom Environment Variables
|
29
|
-
|
30
|
-
Modern.js support custom environment variables, which starting with MODERN_ can be used directly in the code.
|
31
|
-
|
32
|
-
If you need to automatically use environment variables with any name, you need to declare them in the ['source.define'](/docs/configure/app/source/define) configuration.
|
33
|
-
|
34
|
-
## Define Environment Variables
|
35
|
-
|
36
|
-
Environment variables are specified in both `shell` and `.env` files.
|
37
|
-
|
38
|
-
### Specify Via Shell
|
39
|
-
|
40
|
-
Add custom environment variables before the command:
|
41
|
-
|
42
|
-
```shell
|
43
|
-
REACT_APP_FOO=123 BAR=456 pnpm run dev
|
44
|
-
```
|
45
|
-
|
46
|
-
### Specify Via File
|
47
|
-
|
48
|
-
Create a `.env` file in the project with custom environment variables:
|
49
|
-
|
50
|
-
```env
|
51
|
-
REACT_APP_FOO=123
|
52
|
-
BAR=456
|
53
|
-
```
|
54
|
-
|
55
|
-
`.env` files support suffixes NODE_ENV or MODERN_ENV defined environment names, such as `env.development`. Modern.js reads the env file based on the environment name variable value.
|
56
|
-
|
57
|
-
## Using Environment Variables
|
58
|
-
|
59
|
-
### Use In JS
|
60
|
-
|
61
|
-
Get environment variables directly with `process.env.*`.
|
62
|
-
|
63
|
-
```js
|
64
|
-
console.log(process.env.NODE_ENV);
|
65
|
-
// "development"
|
66
|
-
```
|
67
|
-
|
68
|
-
### Use In HTML
|
69
|
-
|
70
|
-
```html title="config/html/head.ejs"
|
71
|
-
<meta name="env" content="<%=process.env.NODE_ENV %>" />
|
72
|
-
```
|
@@ -1,70 +0,0 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 1
|
3
|
-
title: 环境变量
|
4
|
-
---
|
5
|
-
|
6
|
-
Modern.js 内置了对环境变量的支持,包含内置的环境变量和自定义的环境变量。
|
7
|
-
|
8
|
-
## 内置的环境变量
|
9
|
-
|
10
|
-
### NODE_ENV
|
11
|
-
|
12
|
-
表示当前的执行环境,是**只读的**的环境变量,其值在不同的执行命令下具有不同的值:
|
13
|
-
|
14
|
-
- `production`:执行 `modern build`、`modern preview` 命令时的默认值。
|
15
|
-
- `test`:执行 `modern test` 命令时的默认值。
|
16
|
-
- `development`:执行 `modern dev` 命令时的默认值,同时也是其他所有情况下的默认值。
|
17
|
-
|
18
|
-
### MODERN_ENV
|
19
|
-
|
20
|
-
用于设置当前的执行环境,除了上述 NODE_ENV 对应的值之外,这里支持自定义环境名称,比如 `staging`、`boe` 等,Modern.js 会根据该环境变量对应的加载 env.{process.env.MODERN_ENV} 文件中设置的环境变量值。
|
21
|
-
|
22
|
-
当需要通过环境名称使用不同的配置时,可以把环境变量定义到对应的环境名称的 env 文件中。
|
23
|
-
|
24
|
-
MODERN_ENV 的优先级高于 NODE_ENV。
|
25
|
-
|
26
|
-
## 自定义环境变量
|
27
|
-
|
28
|
-
Modern.js 支持自定义的环境变量,以 `MODERN_` 开头的环境变量可以在代码中直接使用。
|
29
|
-
|
30
|
-
如果需要自动以任意名称的环境变量,需要在 [`source.define`](/docs/configure/app/source/define) 配置中声明后使用。
|
31
|
-
|
32
|
-
## 定义环境变量
|
33
|
-
|
34
|
-
环境变量支持通过 `shell` 和 `.env` 文件两种方式指定。
|
35
|
-
|
36
|
-
### 通过 `shell` 指定
|
37
|
-
|
38
|
-
在命令前添加自定义环境变量:
|
39
|
-
|
40
|
-
```shell
|
41
|
-
REACT_APP_FOO=123 BAR=456 pnpm run dev
|
42
|
-
```
|
43
|
-
|
44
|
-
### 通过 `.env` 文件指定
|
45
|
-
|
46
|
-
在项目根目录创建 `.env` 文件,其中添加自定义环境变量:
|
47
|
-
|
48
|
-
```env
|
49
|
-
REACT_APP_FOO=123
|
50
|
-
BAR=456
|
51
|
-
```
|
52
|
-
|
53
|
-
.env 文件支持后缀名后 NODE_ENV 或 MODERN_ENV 定义的环境名称,如 `.env.development`,可根据项目需求进行定义。Modern.js 会根据环境名称变量值读取对应的 env 文件。
|
54
|
-
|
55
|
-
## 使用环境变量
|
56
|
-
|
57
|
-
### JS/TS 代码内使用
|
58
|
-
|
59
|
-
通过 `process.env.*` 直接获取环境变量。
|
60
|
-
|
61
|
-
```js
|
62
|
-
console.log(process.env.NODE_ENV);
|
63
|
-
// "development"
|
64
|
-
```
|
65
|
-
|
66
|
-
### HTML 代码片段使用
|
67
|
-
|
68
|
-
```html title="config/html/head.ejs"
|
69
|
-
<meta name="env" content="<%=process.env.NODE_ENV %>" />
|
70
|
-
```
|