@makoojs/cli 0.4.1 → 0.5.1

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.
Files changed (61) hide show
  1. package/README.CN.md +48 -387
  2. package/README.md +48 -387
  3. package/dist/bin.js +594 -1141
  4. package/dist/cli/commands/build.d.ts +2 -0
  5. package/dist/cli/commands/dev/DevTerminal.d.ts +51 -0
  6. package/dist/cli/commands/dev/dev.d.ts +2 -0
  7. package/dist/cli/commands/preview.d.ts +2 -0
  8. package/dist/cli/injections/renderInjections.d.ts +2 -0
  9. package/dist/{shared → cli}/terminalColor.d.ts +2 -0
  10. package/dist/cli/version.d.ts +1 -0
  11. package/dist/config/defaults.d.ts +3 -24
  12. package/dist/config/errors.d.ts +3 -0
  13. package/dist/config/resolve.d.ts +6 -18
  14. package/dist/config/types.d.ts +8 -106
  15. package/dist/config/validation.d.ts +4 -7
  16. package/dist/index.cjs +89 -38
  17. package/dist/index.d.ts +3 -3
  18. package/dist/index.js +1008 -3255
  19. package/dist/session/DevSession.d.ts +14 -0
  20. package/dist/session/types.d.ts +47 -0
  21. package/dist/vite/makoo.d.ts +3 -0
  22. package/dist/vite/makooDev.d.ts +7 -0
  23. package/package.json +8 -13
  24. package/dist/command/_util.d.ts +0 -8
  25. package/dist/command/add.d.ts +0 -1
  26. package/dist/command/build.d.ts +0 -1
  27. package/dist/command/dev.d.ts +0 -1
  28. package/dist/command/inspect.d.ts +0 -35
  29. package/dist/config/config.d.ts +0 -3
  30. package/dist/error/MakooCliError.d.ts +0 -58
  31. package/dist/generator/generator.d.ts +0 -3
  32. package/dist/generator/render/import/importAdapter.d.ts +0 -3
  33. package/dist/generator/render/import/importComp.d.ts +0 -3
  34. package/dist/generator/render/import/importMakooRuntime.d.ts +0 -1
  35. package/dist/generator/render/import/importManifest.d.ts +0 -3
  36. package/dist/generator/render/import/importRuntimeSetup.d.ts +0 -1
  37. package/dist/generator/render/init/initMakooRuntime.d.ts +0 -4
  38. package/dist/generator/render/init/registerComp.d.ts +0 -3
  39. package/dist/generator/render/init/registerListener.d.ts +0 -4
  40. package/dist/generator/render/run/renderMakooStart.d.ts +0 -1
  41. package/dist/generator/render/util/match.d.ts +0 -1
  42. package/dist/generator/render/util/value.d.ts +0 -1
  43. package/dist/generator/types.d.ts +0 -27
  44. package/dist/manifest/index.cjs +0 -1
  45. package/dist/manifest/index.d.ts +0 -2
  46. package/dist/manifest/index.js +0 -7
  47. package/dist/scanner/collectDependenics.d.ts +0 -2
  48. package/dist/scanner/load/loadManifes.d.ts +0 -3
  49. package/dist/scanner/load/loadMeta.d.ts +0 -2
  50. package/dist/scanner/manifestBinding.d.ts +0 -14
  51. package/dist/scanner/scanner.d.ts +0 -3
  52. package/dist/scanner/types.d.ts +0 -50
  53. package/dist/scanner/util.d.ts +0 -2
  54. package/dist/scanner/validation.d.ts +0 -190
  55. package/dist/vitePlugin/hmrController.d.ts +0 -6
  56. package/dist/vitePlugin/makoo.d.ts +0 -3
  57. package/dist/vitePlugin/makooMonkey.d.ts +0 -3
  58. package/dist/vitePlugin/types.d.ts +0 -18
  59. package/dist/vitePlugin/virtualModule.d.ts +0 -2
  60. package/dist/vitePlugin/watchList.d.ts +0 -5
  61. /package/dist/{command → cli}/bin.d.ts +0 -0
package/README.CN.md CHANGED
@@ -1,412 +1,73 @@
1
- # CLI API
1
+ # @makoojs/cli
2
2
 
3
- ## API 索引
3
+ <p>
4
+ <a href="https://www.npmjs.com/package/@makoojs/cli"><img alt="npm" src="https://img.shields.io/npm/v/@makoojs/cli?style=flat-square"></a>
5
+ <a href="https://github.com/makoojs/Makoo/blob/main/LICENSE"><img alt="License" src="https://img.shields.io/badge/License-MIT-yellow?style=flat-square"></a>
6
+ </p>
4
7
 
5
- ### `@makoojs/cli`
8
+ <p>
9
+ <a href="https://makoojs.github.io/Makoo/zh/api/vite">API 参考</a> •
10
+ <a href="https://github.com/makoojs/Makoo">Makoo</a> •
11
+ <a href="https://github.com/makoojs/Makoo/blob/main/packages/cli/README.md">English</a>
12
+ </p>
6
13
 
7
- - [`makoo()`](#makoo):创建 Makoo 与 userscript Vite plugins
8
- - [`cdn`](#cdn):生成外部依赖的 CDN 配置
9
- - [配置类型](#配置类型):`MakooOptions`、`CliConfig` 和 monkey 配置
14
+ Makoo 的工具链包。它提供 `makoo()` 与 `makooDev()` 两个 Vite 插件、`makoo dev` / `build` / `preview` 命令,以及在脚本中调用 GM API 的 `@makoojs/cli/monkey`。userscript 的元信息、开发服务与构建由 [vite-plugin-monkey](https://github.com/lisonge/vite-plugin-monkey) 完成。
10
15
 
11
- ### `@makoojs/cli/manifest`
16
+ ## 安装
12
17
 
13
- - [`defineInjections()`](#defineinjections):声明顶层 manifest
14
- - [`defineInjection()`](#defineinjection):声明模块 manifest
15
- - [Manifest 类型](#manifest-类型)
16
-
17
- ### `@makoojs/cli/monkey`
18
-
19
- - [GM API](#gm-api):userscript API 封装
20
- - [GM 类型](#gm-类型)
21
-
22
- ### 命令行
23
-
24
- - [CLI 命令](#cli-命令)
25
-
26
- ## `makoo()`
27
-
28
- 根据 Makoo 配置创建 Vite plugins。
29
-
30
- ### Type
31
-
32
- ```ts
33
- function makoo(options: MakooOptions): Plugin[];
34
- ```
35
-
36
- ### Parameters
37
-
38
- `options` 使用 `MakooOptions`,其中 `app` 为必填字段。
39
-
40
- ### Returns
41
-
42
- 返回一个 `Plugin[]`。在 Vite 配置中需要使用展开语法。
43
-
44
- ### Example
45
-
46
- ```ts
47
- export default defineConfig({
48
- plugins: [
49
- ...makoo({
50
- app: {
51
- name: 'my-script',
52
- version: '0.0.1'
53
- },
54
- monkey: {
55
- userscript: {
56
- match: ['https://example.com/*']
57
- }
58
- }
59
- })
60
- ]
61
- });
62
- ```
63
-
64
- ## `cdn`
65
-
66
- 从 `vite-plugin-monkey` 重新导出的 CDN 配置生成器。
67
-
68
- ### Type
69
-
70
- ```ts
71
- type CdnFactory = (
72
- exportVarName?: string,
73
- pathname?: string
74
- ) => [string, ModuleToUrlFc];
75
-
76
- const cdn: {
77
- jsdelivr: CdnFactory;
78
- jsdelivrFastly: CdnFactory;
79
- unpkg: CdnFactory;
80
- cdnjs: CdnFactory;
81
- zhimg: CdnFactory;
82
- elemecdn: CdnFactory;
83
- bdstatic: CdnFactory;
84
- npmmirror: CdnFactory;
85
- bootcdn: CdnFactory;
86
- staticfile: CdnFactory;
87
- };
18
+ ```sh
19
+ pnpm add -D @makoojs/cli vite
88
20
  ```
89
21
 
90
- `bootcdn` 和 `staticfile` 由 `vite-plugin-monkey` 标记为 deprecated。
22
+ > [!TIP]
23
+ > 用 `pnpm dlx @makoojs/create-makoo` 创建的项目已经配置好了它,不需要单独安装。
91
24
 
92
- ## 配置类型
25
+ ## 用法
93
26
 
94
- ### `MakooOptions`
27
+ ### 配置 Vite
95
28
 
96
29
  ```ts
97
- type MakooOptions = CliConfig & {
98
- root?: string;
99
- };
100
- ```
101
-
102
- ### `CliConfig`
103
-
104
- ```ts
105
- type CliConfig = {
106
- app: AppConfig;
107
- monkey?: MonkeyConfig;
108
- source?: SourceConfig;
109
- runtime?: {
110
- setup?: string | string[];
111
- };
112
- };
113
- ```
114
-
115
- ### `AppConfig`
116
-
117
- ```ts
118
- type AppConfig = {
119
- name: string;
120
- version: string;
121
- description?: string;
122
- };
123
- ```
124
-
125
- ### `SourceConfig`
126
-
127
- ```ts
128
- type SourceConfig = {
129
- include?: string[];
130
- exclude?: string[];
131
- };
132
- ```
133
-
134
- `include` 和 `exclude` 匹配 injection 模块目录名。
135
-
136
- ### `MonkeyConfig`
137
-
138
- ```ts
139
- type MonkeyConfig = {
140
- userscript?: MonkeyUserScript;
141
- align?: number | false;
142
- generate?: (options: {
143
- userscript: string;
144
- mode: 'serve' | 'build' | 'meta';
145
- }) => string | Promise<string>;
146
- styleImport?: boolean;
147
- server?: MonkeyServerConfig;
148
- build?: MonkeyBuildConfig;
149
- };
150
- ```
151
-
152
- ### `MonkeyServerConfig`
153
-
154
- ```ts
155
- type MonkeyServerConfig = {
156
- open?: boolean;
157
- prefix?: string | ((name: string) => string) | false;
158
- };
159
- ```
160
-
161
- ### `MonkeyBuildConfig`
162
-
163
- ```ts
164
- type MonkeyBuildConfig = {
165
- fileName?: string;
166
- metaFileName?: string | boolean | ((fileName: string) => string);
167
- externalGlobals?: ExternalGlobals;
168
- autoGrant?: boolean;
169
- externalResource?: ExternalResource;
170
- systemjs?: 'inline' | ((
171
- version: string,
172
- packageName: string,
173
- importName?: string,
174
- resolveName?: string
175
- ) => string);
176
- cssSideEffects?: string | ((css: string) => void);
177
- };
178
- ```
179
-
180
- ## `defineInjections()`
181
-
182
- 声明顶层 `InjectionManifest`,并保留传入对象的具体类型。
183
-
184
- ### Type
30
+ // vite.config.ts
31
+ import { defineConfig } from 'vite';
32
+ import vue from '@vitejs/plugin-vue';
33
+ import { makoo, makooDev } from '@makoojs/cli';
185
34
 
186
- ```ts
187
- function defineInjections<T extends InjectionManifest>(
188
- manifest: StrictShape<InjectionManifest, T>
189
- ): T;
190
- ```
191
-
192
- ### Returns
193
-
194
- 原样返回 `manifest`。
195
-
196
- ### Example
197
-
198
- ```ts
199
- export default defineInjections({
200
- injectionDefaults: {
201
- alive: true
202
- },
203
- injections: {
204
- panel: {
205
- injectAt: 'body',
206
- component: './panel/App.vue'
207
- }
208
- },
209
- listeners: {
210
- escape: {
211
- listenAt: 'body',
212
- type: 'keydown',
213
- capture: true,
214
- callback: onEscape
215
- }
216
- }
217
- });
218
- ```
219
-
220
- ## `defineInjection()`
221
-
222
- 声明单个 `InjectionModuleConfig`,并保留传入对象的具体类型。
223
-
224
- ### Type
225
-
226
- ```ts
227
- function defineInjection<T extends InjectionModuleConfig>(
228
- config: StrictShape<InjectionModuleConfig, T>
229
- ): T;
230
- ```
231
-
232
- ### Returns
233
-
234
- 原样返回 `config`。
235
-
236
- ### Example
237
-
238
- ```ts
239
- export default defineInjection({
240
- name: 'panel',
241
- injectAt: 'body',
242
- component: './App.vue',
243
- hooks: {
244
- 'artifact:mountSuccess': onMounted
245
- },
246
- on: {
247
- listenAt: 'body',
248
- type: 'click',
249
- capture: true,
250
- callback: onClick
251
- }
35
+ export default defineConfig({
36
+ plugins: [
37
+ vue(),
38
+ makoo({
39
+ entry: './src/main.ts',
40
+ app: { name: 'my-script', version: '0.0.1' },
41
+ monkey: { userscript: { match: ['https://example.com/*'] } }
42
+ }),
43
+ // 可选:在开发终端查看注入状态
44
+ makooDev()
45
+ ]
252
46
  });
253
47
  ```
254
48
 
255
- ## Manifest 类型
256
-
257
- ### `InjectionManifest`
258
-
259
- ```ts
260
- type InjectionManifest = {
261
- injectionDefaults?: InjectionDefaults;
262
- injections?: InjectionModuleConfig[] | Record<
263
- string,
264
- Omit<InjectionModuleConfig, 'name'>
265
- >;
266
- listeners?: InjectionListenerConfig[] | Record<
267
- string,
268
- Omit<InjectionListenerConfig, 'name'>
269
- >;
270
- };
271
- ```
272
-
273
- ### `InjectionDefaults`
274
-
275
- ```ts
276
- type InjectionDefaults = {
277
- alive?: boolean;
278
- scope?: 'local' | 'global';
279
- timeout?: number;
280
- hooks?: LifecycleHookMap;
281
- };
282
- ```
283
-
284
- ### `InjectionModuleConfig`
285
-
286
- ```ts
287
- type InjectionModuleConfig = {
288
- name?: string;
289
- injectAt: string;
290
- component: string;
291
- framework?: InjectionFramework;
292
- enabled?: boolean;
293
- match?: InjectionMatchConfig;
294
- alive?: boolean;
295
- scope?: 'local' | 'global';
296
- timeout?: number;
297
- hooks?: LifecycleHookMap;
298
- on?: InjectionModuleListenerConfig;
299
- };
300
- ```
301
-
302
- ### `InjectionListenerConfig`
49
+ ### 开发与构建
303
50
 
304
- ```ts
305
- type InjectionModuleListenerConfig = {
306
- listenAt: string;
307
- type: string;
308
- callback: EventListener;
309
- capture?: boolean;
310
- activitySignal?: () => ActivitySignalSource<boolean>;
311
- };
312
-
313
- type InjectionListenerConfig = InjectionModuleListenerConfig & {
314
- name?: string;
315
- enabled?: boolean;
316
- match?: InjectionMatchConfig;
317
- };
51
+ ```sh
52
+ makoo dev # 启动开发服务,从打印的地址安装开发脚本
53
+ makoo build # 构建出 .user.js
54
+ makoo preview # 预览构建产物
318
55
  ```
319
56
 
320
- 设置 `capture: true` 后,listener 会在 DOM 捕获阶段处理事件。该字段默认为 `false`,
321
- 未配置时使用冒泡阶段。事件 listener 的 signal 仍由 Makoo 内部管理。
57
+ ### 调用 GM API
322
58
 
323
- ### `InjectionFramework`
59
+ 在浏览器代码中按能力导入,构建时会只为用到的 API 生成 `@grant`:
324
60
 
325
61
  ```ts
326
- type InjectionFramework = 'auto' | 'Vue' | 'React';
327
- ```
328
-
329
- ### `InjectionMatchConfig`
62
+ import { gmStorage } from '@makoojs/cli/monkey';
330
63
 
331
- ```ts
332
- type InjectionMatchConfig =
333
- | string[]
334
- | {
335
- include?: string[];
336
- exclude?: string[];
337
- };
64
+ const runs = gmStorage.get<number>('runs', 0) + 1;
65
+ gmStorage.set('runs', runs);
338
66
  ```
339
67
 
340
- ## GM API
341
-
342
- 以下值从 `@makoojs/cli/monkey` 导出。
343
-
344
- | 导出 | 方法或值 |
345
- | --- | --- |
346
- | `GMapi` | 包含 `raw`、`info`、`log`、`storage`、`style`、`request`、`menu`、`clipboard`、`notification`、`tab`、`download` 和 `resource` |
347
- | `gm` | 原始 `GM` 对象 |
348
- | `gmInfo` | `GM_info` |
349
- | `gmLog` | `GM_log` |
350
- | `monkeyWindow` | userscript window |
351
- | `unsafeWindow` | 页面 window |
352
- | `gmClipboard` | `set(data, type?, callback?)` |
353
- | `gmDownload` | `start` |
354
- | `gmMenu` | `register`、`unregister` |
355
- | `gmNotification` | `show` |
356
- | `gmRequest` | `send`、`get`、`post` |
357
- | `gmResource` | `text`、`url` |
358
- | `gmStorage` | `get`、`getMany`、`set`、`setMany`、`remove`、`removeMany`、`keys`、`watch`、`unwatch` |
359
- | `gmStyle` | `add`、`element` |
360
- | `gmTab` | `open`、`get`、`getAll`、`save` |
361
-
362
- ### `gmRequest.get()` / `gmRequest.post()`
363
-
364
- ```ts
365
- gmRequest.get<R extends GmResponseType = 'text', C = unknown>(
366
- url: string,
367
- options?: GmRequestOptions<R, C>
368
- ): GmAbortHandle;
369
-
370
- gmRequest.post<R extends GmResponseType = 'text', C = unknown>(
371
- url: string,
372
- options?: GmRequestOptions<R, C>
373
- ): GmAbortHandle;
374
- ```
375
-
376
- `get()` 和 `post()` 分别固定请求方法为 `GET` 和 `POST`。
377
-
378
- ## GM 类型
379
-
380
- ```ts
381
- type GmRequestOptions<R extends GmResponseType = 'text', C = unknown> = Omit<
382
- GmXmlhttpRequestOption<R, C>,
383
- 'url' | 'method'
384
- >;
385
- ```
386
-
387
- `@makoojs/cli/monkey` 还重新导出以下 `vite-plugin-monkey` 类型:
388
-
389
- - `GmAbortHandle`
390
- - `GmAddElementAttributes`
391
- - `GmDownloadOptions`
392
- - `GmInfoType`
393
- - `GmMenuCommandOptions`
394
- - `GmNotificationOptions`
395
- - `GmOpenInTabOptions`
396
- - `GmResponseEvent`
397
- - `GmResponseType`
398
- - `GmTabControl`
399
- - `GmType`
400
- - `GmValueListenerId`
401
- - `GmXmlhttpRequestOption`
402
- - `MonkeyWindow`
403
-
404
- ## CLI 命令
68
+ ## 文档
405
69
 
406
- | 命令 | 说明 |
407
- | --- | --- |
408
- | `makoo dev` | 启动 Vite 开发服务器 |
409
- | `makoo build` | 执行 Vite 构建 |
410
- | `makoo add <name>` | 创建 React injection 模块并更新 manifest |
411
- | `makoo add <name> --framework Vue` | 创建 Vue injection 模块并更新 manifest |
412
- | `makoo inspect` | 输出解析后的配置、扫描结果和 framework 信息 |
70
+ - [Vite 插件](https://makoojs.github.io/Makoo/zh/api/vite):`makoo()` 的全部配置、默认值与 `makooDev()`。
71
+ - [CLI 命令](https://makoojs.github.io/Makoo/zh/api/cli):各命令的参数。
72
+ - [Userscript API](https://makoojs.github.io/Makoo/zh/api/monkey):`@makoojs/cli/monkey` 导出的 GM API。
73
+ - [项目配置](https://makoojs.github.io/Makoo/zh/docs/configuration):匹配网址、权限与外部依赖的配置方法。