@modern-js/main-doc 2.60.6 → 2.61.0
Sign up to get free protection for your applications and to get access to all the features.
- package/docs/en/apis/app/runtime/core/use-runtime-context.mdx +86 -33
- package/docs/en/components/init-app.mdx +0 -1
- package/docs/en/components/init-rspack-app.mdx +0 -1
- package/docs/en/guides/advanced-features/source-build.mdx +11 -4
- package/docs/en/guides/basic-features/deploy.mdx +104 -2
- package/docs/en/guides/concept/entries.mdx +58 -5
- package/docs/en/guides/get-started/introduction.mdx +1 -38
- package/docs/zh/apis/app/runtime/core/use-runtime-context.mdx +86 -33
- package/docs/zh/components/default-mwa-generate.mdx +0 -1
- package/docs/zh/components/init-app.mdx +0 -1
- package/docs/zh/components/init-rspack-app.mdx +0 -1
- package/docs/zh/guides/advanced-features/source-build.mdx +11 -4
- package/docs/zh/guides/basic-features/deploy.mdx +103 -5
- package/docs/zh/guides/concept/entries.mdx +52 -4
- package/docs/zh/guides/get-started/introduction.mdx +1 -36
- package/package.json +4 -4
- package/src/i18n/enUS.ts +0 -7
- package/src/i18n/zhCN.ts +0 -7
- package/src/pages/index.tsx +3 -32
- package/docs/en/guides/topic-detail/generator/_meta.json +0 -17
- package/docs/en/guides/topic-detail/generator/create/_meta.json +0 -1
- package/docs/en/guides/topic-detail/generator/create/config.mdx +0 -59
- package/docs/en/guides/topic-detail/generator/create/option.md +0 -146
- package/docs/en/guides/topic-detail/generator/create/use.mdx +0 -48
- package/docs/en/guides/topic-detail/generator/new/_meta.json +0 -1
- package/docs/en/guides/topic-detail/generator/new/config.md +0 -115
- package/docs/en/guides/topic-detail/generator/new/option.md +0 -67
- package/docs/en/guides/topic-detail/generator/new/use.md +0 -75
- package/docs/en/guides/topic-detail/generator/plugin/_meta.json +0 -11
- package/docs/en/guides/topic-detail/generator/plugin/api/afterForged.md +0 -49
- package/docs/en/guides/topic-detail/generator/plugin/api/context.md +0 -184
- package/docs/en/guides/topic-detail/generator/plugin/api/input.md +0 -124
- package/docs/en/guides/topic-detail/generator/plugin/api/onForged.md +0 -310
- package/docs/en/guides/topic-detail/generator/plugin/category.md +0 -88
- package/docs/en/guides/topic-detail/generator/plugin/context.md +0 -104
- package/docs/en/guides/topic-detail/generator/plugin/structure.md +0 -106
- package/docs/en/guides/topic-detail/generator/plugin/use.md +0 -33
- package/docs/zh/guides/topic-detail/generator/_meta.json +0 -17
- package/docs/zh/guides/topic-detail/generator/create/_meta.json +0 -1
- package/docs/zh/guides/topic-detail/generator/create/config.mdx +0 -60
- package/docs/zh/guides/topic-detail/generator/create/option.md +0 -146
- package/docs/zh/guides/topic-detail/generator/create/use.mdx +0 -48
- package/docs/zh/guides/topic-detail/generator/new/_meta.json +0 -1
- package/docs/zh/guides/topic-detail/generator/new/config.md +0 -116
- package/docs/zh/guides/topic-detail/generator/new/option.md +0 -67
- package/docs/zh/guides/topic-detail/generator/new/use.md +0 -74
- package/docs/zh/guides/topic-detail/generator/plugin/_meta.json +0 -11
- package/docs/zh/guides/topic-detail/generator/plugin/api/afterForged.md +0 -50
- package/docs/zh/guides/topic-detail/generator/plugin/api/context.md +0 -184
- package/docs/zh/guides/topic-detail/generator/plugin/api/input.md +0 -124
- package/docs/zh/guides/topic-detail/generator/plugin/api/onForged.md +0 -310
- package/docs/zh/guides/topic-detail/generator/plugin/category.md +0 -93
- package/docs/zh/guides/topic-detail/generator/plugin/context.md +0 -105
- package/docs/zh/guides/topic-detail/generator/plugin/structure.md +0 -106
- package/docs/zh/guides/topic-detail/generator/plugin/use.md +0 -33
@@ -1,310 +0,0 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 2
|
3
|
-
---
|
4
|
-
|
5
|
-
# onForged
|
6
|
-
|
7
|
-
`onForged` is a lifecycle function used for file operations in generator plugin.
|
8
|
-
|
9
|
-
## Types
|
10
|
-
|
11
|
-
```ts
|
12
|
-
export type ForgedAPI = {
|
13
|
-
addFile: (params: AddFileParams) => Promise<void>;
|
14
|
-
addManyFiles: (params: AddManyFilesParams) => Promise<void>;
|
15
|
-
updateJSONFile: (fileName: string, updateInfo: Record<string, unknown>) => Promise<void>;
|
16
|
-
updateTextRawFile: (fileName: string, update: (content: string[]) => string[]) => Promise<void>;
|
17
|
-
updateModernConfig: (updateInfo: Record<string, any>) => Promise<void>;
|
18
|
-
rmFile: (fileName: string) => Promise<void>;
|
19
|
-
rmDir: (dirName: string) => Promise<void>;
|
20
|
-
addHelper: (name: string, fn: Handlebars.HelperDelegate) => void;
|
21
|
-
addPartial: (name: string, str: Handlebars.Template) => void;
|
22
|
-
createElement: (element: ActionElement, params: Record<string, unknown>) => Promise<void>;
|
23
|
-
enableFunc: (func: ActionFunction, params?: Record<string, unknown> | undefined) => Promise<void>;
|
24
|
-
};
|
25
|
-
|
26
|
-
export type PluginForgedFunc = (
|
27
|
-
api: ForgedAPI,
|
28
|
-
inputData: Record<string, unknown>,
|
29
|
-
) => void | Promise<void>;
|
30
|
-
|
31
|
-
export interface IPluginContext {
|
32
|
-
onForged: (func: PluginForgedFunc) => void;
|
33
|
-
...
|
34
|
-
}
|
35
|
-
```
|
36
|
-
|
37
|
-
The `onForged` parameter is a callback function with `api` and `input` as parameters, which are used to provide the APIs and current input information provided by the lifecycle function.
|
38
|
-
|
39
|
-
## Concepts
|
40
|
-
|
41
|
-
### File Types
|
42
|
-
|
43
|
-
The generator plugin classifies files into four categories:
|
44
|
-
|
45
|
-
- Text files
|
46
|
-
|
47
|
-
Files with pure text content that can be processed using [Handlebars](https://handlebarsjs.com/) or [EJS](https://ejs.co/) templates.
|
48
|
-
|
49
|
-
- Binary files
|
50
|
-
|
51
|
-
Files such as images, audio, and video.
|
52
|
-
|
53
|
-
- JSON files
|
54
|
-
|
55
|
-
Files in JSON format.
|
56
|
-
|
57
|
-
- Text list files
|
58
|
-
|
59
|
-
Files composed of lines of text, such as `.gitignore`, `.editorconfig`, and `.npmrc`.
|
60
|
-
|
61
|
-
The type definitions for the four types of files are:
|
62
|
-
|
63
|
-
```ts
|
64
|
-
export enum FileType {
|
65
|
-
Text = 'text',
|
66
|
-
Binary = 'binary',
|
67
|
-
Json = 'json',
|
68
|
-
TextRaw = 'textRaw',
|
69
|
-
}
|
70
|
-
```
|
71
|
-
|
72
|
-
## API
|
73
|
-
|
74
|
-
The APIs provided by the `api` parameter will be introduced below.
|
75
|
-
|
76
|
-
### addFile
|
77
|
-
|
78
|
-
Adds a single file.
|
79
|
-
|
80
|
-
Parameter types:
|
81
|
-
|
82
|
-
```ts
|
83
|
-
export interface AddFileParams {
|
84
|
-
type: FileType;
|
85
|
-
file: string;
|
86
|
-
template?: string;
|
87
|
-
templateFile?: string;
|
88
|
-
force?: boolean;
|
89
|
-
data?: Record<string, string>;
|
90
|
-
}
|
91
|
-
```
|
92
|
-
|
93
|
-
- `type`: File type.
|
94
|
-
- `file`: Target file path, relative to the target project directory.
|
95
|
-
- `template`: Template content, the value of this field can be directly used to render the file. Lower priority than `templateFile`.
|
96
|
-
- `templateFile`: Template file path, relative to the templates directory.
|
97
|
-
- `force`: Whether to force overwrite when the target file exists, default is false.
|
98
|
-
- `data`: Template rendering data.
|
99
|
-
|
100
|
-
:::info
|
101
|
-
Text files are processed using Handlebars by default. If the template file ends with `.ejs`, [EJS](https://ejs.co/) will be used for template rendering.
|
102
|
-
:::
|
103
|
-
|
104
|
-
For example, add the template file `App.tsx.hanlebars` to `src/App.tsx`:
|
105
|
-
|
106
|
-
```ts
|
107
|
-
context.onForged(async (api: ForgedAPI, _input: Record<string, unknown>) => {
|
108
|
-
await api.addFile({
|
109
|
-
type: FileType.Text,
|
110
|
-
file: `src/App.tsx`,
|
111
|
-
templateFile: `App.tsx.handlebars`,
|
112
|
-
});
|
113
|
-
})
|
114
|
-
```
|
115
|
-
|
116
|
-
### addManyFiles
|
117
|
-
|
118
|
-
Adds multiple files, usually used to add multiple files to the same target directory.
|
119
|
-
|
120
|
-
Parameter types:
|
121
|
-
|
122
|
-
```ts
|
123
|
-
export interface AddManyFilesParams {
|
124
|
-
type: FileType;
|
125
|
-
destination: string;
|
126
|
-
templateFiles: string[] | (() => string[]);
|
127
|
-
templateBase?: string;
|
128
|
-
fileNameFunc?: (name: string) => string;
|
129
|
-
data?: Record<string, string>;
|
130
|
-
}
|
131
|
-
```
|
132
|
-
|
133
|
-
- `type`: File type.
|
134
|
-
- `destination`: Target folder, relative path to the target project directory.
|
135
|
-
- `templateFiles`: Template file list, supporting regular expressions from [globby](https://www.npmjs.com/package/globby).
|
136
|
-
- `templateBase`: Common path of template files. When using this parameter, the target file will automatically delete this path.
|
137
|
-
- `fileNameFunc`: Function to rename files. During the file addition process, the file name will be passed to this function in turn, and the renaming can be performed as needed.
|
138
|
-
- `data`: Template rendering data.
|
139
|
-
|
140
|
-
For example, render all files in the `src-ts` directory of the template file to the `src` directory:
|
141
|
-
|
142
|
-
```ts
|
143
|
-
context.onForged(async (api: ForgedAPI, _input: Record<string, unknown>) => {
|
144
|
-
await api.addManyFiles({
|
145
|
-
type: FileType.Text,
|
146
|
-
destination: 'src',
|
147
|
-
templateFiles: ['src-ts/**/*'],
|
148
|
-
templateBase: 'src-ts',
|
149
|
-
fileNameFunc: name => name.replace('.handlebars', ''),
|
150
|
-
});
|
151
|
-
})
|
152
|
-
```
|
153
|
-
|
154
|
-
### updateJSONFile
|
155
|
-
|
156
|
-
Update fields in a JSON file.
|
157
|
-
|
158
|
-
Parameter types:
|
159
|
-
|
160
|
-
```ts
|
161
|
-
fileName: strings
|
162
|
-
updateInfo: Record<string, unknown>
|
163
|
-
```
|
164
|
-
|
165
|
-
- `fileName`: JSON file path, relative to the target project path.
|
166
|
-
- `updateInfo`: Update information. Nested field updates need to be flattened, otherwise the entire update will cause content loss.
|
167
|
-
|
168
|
-
For example, update the `name` field of `package.json`:
|
169
|
-
|
170
|
-
```ts
|
171
|
-
context.onForged(async (api: ForgedAPI, _input: Record<string, unknown>) => {
|
172
|
-
await api.updateJSONFile('package.json', { name: 'new_name' });
|
173
|
-
})
|
174
|
-
```
|
175
|
-
|
176
|
-
For example, update the version of `react-dom` in `dependencies`:
|
177
|
-
|
178
|
-
```ts
|
179
|
-
context.onForged(async (api: ForgedAPI, _input: Record<string, unknown>) => {
|
180
|
-
await api.updateJSONFile('package.json', {
|
181
|
-
'dependencies.react-dom': '^18',
|
182
|
-
});
|
183
|
-
})
|
184
|
-
```
|
185
|
-
|
186
|
-
### updateTextRawFile
|
187
|
-
|
188
|
-
Update the contents of a text list file.
|
189
|
-
|
190
|
-
Parameter types:
|
191
|
-
|
192
|
-
```ts
|
193
|
-
fileName: string
|
194
|
-
update: (content: string[]) => string[]
|
195
|
-
```
|
196
|
-
|
197
|
-
- `fileName`: Text list file path, relative to the target project path.
|
198
|
-
- `update`: Update function. The parameter is an array divided by `\n` of the current file content, and the return value is also the modified array after modification. `@modern-js/create` will automatically merge it with `\n` and write it to the source file.
|
199
|
-
|
200
|
-
For example, add the `dist` directory to the `.gitinore` file:
|
201
|
-
|
202
|
-
```ts
|
203
|
-
context.onForged(async (api: ForgedAPI, _input: Record<string, unknown>) => {
|
204
|
-
await api.updateTextRawFile('.gitinore', (content) => [...content, 'dist']);
|
205
|
-
})
|
206
|
-
```
|
207
|
-
|
208
|
-
### updateModernConfig (not recommended)
|
209
|
-
|
210
|
-
In addition to configuring in `modern.config.[tj]s`, Modern.js configuration also supports configuring `modernConfig` in `package.json`. This function is used to update this field.
|
211
|
-
|
212
|
-
Parameter types:
|
213
|
-
|
214
|
-
```ts
|
215
|
-
updateInfo: Record<string, any>
|
216
|
-
```
|
217
|
-
|
218
|
-
- `updateInfo`: Update content information. `updateModernConfig` is a package based on `updateJSONFile`, which will automatically update under the `modernConfig` field. Only the configuration fields under `modernConfig` need to be filled in `updateInfo`.
|
219
|
-
|
220
|
-
For example, enable SSR:
|
221
|
-
|
222
|
-
```ts
|
223
|
-
context.onForged(async (api: ForgedAPI, _input: Record<string, unknown>) => {
|
224
|
-
await api.updateModernConfig({ 'server.ssr': true });
|
225
|
-
})
|
226
|
-
```
|
227
|
-
|
228
|
-
### rmFile
|
229
|
-
|
230
|
-
Delete files.
|
231
|
-
|
232
|
-
Parameter types:
|
233
|
-
|
234
|
-
```ts
|
235
|
-
fileName: string
|
236
|
-
```
|
237
|
-
|
238
|
-
- `fileName`: The path of the file to be deleted, relative to the target project path.
|
239
|
-
|
240
|
-
### rmDir
|
241
|
-
|
242
|
-
Delete a folder.
|
243
|
-
|
244
|
-
Parameter types:
|
245
|
-
|
246
|
-
```ts
|
247
|
-
dirName: string
|
248
|
-
```
|
249
|
-
|
250
|
-
- `dirName`: The path of the folder to be deleted, relative to the target project path.
|
251
|
-
|
252
|
-
### addHelper
|
253
|
-
|
254
|
-
Add a [custom helper](https://handlebarsjs.com/guide/#custom-helpers) rendered by handlebrs.
|
255
|
-
|
256
|
-
Parameter types:
|
257
|
-
|
258
|
-
```ts
|
259
|
-
name: string
|
260
|
-
fn: Handlebars.HelperDelegate
|
261
|
-
```
|
262
|
-
|
263
|
-
- `name`: Helper function name.
|
264
|
-
- `fn`: Helper function implementation.
|
265
|
-
|
266
|
-
### addPartial
|
267
|
-
|
268
|
-
Add a [Partial](https://handlebarsjs.com/guide/partials.html#basic-partials) rendered by Handlebars.
|
269
|
-
|
270
|
-
Parameter types:
|
271
|
-
|
272
|
-
```ts
|
273
|
-
name: string
|
274
|
-
str: Handlebars.Template
|
275
|
-
```
|
276
|
-
|
277
|
-
- `name`: Partial name.
|
278
|
-
- `str`: Template string of the Partial.
|
279
|
-
|
280
|
-
### createElement
|
281
|
-
|
282
|
-
Automatically run the `new` command to create project elements.
|
283
|
-
|
284
|
-
Parameter types:
|
285
|
-
|
286
|
-
```ts
|
287
|
-
element: ActionElement
|
288
|
-
params: Record<string, unknown>
|
289
|
-
```
|
290
|
-
|
291
|
-
- `element`: Type of project element, new entry or new custom Web Server source directory.
|
292
|
-
- `params`: Other parameters corresponding to creating project elements.
|
293
|
-
|
294
|
-
### enableFunc
|
295
|
-
|
296
|
-
Automatically run the `new` command to enable optional features.
|
297
|
-
|
298
|
-
Parameter types:
|
299
|
-
|
300
|
-
```ts
|
301
|
-
func: ActionFunction
|
302
|
-
params?: Record<string, unknown>
|
303
|
-
```
|
304
|
-
|
305
|
-
- `func`: Name of the feature to enable.
|
306
|
-
- `params`: Other parameters corresponding to enabling the feature.
|
307
|
-
|
308
|
-
:::info
|
309
|
-
Refer to [Configuration Parameters](/guides/topic-detail/generator/new/config.html) for creating project elements and enabling feature configurations.
|
310
|
-
:::
|
@@ -1,88 +0,0 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 2
|
3
|
-
---
|
4
|
-
|
5
|
-
# Introduction
|
6
|
-
|
7
|
-
There are two types of generator plugin:
|
8
|
-
|
9
|
-
- Extension: Web App(Npm Module) = Web App(Npm Module) + Generator Plugin
|
10
|
-
|
11
|
-
- Custom: New Application(Npm Module) = Web App(Npm Module) + Generator Plugin
|
12
|
-
|
13
|
-
|
14
|
-
In simpler terms, extension means using the original project name of Modern.js, while custom means creating a new name.
|
15
|
-
|
16
|
-
## Identifier
|
17
|
-
|
18
|
-
The type of generator plugin is provided through the `meta` field in `package.json`:
|
19
|
-
|
20
|
-
### Extension
|
21
|
-
|
22
|
-
```json title="package.json"
|
23
|
-
{
|
24
|
-
"meta": {
|
25
|
-
"extend": "mwa" // module
|
26
|
-
}
|
27
|
-
}
|
28
|
-
```
|
29
|
-
|
30
|
-
### Custom
|
31
|
-
|
32
|
-
```json title="package.json"
|
33
|
-
{
|
34
|
-
"meta": {
|
35
|
-
"key": "new_app",
|
36
|
-
"name": "new App",
|
37
|
-
"type": "mwa" // module and custom
|
38
|
-
}
|
39
|
-
}
|
40
|
-
```
|
41
|
-
|
42
|
-
:::info
|
43
|
-
For custom type, the `type` field supports the `custom` value, which utilizes some best practice templates for project development provided by Modern.js, such as `.gitignore`, `.editorConfig`, etc. Other business-related code templates need to be manually provided through generator plugin.
|
44
|
-
:::
|
45
|
-
|
46
|
-
# Creating Initial Project
|
47
|
-
|
48
|
-
### Extension
|
49
|
-
|
50
|
-
```bash
|
51
|
-
npx @modern-js/create@latest plugin --plugin @modern-js/generator-plugin-plugin
|
52
|
-
? Please select the type of project you want to create: Npm Module
|
53
|
-
? Please select the project scenario: Generator Plugin
|
54
|
-
? Please fill in the package name of the generator plugin: plugin
|
55
|
-
? Please select the programming language: TS
|
56
|
-
? Please select the package manager: pnpm
|
57
|
-
? Please select the type of plugin: extend
|
58
|
-
? Please select the base type of the plugin: Web App
|
59
|
-
```
|
60
|
-
|
61
|
-
### Custom
|
62
|
-
|
63
|
-
```bash
|
64
|
-
npx @modern-js/create@latest plugin --plugin @modern-js/generator-plugin-plugin
|
65
|
-
? Please select the type of project you want to create: Npm Module
|
66
|
-
? Please select the project scenario: Generator Plugin
|
67
|
-
? Please fill in the package name of the generator plugin: plugin
|
68
|
-
? Please select the programming language: TS
|
69
|
-
? Please select the package manager: pnpm
|
70
|
-
? Please select the type of plugin: custom
|
71
|
-
? Please fill in the plugin keyword: new_app
|
72
|
-
? Please fill in the display name of the plugin: new App
|
73
|
-
? Please select the base type of the plugin: Web App
|
74
|
-
```
|
75
|
-
|
76
|
-
## Execution Order
|
77
|
-
|
78
|
-
Generator plugins support using multiple plugins at the same time, which means that multiple `--plugin` parameters are supported when executing `@modern-js/create`.
|
79
|
-
|
80
|
-
### Extension
|
81
|
-
|
82
|
-
For extension generator plugins, when executing with the declared generator plugin parameters, the corresponding lifecycle functions of the project scheme that is extended will be executed in order.
|
83
|
-
|
84
|
-
For example, if there are two generator plugins A and B, both of which extend the Modern.js application scheme. A plugin declares to add the `a.ts` file, and B plugin declares to add the `b.ts` file. When executing `npx @modern-js/create@latest --plugin A --plugin B`, and selecting the Web APP scheme, the default project files of the Web APP will be generated first. Then, the A plugin will create the `a.ts` file, and then the B plugin will generate the `b.ts` file.
|
85
|
-
|
86
|
-
### Custom
|
87
|
-
|
88
|
-
For custom generator plugins, only one plugin can be executed at a time. When declaring the `--plugin` parameter for `@modern-js/create`, a selection option for the scenario of choosing the project scheme will be added after selecting the project scheme, which is the new name defined in the `package.json`. Choosing the corresponding generator plugin for the name will execute the corresponding generator plugin logic after the default project scheme is completed. When declaring the `--plugin` parameter, the corresponding generator plugin names will be listed one by one for selection in the project scheme scenario.
|
@@ -1,104 +0,0 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 4
|
3
|
-
---
|
4
|
-
|
5
|
-
# Context
|
6
|
-
|
7
|
-
By default, generator plugins will export a function that takes `context` as a parameter. During execution, `context` will be automatically injected into the generator plugin.
|
8
|
-
|
9
|
-
```ts title="src/index.ts"
|
10
|
-
import { IPluginContext, ForgedAPI } from '@modern-js/generator-plugin';
|
11
|
-
|
12
|
-
export default function (context: IPluginContext) {
|
13
|
-
context.onForged(async (_api: ForgedAPI, _input: Record<string, unknown>) => {
|
14
|
-
/**
|
15
|
-
* Todo
|
16
|
-
*/
|
17
|
-
});
|
18
|
-
}
|
19
|
-
```
|
20
|
-
`context` provides two types of APIs, which are used to customize input and define generator plugin lifecycle logic.
|
21
|
-
|
22
|
-
:::info
|
23
|
-
Only some APIs are briefly explained below. For the complete API, please refer to [Generator Plugin API](/guides/topic-detail/generator/plugin/api/context.html).
|
24
|
-
:::
|
25
|
-
|
26
|
-
## Customize Input
|
27
|
-
|
28
|
-
Both Modern.js Framework and Modern.js Module have default input interactions. These APIs can be used to add, modify, hide, and provide default values for these inputs.
|
29
|
-
|
30
|
-
For example:
|
31
|
-
|
32
|
-
- Add question
|
33
|
-
|
34
|
-
```ts
|
35
|
-
context.addInputBefore('packageManager', {
|
36
|
-
type: 'object',
|
37
|
-
properties: {
|
38
|
-
'username': {
|
39
|
-
type: 'string',
|
40
|
-
title: 'Username',
|
41
|
-
},
|
42
|
-
},
|
43
|
-
});
|
44
|
-
```
|
45
|
-
|
46
|
-
- Hide question by setting `config`
|
47
|
-
|
48
|
-
```ts
|
49
|
-
context.setDefaultConfig({ langauge: 'ts' });
|
50
|
-
```
|
51
|
-
|
52
|
-
## Lifecycle
|
53
|
-
|
54
|
-
Generator plugin provide two lifecycle hooks to define generator plugin behavior:
|
55
|
-
|
56
|
-
- `onForged`: Lifecycle after file operations are completed.
|
57
|
-
|
58
|
-
- `afterForged`: Lifecycle after the `onForged` hook function is executed.
|
59
|
-
|
60
|
-
## `onForged`
|
61
|
-
|
62
|
-
Hook function after the Modern.js project scheme generator has completed file operations. It is used to complete file operations in the generator plugin, such as adding template files, overwriting existing files, deleting existing files, etc.
|
63
|
-
|
64
|
-
When multiple generator plugins are executed simultaneously for extension type, the `onForged` operations of the corresponding generator plugins will be executed in order according to the declared order.
|
65
|
-
|
66
|
-
The `onForged` function takes a callback function as a parameter, with `api` and `input` as arguments.
|
67
|
-
|
68
|
-
```ts
|
69
|
-
context.onForged(async (api: ForgedAPI, input: Record<string, unknown>) => {
|
70
|
-
const { language } = input;
|
71
|
-
api.addFile({
|
72
|
-
type: FileType.Text,
|
73
|
-
file: `src/index.${language as string}`,
|
74
|
-
templateFile: `index.${language as string}.handlebars`,
|
75
|
-
force: true,
|
76
|
-
});
|
77
|
-
})
|
78
|
-
```
|
79
|
-
|
80
|
-
The `api` object provides file operation-related methods supported by generator plugin.
|
81
|
-
|
82
|
-
`input` is the current user input, which includes the `--config` parameter definition, the default scheme interaction, and the user input defined by the generator plugin.
|
83
|
-
|
84
|
-
When adding a new file template, define the template file in the `templates` directory, and then operate on it through the `api` method above. The generator plugin defaults to operating on files in the `templates` directory, so there is no need to declare the `templates` path.
|
85
|
-
|
86
|
-
## `afterForged`
|
87
|
-
|
88
|
-
Executed after the `onForged` hook function is completed. It is mainly used to install dependencies, perform git operations, etc.
|
89
|
-
|
90
|
-
By default, Modern.js project scheme will install dependencies and initialize Git after completing file operations, and perform git initial submissions, etc. This hook function can be omitted.
|
91
|
-
|
92
|
-
For custom generator plugins that also support the `custom` type, which only provides a small number of best practice project configurations, installation of dependencies and Git initialization operations need to be completed in this hook function.
|
93
|
-
|
94
|
-
The `afterForged` function also takes a callback function as a parameter, with `api` and `input` as arguments.
|
95
|
-
|
96
|
-
```ts
|
97
|
-
context.afterForged(
|
98
|
-
async (api: AfterForgedAPI, input: Record<string, unknown>) => {
|
99
|
-
const { packageManager } = input;
|
100
|
-
console.info('packageManager:', packageManager);
|
101
|
-
await api.install();
|
102
|
-
},
|
103
|
-
);
|
104
|
-
```
|
@@ -1,106 +0,0 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 1
|
3
|
-
---
|
4
|
-
|
5
|
-
# Introduction and Project Creation
|
6
|
-
|
7
|
-
## Introduction
|
8
|
-
|
9
|
-
Modern.js provides engineering solutions such as Web App and Npm Module. By using the `@modern-js/create` tool, you can create initial project templates for engineering solutions. The initial project template provides a basic code development environment, simple example code, and configuration, etc.
|
10
|
-
|
11
|
-
The initial templates provided by Modern.js are generic and can meet some common project development needs.
|
12
|
-
|
13
|
-
When you use Modern.js deeply, you will inevitably find that each created project will make some specific similar changes for its own project, such as modifying example code, adding some configurations, enabling certain features, etc.
|
14
|
-
|
15
|
-
Generator plugin can help you to deposit these specific changes for individuals or teams. When running `npx @modern-js/create@latest`, you only need to simply add the `--plugin` parameter to avoid repetitive modification of the project after each project creation.
|
16
|
-
|
17
|
-
Generator plugin is based on the initial template projects provided by Modern.js, providing methods to add, delete, and modify templates, and modifying `package.json`, `modernConfig` configurations, and enabling features through a convenient way.
|
18
|
-
|
19
|
-
## Create Project
|
20
|
-
|
21
|
-
Use `@modern-js/create` to create a generator plugin project directly:
|
22
|
-
|
23
|
-
```bash
|
24
|
-
npx @modern-js/create@latest plugin --plugin @modern-js/generator-plugin-plugin
|
25
|
-
? Please select the type of project you want to create: Npm Module
|
26
|
-
? Please select the project scenario: Generator Plugin
|
27
|
-
? Please fill in the package name of the generator plugin: plugin
|
28
|
-
? Please select the programming language: TS
|
29
|
-
? Please select the package manager: pnpm
|
30
|
-
? Please select the type of plugin: extend
|
31
|
-
? Please select the base type of the plugin: Web App
|
32
|
-
```
|
33
|
-
|
34
|
-
After creating, we can take a look at the directory structure of this project:
|
35
|
-
|
36
|
-
```bash
|
37
|
-
.
|
38
|
-
├── .changeset
|
39
|
-
│ └── config.json
|
40
|
-
├── .eslintrc.js
|
41
|
-
├── .gitignore
|
42
|
-
├── .npmrc
|
43
|
-
├── .nvmrc
|
44
|
-
├── .prettierrc
|
45
|
-
├── README.md
|
46
|
-
├── modern.config.ts
|
47
|
-
├── package.json
|
48
|
-
├── src
|
49
|
-
│ ├── modern-app-env.d.ts
|
50
|
-
│ └── index.ts
|
51
|
-
├── templates
|
52
|
-
│ └── .gitkeep
|
53
|
-
└── tsconfig.json
|
54
|
-
```
|
55
|
-
|
56
|
-
The project is based on the Npm module project, and the core files are as follows:
|
57
|
-
|
58
|
-
```bash
|
59
|
-
*
|
60
|
-
├── package.json
|
61
|
-
├── src
|
62
|
-
│ └── index.ts
|
63
|
-
├── templates
|
64
|
-
│ └── .gitkeep
|
65
|
-
```
|
66
|
-
|
67
|
-
## package.json
|
68
|
-
|
69
|
-
In addition to the normal fields of a module project, `package.json` provides a `meta` field to describe information about the generator plugin.
|
70
|
-
|
71
|
-
Generator plugin is divided into two categories: extension and custom. For specific classification definitions, please refer to [Type](/guides/topic-detail/generator/plugin/category).
|
72
|
-
|
73
|
-
```json title="package.json"
|
74
|
-
{
|
75
|
-
...,
|
76
|
-
"meta": {
|
77
|
-
"extend": "mwa"
|
78
|
-
}
|
79
|
-
}
|
80
|
-
```
|
81
|
-
|
82
|
-
## src/index.ts
|
83
|
-
|
84
|
-
This file is used to complete the content development of the generator plugin.
|
85
|
-
|
86
|
-
```js
|
87
|
-
import { IPluginContext, ForgedAPI } from '@modern-js/generator-plugin';
|
88
|
-
|
89
|
-
export default function (context: IPluginContext) {
|
90
|
-
context.onForged(async (_api: ForgedAPI, _input: Record<string, unknown>) => {
|
91
|
-
/**
|
92
|
-
* todo
|
93
|
-
*/
|
94
|
-
});
|
95
|
-
}
|
96
|
-
```
|
97
|
-
|
98
|
-
This file exports a function by default, and the function takes `context` as a parameter. The `context` object provides API methods supported by the generator plugin, which can be used to implement the logic of the generator plugin. The capabilities provided by `context` can be found in [context](/guides/topic-detail/generator/plugin/context).
|
99
|
-
|
100
|
-
## templates
|
101
|
-
|
102
|
-
The `templates` directory contains template files for the current customization method, supporting [Handlebars](https://handlebarsjs.com/) and [EJS](https://ejs.co/) formats. Different rendering engines will be used for rendering according to the template file suffix. If there is no suffix, the template file will be copied directly to the target directory.
|
103
|
-
|
104
|
-
If the template file is a `js`, `ts`, or `json` file, it is recommended to use the `.handlebars` or `.ejs` suffix directly to avoid project eslint errors and recognition issues in Monorepo projects.
|
105
|
-
|
106
|
-
The `.gitignore` and `.npmrc` files in the template will be automatically deleted when publishing the npm package, so it is necessary to use the `.handlebars` or `.ejs` suffix to keep them.
|
@@ -1,33 +0,0 @@
|
|
1
|
-
---
|
2
|
-
sidebar_position: 3
|
3
|
-
---
|
4
|
-
|
5
|
-
# Usage
|
6
|
-
|
7
|
-
`@modern-js/create` provides the `--plugin` parameter to run the generator plugin project.
|
8
|
-
|
9
|
-
`--plugin` supports three formats:
|
10
|
-
|
11
|
-
## Absolute path
|
12
|
-
|
13
|
-
Suitable for local development and debugging. After development is completed, build the project by running `npm run build` in the generator plugin project, and then use the following command for testing:
|
14
|
-
|
15
|
-
```bash
|
16
|
-
npx @modern-js/create@latest --plugin <pluginPath>
|
17
|
-
```
|
18
|
-
|
19
|
-
## Relative path
|
20
|
-
|
21
|
-
Suitable for local development and debugging or when the generator plugin project and the target project to be created are in the same Monorepo and there is no need to publish an NPM package. After building the generator plugin project, use the following command:
|
22
|
-
|
23
|
-
```bash
|
24
|
-
npx @modern-js/create@latest --plugin file:../<pluginPath>
|
25
|
-
```
|
26
|
-
|
27
|
-
## NPM package
|
28
|
-
|
29
|
-
Suitable for scenarios where the generator project is published on npm for sharing.
|
30
|
-
|
31
|
-
```bash
|
32
|
-
npx @modern-js/create@latest --plugin <pluginPackage>
|
33
|
-
```
|
@@ -1 +0,0 @@
|
|
1
|
-
["use", "option", "config"]
|