1 | import 'cheerio';
|
2 | import IConfig, { IPlugin, IAFWebpackConfig, IRoute } from './config';
|
3 | import * as IUi from './ui';
|
4 | import { DefaultMethods } from 'signale';
|
5 | import * as lodash from 'lodash';
|
6 | import * as IWebpack from 'webpack';
|
7 | import * as IWebpackChainConfig from 'webpack-chain';
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | declare enum API_TYPE {
|
14 | ADD,
|
15 | MODIFY,
|
16 | EVENT,
|
17 | }
|
18 |
|
19 |
|
20 | export interface MultiStats {
|
21 | stats: IWebpack.Stats[];
|
22 | hash: string;
|
23 | }
|
24 |
|
25 | type IUiApi = IUi.IApi;
|
26 |
|
27 | export { IConfig, IPlugin, IRoute, IWebpackChainConfig, IWebpack, IUi, IUiApi };
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 | export interface IRegisterPluginOpts {
|
34 | id: string;
|
35 | apply: any;
|
36 | opts?: object;
|
37 | }
|
38 |
|
39 | interface IRegisterPlugin {
|
40 | (plugin: IRegisterPluginOpts): void;
|
41 | }
|
42 |
|
43 | interface IRegister {
|
44 | (hook: string, handler: Function): void;
|
45 | }
|
46 |
|
47 | export interface IPluginMethodOpts {
|
48 | |
49 |
|
50 |
|
51 | memo?: any;
|
52 | args?: any;
|
53 | }
|
54 |
|
55 | export interface IPluginMethod {
|
56 | |
57 |
|
58 |
|
59 |
|
60 | (opts: IPluginMethodOpts, ...args: any[]): any;
|
61 | }
|
62 |
|
63 | export interface IRegisterMethodOpts {
|
64 | |
65 |
|
66 |
|
67 |
|
68 | type?: API_TYPE;
|
69 | apply?: IPluginMethod;
|
70 | }
|
71 |
|
72 | interface IRegisterMethod {
|
73 | (methodName: string, opts: IRegisterMethodOpts): void;
|
74 | }
|
75 |
|
76 | export interface IApplyPluginsOpts {
|
77 | args?: any;
|
78 | initialValue?: any;
|
79 | }
|
80 |
|
81 | interface IApplyPlugins {
|
82 | (methodName: string, opts?: IApplyPluginsOpts): any[] | undefined | any;
|
83 | }
|
84 |
|
85 | interface IReDo<T> {
|
86 | (message?: T): void;
|
87 | }
|
88 |
|
89 | interface IChangePluginOption {
|
90 | (pluginId: string, opts: any): void;
|
91 | }
|
92 |
|
93 | export interface ICommandOpts {
|
94 | |
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 |
|
102 | description?: string;
|
103 | details?: string;
|
104 | hide?: boolean;
|
105 | options?: object;
|
106 | usage?: string;
|
107 | webpack?: boolean;
|
108 | }
|
109 |
|
110 | interface IRegisterCommand {
|
111 | (commandName: string, opts: ICommandOpts, fn: (args: any) => any): void;
|
112 | (commandName: string, fn: (args: any) => any): void;
|
113 | }
|
114 |
|
115 | export interface IRegisterConfigOpts<T = any> {
|
116 | |
117 |
|
118 |
|
119 |
|
120 |
|
121 | name: string;
|
122 | validate?: (value: T) => void;
|
123 | onChange?: (newConfig: IConfig, oldConfig: IConfig) => void;
|
124 | }
|
125 |
|
126 | export interface IRegisterConfigFunc {
|
127 | (api: IApi): IRegisterConfigOpts;
|
128 | }
|
129 |
|
130 | interface IRegisterConfig {
|
131 | (fn: IRegisterConfigFunc): void;
|
132 | }
|
133 |
|
134 | export interface IModifyCommandFuncOpts {
|
135 | name: string;
|
136 | args?: any;
|
137 | }
|
138 |
|
139 | export interface IModifyCommandFunc {
|
140 | (opts: IModifyCommandFuncOpts): IModifyCommandFuncOpts;
|
141 | }
|
142 |
|
143 | interface IModifyCommand {
|
144 | (fn: IModifyCommandFunc): void;
|
145 | }
|
146 |
|
147 | export 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 |
|
163 |
|
164 |
|
165 | interface ILog<T = string> {
|
166 | (message: T, ...messages: string[]): void;
|
167 | }
|
168 |
|
169 | interface IWriteTmpFile {
|
170 | (file: string, content: string): void;
|
171 | }
|
172 |
|
173 | interface IGetRoutes {
|
174 | (): IRoute[];
|
175 | }
|
176 |
|
177 | interface IGetRouteComponents {
|
178 | (): string[];
|
179 | }
|
180 |
|
181 | interface IWinPath {
|
182 | (path: string): string;
|
183 | }
|
184 |
|
185 | type IRelativeToTmp = (path: string) => string;
|
186 |
|
187 | interface IFind {
|
188 | (baseDir: string, fileNameWithoutExtname?: string): string | null;
|
189 | }
|
190 |
|
191 | interface ICompatDirname<T = any> {
|
192 | (path: string, cwd: string, fallback?: T): T | string;
|
193 | }
|
194 |
|
195 |
|
196 |
|
197 |
|
198 |
|
199 | export interface IBeforeDevServerFunc {
|
200 | (args: { server: any }): void;
|
201 | }
|
202 |
|
203 | export interface IAfterDevServerFunc {
|
204 | (args: { server: any; devServerPort: number }): void;
|
205 | }
|
206 |
|
207 | export interface IBeforeBlockWritingFunc {
|
208 | (args: { service: any; sourcePath: string; blockPath: string }): void;
|
209 | }
|
210 |
|
211 | interface IBeforeDevServer {
|
212 | (fn: IBeforeDevServerFunc): void;
|
213 | }
|
214 |
|
215 | interface IBeforeDevServerAsync {
|
216 | (fn: IBeforeDevServerFunc): Promise<any>;
|
217 | }
|
218 |
|
219 | interface IAfterDevServer {
|
220 | (fn: IAfterDevServerFunc): void;
|
221 | }
|
222 |
|
223 | interface IBeforeBlockWriting {
|
224 | (fn: IBeforeBlockWritingFunc): void;
|
225 | }
|
226 |
|
227 | interface IOnStart {
|
228 | (fn: () => void): void;
|
229 | }
|
230 |
|
231 | interface onPrintUmiError {
|
232 | (args: { error: any; opts: object }): void;
|
233 | }
|
234 |
|
235 | interface IEventAsync {
|
236 | (fn: () => void): Promise<any>;
|
237 | }
|
238 |
|
239 | export interface IOnDevCompileDoneFunc {
|
240 | (args: { isFirstCompile: boolean; stats: IWebpack.Stats }): void;
|
241 | }
|
242 |
|
243 | interface IOnDevCompileDone {
|
244 | (fn: IOnDevCompileDoneFunc): void;
|
245 | }
|
246 |
|
247 | export interface IOnOptionChangeFunc<T = any> {
|
248 | (newOpts: T): void;
|
249 | }
|
250 |
|
251 | interface IOnOptionChange {
|
252 | (fn: IOnOptionChangeFunc): void;
|
253 | }
|
254 |
|
255 | export interface IOnBuildSuccessFunc {
|
256 | (args: { stats: IWebpack.Stats[] }): void;
|
257 | }
|
258 |
|
259 | interface IOnBuildSuccess {
|
260 | (fn: IOnBuildSuccessFunc): void;
|
261 | }
|
262 |
|
263 | interface IOnBuildSuccessAsync {
|
264 | (fn: IOnBuildSuccessFunc): Promise<any>;
|
265 | }
|
266 |
|
267 | export interface IOnBuildFailFunc {
|
268 | (args: { stats: IWebpack.Stats[]; err: Error }): void;
|
269 | }
|
270 |
|
271 | interface IOnBuildFail {
|
272 | (fn: IOnBuildFailFunc): void;
|
273 | }
|
274 |
|
275 | interface IOnHTMLRebuild {
|
276 | (fn: () => void): void;
|
277 | }
|
278 |
|
279 | export interface IOnGenerateFilesFunc {
|
280 | (args: { isRebuild?: boolean }): void;
|
281 | }
|
282 |
|
283 | interface IOnGenerateFiles {
|
284 | (fn: IOnGenerateFilesFunc): void;
|
285 | }
|
286 |
|
287 | export interface IOnPatchRouteFunc {
|
288 | (args: { route: IRoute }): void;
|
289 | }
|
290 |
|
291 | interface IOnPatchRoute {
|
292 | (fn: IOnPatchRouteFunc): void;
|
293 | }
|
294 |
|
295 | interface IAction<T = object> {
|
296 | type: string;
|
297 | payload?: T;
|
298 | lang?: IUi.ILang;
|
299 | }
|
300 |
|
301 | export type ISend = (action: IAction<{}>) => void;
|
302 | export type ISuccess<T = object> = (payload: T) => void;
|
303 | export type IFailure<T = object> = (payload: T) => void;
|
304 | type IUiLogType = 'error' | 'info';
|
305 | export type IUiLog = (type: IUiLogType, payload: string) => void;
|
306 |
|
307 | export interface IOnUISocketFunc {
|
308 | (args: {
|
309 | action: IAction;
|
310 | send: ISend;
|
311 | success: ISuccess<{}>;
|
312 | failure: IFailure<{}>;
|
313 | log: IUiLog;
|
314 | }): void;
|
315 | }
|
316 |
|
317 | export interface IOnUISocket {
|
318 | (fn: IOnUISocketFunc): void;
|
319 | }
|
320 |
|
321 | export interface IChangeWebpackConfigFunc<T, U> {
|
322 | (webpackConfig: T, AFWebpack: { webpack: U }): T | void;
|
323 | }
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 | export interface IChangeWebpackConfig<T, U> {
|
330 | (fn: IChangeWebpackConfigFunc<T, U>): void;
|
331 | }
|
332 |
|
333 | export interface IModifyFunc<T, U> {
|
334 | |
335 |
|
336 |
|
337 | (memo: T, args: U): T | T;
|
338 | }
|
339 |
|
340 | export interface IModify<T, U = {}> {
|
341 | (fn: IModifyFunc<T, U> | T): void;
|
342 | }
|
343 |
|
344 | export interface IAddFunc<T, U> {
|
345 | |
346 |
|
347 |
|
348 | (memo: T[], args: U): T | T[];
|
349 | }
|
350 |
|
351 | export interface IAdd<T, U = {}> {
|
352 | (fn: IAddFunc<T, U> | T | T[]): void;
|
353 | }
|
354 |
|
355 | interface IGetChunkPath {
|
356 | (fileName: string): string | null;
|
357 | }
|
358 |
|
359 | interface IModifyHTMLWithASTArgs {
|
360 | route: IRoute;
|
361 | getChunkPath: IGetChunkPath;
|
362 | }
|
363 |
|
364 | export interface IModifyHTMLWithASTFunc {
|
365 | ($: CheerioStatic, args: IModifyHTMLWithASTArgs): CheerioStatic;
|
366 | }
|
367 |
|
368 | interface IModifyHTMLWithAST {
|
369 | (fn: IModifyHTMLWithASTFunc): void;
|
370 | }
|
371 |
|
372 | export interface IAddImportOpts {
|
373 | |
374 |
|
375 |
|
376 |
|
377 | source: string;
|
378 | specifier?: string;
|
379 | }
|
380 |
|
381 | interface IModifyRouteComponentArgs {
|
382 | importPath: string;
|
383 | webpackChunkName: string;
|
384 | component: string;
|
385 | }
|
386 |
|
387 | export interface IPkg {
|
388 | name?: string;
|
389 | version?: string;
|
390 | dependencies?: {
|
391 | [prop: string]: string;
|
392 | };
|
393 | devDependencies?: {
|
394 | [prop: string]: string;
|
395 | };
|
396 | }
|
397 |
|
398 | interface IModifyBlockFileArgs {
|
399 | targetPath: string;
|
400 | }
|
401 |
|
402 | interface IModifyBlockTargetArgs {
|
403 | sourceName: string;
|
404 | }
|
405 |
|
406 | export interface IBlockDependencies {
|
407 | conflicts: [string, string, string][];
|
408 | lacks: [string, string][];
|
409 | devConflicts: [string, string, string][];
|
410 | devLacks: [string, string][];
|
411 | }
|
412 |
|
413 | export interface IMiddlewareFunction {
|
414 | (req: any, res: any, next: any): void;
|
415 | }
|
416 |
|
417 | export interface IApi {
|
418 | |
419 |
|
420 |
|
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 |
|
444 |
|
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 |
|
462 |
|
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 |
|
479 |
|
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 |
|
501 |
|
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 | }
|