@umijs/core 4.0.0-beta.13 → 4.0.0-beta.17

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.
@@ -114,7 +114,6 @@ class Config {
114
114
  let config = {};
115
115
  let files = [];
116
116
  for (const configFile of opts.configFiles) {
117
- files.push(configFile);
118
117
  if ((0, fs_1.existsSync)(configFile)) {
119
118
  utils_1.register.register({
120
119
  implementor: esbuild_1.default,
@@ -124,9 +123,13 @@ class Config {
124
123
  for (const file of utils_1.register.getFiles()) {
125
124
  delete require.cache[file];
126
125
  }
126
+ // includes the config File
127
127
  files.push(...utils_1.register.getFiles());
128
128
  utils_1.register.restore();
129
129
  }
130
+ else {
131
+ files.push(configFile);
132
+ }
130
133
  }
131
134
  return {
132
135
  config,
@@ -4,4 +4,5 @@ export declare function addParentRoute(opts: {
4
4
  addToAll?: boolean;
5
5
  target: IRoute;
6
6
  routes: Record<string, IRoute>;
7
+ test?: Function;
7
8
  }): void;
@@ -4,13 +4,16 @@ exports.addParentRoute = void 0;
4
4
  function addParentRoute(opts) {
5
5
  if (opts.addToAll) {
6
6
  for (const id of Object.keys(opts.routes)) {
7
- if (opts.routes[id].parentId === undefined) {
7
+ if (opts.routes[id].parentId === undefined &&
8
+ (!opts.test || opts.test(opts.routes[id]))) {
8
9
  opts.routes[id].parentId = opts.target.id;
9
10
  }
10
11
  }
11
12
  }
12
13
  else if (opts.id) {
13
- opts.routes[opts.id].parentId = opts.target.id;
14
+ if (!opts.test || opts.test(opts.routes[opts.id])) {
15
+ opts.routes[opts.id].parentId = opts.target.id;
16
+ }
14
17
  }
15
18
  else {
16
19
  throw new Error(`addParentRoute failed, opts.addToAll or opts.id must be supplied.`);
@@ -1,4 +1,5 @@
1
1
  export declare function getConventionRoutes(opts: {
2
2
  base: string;
3
3
  prefix?: string;
4
+ exclude?: RegExp[];
4
5
  }): any;
@@ -18,12 +18,9 @@ function getConventionRoutes(opts) {
18
18
  dir: opts.base,
19
19
  visitor: (file) => {
20
20
  const routeId = (0, utils_2.createRouteId)(file);
21
- if ((0, utils_2.isRouteModuleFile)({ file })) {
21
+ if ((0, utils_2.isRouteModuleFile)({ file: (0, utils_1.winPath)(file), exclude: opts.exclude })) {
22
22
  files[routeId] = (0, utils_1.winPath)(file);
23
23
  }
24
- else {
25
- throw new Error(`Invalid route module file: ${(0, path_1.join)(opts.base, file)}`);
26
- }
27
24
  },
28
25
  });
29
26
  const routeIds = Object.keys(files).sort(utils_2.byLongestFirst);
@@ -52,13 +49,13 @@ function visitFiles(opts) {
52
49
  visitFiles(Object.assign(Object.assign({}, opts), { dir: file }));
53
50
  }
54
51
  else if (stat.isFile() &&
55
- ['.tsx', '.ts', '.js', '.jsx'].includes((0, path_1.extname)(file))) {
52
+ ['.tsx', '.ts', '.js', '.jsx', '.md', '.mdx'].includes((0, path_1.extname)(file))) {
56
53
  opts.visitor((0, path_1.relative)(opts.baseDir, file));
57
54
  }
58
55
  }
59
56
  }
60
57
  function createRoutePath(routeId) {
61
- const path = routeId
58
+ let path = routeId
62
59
  // routes/$ -> routes/*
63
60
  // routes/nested/$.tsx (with a "routes/nested.tsx" layout)
64
61
  .replace(/^\$$/, '*')
@@ -69,5 +66,7 @@ function createRoutePath(routeId) {
69
66
  .replace(/\$/g, ':')
70
67
  // routes/not.nested -> routes/not/nested
71
68
  .replace(/\./g, '/');
72
- return /\b\/?index$/.test(path) ? path.replace(/\/?index$/, '') : path;
69
+ path = /\b\/?index$/.test(path) ? path.replace(/\/?index$/, '') : path;
70
+ path = /\b\/?README$/.test(path) ? path.replace(/\/?README$/, '') : path;
71
+ return path;
73
72
  }
@@ -4,4 +4,5 @@ export declare function byLongestFirst(a: string, b: string): number;
4
4
  export declare function findParentRouteId(routeIds: string[], childRouteId: string): string | undefined;
5
5
  export declare function isRouteModuleFile(opts: {
6
6
  file: string;
7
+ exclude?: RegExp[];
7
8
  }): boolean;
@@ -21,6 +21,14 @@ function findParentRouteId(routeIds, childRouteId) {
21
21
  exports.findParentRouteId = findParentRouteId;
22
22
  const routeModuleExts = ['.js', '.jsx', '.ts', '.tsx', '.md', '.mdx'];
23
23
  function isRouteModuleFile(opts) {
24
+ // TODO: add cache strategy
25
+ for (const excludeRegExp of opts.exclude || []) {
26
+ if (opts.file &&
27
+ excludeRegExp instanceof RegExp &&
28
+ excludeRegExp.test(opts.file)) {
29
+ return false;
30
+ }
31
+ }
24
32
  return routeModuleExts.includes((0, path_1.extname)(opts.file));
25
33
  }
26
34
  exports.isRouteModuleFile = isRouteModuleFile;
@@ -21,6 +21,7 @@ export declare class Plugin {
21
21
  apply: Function;
22
22
  config: IPluginConfig;
23
23
  enableBy: EnableBy | ((opts: {
24
+ userConfig: any;
24
25
  config: any;
25
26
  env: Env;
26
27
  }) => boolean);
@@ -114,9 +114,9 @@ class Plugin {
114
114
  .split(',')
115
115
  .filter(Boolean),
116
116
  // dependencies
117
- ...Object.keys(opts.pkg.devDependencies || {})
118
- .concat(Object.keys(opts.pkg.dependencies || {}))
119
- .filter(Plugin.isPluginOrPreset.bind(null, type)),
117
+ // ...Object.keys(opts.pkg.devDependencies || {})
118
+ // .concat(Object.keys(opts.pkg.dependencies || {}))
119
+ // .filter(Plugin.isPluginOrPreset.bind(null, type)),
120
120
  // user config
121
121
  ...(opts.userConfig[types] || []),
122
122
  ].map((path) => {
@@ -18,7 +18,7 @@ export declare class PluginAPI {
18
18
  key?: string;
19
19
  config?: IPluginConfig;
20
20
  enableBy?: EnableBy | ((enableByOpts: {
21
- config: any;
21
+ userConfig: any;
22
22
  env: Env;
23
23
  }) => boolean);
24
24
  }): void;
@@ -16,7 +16,11 @@ interface IOpts {
16
16
  export declare class Service {
17
17
  private opts;
18
18
  appData: {
19
- deps?: Record<string, string>;
19
+ deps?: Record<string, {
20
+ version: string;
21
+ matches: string[];
22
+ subpaths: string[];
23
+ }>;
20
24
  [key: string]: any;
21
25
  };
22
26
  args: yParser.Arguments;
@@ -39,6 +43,7 @@ export declare class Service {
39
43
  absOutputPath?: string;
40
44
  };
41
45
  plugins: Record<string, Plugin>;
46
+ keyToPluginMap: Record<string, Plugin>;
42
47
  pluginMethods: Record<string, {
43
48
  plugin: Plugin;
44
49
  fn: Function;
@@ -47,7 +52,14 @@ export declare class Service {
47
52
  stage: ServiceStage;
48
53
  userConfig: Record<string, any>;
49
54
  configManager: Config | null;
50
- pkg: Record<string, string | Record<string, any>>;
55
+ pkg: {
56
+ name?: string;
57
+ version?: string;
58
+ dependencies?: Record<string, string>;
59
+ devDependencies?: Record<string, string>;
60
+ [key: string]: any;
61
+ };
62
+ pkgPath: string;
51
63
  constructor(opts: IOpts);
52
64
  applyPlugins<T>(opts: {
53
65
  key: string;
@@ -69,7 +81,7 @@ export declare class Service {
69
81
  presets?: Plugin[];
70
82
  plugins: Plugin[];
71
83
  }): Promise<any>;
72
- isPluginEnable(hook: Hook): boolean;
84
+ isPluginEnable(hook: Hook | string): boolean;
73
85
  }
74
86
  export interface IServicePluginAPI {
75
87
  appData: typeof Service.prototype.appData;
@@ -79,10 +91,12 @@ export interface IServicePluginAPI {
79
91
  cwd: typeof Service.prototype.cwd;
80
92
  generators: typeof Service.prototype.generators;
81
93
  pkg: typeof Service.prototype.pkg;
94
+ pkgPath: typeof Service.prototype.pkgPath;
82
95
  name: typeof Service.prototype.name;
83
96
  paths: Required<typeof Service.prototype.paths>;
84
97
  userConfig: typeof Service.prototype.userConfig;
85
98
  env: typeof Service.prototype.env;
99
+ isPluginEnable: typeof Service.prototype.isPluginEnable;
86
100
  onCheck: IEvent<null>;
87
101
  onStart: IEvent<null>;
88
102
  modifyAppData: IModify<typeof Service.prototype.appData, null>;
@@ -41,12 +41,14 @@ 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;
47
48
  this.userConfig = {};
48
49
  this.configManager = null;
49
50
  this.pkg = {};
51
+ this.pkgPath = '';
50
52
  this.cwd = opts.cwd;
51
53
  this.env = opts.env;
52
54
  this.opts = opts;
@@ -135,19 +137,23 @@ class Service {
135
137
  (0, env_1.loadEnv)({ cwd: this.cwd, envFile: '.env' });
136
138
  // get pkg from package.json
137
139
  let pkg = {};
140
+ let pkgPath = '';
138
141
  try {
139
142
  pkg = require((0, path_1.join)(this.cwd, 'package.json'));
143
+ pkgPath = (0, path_1.join)(this.cwd, 'package.json');
140
144
  }
141
145
  catch (_e) {
142
146
  // APP_ROOT
143
147
  if (this.cwd !== process.cwd()) {
144
148
  try {
145
149
  pkg = require((0, path_1.join)(process.cwd(), 'package.json'));
150
+ pkgPath = (0, path_1.join)(process.cwd(), 'package.json');
146
151
  }
147
152
  catch (_e) { }
148
153
  }
149
154
  }
150
155
  this.pkg = pkg;
156
+ this.pkgPath = pkgPath;
151
157
  // get user config
152
158
  const configManager = new config_1.Config({
153
159
  cwd: this.cwd,
@@ -181,6 +187,10 @@ class Service {
181
187
  while (plugins.length) {
182
188
  yield this.initPlugin({ plugin: plugins.shift(), plugins });
183
189
  }
190
+ // keyToPluginMap
191
+ for (const id of Object.keys(this.plugins)) {
192
+ this.keyToPluginMap[this.plugins[id].key] = this.plugins[id];
193
+ }
184
194
  // collect configSchemas and configDefaults
185
195
  for (const id of Object.keys(this.plugins)) {
186
196
  const { config, key } = this.plugins[id];
@@ -203,9 +213,12 @@ class Service {
203
213
  this.stage = types_1.ServiceStage.resolveConfig;
204
214
  const config = yield this.applyPlugins({
205
215
  key: 'modifyConfig',
206
- initialValue: configManager.getConfig({
216
+ // why clone deep?
217
+ // user may change the config in modifyConfig
218
+ // e.g. memo.alias = xxx
219
+ initialValue: utils_1.lodash.cloneDeep(configManager.getConfig({
207
220
  schemas: this.configSchemas,
208
- }).config,
221
+ }).config),
209
222
  args: { paths },
210
223
  });
211
224
  const defaultConfig = yield this.applyPlugins({
@@ -226,6 +239,7 @@ class Service {
226
239
  // base
227
240
  cwd: this.cwd,
228
241
  pkg,
242
+ pkgPath,
229
243
  plugins,
230
244
  presets,
231
245
  name,
@@ -299,10 +313,12 @@ class Service {
299
313
  'config',
300
314
  'cwd',
301
315
  'pkg',
316
+ 'pkgPath',
302
317
  'name',
303
318
  'paths',
304
319
  'userConfig',
305
320
  'env',
321
+ 'isPluginEnable',
306
322
  ],
307
323
  staticProps: {
308
324
  ApplyPluginsType: types_1.ApplyPluginsType,
@@ -337,21 +353,30 @@ class Service {
337
353
  });
338
354
  }
339
355
  isPluginEnable(hook) {
340
- const { id, key, enableBy } = hook.plugin;
356
+ let plugin;
357
+ if (hook.plugin) {
358
+ plugin = hook.plugin;
359
+ }
360
+ else {
361
+ plugin = this.keyToPluginMap[hook];
362
+ }
363
+ const { id, key, enableBy } = plugin;
341
364
  if (this.skipPluginIds.has(id))
342
365
  return false;
343
366
  if (this.userConfig[key] === false)
344
367
  return false;
345
- if (enableBy === types_1.EnableBy.config &&
346
- // this.config is not ready when modifyConfig or modifyDefaultConfig is called
347
- // fallback to this.userConfig
348
- !(key in
349
- (['modifyConfig', 'modifyDefaultConfig'].includes(hook.key)
350
- ? this.userConfig
351
- : this.config)))
368
+ if (this.config[key] === false)
352
369
  return false;
370
+ if (enableBy === types_1.EnableBy.config) {
371
+ // TODO: 提供单独的命令用于启用插件
372
+ return key in this.userConfig;
373
+ }
353
374
  if (typeof enableBy === 'function')
354
- return enableBy({ config: this.config, env: this.env });
375
+ return enableBy({
376
+ userConfig: this.userConfig,
377
+ config: this.config,
378
+ env: this.env,
379
+ });
355
380
  // EnableBy.register
356
381
  return true;
357
382
  }
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.13",
3
+ "version": "4.0.0-beta.17",
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.13",
24
- "@umijs/utils": "4.0.0-beta.13"
23
+ "@umijs/bundler-utils": "4.0.0-beta.17",
24
+ "@umijs/utils": "4.0.0-beta.17"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@hapi/joi": "17.1.1",