@umijs/core 4.0.0-rc.15 → 4.0.0-rc.16

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 CHANGED
@@ -1,6 +1,6 @@
1
1
  export * from './config/config';
2
2
  export * from './route/route';
3
- export { GeneratorType, IGeneratorOpts } from './service/generator';
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,5 +1,6 @@
1
1
  interface IOpts {
2
2
  routes: any[];
3
+ onResolveComponent?: (component: string) => string;
3
4
  }
4
5
  export declare function getConfigRoutes(opts: IOpts): any[];
5
6
  export {};
@@ -7,13 +7,23 @@ exports.getConfigRoutes = void 0;
7
7
  const assert_1 = __importDefault(require("assert"));
8
8
  function getConfigRoutes(opts) {
9
9
  const memo = { ret: {}, id: 1 };
10
- transformRoutes({ routes: opts.routes, parentId: undefined, memo });
10
+ transformRoutes({
11
+ routes: opts.routes,
12
+ parentId: undefined,
13
+ memo,
14
+ onResolveComponent: opts.onResolveComponent,
15
+ });
11
16
  return memo.ret;
12
17
  }
13
18
  exports.getConfigRoutes = getConfigRoutes;
14
19
  function transformRoutes(opts) {
15
20
  opts.routes.forEach((route) => {
16
- transformRoute({ route, parentId: opts.parentId, memo: opts.memo });
21
+ transformRoute({
22
+ route,
23
+ parentId: opts.parentId,
24
+ memo: opts.memo,
25
+ onResolveComponent: opts.onResolveComponent,
26
+ });
17
27
  });
18
28
  }
19
29
  function transformRoute(opts) {
@@ -30,7 +40,13 @@ function transformRoute(opts) {
30
40
  opts.memo.ret[id] = {
31
41
  ...routeProps,
32
42
  path: opts.route.path,
33
- ...(component ? { file: component } : {}),
43
+ ...(component
44
+ ? {
45
+ file: opts.onResolveComponent
46
+ ? opts.onResolveComponent(component)
47
+ : component,
48
+ }
49
+ : {}),
34
50
  parentId: opts.parentId,
35
51
  id,
36
52
  };
@@ -45,6 +61,7 @@ function transformRoute(opts) {
45
61
  route: { path, component: wrapper },
46
62
  parentId,
47
63
  memo: opts.memo,
64
+ onResolveComponent: opts.onResolveComponent,
48
65
  });
49
66
  parentId = id;
50
67
  path = '';
@@ -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;
@@ -9,6 +9,7 @@ class Command {
9
9
  this.details = opts.details;
10
10
  this.fn = opts.fn;
11
11
  this.plugin = opts.plugin;
12
+ this.configResolveMode = opts.configResolveMode || 'strict';
12
13
  }
13
14
  }
14
15
  exports.Command = Command;
@@ -10,6 +10,7 @@ exports.default = (api) => {
10
10
  umi generate
11
11
  `,
12
12
  description: 'generate code snippets quickly',
13
+ configResolveMode: 'loose',
13
14
  async fn({ args }) {
14
15
  var _a;
15
16
  const [type] = args._;
@@ -31,32 +32,37 @@ umi generate
31
32
  args,
32
33
  }));
33
34
  if (!enable) {
34
- throw new Error(`Generator ${type} is unable.The corresponding function has been turned on or is not available.`);
35
+ if (typeof generator.disabledDescription === 'function') {
36
+ utils_1.logger.warn(generator.disabledDescription());
37
+ }
38
+ else {
39
+ utils_1.logger.warn(generator.disabledDescription);
40
+ }
41
+ return;
35
42
  }
36
43
  }
37
44
  await runGenerator(generator);
38
45
  }
39
46
  else {
40
47
  const getEnableGenerators = async (generators) => {
41
- var _a, _b;
48
+ var _a;
42
49
  const questions = [];
43
50
  for (const key of Object.keys(generators)) {
44
- if (generators[key].type === generator_1.GeneratorType.generate) {
51
+ const g = generators[key];
52
+ if (g.type === generator_1.GeneratorType.generate) {
45
53
  questions.push({
46
- title: `${generators[key].name} -- ${generators[key].description}` ||
47
- '',
48
- value: generators[key].key,
54
+ title: `${g.name} -- ${g.description}` || '',
55
+ value: g.key,
49
56
  });
50
57
  }
51
58
  else {
52
- const enable = await ((_b = (_a = generators[key]) === null || _a === void 0 ? void 0 : _a.checkEnable) === null || _b === void 0 ? void 0 : _b.call(_a, {
59
+ const enable = await ((_a = g === null || g === void 0 ? void 0 : g.checkEnable) === null || _a === void 0 ? void 0 : _a.call(g, {
53
60
  args,
54
61
  }));
55
62
  if (enable) {
56
63
  questions.push({
57
- title: `${generators[key].name} -- ${generators[key].description}` ||
58
- '',
59
- value: generators[key].key,
64
+ title: `${g.name} -- ${g.description}` || '',
65
+ value: g.key,
60
66
  });
61
67
  }
62
68
  }
@@ -4,16 +4,45 @@ export declare enum GeneratorType {
4
4
  generate = "generate",
5
5
  enable = "enable"
6
6
  }
7
- export interface IGeneratorOpts {
7
+ declare type IGeneratorOptsWithoutEnableCheck = {
8
8
  key: string;
9
9
  name: string;
10
10
  description: string;
11
- type?: GeneratorType;
12
- checkEnable?: {
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;
@@ -36,14 +65,7 @@ export interface IGeneratorOpts {
36
65
  }): void;
37
66
  };
38
67
  plugin: Plugin;
39
- }
40
- export declare class Generator {
41
- key: IGeneratorOpts['key'];
42
- name: IGeneratorOpts['name'];
43
- description: IGeneratorOpts['description'];
44
- type?: IGeneratorOpts['type'];
45
- checkEnable?: IGeneratorOpts['checkEnable'];
46
- fn: IGeneratorOpts['fn'];
47
- plugin: IGeneratorOpts['plugin'];
48
- constructor(opts: IGeneratorOpts);
49
- }
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.Generator = exports.GeneratorType = void 0;
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
- class Generator {
10
- constructor(opts) {
11
- this.key = opts.key;
12
- this.name = opts.name;
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.Generator = Generator;
14
+ exports.makeGenerator = makeGenerator;
@@ -12,3 +12,4 @@ export declare function getPaths(opts: {
12
12
  absNodeModulesPath: string;
13
13
  absOutputPath: string;
14
14
  };
15
+ export declare type Paths = ReturnType<typeof getPaths>;
@@ -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 { IGeneratorOpts } from './generator';
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: Omit<IGeneratorOpts, 'plugin'>): void;
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,7 +49,9 @@ 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
56
  this.service.commands[name] = new command_1.Command({
54
57
  ...commandOpts,
@@ -67,7 +70,7 @@ class PluginAPI {
67
70
  var _a;
68
71
  const { key } = opts;
69
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}.`);
70
- this.service.generators[key] = new generator_1.Generator({
73
+ this.service.generators[key] = (0, generator_1.makeGenerator)({
71
74
  ...opts,
72
75
  plugin: this.plugin,
73
76
  });
@@ -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;
@@ -80,10 +81,9 @@ export declare class Service {
80
81
  name: string;
81
82
  args?: any;
82
83
  }): Promise<void>;
83
- generateFinalConfig(): Promise<{
84
+ resolveConfig(): Promise<{
84
85
  config: any;
85
86
  defaultConfig: any;
86
- finalConfig: Record<string, any>;
87
87
  }>;
88
88
  _baconPlugins(): void;
89
89
  initPreset(opts: {
@@ -71,7 +71,7 @@ class Service {
71
71
  continue;
72
72
  tAdd.tapPromise({
73
73
  name: hook.plugin.key,
74
- stage: hook.stage,
74
+ stage: hook.stage || 0,
75
75
  before: hook.before,
76
76
  }, async (memo) => {
77
77
  var _a, _b;
@@ -90,7 +90,7 @@ class Service {
90
90
  continue;
91
91
  tModify.tapPromise({
92
92
  name: hook.plugin.key,
93
- stage: hook.stage,
93
+ stage: hook.stage || 0,
94
94
  before: hook.before,
95
95
  }, async (memo) => {
96
96
  var _a, _b;
@@ -216,6 +216,8 @@ class Service {
216
216
  while (plugins.length) {
217
217
  await this.initPlugin({ plugin: plugins.shift(), plugins });
218
218
  }
219
+ const command = this.commands[name];
220
+ (0, assert_1.default)(command, `Invalid command ${name}, it's not registered.`);
219
221
  // collect configSchemas and configDefaults
220
222
  for (const id of Object.keys(this.plugins)) {
221
223
  const { config, key } = this.plugins[id];
@@ -228,7 +230,7 @@ class Service {
228
230
  }
229
231
  // setup api.config from modifyConfig and modifyDefaultConfig
230
232
  this.stage = types_1.ServiceStage.resolveConfig;
231
- const { config, defaultConfig } = await this.generateFinalConfig();
233
+ const { config, defaultConfig } = await this.resolveConfig();
232
234
  if (this.config.outputPath) {
233
235
  paths.absOutputPath = (0, path_1.isAbsolute)(this.config.outputPath)
234
236
  ? this.config.outputPath
@@ -282,34 +284,32 @@ class Service {
282
284
  });
283
285
  // run command
284
286
  this.stage = types_1.ServiceStage.runCommand;
285
- const command = this.commands[name];
286
- (0, assert_1.default)(command, `Invalid command ${name}, it's not registered.`);
287
287
  let ret = await command.fn({ args });
288
288
  this._baconPlugins();
289
289
  return ret;
290
290
  }
291
- // generate config and defaultConfig, and assign values to this.config
292
- async generateFinalConfig() {
293
- var _a;
291
+ async resolveConfig() {
294
292
  // configManager and paths are not available until the init stage
295
293
  (0, assert_1.default)(this.stage > types_1.ServiceStage.init, `Can't generate final config before init stage`);
294
+ const resolveMode = this.commands[this.name].configResolveMode;
296
295
  const config = await this.applyPlugins({
297
296
  key: 'modifyConfig',
298
297
  // why clone deep?
299
298
  // user may change the config in modifyConfig
300
299
  // e.g. memo.alias = xxx
301
- initialValue: utils_1.lodash.cloneDeep((_a = this.configManager) === null || _a === void 0 ? void 0 : _a.getConfig({
302
- schemas: this.configSchemas,
303
- }).config),
300
+ initialValue: utils_1.lodash.cloneDeep(resolveMode === 'strict'
301
+ ? this.configManager.getConfig({
302
+ schemas: this.configSchemas,
303
+ }).config
304
+ : this.configManager.getUserConfig().config),
304
305
  args: { paths: this.paths },
305
306
  });
306
307
  const defaultConfig = await this.applyPlugins({
307
308
  key: 'modifyDefaultConfig',
308
309
  initialValue: this.configDefaults,
309
310
  });
310
- const finalConfig = utils_1.lodash.merge(defaultConfig, config);
311
- this.config = finalConfig;
312
- return { config, defaultConfig, finalConfig };
311
+ this.config = utils_1.lodash.merge(defaultConfig, config);
312
+ return { config, defaultConfig };
313
313
  }
314
314
  _baconPlugins() {
315
315
  // TODO: prettier
package/dist/types.d.ts CHANGED
@@ -108,3 +108,4 @@ export interface IAdd<T, U> {
108
108
  };
109
109
  }): void;
110
110
  }
111
+ export declare type IFrameworkType = 'vue' | 'react';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/core",
3
- "version": "4.0.0-rc.15",
3
+ "version": "4.0.0-rc.16",
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": {
@@ -21,8 +21,8 @@
21
21
  "test": "umi-scripts jest-turbo"
22
22
  },
23
23
  "dependencies": {
24
- "@umijs/bundler-utils": "4.0.0-rc.15",
25
- "@umijs/utils": "4.0.0-rc.15"
24
+ "@umijs/bundler-utils": "4.0.0-rc.16",
25
+ "@umijs/utils": "4.0.0-rc.16"
26
26
  },
27
27
  "devDependencies": {
28
28
  "@hapi/joi": "17.1.1",