@umijs/core 4.0.0-rc.6 → 4.0.0-rc.9

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) {
@@ -50,9 +48,9 @@ umi generate
50
48
  }
51
49
  else {
52
50
  const getEnableGenerators = (generators) => __awaiter(this, void 0, void 0, function* () {
51
+ var _b, _c;
53
52
  const questions = [];
54
- Object.keys(generators).forEach((key) => __awaiter(this, void 0, void 0, function* () {
55
- var _b, _c;
53
+ for (const key of Object.keys(generators)) {
56
54
  if (generators[key].type === generator_1.GeneratorType.generate) {
57
55
  questions.push({
58
56
  title: `${generators[key].name} -- ${generators[key].description}` ||
@@ -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) {
@@ -73,7 +70,7 @@ umi generate
73
70
  });
74
71
  }
75
72
  }
76
- }));
73
+ }
77
74
  return questions;
78
75
  });
79
76
  const questions = yield getEnableGenerators(api.service.generators);
@@ -1,7 +1,5 @@
1
- import { prompts } from '@umijs/utils';
1
+ import { generateFile } 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,21 +12,12 @@ 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
- generateFile: {
25
- (opts: {
26
- path: string;
27
- target: string;
28
- data?: any;
29
- questions?: prompts.PromptObject[];
30
- }): void;
31
- };
20
+ generateFile: typeof generateFile;
32
21
  updatePackageJSON: {
33
22
  (opts: {
34
23
  opts: object;
@@ -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);
@@ -37,6 +38,7 @@ class Plugin {
37
38
  this.apply = () => {
38
39
  utils_1.register.register({
39
40
  implementor: esbuild_1.default,
41
+ exts: ['.ts', '.mjs'],
40
42
  });
41
43
  utils_1.register.clearFiles();
42
44
  let ret;
@@ -46,10 +48,9 @@ class Plugin {
46
48
  catch (e) {
47
49
  throw new Error(`Register ${this.type} ${this.path} failed, since ${e.message}`);
48
50
  }
49
- for (const file of utils_1.register.getFiles()) {
50
- delete require.cache[file];
51
+ finally {
52
+ utils_1.register.restore();
51
53
  }
52
- utils_1.register.restore();
53
54
  // use the default member for es modules
54
55
  return ret.__esModule ? ret.default : ret;
55
56
  };
@@ -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 {
@@ -73,6 +73,7 @@ export declare class Service {
73
73
  name: string;
74
74
  args?: any;
75
75
  }): Promise<void>;
76
+ _baconPlugins(): void;
76
77
  initPreset(opts: {
77
78
  preset: Plugin;
78
79
  presets: Plugin[];
@@ -25,7 +25,6 @@ const env_1 = require("./env");
25
25
  const path_2 = require("./path");
26
26
  const plugin_1 = require("./plugin");
27
27
  const pluginAPI_1 = require("./pluginAPI");
28
- const utils_2 = require("./utils");
29
28
  class Service {
30
29
  constructor(opts) {
31
30
  this.appData = {};
@@ -85,7 +84,11 @@ class Service {
85
84
  stage: hook.stage,
86
85
  before: hook.before,
87
86
  }, (memo) => __awaiter(this, void 0, void 0, function* () {
87
+ var _a, _b;
88
+ const dateStart = new Date();
88
89
  const items = yield hook.fn(opts.args);
90
+ (_a = hook.plugin.time.hooks)[_b = opts.key] || (_a[_b] = []);
91
+ hook.plugin.time.hooks[opts.key].push(new Date().getTime() - dateStart.getTime());
89
92
  return memo.concat(items);
90
93
  }));
91
94
  }
@@ -100,7 +103,12 @@ class Service {
100
103
  stage: hook.stage,
101
104
  before: hook.before,
102
105
  }, (memo) => __awaiter(this, void 0, void 0, function* () {
103
- return yield hook.fn(memo, opts.args);
106
+ var _c, _d;
107
+ const dateStart = new Date();
108
+ const ret = yield hook.fn(memo, opts.args);
109
+ (_c = hook.plugin.time.hooks)[_d = opts.key] || (_c[_d] = []);
110
+ hook.plugin.time.hooks[opts.key].push(new Date().getTime() - dateStart.getTime());
111
+ return ret;
104
112
  }));
105
113
  }
106
114
  return (yield tModify.promise(opts.initialValue));
@@ -114,7 +122,11 @@ class Service {
114
122
  stage: hook.stage || 0,
115
123
  before: hook.before,
116
124
  }, () => __awaiter(this, void 0, void 0, function* () {
125
+ var _f, _g;
126
+ const dateStart = new Date();
117
127
  yield hook.fn(opts.args);
128
+ (_f = hook.plugin.time.hooks)[_g = opts.key] || (_f[_g] = []);
129
+ hook.plugin.time.hooks[opts.key].push(new Date().getTime() - dateStart.getTime());
118
130
  }));
119
131
  }
120
132
  return (yield tEvent.promise(1));
@@ -153,12 +165,14 @@ class Service {
153
165
  }
154
166
  }
155
167
  this.pkg = pkg;
156
- this.pkgPath = pkgPath;
168
+ this.pkgPath = pkgPath || (0, path_1.join)(this.cwd, 'package.json');
169
+ const prefix = this.opts.frameworkName || constants_1.DEFAULT_FRAMEWORK_NAME;
157
170
  // get user config
158
171
  const configManager = new config_1.Config({
159
172
  cwd: this.cwd,
160
173
  env: this.env,
161
174
  defaultConfigFiles: this.opts.defaultConfigFiles,
175
+ specifiedEnv: process.env[`${prefix}_ENV`.toUpperCase()],
162
176
  });
163
177
  this.configManager = configManager;
164
178
  this.userConfig = configManager.getUserConfig().config;
@@ -170,7 +184,7 @@ class Service {
170
184
  plugins: [require.resolve('./generatePlugin')].concat(this.opts.plugins || []),
171
185
  presets: [require.resolve('./servicePlugin')].concat(this.opts.presets || []),
172
186
  userConfig: this.userConfig,
173
- prefix: this.opts.frameworkName || constants_1.DEFAULT_FRAMEWORK_NAME,
187
+ prefix,
174
188
  });
175
189
  // register presets and plugins
176
190
  this.stage = types_1.ServiceStage.initPresets;
@@ -272,9 +286,21 @@ class Service {
272
286
  this.stage = types_1.ServiceStage.runCommand;
273
287
  const command = this.commands[name];
274
288
  (0, assert_1.default)(command, `Invalid command ${name}, it's not registered.`);
275
- return command.fn({ args });
289
+ let ret = yield command.fn({ args });
290
+ this._baconPlugins();
291
+ return ret;
276
292
  });
277
293
  }
294
+ _baconPlugins() {
295
+ // TODO: prettier
296
+ if (this.args.baconPlugins) {
297
+ console.log();
298
+ for (const id of Object.keys(this.plugins)) {
299
+ const plugin = this.plugins[id];
300
+ console.log(utils_1.chalk.green('plugin'), plugin.id, plugin.time);
301
+ }
302
+ }
303
+ }
278
304
  initPreset(opts) {
279
305
  return __awaiter(this, void 0, void 0, function* () {
280
306
  const { presets, plugins } = yield this.initPlugin({
@@ -324,10 +350,9 @@ class Service {
324
350
  service: this,
325
351
  },
326
352
  });
327
- let ret = opts.plugin.apply()(proxyPluginAPI);
328
- if ((0, utils_2.isPromise)(ret)) {
329
- ret = yield ret;
330
- }
353
+ let dateStart = new Date();
354
+ let ret = yield opts.plugin.apply()(proxyPluginAPI);
355
+ opts.plugin.time.register = new Date().getTime() - dateStart.getTime();
331
356
  if (opts.plugin.type === 'plugin') {
332
357
  (0, assert_1.default)(!ret, `plugin should return nothing`);
333
358
  }
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.6",
3
+ "version": "4.0.0-rc.9",
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.6",
24
- "@umijs/utils": "4.0.0-rc.6"
24
+ "@umijs/bundler-utils": "4.0.0-rc.9",
25
+ "@umijs/utils": "4.0.0-rc.9"
25
26
  },
26
27
  "devDependencies": {
27
28
  "@hapi/joi": "17.1.1",