@umijs/core 4.0.0-rc.5 → 4.0.0-rc.8

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.
@@ -7,13 +7,16 @@ function defineRoutes(callback) {
7
7
  const parentRoutes = [];
8
8
  const defineRoute = (opts) => {
9
9
  opts.options = opts.options || {};
10
+ const parentRoute = parentRoutes.length > 0 ? parentRoutes[parentRoutes.length - 1] : null;
11
+ const parentId = parentRoute === null || parentRoute === void 0 ? void 0 : parentRoute.id;
12
+ const parentAbsPath = parentRoute === null || parentRoute === void 0 ? void 0 : parentRoute.absPath;
13
+ const absPath = [parentAbsPath, opts.path].join('/');
10
14
  const route = {
11
15
  path: opts.path || '/',
12
16
  id: (0, utils_1.createRouteId)(opts.file),
13
- parentId: parentRoutes.length > 0
14
- ? parentRoutes[parentRoutes.length - 1].id
15
- : undefined,
17
+ parentId,
16
18
  file: opts.file,
19
+ absPath,
17
20
  };
18
21
  routes[route.id] = route;
19
22
  if (opts.children) {
@@ -31,7 +31,17 @@ function transformRoute(opts) {
31
31
  (0, assert_1.default)(!opts.route.children, 'children is not allowed in route props, use routes instead.');
32
32
  const id = String(opts.memo.id++);
33
33
  const _a = opts.route, { routes, component } = _a, routeProps = __rest(_a, ["routes", "component"]);
34
+ let absPath = opts.route.path;
35
+ if ((absPath === null || absPath === void 0 ? void 0 : absPath.charAt(0)) !== '/') {
36
+ const parentAbsPath = opts.parentId
37
+ ? opts.memo.ret[opts.parentId].absPath.replace(/\/*$/, '/') // to remove '/'s on the tail
38
+ : '/';
39
+ absPath = parentAbsPath + absPath;
40
+ }
34
41
  opts.memo.ret[id] = Object.assign(Object.assign(Object.assign(Object.assign({}, routeProps), { path: opts.route.path }), (component ? { file: component } : {})), { parentId: opts.parentId, id });
42
+ if (absPath) {
43
+ opts.memo.ret[id].absPath = absPath;
44
+ }
35
45
  if (opts.route.routes) {
36
46
  transformRoutes({
37
47
  routes: opts.route.routes,
@@ -25,7 +25,6 @@ umi generate
25
25
  const [type] = args._;
26
26
  const runGenerator = (generator) => __awaiter(this, void 0, void 0, function* () {
27
27
  yield (generator === null || generator === void 0 ? void 0 : generator.fn({
28
- api,
29
28
  args,
30
29
  generateFile: utils_1.generateFile,
31
30
  installDeps: utils_1.installDeps,
@@ -39,7 +38,6 @@ umi generate
39
38
  }
40
39
  if (generator.type === generator_1.GeneratorType.enable) {
41
40
  const enable = yield ((_a = generator.checkEnable) === null || _a === void 0 ? void 0 : _a.call(generator, {
42
- api,
43
41
  args,
44
42
  }));
45
43
  if (!enable) {
@@ -62,7 +60,6 @@ umi generate
62
60
  }
63
61
  else {
64
62
  const enable = yield ((_c = (_b = generators[key]) === null || _b === void 0 ? void 0 : _b.checkEnable) === null || _c === void 0 ? void 0 : _c.call(_b, {
65
- api,
66
63
  args,
67
64
  }));
68
65
  if (enable) {
@@ -1,7 +1,5 @@
1
1
  import { prompts } from '@umijs/utils';
2
2
  import { Plugin } from './plugin';
3
- import { PluginAPI } from './pluginAPI';
4
- import { IServicePluginAPI } from './service';
5
3
  export declare enum GeneratorType {
6
4
  generate = "generate",
7
5
  enable = "enable"
@@ -14,20 +12,18 @@ export interface IGeneratorOpts {
14
12
  checkEnable?: {
15
13
  (opts: {
16
14
  args: any;
17
- api: PluginAPI & IServicePluginAPI;
18
15
  }): boolean;
19
16
  };
20
17
  fn: {
21
18
  (opts: {
22
19
  args: any;
23
- api: PluginAPI & IServicePluginAPI;
24
20
  generateFile: {
25
21
  (opts: {
26
22
  path: string;
27
23
  target: string;
28
24
  data?: any;
29
25
  questions?: prompts.PromptObject[];
30
- }): void;
26
+ }): Promise<void>;
31
27
  };
32
28
  updatePackageJSON: {
33
29
  (opts: {
@@ -7,6 +7,7 @@ export declare function getPaths(opts: {
7
7
  cwd: string;
8
8
  absSrcPath: string;
9
9
  absPagesPath: string;
10
+ absApiRoutesPath: string;
10
11
  absTmpPath: string;
11
12
  absNodeModulesPath: string;
12
13
  absOutputPath: string;
@@ -13,6 +13,7 @@ function getPaths(opts) {
13
13
  const src = winJoin(cwd, 'src');
14
14
  const absSrcPath = (0, fs_1.existsSync)(src) && (0, fs_1.statSync)(src).isDirectory() ? src : cwd;
15
15
  const absPagesPath = winJoin(absSrcPath, 'pages');
16
+ const absApiRoutesPath = winJoin(absSrcPath, 'api');
16
17
  const tmp = opts.env === types_1.Env.development
17
18
  ? `.${opts.prefix}`
18
19
  : `.${opts.prefix}-${opts.env}`;
@@ -23,6 +24,7 @@ function getPaths(opts) {
23
24
  cwd,
24
25
  absSrcPath,
25
26
  absPagesPath,
27
+ absApiRoutesPath,
26
28
  absTmpPath,
27
29
  absNodeModulesPath,
28
30
  absOutputPath,
@@ -20,6 +20,10 @@ export declare class Plugin {
20
20
  key: string;
21
21
  apply: Function;
22
22
  config: IPluginConfig;
23
+ time: {
24
+ register?: number;
25
+ hooks: Record<string, number[]>;
26
+ };
23
27
  enableBy: EnableBy | ((opts: {
24
28
  userConfig: any;
25
29
  config: any;
@@ -17,6 +17,7 @@ const RE = {
17
17
  class Plugin {
18
18
  constructor(opts) {
19
19
  this.config = {};
20
+ this.time = { hooks: {} };
20
21
  this.enableBy = types_1.EnableBy.register;
21
22
  this.type = opts.type;
22
23
  this.path = (0, utils_1.winPath)(opts.path);
@@ -33,7 +33,7 @@ export declare class PluginAPI {
33
33
  }): void;
34
34
  registerPresets(source: Plugin[], presets: any[]): void;
35
35
  registerPlugins(source: Plugin[], plugins: any[]): void;
36
- skipPlugins(ids: string[]): void;
36
+ skipPlugins(keys: string[]): void;
37
37
  static proxyPluginAPI(opts: {
38
38
  pluginAPI: PluginAPI;
39
39
  service: Service;
@@ -36,6 +36,11 @@ class PluginAPI {
36
36
  }, {});
37
37
  }
38
38
  describe(opts) {
39
+ var _a;
40
+ // default 值 + 配置开启冲突,会导致就算用户没有配 key,插件也会生效
41
+ if (opts.enableBy === types_1.EnableBy.config && ((_a = opts.config) === null || _a === void 0 ? void 0 : _a.default)) {
42
+ throw new Error(`[plugin: ${this.plugin.id}] The config.default is not allowed when enableBy is EnableBy.config.`);
43
+ }
39
44
  this.plugin.merge(opts);
40
45
  }
41
46
  registerCommand(opts) {
@@ -63,6 +68,7 @@ class PluginAPI {
63
68
  }
64
69
  register(opts) {
65
70
  var _a, _b;
71
+ (0, assert_1.default)(this.service.stage <= types_1.ServiceStage.initPlugins, 'api.register() should not be called after plugin register stage.');
66
72
  (_a = this.service.hooks)[_b = opts.key] || (_a[_b] = []);
67
73
  this.service.hooks[opts.key].push(new hook_1.Hook(Object.assign(Object.assign({}, opts), { plugin: this.plugin })));
68
74
  }
@@ -85,7 +91,7 @@ class PluginAPI {
85
91
  return new plugin_1.Plugin({
86
92
  path: preset,
87
93
  cwd: this.service.cwd,
88
- type: types_1.PluginType.plugin,
94
+ type: types_1.PluginType.preset,
89
95
  });
90
96
  }));
91
97
  }
@@ -99,6 +105,7 @@ class PluginAPI {
99
105
  plugin.enableBy = plugin.enableBy || types_1.EnableBy.register;
100
106
  plugin.apply = plugin.apply || (() => () => { });
101
107
  plugin.config = plugin.config || {};
108
+ plugin.time = { hooks: {} };
102
109
  return plugin;
103
110
  }
104
111
  else {
@@ -116,9 +123,11 @@ class PluginAPI {
116
123
  source.splice(0, 0, ...mappedPlugins);
117
124
  }
118
125
  }
119
- skipPlugins(ids) {
120
- ids.forEach((id) => {
121
- this.service.skipPluginIds.add(id);
126
+ skipPlugins(keys) {
127
+ keys.forEach((key) => {
128
+ (0, assert_1.default)(!(this.plugin.key === key), `plugin ${key} can't skip itself!`);
129
+ (0, assert_1.default)(this.service.keyToPluginMap[key], `key: ${key} is not be registered by any plugin. You can't skip it!`);
130
+ this.service.skipPluginIds.add(this.service.keyToPluginMap[key].id);
122
131
  });
123
132
  }
124
133
  static proxyPluginAPI(opts) {
@@ -39,6 +39,7 @@ export declare class Service {
39
39
  cwd?: string;
40
40
  absSrcPath?: string;
41
41
  absPagesPath?: string;
42
+ absApiRoutesPath?: string;
42
43
  absTmpPath?: string;
43
44
  absNodeModulesPath?: string;
44
45
  absOutputPath?: string;
@@ -72,6 +73,7 @@ export declare class Service {
72
73
  name: string;
73
74
  args?: any;
74
75
  }): Promise<void>;
76
+ _baconPlugins(): void;
75
77
  initPreset(opts: {
76
78
  preset: Plugin;
77
79
  presets: Plugin[];
@@ -81,11 +81,15 @@ class Service {
81
81
  if (!this.isPluginEnable(hook))
82
82
  continue;
83
83
  tAdd.tapPromise({
84
- name: hook.plugin.id,
84
+ name: hook.plugin.key,
85
85
  stage: hook.stage,
86
86
  before: hook.before,
87
87
  }, (memo) => __awaiter(this, void 0, void 0, function* () {
88
+ var _a, _b;
89
+ const dateStart = new Date();
88
90
  const items = yield hook.fn(opts.args);
91
+ (_a = hook.plugin.time.hooks)[_b = opts.key] || (_a[_b] = []);
92
+ hook.plugin.time.hooks[opts.key].push(new Date().getTime() - dateStart.getTime());
89
93
  return memo.concat(items);
90
94
  }));
91
95
  }
@@ -96,11 +100,16 @@ class Service {
96
100
  if (!this.isPluginEnable(hook))
97
101
  continue;
98
102
  tModify.tapPromise({
99
- name: hook.plugin.id,
103
+ name: hook.plugin.key,
100
104
  stage: hook.stage,
101
105
  before: hook.before,
102
106
  }, (memo) => __awaiter(this, void 0, void 0, function* () {
103
- return yield hook.fn(memo, opts.args);
107
+ var _c, _d;
108
+ const dateStart = new Date();
109
+ const ret = yield hook.fn(memo, opts.args);
110
+ (_c = hook.plugin.time.hooks)[_d = opts.key] || (_c[_d] = []);
111
+ hook.plugin.time.hooks[opts.key].push(new Date().getTime() - dateStart.getTime());
112
+ return ret;
104
113
  }));
105
114
  }
106
115
  return (yield tModify.promise(opts.initialValue));
@@ -110,11 +119,15 @@ class Service {
110
119
  if (!this.isPluginEnable(hook))
111
120
  continue;
112
121
  tEvent.tapPromise({
113
- name: hook.plugin.id,
122
+ name: hook.plugin.key,
114
123
  stage: hook.stage || 0,
115
124
  before: hook.before,
116
125
  }, () => __awaiter(this, void 0, void 0, function* () {
126
+ var _f, _g;
127
+ const dateStart = new Date();
117
128
  yield hook.fn(opts.args);
129
+ (_f = hook.plugin.time.hooks)[_g = opts.key] || (_f[_g] = []);
130
+ hook.plugin.time.hooks[opts.key].push(new Date().getTime() - dateStart.getTime());
118
131
  }));
119
132
  }
120
133
  return (yield tEvent.promise(1));
@@ -153,7 +166,7 @@ class Service {
153
166
  }
154
167
  }
155
168
  this.pkg = pkg;
156
- this.pkgPath = pkgPath;
169
+ this.pkgPath = pkgPath || (0, path_1.join)(this.cwd, 'package.json');
157
170
  // get user config
158
171
  const configManager = new config_1.Config({
159
172
  cwd: this.cwd,
@@ -187,10 +200,6 @@ class Service {
187
200
  while (plugins.length) {
188
201
  yield this.initPlugin({ plugin: plugins.shift(), plugins });
189
202
  }
190
- // keyToPluginMap
191
- for (const id of Object.keys(this.plugins)) {
192
- this.keyToPluginMap[this.plugins[id].key] = this.plugins[id];
193
- }
194
203
  // collect configSchemas and configDefaults
195
204
  for (const id of Object.keys(this.plugins)) {
196
205
  const { config, key } = this.plugins[id];
@@ -276,9 +285,24 @@ class Service {
276
285
  this.stage = types_1.ServiceStage.runCommand;
277
286
  const command = this.commands[name];
278
287
  (0, assert_1.default)(command, `Invalid command ${name}, it's not registered.`);
279
- return command.fn({ args });
288
+ let ret = command.fn({ args });
289
+ if ((0, utils_2.isPromise)(ret)) {
290
+ ret = yield ret;
291
+ }
292
+ this._baconPlugins();
293
+ return ret;
280
294
  });
281
295
  }
296
+ _baconPlugins() {
297
+ // TODO: prettier
298
+ if (this.args.baconPlugins) {
299
+ console.log();
300
+ for (const id of Object.keys(this.plugins)) {
301
+ const plugin = this.plugins[id];
302
+ console.log(utils_1.chalk.green('plugin'), plugin.id, plugin.time);
303
+ }
304
+ }
305
+ }
282
306
  initPreset(opts) {
283
307
  return __awaiter(this, void 0, void 0, function* () {
284
308
  const { presets, plugins } = yield this.initPlugin({
@@ -291,7 +315,7 @@ class Service {
291
315
  });
292
316
  }
293
317
  initPlugin(opts) {
294
- var _a;
318
+ var _a, _b;
295
319
  return __awaiter(this, void 0, void 0, function* () {
296
320
  // register to this.plugins
297
321
  (0, assert_1.default)(!this.plugins[opts.plugin.id], `${opts.plugin.type} ${opts.plugin.id} is already registered by ${(_a = this.plugins[opts.plugin.id]) === null || _a === void 0 ? void 0 : _a.path}, ${opts.plugin.type} from ${opts.plugin.path} register failed.`);
@@ -328,13 +352,18 @@ class Service {
328
352
  service: this,
329
353
  },
330
354
  });
355
+ let dateStart = new Date();
331
356
  let ret = opts.plugin.apply()(proxyPluginAPI);
332
357
  if ((0, utils_2.isPromise)(ret)) {
333
358
  ret = yield ret;
334
359
  }
360
+ opts.plugin.time.register = new Date().getTime() - dateStart.getTime();
335
361
  if (opts.plugin.type === 'plugin') {
336
362
  (0, assert_1.default)(!ret, `plugin should return nothing`);
337
363
  }
364
+ // key should be unique
365
+ (0, assert_1.default)(!this.keyToPluginMap[opts.plugin.key], `key ${opts.plugin.key} is already registered by ${(_b = this.keyToPluginMap[opts.plugin.key]) === null || _b === void 0 ? void 0 : _b.path}, ${opts.plugin.type} from ${opts.plugin.path} register failed.`);
366
+ this.keyToPluginMap[opts.plugin.key] = opts.plugin;
338
367
  if (ret === null || ret === void 0 ? void 0 : ret.presets) {
339
368
  ret.presets = ret.presets.map((preset) => new plugin_1.Plugin({
340
369
  path: preset,
package/dist/types.d.ts CHANGED
@@ -41,6 +41,7 @@ export declare enum EnableBy {
41
41
  }
42
42
  export interface IRoute {
43
43
  path: string;
44
+ absPath: string;
44
45
  file: string;
45
46
  id: string;
46
47
  parentId?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umijs/core",
3
- "version": "4.0.0-rc.5",
3
+ "version": "4.0.0-rc.8",
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": {
@@ -17,11 +17,12 @@
17
17
  "scripts": {
18
18
  "build": "pnpm tsc",
19
19
  "build:deps": "pnpm esno ../../scripts/bundleDeps.ts",
20
- "dev": "pnpm build -- --watch"
20
+ "dev": "pnpm build -- --watch",
21
+ "test": "jest -c ../../jest.turbo.config.ts"
21
22
  },
22
23
  "dependencies": {
23
- "@umijs/bundler-utils": "4.0.0-rc.5",
24
- "@umijs/utils": "4.0.0-rc.5"
24
+ "@umijs/bundler-utils": "4.0.0-rc.8",
25
+ "@umijs/utils": "4.0.0-rc.8"
25
26
  },
26
27
  "devDependencies": {
27
28
  "@hapi/joi": "17.1.1",