@umijs/core 4.0.0-beta.10 → 4.0.0-beta.14

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.
@@ -10,8 +10,12 @@ var __rest = (this && this.__rest) || function (s, e) {
10
10
  }
11
11
  return t;
12
12
  };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
13
16
  Object.defineProperty(exports, "__esModule", { value: true });
14
17
  exports.getConfigRoutes = void 0;
18
+ const assert_1 = __importDefault(require("assert"));
15
19
  function getConfigRoutes(opts) {
16
20
  const memo = { ret: {}, id: 1 };
17
21
  transformRoutes({ routes: opts.routes, parentId: undefined, memo });
@@ -24,6 +28,7 @@ function transformRoutes(opts) {
24
28
  });
25
29
  }
26
30
  function transformRoute(opts) {
31
+ (0, assert_1.default)(!opts.route.children, 'children is not allowed in route props, use routes instead.');
27
32
  const id = String(opts.memo.id++);
28
33
  const _a = opts.route, { routes, component } = _a, routeProps = __rest(_a, ["routes", "component"]);
29
34
  opts.memo.ret[id] = Object.assign(Object.assign(Object.assign(Object.assign({}, routeProps), { path: opts.route.path }), (component ? { file: component } : {})), { parentId: opts.parentId, id });
@@ -1,4 +1,4 @@
1
- import { EnableBy, IPluginConfig } from '../types';
1
+ import { EnableBy, Env, IPluginConfig } from '../types';
2
2
  declare type PluginType = 'plugin' | 'preset';
3
3
  interface IOpts {
4
4
  path: string;
@@ -21,13 +21,15 @@ export declare class Plugin {
21
21
  apply: Function;
22
22
  config: IPluginConfig;
23
23
  enableBy: EnableBy | ((opts: {
24
+ userConfig: any;
24
25
  config: any;
26
+ env: Env;
25
27
  }) => boolean);
26
28
  constructor(opts: IOpts);
27
29
  merge(opts: {
28
30
  key?: string;
29
31
  config?: IPluginConfig;
30
- enableBy?: EnableBy | (() => boolean);
32
+ enableBy?: any;
31
33
  }): void;
32
34
  getId(opts: {
33
35
  pkg: any;
@@ -1,5 +1,5 @@
1
1
  import { logger } from '@umijs/utils';
2
- import { EnableBy, IPluginConfig } from '../types';
2
+ import { EnableBy, Env, IPluginConfig } from '../types';
3
3
  import { IOpts as ICommandOpts } from './command';
4
4
  import { IGeneratorOpts } from './generator';
5
5
  import { IOpts as IHookOpts } from './hook';
@@ -17,7 +17,10 @@ export declare class PluginAPI {
17
17
  describe(opts: {
18
18
  key?: string;
19
19
  config?: IPluginConfig;
20
- enableBy?: EnableBy | (() => boolean);
20
+ enableBy?: EnableBy | ((enableByOpts: {
21
+ userConfig: any;
22
+ env: Env;
23
+ }) => boolean);
21
24
  }): void;
22
25
  registerCommand(opts: Omit<ICommandOpts, 'plugin'> & {
23
26
  alias?: string | string[];
@@ -15,7 +15,10 @@ interface IOpts {
15
15
  }
16
16
  export declare class Service {
17
17
  private opts;
18
- appData: Record<string, any>;
18
+ appData: {
19
+ deps?: Record<string, string>;
20
+ [key: string]: any;
21
+ };
19
22
  args: yParser.Arguments;
20
23
  commands: Record<string, Command>;
21
24
  generators: Record<string, Generator>;
@@ -36,6 +39,7 @@ export declare class Service {
36
39
  absOutputPath?: string;
37
40
  };
38
41
  plugins: Record<string, Plugin>;
42
+ keyToPluginMap: Record<string, Plugin>;
39
43
  pluginMethods: Record<string, {
40
44
  plugin: Plugin;
41
45
  fn: Function;
@@ -66,7 +70,7 @@ export declare class Service {
66
70
  presets?: Plugin[];
67
71
  plugins: Plugin[];
68
72
  }): Promise<any>;
69
- isPluginEnable(hook: Hook): boolean;
73
+ isPluginEnable(hook: Hook | string): boolean;
70
74
  }
71
75
  export interface IServicePluginAPI {
72
76
  appData: typeof Service.prototype.appData;
@@ -79,6 +83,8 @@ export interface IServicePluginAPI {
79
83
  name: typeof Service.prototype.name;
80
84
  paths: Required<typeof Service.prototype.paths>;
81
85
  userConfig: typeof Service.prototype.userConfig;
86
+ env: typeof Service.prototype.env;
87
+ isPluginEnable: typeof Service.prototype.isPluginEnable;
82
88
  onCheck: IEvent<null>;
83
89
  onStart: IEvent<null>;
84
90
  modifyAppData: IModify<typeof Service.prototype.appData, null>;
@@ -41,6 +41,7 @@ class Service {
41
41
  this.paths = {};
42
42
  // preset is plugin with different type
43
43
  this.plugins = {};
44
+ this.keyToPluginMap = {};
44
45
  this.pluginMethods = {};
45
46
  this.skipPluginIds = new Set();
46
47
  this.stage = types_1.ServiceStage.uninitialized;
@@ -181,6 +182,10 @@ class Service {
181
182
  while (plugins.length) {
182
183
  yield this.initPlugin({ plugin: plugins.shift(), plugins });
183
184
  }
185
+ // keyToPluginMap
186
+ for (const id of Object.keys(this.plugins)) {
187
+ this.keyToPluginMap[this.plugins[id].key] = this.plugins[id];
188
+ }
184
189
  // collect configSchemas and configDefaults
185
190
  for (const id of Object.keys(this.plugins)) {
186
191
  const { config, key } = this.plugins[id];
@@ -302,6 +307,8 @@ class Service {
302
307
  'name',
303
308
  'paths',
304
309
  'userConfig',
310
+ 'env',
311
+ 'isPluginEnable',
305
312
  ],
306
313
  staticProps: {
307
314
  ApplyPluginsType: types_1.ApplyPluginsType,
@@ -315,6 +322,9 @@ class Service {
315
322
  if ((0, utils_2.isPromise)(ret)) {
316
323
  ret = yield ret;
317
324
  }
325
+ if (opts.plugin.type === 'plugin') {
326
+ (0, assert_1.default)(!ret, `plugin should return nothing`);
327
+ }
318
328
  if (ret === null || ret === void 0 ? void 0 : ret.presets) {
319
329
  ret.presets = ret.presets.map((preset) => new plugin_1.Plugin({
320
330
  path: preset,
@@ -333,15 +343,30 @@ class Service {
333
343
  });
334
344
  }
335
345
  isPluginEnable(hook) {
336
- const { id, key, enableBy } = hook.plugin;
346
+ let plugin;
347
+ if (hook.plugin) {
348
+ plugin = hook.plugin;
349
+ }
350
+ else {
351
+ plugin = this.keyToPluginMap[hook];
352
+ }
353
+ const { id, key, enableBy } = plugin;
337
354
  if (this.skipPluginIds.has(id))
338
355
  return false;
339
356
  if (this.userConfig[key] === false)
340
357
  return false;
341
- if (enableBy === types_1.EnableBy.config && !(key in this.config))
358
+ if (this.config[key] === false)
342
359
  return false;
360
+ if (enableBy === types_1.EnableBy.config) {
361
+ // TODO: 提供单独的命令用于启用插件
362
+ return key in this.userConfig;
363
+ }
343
364
  if (typeof enableBy === 'function')
344
- return enableBy({ config: this.config });
365
+ return enableBy({
366
+ userConfig: this.userConfig,
367
+ config: this.config,
368
+ env: this.env,
369
+ });
345
370
  // EnableBy.register
346
371
  return true;
347
372
  }
package/dist/types.d.ts CHANGED
@@ -44,6 +44,7 @@ export interface IRoute {
44
44
  file: string;
45
45
  id: string;
46
46
  parentId?: string;
47
+ [key: string]: any;
47
48
  }
48
49
  export interface IEvent<T> {
49
50
  (fn: {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/core",
3
- "version": "4.0.0-beta.10",
3
+ "version": "4.0.0-beta.14",
4
4
  "homepage": "https://github.com/umijs/umi-next/tree/master/packages/core#readme",
5
5
  "bugs": "https://github.com/umijs/umi-next/issues",
6
6
  "repository": {
@@ -20,8 +20,8 @@
20
20
  "dev": "pnpm build -- --watch"
21
21
  },
22
22
  "dependencies": {
23
- "@umijs/bundler-utils": "4.0.0-beta.10",
24
- "@umijs/utils": "4.0.0-beta.10"
23
+ "@umijs/bundler-utils": "4.0.0-beta.14",
24
+ "@umijs/utils": "4.0.0-beta.14"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@hapi/joi": "17.1.1",