@umijs/core 4.0.0-rc.7 → 4.0.0
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/dist/index.d.ts +1 -1
- package/dist/route/routesConfig.d.ts +1 -0
- package/dist/route/routesConfig.js +45 -16
- package/dist/route/routesConvention.js +2 -2
- package/dist/route/utils.js +1 -1
- package/dist/service/command.d.ts +3 -0
- package/dist/service/command.js +1 -0
- package/dist/service/generatePlugin.js +59 -64
- package/dist/service/generator.d.ts +40 -25
- package/dist/service/generator.js +6 -12
- package/dist/service/path.d.ts +1 -0
- package/dist/service/plugin.js +3 -3
- package/dist/service/pluginAPI.d.ts +5 -2
- package/dist/service/pluginAPI.js +18 -6
- package/dist/service/service.d.ts +13 -1
- package/dist/service/service.js +320 -307
- package/dist/types.d.ts +1 -0
- package/package.json +8 -10
- package/compiled/tapable/LICENSE +0 -21
- package/compiled/tapable/index.js +0 -1
- package/compiled/tapable/package.json +0 -1
- package/compiled/tapable/tapable.d.ts +0 -116
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
export * from './config/config';
|
|
2
2
|
export * from './route/route';
|
|
3
|
-
export {
|
|
3
|
+
export { Generator, GeneratorType } from './service/generator';
|
|
4
4
|
export * from './service/pluginAPI';
|
|
5
5
|
export * from './service/service';
|
|
6
6
|
export { Env, IAdd, IEvent, IModify, IRoute } from './types';
|
|
@@ -1,15 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
3
|
-
var t = {};
|
|
4
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
5
|
-
t[p] = s[p];
|
|
6
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
7
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
8
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
9
|
-
t[p[i]] = s[p[i]];
|
|
10
|
-
}
|
|
11
|
-
return t;
|
|
12
|
-
};
|
|
13
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
14
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
15
4
|
};
|
|
@@ -18,35 +7,75 @@ exports.getConfigRoutes = void 0;
|
|
|
18
7
|
const assert_1 = __importDefault(require("assert"));
|
|
19
8
|
function getConfigRoutes(opts) {
|
|
20
9
|
const memo = { ret: {}, id: 1 };
|
|
21
|
-
transformRoutes({
|
|
10
|
+
transformRoutes({
|
|
11
|
+
routes: opts.routes,
|
|
12
|
+
parentId: undefined,
|
|
13
|
+
memo,
|
|
14
|
+
onResolveComponent: opts.onResolveComponent,
|
|
15
|
+
});
|
|
22
16
|
return memo.ret;
|
|
23
17
|
}
|
|
24
18
|
exports.getConfigRoutes = getConfigRoutes;
|
|
25
19
|
function transformRoutes(opts) {
|
|
26
20
|
opts.routes.forEach((route) => {
|
|
27
|
-
transformRoute({
|
|
21
|
+
transformRoute({
|
|
22
|
+
route,
|
|
23
|
+
parentId: opts.parentId,
|
|
24
|
+
memo: opts.memo,
|
|
25
|
+
onResolveComponent: opts.onResolveComponent,
|
|
26
|
+
});
|
|
28
27
|
});
|
|
29
28
|
}
|
|
30
29
|
function transformRoute(opts) {
|
|
31
30
|
(0, assert_1.default)(!opts.route.children, 'children is not allowed in route props, use routes instead.');
|
|
32
31
|
const id = String(opts.memo.id++);
|
|
33
|
-
const
|
|
32
|
+
const { routes, component, wrappers, ...routeProps } = opts.route;
|
|
34
33
|
let absPath = opts.route.path;
|
|
35
34
|
if ((absPath === null || absPath === void 0 ? void 0 : absPath.charAt(0)) !== '/') {
|
|
36
35
|
const parentAbsPath = opts.parentId
|
|
37
|
-
? opts.memo.ret[opts.parentId].absPath.replace(
|
|
36
|
+
? opts.memo.ret[opts.parentId].absPath.replace(/\/+$/, '/') // to remove '/'s on the tail
|
|
38
37
|
: '/';
|
|
39
38
|
absPath = parentAbsPath + absPath;
|
|
40
39
|
}
|
|
41
|
-
opts.memo.ret[id] =
|
|
40
|
+
opts.memo.ret[id] = {
|
|
41
|
+
...routeProps,
|
|
42
|
+
path: opts.route.path,
|
|
43
|
+
...(component
|
|
44
|
+
? {
|
|
45
|
+
file: opts.onResolveComponent
|
|
46
|
+
? opts.onResolveComponent(component)
|
|
47
|
+
: component,
|
|
48
|
+
}
|
|
49
|
+
: {}),
|
|
50
|
+
parentId: opts.parentId,
|
|
51
|
+
id,
|
|
52
|
+
};
|
|
42
53
|
if (absPath) {
|
|
43
54
|
opts.memo.ret[id].absPath = absPath;
|
|
44
55
|
}
|
|
56
|
+
if (wrappers === null || wrappers === void 0 ? void 0 : wrappers.length) {
|
|
57
|
+
let parentId = opts.parentId;
|
|
58
|
+
let path = opts.route.path;
|
|
59
|
+
wrappers.forEach((wrapper) => {
|
|
60
|
+
const { id } = transformRoute({
|
|
61
|
+
route: { path, component: wrapper },
|
|
62
|
+
parentId,
|
|
63
|
+
memo: opts.memo,
|
|
64
|
+
onResolveComponent: opts.onResolveComponent,
|
|
65
|
+
});
|
|
66
|
+
parentId = id;
|
|
67
|
+
path = '';
|
|
68
|
+
});
|
|
69
|
+
opts.memo.ret[id].parentId = parentId;
|
|
70
|
+
opts.memo.ret[id].path = path;
|
|
71
|
+
}
|
|
45
72
|
if (opts.route.routes) {
|
|
46
73
|
transformRoutes({
|
|
47
74
|
routes: opts.route.routes,
|
|
48
75
|
parentId: id,
|
|
49
76
|
memo: opts.memo,
|
|
77
|
+
onResolveComponent: opts.onResolveComponent,
|
|
50
78
|
});
|
|
51
79
|
}
|
|
80
|
+
return { id };
|
|
52
81
|
}
|
|
@@ -44,10 +44,10 @@ function visitFiles(opts) {
|
|
|
44
44
|
let file = (0, path_1.resolve)(opts.dir, filename);
|
|
45
45
|
let stat = (0, fs_1.lstatSync)(file);
|
|
46
46
|
if (stat.isDirectory()) {
|
|
47
|
-
visitFiles(
|
|
47
|
+
visitFiles({ ...opts, dir: file });
|
|
48
48
|
}
|
|
49
49
|
else if (stat.isFile() &&
|
|
50
|
-
['.tsx', '.ts', '.js', '.jsx', '.md', '.mdx'].includes((0, path_1.extname)(file))) {
|
|
50
|
+
['.tsx', '.ts', '.js', '.jsx', '.md', '.mdx', '.vue'].includes((0, path_1.extname)(file))) {
|
|
51
51
|
opts.visitor((0, path_1.relative)(opts.baseDir, file));
|
|
52
52
|
}
|
|
53
53
|
}
|
package/dist/route/utils.js
CHANGED
|
@@ -19,7 +19,7 @@ function findParentRouteId(routeIds, childRouteId) {
|
|
|
19
19
|
return routeIds.find((id) => childRouteId.startsWith(`${id}/`));
|
|
20
20
|
}
|
|
21
21
|
exports.findParentRouteId = findParentRouteId;
|
|
22
|
-
const routeModuleExts = ['.js', '.jsx', '.ts', '.tsx', '.md', '.mdx'];
|
|
22
|
+
const routeModuleExts = ['.js', '.jsx', '.ts', '.tsx', '.md', '.mdx', '.vue'];
|
|
23
23
|
function isRouteModuleFile(opts) {
|
|
24
24
|
// TODO: add cache strategy
|
|
25
25
|
for (const excludeRegExp of opts.exclude || []) {
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { yParser } from '@umijs/utils';
|
|
2
2
|
import { Plugin } from './plugin';
|
|
3
|
+
import { ResolveConfigMode } from './pluginAPI';
|
|
3
4
|
export interface IOpts {
|
|
4
5
|
name: string;
|
|
5
6
|
description?: string;
|
|
@@ -11,12 +12,14 @@ export interface IOpts {
|
|
|
11
12
|
}): void;
|
|
12
13
|
};
|
|
13
14
|
plugin: Plugin;
|
|
15
|
+
configResolveMode?: ResolveConfigMode;
|
|
14
16
|
}
|
|
15
17
|
export declare class Command {
|
|
16
18
|
name: string;
|
|
17
19
|
description?: string;
|
|
18
20
|
options?: string;
|
|
19
21
|
details?: string;
|
|
22
|
+
configResolveMode: ResolveConfigMode;
|
|
20
23
|
fn: {
|
|
21
24
|
({ args }: {
|
|
22
25
|
args: yParser.Arguments;
|
package/dist/service/command.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
3
|
const utils_1 = require("@umijs/utils");
|
|
13
4
|
const generator_1 = require("./generator");
|
|
@@ -19,70 +10,74 @@ exports.default = (api) => {
|
|
|
19
10
|
umi generate
|
|
20
11
|
`,
|
|
21
12
|
description: 'generate code snippets quickly',
|
|
22
|
-
|
|
13
|
+
configResolveMode: 'loose',
|
|
14
|
+
async fn({ args }) {
|
|
23
15
|
var _a;
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
16
|
+
const [type] = args._;
|
|
17
|
+
const runGenerator = async (generator) => {
|
|
18
|
+
await (generator === null || generator === void 0 ? void 0 : generator.fn({
|
|
19
|
+
args,
|
|
20
|
+
generateFile: utils_1.generateFile,
|
|
21
|
+
installDeps: utils_1.installDeps,
|
|
22
|
+
updatePackageJSON: utils_1.updatePackageJSON,
|
|
23
|
+
}));
|
|
24
|
+
};
|
|
25
|
+
if (type) {
|
|
26
|
+
const generator = api.service.generators[type];
|
|
27
|
+
if (!generator) {
|
|
28
|
+
throw new Error(`Generator ${type} not found.`);
|
|
29
|
+
}
|
|
30
|
+
if (generator.type === generator_1.GeneratorType.enable) {
|
|
31
|
+
const enable = await ((_a = generator.checkEnable) === null || _a === void 0 ? void 0 : _a.call(generator, {
|
|
28
32
|
args,
|
|
29
|
-
generateFile: utils_1.generateFile,
|
|
30
|
-
installDeps: utils_1.installDeps,
|
|
31
|
-
updatePackageJSON: utils_1.updatePackageJSON,
|
|
32
33
|
}));
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
if (generator.type === generator_1.GeneratorType.enable) {
|
|
40
|
-
const enable = yield ((_a = generator.checkEnable) === null || _a === void 0 ? void 0 : _a.call(generator, {
|
|
41
|
-
args,
|
|
42
|
-
}));
|
|
43
|
-
if (!enable) {
|
|
44
|
-
throw new Error(`Generator ${type} is unable.The corresponding function has been turned on or is not available.`);
|
|
34
|
+
if (!enable) {
|
|
35
|
+
if (typeof generator.disabledDescription === 'function') {
|
|
36
|
+
utils_1.logger.warn(generator.disabledDescription());
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
utils_1.logger.warn(generator.disabledDescription);
|
|
45
40
|
}
|
|
41
|
+
return;
|
|
46
42
|
}
|
|
47
|
-
yield runGenerator(generator);
|
|
48
43
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
44
|
+
await runGenerator(generator);
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
const getEnableGenerators = async (generators) => {
|
|
48
|
+
var _a;
|
|
49
|
+
const questions = [];
|
|
50
|
+
for (const key of Object.keys(generators)) {
|
|
51
|
+
const g = generators[key];
|
|
52
|
+
if (g.type === generator_1.GeneratorType.generate) {
|
|
53
|
+
questions.push({
|
|
54
|
+
title: `${g.name} -- ${g.description}` || '',
|
|
55
|
+
value: g.key,
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
const enable = await ((_a = g === null || g === void 0 ? void 0 : g.checkEnable) === null || _a === void 0 ? void 0 : _a.call(g, {
|
|
60
|
+
args,
|
|
61
|
+
}));
|
|
62
|
+
if (enable) {
|
|
55
63
|
questions.push({
|
|
56
|
-
title: `${
|
|
57
|
-
|
|
58
|
-
value: generators[key].key,
|
|
64
|
+
title: `${g.name} -- ${g.description}` || '',
|
|
65
|
+
value: g.key,
|
|
59
66
|
});
|
|
60
67
|
}
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
return questions;
|
|
75
|
-
});
|
|
76
|
-
const questions = yield getEnableGenerators(api.service.generators);
|
|
77
|
-
const { gType } = yield (0, utils_1.prompts)({
|
|
78
|
-
type: 'select',
|
|
79
|
-
name: 'gType',
|
|
80
|
-
message: 'Pick generator type',
|
|
81
|
-
choices: questions,
|
|
82
|
-
});
|
|
83
|
-
yield runGenerator(api.service.generators[gType]);
|
|
84
|
-
}
|
|
85
|
-
});
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return questions;
|
|
71
|
+
};
|
|
72
|
+
const questions = await getEnableGenerators(api.service.generators);
|
|
73
|
+
const { gType } = await (0, utils_1.prompts)({
|
|
74
|
+
type: 'select',
|
|
75
|
+
name: 'gType',
|
|
76
|
+
message: 'Pick generator type',
|
|
77
|
+
choices: questions,
|
|
78
|
+
});
|
|
79
|
+
await runGenerator(api.service.generators[gType]);
|
|
80
|
+
}
|
|
86
81
|
},
|
|
87
82
|
});
|
|
88
83
|
};
|
|
@@ -1,30 +1,52 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { generateFile } from '@umijs/utils';
|
|
2
2
|
import { Plugin } from './plugin';
|
|
3
3
|
export declare enum GeneratorType {
|
|
4
4
|
generate = "generate",
|
|
5
5
|
enable = "enable"
|
|
6
6
|
}
|
|
7
|
-
|
|
7
|
+
declare type IGeneratorOptsWithoutEnableCheck = {
|
|
8
8
|
key: string;
|
|
9
|
-
name
|
|
10
|
-
description
|
|
11
|
-
type
|
|
12
|
-
|
|
9
|
+
name: string;
|
|
10
|
+
description: string;
|
|
11
|
+
type: GeneratorType.generate;
|
|
12
|
+
fn: {
|
|
13
|
+
(opts: {
|
|
14
|
+
args: any;
|
|
15
|
+
generateFile: typeof generateFile;
|
|
16
|
+
updatePackageJSON: {
|
|
17
|
+
(opts: {
|
|
18
|
+
opts: object;
|
|
19
|
+
cwd?: string;
|
|
20
|
+
}): void;
|
|
21
|
+
};
|
|
22
|
+
installDeps: {
|
|
23
|
+
(opts: {
|
|
24
|
+
opts: {
|
|
25
|
+
devDependencies?: string[];
|
|
26
|
+
dependencies?: string[];
|
|
27
|
+
};
|
|
28
|
+
cwd?: string;
|
|
29
|
+
}): void;
|
|
30
|
+
};
|
|
31
|
+
}): void;
|
|
32
|
+
};
|
|
33
|
+
plugin: Plugin;
|
|
34
|
+
};
|
|
35
|
+
declare type IGeneratorOptsWithEnableCheck = {
|
|
36
|
+
key: string;
|
|
37
|
+
name: string;
|
|
38
|
+
description: string;
|
|
39
|
+
type: GeneratorType.enable;
|
|
40
|
+
checkEnable: {
|
|
13
41
|
(opts: {
|
|
14
42
|
args: any;
|
|
15
43
|
}): boolean;
|
|
16
44
|
};
|
|
45
|
+
disabledDescription: string | (() => string);
|
|
17
46
|
fn: {
|
|
18
47
|
(opts: {
|
|
19
48
|
args: any;
|
|
20
|
-
generateFile:
|
|
21
|
-
(opts: {
|
|
22
|
-
path: string;
|
|
23
|
-
target: string;
|
|
24
|
-
data?: any;
|
|
25
|
-
questions?: prompts.PromptObject[];
|
|
26
|
-
}): Promise<void>;
|
|
27
|
-
};
|
|
49
|
+
generateFile: typeof generateFile;
|
|
28
50
|
updatePackageJSON: {
|
|
29
51
|
(opts: {
|
|
30
52
|
opts: object;
|
|
@@ -43,14 +65,7 @@ export interface IGeneratorOpts {
|
|
|
43
65
|
}): void;
|
|
44
66
|
};
|
|
45
67
|
plugin: Plugin;
|
|
46
|
-
}
|
|
47
|
-
export declare
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
description?: IGeneratorOpts['description'];
|
|
51
|
-
type?: IGeneratorOpts['type'];
|
|
52
|
-
checkEnable?: IGeneratorOpts['checkEnable'];
|
|
53
|
-
fn: IGeneratorOpts['fn'];
|
|
54
|
-
plugin: IGeneratorOpts['plugin'];
|
|
55
|
-
constructor(opts: IGeneratorOpts);
|
|
56
|
-
}
|
|
68
|
+
};
|
|
69
|
+
export declare type Generator = IGeneratorOptsWithEnableCheck | IGeneratorOptsWithoutEnableCheck;
|
|
70
|
+
export declare function makeGenerator<T>(opts: T): T;
|
|
71
|
+
export {};
|
|
@@ -1,20 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.makeGenerator = exports.GeneratorType = void 0;
|
|
4
4
|
var GeneratorType;
|
|
5
5
|
(function (GeneratorType) {
|
|
6
6
|
GeneratorType["generate"] = "generate";
|
|
7
7
|
GeneratorType["enable"] = "enable";
|
|
8
8
|
})(GeneratorType = exports.GeneratorType || (exports.GeneratorType = {}));
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
this.type = opts.type;
|
|
14
|
-
this.description = opts.description;
|
|
15
|
-
this.checkEnable = opts.checkEnable;
|
|
16
|
-
this.fn = opts.fn;
|
|
17
|
-
this.plugin = opts.plugin;
|
|
18
|
-
}
|
|
9
|
+
function makeGenerator(opts) {
|
|
10
|
+
return {
|
|
11
|
+
...opts,
|
|
12
|
+
};
|
|
19
13
|
}
|
|
20
|
-
exports.
|
|
14
|
+
exports.makeGenerator = makeGenerator;
|
package/dist/service/path.d.ts
CHANGED
package/dist/service/plugin.js
CHANGED
|
@@ -38,6 +38,7 @@ class Plugin {
|
|
|
38
38
|
this.apply = () => {
|
|
39
39
|
utils_1.register.register({
|
|
40
40
|
implementor: esbuild_1.default,
|
|
41
|
+
exts: ['.ts', '.mjs'],
|
|
41
42
|
});
|
|
42
43
|
utils_1.register.clearFiles();
|
|
43
44
|
let ret;
|
|
@@ -47,10 +48,9 @@ class Plugin {
|
|
|
47
48
|
catch (e) {
|
|
48
49
|
throw new Error(`Register ${this.type} ${this.path} failed, since ${e.message}`);
|
|
49
50
|
}
|
|
50
|
-
|
|
51
|
-
|
|
51
|
+
finally {
|
|
52
|
+
utils_1.register.restore();
|
|
52
53
|
}
|
|
53
|
-
utils_1.register.restore();
|
|
54
54
|
// use the default member for es modules
|
|
55
55
|
return ret.__esModule ? ret.default : ret;
|
|
56
56
|
};
|
|
@@ -1,11 +1,14 @@
|
|
|
1
1
|
import { logger } from '@umijs/utils';
|
|
2
2
|
import { EnableBy, Env, IPluginConfig } from '../types';
|
|
3
3
|
import { IOpts as ICommandOpts } from './command';
|
|
4
|
-
import {
|
|
4
|
+
import { Generator } from './generator';
|
|
5
5
|
import { IOpts as IHookOpts } from './hook';
|
|
6
6
|
import { Plugin } from './plugin';
|
|
7
7
|
import { Service } from './service';
|
|
8
8
|
declare type Logger = typeof logger;
|
|
9
|
+
declare const resolveConfigModes: readonly ["strict", "loose"];
|
|
10
|
+
export declare type ResolveConfigMode = typeof resolveConfigModes[number];
|
|
11
|
+
declare type DistributiveOmit<T, K extends keyof any> = T extends any ? Omit<T, K> : never;
|
|
9
12
|
export declare class PluginAPI {
|
|
10
13
|
service: Service;
|
|
11
14
|
plugin: Plugin;
|
|
@@ -25,7 +28,7 @@ export declare class PluginAPI {
|
|
|
25
28
|
registerCommand(opts: Omit<ICommandOpts, 'plugin'> & {
|
|
26
29
|
alias?: string | string[];
|
|
27
30
|
}): void;
|
|
28
|
-
registerGenerator(opts:
|
|
31
|
+
registerGenerator(opts: DistributiveOmit<Generator, 'plugin'>): void;
|
|
29
32
|
register(opts: Omit<IHookOpts, 'plugin'>): void;
|
|
30
33
|
registerMethod(opts: {
|
|
31
34
|
name: string;
|
|
@@ -12,6 +12,7 @@ const generator_1 = require("./generator");
|
|
|
12
12
|
const hook_1 = require("./hook");
|
|
13
13
|
const plugin_1 = require("./plugin");
|
|
14
14
|
const utils_2 = require("./utils");
|
|
15
|
+
const resolveConfigModes = ['strict', 'loose'];
|
|
15
16
|
class PluginAPI {
|
|
16
17
|
constructor(opts) {
|
|
17
18
|
this.service = opts.service;
|
|
@@ -48,15 +49,20 @@ class PluginAPI {
|
|
|
48
49
|
delete opts.alias;
|
|
49
50
|
const registerCommand = (commandOpts) => {
|
|
50
51
|
var _a;
|
|
51
|
-
const { name } = commandOpts;
|
|
52
|
+
const { name, configResolveMode } = commandOpts;
|
|
53
|
+
(0, assert_1.default)(!configResolveMode ||
|
|
54
|
+
resolveConfigModes.indexOf(configResolveMode) >= 0, `configResolveMode must be one of ${resolveConfigModes.join(',')}, but got ${configResolveMode}`);
|
|
52
55
|
(0, assert_1.default)(!this.service.commands[name], `api.registerCommand() failed, the command ${name} is exists from ${(_a = this.service.commands[name]) === null || _a === void 0 ? void 0 : _a.plugin.id}.`);
|
|
53
|
-
this.service.commands[name] = new command_1.Command(
|
|
56
|
+
this.service.commands[name] = new command_1.Command({
|
|
57
|
+
...commandOpts,
|
|
58
|
+
plugin: this.plugin,
|
|
59
|
+
});
|
|
54
60
|
};
|
|
55
61
|
registerCommand(opts);
|
|
56
62
|
if (alias) {
|
|
57
63
|
const aliases = (0, utils_2.makeArray)(alias);
|
|
58
64
|
aliases.forEach((alias) => {
|
|
59
|
-
registerCommand(
|
|
65
|
+
registerCommand({ ...opts, name: alias });
|
|
60
66
|
});
|
|
61
67
|
}
|
|
62
68
|
}
|
|
@@ -64,13 +70,16 @@ class PluginAPI {
|
|
|
64
70
|
var _a;
|
|
65
71
|
const { key } = opts;
|
|
66
72
|
(0, assert_1.default)(!this.service.generators[key], `api.registerGenerator() failed, the generator ${key} is exists from ${(_a = this.service.generators[key]) === null || _a === void 0 ? void 0 : _a.plugin.id}.`);
|
|
67
|
-
this.service.generators[key] =
|
|
73
|
+
this.service.generators[key] = (0, generator_1.makeGenerator)({
|
|
74
|
+
...opts,
|
|
75
|
+
plugin: this.plugin,
|
|
76
|
+
});
|
|
68
77
|
}
|
|
69
78
|
register(opts) {
|
|
70
79
|
var _a, _b;
|
|
71
80
|
(0, assert_1.default)(this.service.stage <= types_1.ServiceStage.initPlugins, 'api.register() should not be called after plugin register stage.');
|
|
72
81
|
(_a = this.service.hooks)[_b = opts.key] || (_a[_b] = []);
|
|
73
|
-
this.service.hooks[opts.key].push(new hook_1.Hook(
|
|
82
|
+
this.service.hooks[opts.key].push(new hook_1.Hook({ ...opts, plugin: this.plugin }));
|
|
74
83
|
}
|
|
75
84
|
registerMethod(opts) {
|
|
76
85
|
(0, assert_1.default)(!this.service.pluginMethods[opts.name], `api.registerMethod() failed, method ${opts.name} is already exist.`);
|
|
@@ -81,7 +90,10 @@ class PluginAPI {
|
|
|
81
90
|
// 否则 pluginId 会不会,导致不能正确 skip plugin
|
|
82
91
|
function (fn) {
|
|
83
92
|
// @ts-ignore
|
|
84
|
-
this.register(
|
|
93
|
+
this.register({
|
|
94
|
+
key: opts.name,
|
|
95
|
+
...(utils_1.lodash.isPlainObject(fn) ? fn : { fn }),
|
|
96
|
+
});
|
|
85
97
|
},
|
|
86
98
|
};
|
|
87
99
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { yParser } from '@umijs/utils';
|
|
2
2
|
import { Config } from '../config/config';
|
|
3
|
-
import { ApplyPluginsType, ConfigChangeType, EnableBy, Env, IEvent, IModify, ServiceStage } from '../types';
|
|
3
|
+
import { ApplyPluginsType, ConfigChangeType, EnableBy, Env, IEvent, IFrameworkType, IModify, ServiceStage } from '../types';
|
|
4
4
|
import { Command } from './command';
|
|
5
5
|
import { Generator } from './generator';
|
|
6
6
|
import { Hook } from './hook';
|
|
@@ -22,6 +22,7 @@ export declare class Service {
|
|
|
22
22
|
subpaths: string[];
|
|
23
23
|
external?: boolean;
|
|
24
24
|
}>;
|
|
25
|
+
framework?: IFrameworkType;
|
|
25
26
|
[key: string]: any;
|
|
26
27
|
};
|
|
27
28
|
args: yParser.Arguments;
|
|
@@ -63,6 +64,13 @@ export declare class Service {
|
|
|
63
64
|
};
|
|
64
65
|
pkgPath: string;
|
|
65
66
|
constructor(opts: IOpts);
|
|
67
|
+
applyPlugins<T>(opts: {
|
|
68
|
+
key: string;
|
|
69
|
+
type?: ApplyPluginsType.event;
|
|
70
|
+
initialValue?: any;
|
|
71
|
+
args?: any;
|
|
72
|
+
sync: true;
|
|
73
|
+
}): typeof opts.initialValue | T;
|
|
66
74
|
applyPlugins<T>(opts: {
|
|
67
75
|
key: string;
|
|
68
76
|
type?: ApplyPluginsType;
|
|
@@ -73,6 +81,10 @@ export declare class Service {
|
|
|
73
81
|
name: string;
|
|
74
82
|
args?: any;
|
|
75
83
|
}): Promise<void>;
|
|
84
|
+
resolveConfig(): Promise<{
|
|
85
|
+
config: any;
|
|
86
|
+
defaultConfig: any;
|
|
87
|
+
}>;
|
|
76
88
|
_baconPlugins(): void;
|
|
77
89
|
initPreset(opts: {
|
|
78
90
|
preset: Plugin;
|