UNPKG

13.1 kBTypeScriptView Raw
1import 'cheerio';
2import IConfig, { IPlugin, IAFWebpackConfig, IRoute } from './config';
3import * as IUi from './ui';
4import { DefaultMethods } from 'signale';
5import * as lodash from 'lodash';
6import * as IWebpack from 'webpack';
7import * as IWebpackChainConfig from 'webpack-chain';
8
9/**
10 * System level variable
11 * https://umijs.org/plugin/develop.html#system-level-variable
12 */
13declare enum API_TYPE {
14 ADD,
15 MODIFY,
16 EVENT,
17}
18
19// for multiStats, multiply webpack configs
20export interface MultiStats {
21 stats: IWebpack.Stats[];
22 hash: string;
23}
24// for ui plugin developer
25type IUiApi = IUi.IApi;
26
27export { IConfig, IPlugin, IRoute, IWebpackChainConfig, IWebpack, IUi, IUiApi };
28
29/**
30 * System level API
31 * https://umijs.org/plugin/develop.html#system-level-api
32 */
33export interface IRegisterPluginOpts {
34 id: string;
35 apply: any;
36 opts?: object;
37}
38
39interface IRegisterPlugin {
40 (plugin: IRegisterPluginOpts): void;
41}
42
43interface IRegister {
44 (hook: string, handler: Function): void;
45}
46
47export interface IPluginMethodOpts {
48 /**
49 * @param args: Come from `applyPlugins(, { args: YOUR_ARGS })`
50 */
51 memo?: any;
52 args?: any;
53}
54
55export interface IPluginMethod {
56 /**
57 * @param opts: Includes args passed in from `applyPlugins` and memo
58 * @param args: Arguments passed in from other plug-ins when they call this method
59 */
60 (opts: IPluginMethodOpts, ...args: any[]): any;
61}
62
63export interface IRegisterMethodOpts {
64 /**
65 * Choose one of `type` and `apply`.
66 * View more at https://umijs.org/plugin/develop.html#registermethod
67 */
68 type?: API_TYPE;
69 apply?: IPluginMethod;
70}
71
72interface IRegisterMethod {
73 (methodName: string, opts: IRegisterMethodOpts): void;
74}
75
76export interface IApplyPluginsOpts {
77 args?: any;
78 initialValue?: any;
79}
80
81interface IApplyPlugins {
82 (methodName: string, opts?: IApplyPluginsOpts): any[] | undefined | any;
83}
84
85interface IReDo<T> {
86 (message?: T): void;
87}
88
89interface IChangePluginOption {
90 (pluginId: string, opts: any): void;
91}
92
93export interface ICommandOpts {
94 /**
95 * @param description: Description displayed when running `umi help`
96 * @param details: Details displayed when running `umi help [YOUR_COMMAND]`
97 * @param hide: Hide your command in `umi help`
98 * @param options: Options displayed when running `umi help [YOUR_COMMAND]`
99 * @param usage: Usage displayed when running `umi help [YOUR_COMMAND]`
100 * @param webpack: Whether to initialize webpack config
101 */
102 description?: string;
103 details?: string;
104 hide?: boolean;
105 options?: object;
106 usage?: string;
107 webpack?: boolean;
108}
109
110interface IRegisterCommand {
111 (commandName: string, opts: ICommandOpts, fn: (args: any) => any): void;
112 (commandName: string, fn: (args: any) => any): void;
113}
114
115export interface IRegisterConfigOpts<T = any> {
116 /**
117 * @param name: Name of your configuration
118 * @param validate: Verify that the value of configuration is valid
119 * @param onChange: Callback when the value of configuration changes
120 */
121 name: string;
122 validate?: (value: T) => void;
123 onChange?: (newConfig: IConfig, oldConfig: IConfig) => void;
124}
125
126export interface IRegisterConfigFunc {
127 (api: IApi): IRegisterConfigOpts;
128}
129
130interface IRegisterConfig {
131 (fn: IRegisterConfigFunc): void;
132}
133
134export interface IModifyCommandFuncOpts {
135 name: string;
136 args?: any;
137}
138
139export interface IModifyCommandFunc {
140 (opts: IModifyCommandFuncOpts): IModifyCommandFuncOpts;
141}
142
143interface IModifyCommand {
144 (fn: IModifyCommandFunc): void;
145}
146
147export interface IModifyHelpInfoOpts {
148 scriptName: string;
149 commands: {
150 [commandName: string]: {
151 opts: {
152 hide: boolean;
153 options: {
154 [key: string]: string;
155 };
156 };
157 };
158 };
159}
160
161/**
162 * Tool class API
163 * https://umijs.org/plugin/develop.html#tool-class-api
164 */
165interface ILog<T = string> {
166 (message: T, ...messages: string[]): void;
167}
168
169interface IWriteTmpFile {
170 (file: string, content: string): void;
171}
172
173interface IGetRoutes {
174 (): IRoute[];
175}
176
177interface IGetRouteComponents {
178 (): string[];
179}
180
181interface IWinPath {
182 (path: string): string;
183}
184
185type IRelativeToTmp = (path: string) => string;
186
187interface IFind {
188 (baseDir: string, fileNameWithoutExtname?: string): string | null;
189}
190
191interface ICompatDirname<T = any> {
192 (path: string, cwd: string, fallback?: T): T | string;
193}
194
195/**
196 * Event class API
197 * https://umijs.org/plugin/develop.html#event-class-api
198 */
199export interface IBeforeDevServerFunc {
200 (args: { server: any }): void;
201}
202
203export interface IAfterDevServerFunc {
204 (args: { server: any; devServerPort: number }): void;
205}
206
207export interface IBeforeBlockWritingFunc {
208 (args: { service: any; sourcePath: string; blockPath: string }): void;
209}
210
211interface IBeforeDevServer {
212 (fn: IBeforeDevServerFunc): void;
213}
214
215interface IBeforeDevServerAsync {
216 (fn: IBeforeDevServerFunc): Promise<any>;
217}
218
219interface IAfterDevServer {
220 (fn: IAfterDevServerFunc): void;
221}
222
223interface IBeforeBlockWriting {
224 (fn: IBeforeBlockWritingFunc): void;
225}
226
227interface IOnStart {
228 (fn: () => void): void;
229}
230
231interface onPrintUmiError {
232 (args: { error: any; opts: object }): void;
233}
234
235interface IEventAsync {
236 (fn: () => void): Promise<any>;
237}
238
239export interface IOnDevCompileDoneFunc {
240 (args: { isFirstCompile: boolean; stats: IWebpack.Stats }): void;
241}
242
243interface IOnDevCompileDone {
244 (fn: IOnDevCompileDoneFunc): void;
245}
246
247export interface IOnOptionChangeFunc<T = any> {
248 (newOpts: T): void;
249}
250
251interface IOnOptionChange {
252 (fn: IOnOptionChangeFunc): void;
253}
254
255export interface IOnBuildSuccessFunc {
256 (args: { stats: IWebpack.Stats[] }): void;
257}
258
259interface IOnBuildSuccess {
260 (fn: IOnBuildSuccessFunc): void;
261}
262
263interface IOnBuildSuccessAsync {
264 (fn: IOnBuildSuccessFunc): Promise<any>;
265}
266
267export interface IOnBuildFailFunc {
268 (args: { stats: IWebpack.Stats[]; err: Error }): void;
269}
270
271interface IOnBuildFail {
272 (fn: IOnBuildFailFunc): void;
273}
274
275interface IOnHTMLRebuild {
276 (fn: () => void): void;
277}
278
279export interface IOnGenerateFilesFunc {
280 (args: { isRebuild?: boolean }): void;
281}
282
283interface IOnGenerateFiles {
284 (fn: IOnGenerateFilesFunc): void;
285}
286
287export interface IOnPatchRouteFunc {
288 (args: { route: IRoute }): void;
289}
290
291interface IOnPatchRoute {
292 (fn: IOnPatchRouteFunc): void;
293}
294
295interface IAction<T = object> {
296 type: string;
297 payload?: T;
298 lang?: IUi.ILang;
299}
300
301export type ISend = (action: IAction<{}>) => void;
302export type ISuccess<T = object> = (payload: T) => void;
303export type IFailure<T = object> = (payload: T) => void;
304type IUiLogType = 'error' | 'info';
305export type IUiLog = (type: IUiLogType, payload: string) => void;
306
307export interface IOnUISocketFunc {
308 (args: {
309 action: IAction;
310 send: ISend;
311 success: ISuccess<{}>;
312 failure: IFailure<{}>;
313 log: IUiLog;
314 }): void;
315}
316
317export interface IOnUISocket {
318 (fn: IOnUISocketFunc): void;
319}
320
321export interface IChangeWebpackConfigFunc<T, U> {
322 (webpackConfig: T, AFWebpack: { webpack: U }): T | void;
323}
324
325/**
326 * Application class API
327 * https://umijs.org/plugin/develop.html#application-class-api
328 */
329export interface IChangeWebpackConfig<T, U> {
330 (fn: IChangeWebpackConfigFunc<T, U>): void;
331}
332
333export interface IModifyFunc<T, U> {
334 /**
335 * https://umijs.org/plugin/develop.html#registermethod
336 */
337 (memo: T, args: U): T | T;
338}
339
340export interface IModify<T, U = {}> {
341 (fn: IModifyFunc<T, U> | T): void;
342}
343
344export interface IAddFunc<T, U> {
345 /**
346 * https://umijs.org/plugin/develop.html#registermethod
347 */
348 (memo: T[], args: U): T | T[];
349}
350
351export interface IAdd<T, U = {}> {
352 (fn: IAddFunc<T, U> | T | T[]): void;
353}
354
355interface IGetChunkPath {
356 (fileName: string): string | null;
357}
358
359interface IModifyHTMLWithASTArgs {
360 route: IRoute;
361 getChunkPath: IGetChunkPath;
362}
363
364export interface IModifyHTMLWithASTFunc {
365 ($: CheerioStatic, args: IModifyHTMLWithASTArgs): CheerioStatic;
366}
367
368interface IModifyHTMLWithAST {
369 (fn: IModifyHTMLWithASTFunc): void;
370}
371
372export interface IAddImportOpts {
373 /**
374 * @param source: Path to module
375 * @param specifier: Module name with import, can be ignored
376 */
377 source: string;
378 specifier?: string;
379}
380
381interface IModifyRouteComponentArgs {
382 importPath: string;
383 webpackChunkName: string;
384 component: string;
385}
386
387export interface IPkg {
388 name?: string;
389 version?: string;
390 dependencies?: {
391 [prop: string]: string;
392 };
393 devDependencies?: {
394 [prop: string]: string;
395 };
396}
397
398interface IModifyBlockFileArgs {
399 targetPath: string;
400}
401
402interface IModifyBlockTargetArgs {
403 sourceName: string;
404}
405
406export interface IBlockDependencies {
407 conflicts: [string, string, string][];
408 lacks: [string, string][];
409 devConflicts: [string, string, string][];
410 devLacks: [string, string][];
411}
412
413export interface IMiddlewareFunction {
414 (req: any, res: any, next: any): void;
415}
416
417export interface IApi {
418 /**
419 * System level variable
420 * https://umijs.org/plugin/develop.html#system-level-variable
421 */
422 API_TYPE: typeof API_TYPE;
423 config: IConfig;
424 cwd: string;
425 pkg: IPkg;
426 webpackConfig: IWebpack.Configuration;
427 service: any;
428 locale: any;
429 paths: {
430 cwd: string;
431 outputPath: string;
432 absOutputPath: string;
433 absNodeModulesPath: string;
434 pagesPath: string;
435 absPagesPath: string;
436 absSrcPath: string;
437 tmpDirPath: string;
438 absTmpDirPath: string;
439 };
440 routes: IRoute[];
441
442 /**
443 * System level API
444 * https://umijs.org/plugin/develop.html#system-level-api
445 */
446 register: IRegister;
447 registerPlugin: IRegisterPlugin;
448 registerMethod: IRegisterMethod;
449 applyPlugins: IApplyPlugins;
450 restart: IReDo<string>;
451 rebuildTmpFiles: IReDo<string>;
452 refreshBrowser: IReDo<void>;
453 rebuildHTML: IReDo<void>;
454 changePluginOption: IChangePluginOption;
455 registerCommand: IRegisterCommand;
456 _registerConfig: IRegisterConfig;
457 _modifyCommand: IModifyCommand;
458 _modifyHelpInfo: IModify<IModifyHelpInfoOpts>;
459
460 /**
461 * Tool class API
462 * https://umijs.org/plugin/develop.html#tool-class-api
463 */
464 log: { [key in DefaultMethods]: ILog<any> };
465 _: typeof lodash;
466 winPath: IWinPath;
467 relativeToTmp: IRelativeToTmp;
468 debug: ILog;
469 writeTmpFile: IWriteTmpFile;
470 getRoutes: IGetRoutes;
471 getRouteComponents: IGetRouteComponents;
472 findJS: IFind;
473 findCSS: IFind;
474 compatDirname: ICompatDirname;
475 UmiError: any;
476
477 /**
478 * Event class API
479 * https://umijs.org/plugin/develop.html#event-class-api
480 */
481 beforeDevServer: IBeforeDevServer;
482 _beforeDevServerAsync: IBeforeDevServerAsync;
483 afterDevServer: IAfterDevServer;
484 beforeBlockWriting: IBeforeBlockWriting;
485 onStart: IOnStart;
486 onPrintUmiError: onPrintUmiError;
487 onStartAsync: IEventAsync;
488 onDevCompileDone: IOnDevCompileDone;
489 onOptionChange: IOnOptionChange;
490 onBuildSuccess: IOnBuildSuccess;
491 onBuildSuccessAsync: IOnBuildSuccessAsync;
492 onBuildFail: IOnBuildFail;
493 onHTMLRebuild: IOnHTMLRebuild;
494 onGenerateFiles: IOnGenerateFiles;
495 onPatchRoute: IOnPatchRoute;
496 onUISocket: IOnUISocket;
497 onRouteChange: (callback: () => void) => void;
498
499 /**
500 * Application class API
501 * https://umijs.org/plugin/develop.html#application-class-api
502 */
503 modifyDefaultConfig: IModify<object>;
504 addUmiExports: IAdd<object>;
505 addPageWatcher: IAdd<string>;
506 addHTMLMeta: IAdd<object, { route?: IRoute }>;
507 addHTMLLink: IAdd<object, { route?: IRoute }>;
508 addHTMLStyle: IAdd<object, { route?: IRoute }>;
509 addHTMLScript: IAdd<object, { route?: IRoute }>;
510 addHTMLHeadScript: IAdd<object, { route?: IRoute }>;
511 modifyHTMLChunks: IModify<string[], { route?: IRoute }>;
512 modifyHTMLWithAST: IModifyHTMLWithAST;
513 modifyHTMLContext: IModify<object, { route?: IRoute }>;
514 modifyPublicPathStr: IModify<string>;
515 modifyRoutes: IModify<IRoute[]>;
516 addEntryImportAhead: IAdd<IAddImportOpts>;
517 addEntryPolyfillImports: IAdd<IAddImportOpts>;
518 addEntryImport: IAdd<IAddImportOpts>;
519 addEntryCodeAhead: IAdd<string>;
520 addEntryCode: IAdd<string>;
521 addUIPlugin: IAdd<string>;
522 addRouterImport: IAdd<IAddImportOpts>;
523 addRouterImportAhead: IAdd<IAddImportOpts>;
524 addRendererWrapperWithComponent: IAdd<string, () => string>;
525 addRendererWrapperWithModule: IAdd<string>;
526 modifyEntryRender: IModify<string>;
527 modifyEntryHistory: IModify<string>;
528 modifyRouteComponent: IModify<string, IModifyRouteComponentArgs>;
529 modifyRouterRootComponent: IModify<string>;
530 modifyWebpackConfig: IModify<IWebpack.Configuration>;
531 modifyAFWebpackOpts: IModify<IAFWebpackConfig>;
532 chainWebpackConfig: IChangeWebpackConfig<IWebpackChainConfig, IAFWebpackConfig>;
533 addMiddleware: IAdd<IMiddlewareFunction>;
534 addMiddlewareAhead: IAdd<IMiddlewareFunction>;
535 addMiddlewareBeforeMock: IAdd<IMiddlewareFunction>;
536 addMiddlewareAfterMock: IAdd<IMiddlewareFunction>;
537 addVersionInfo: IAdd<string>;
538 addRuntimePlugin: IAdd<string>;
539 addRuntimePluginKey: IAdd<string>;
540 addBlockUIResource: IAdd<object>;
541 modifyBlockUIResources: IModify<object[]>;
542 _modifyBlockPackageJSONPath: IModify<string>;
543 _modifyBlockDependencies: IModify<IBlockDependencies>;
544 _modifyBlockFile: IModify<string, IModifyBlockFileArgs>;
545 _modifyBlockTarget: IModify<string, IModifyBlockTargetArgs>;
546 _modifyBlockNewRouteConfig: IModify<any>;
547}