@umijs/core 4.0.0-beta.6 → 4.0.0-rc.1

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,7 +123,12 @@ 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
+ utils_1.register.restore();
129
+ }
130
+ else {
131
+ files.push(configFile);
128
132
  }
129
133
  }
130
134
  return {
@@ -1,2 +1,3 @@
1
+ export * from './routesConfig';
1
2
  export * from './routesConvention';
2
3
  export * from './routeUtils';
@@ -14,5 +14,6 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./routesConfig"), exports);
17
18
  __exportStar(require("./routesConvention"), exports);
18
19
  __exportStar(require("./routeUtils"), exports);
@@ -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.`);
@@ -0,0 +1,5 @@
1
+ interface IOpts {
2
+ routes: any[];
3
+ }
4
+ export declare function getConfigRoutes(opts: IOpts): any[];
5
+ export {};
@@ -1 +1,42 @@
1
1
  "use strict";
2
+ var __rest = (this && this.__rest) || function (s, e) {
3
+ var t = {};
4
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
5
+ t[p] = s[p];
6
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
7
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
8
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
9
+ t[p[i]] = s[p[i]];
10
+ }
11
+ return t;
12
+ };
13
+ var __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.getConfigRoutes = void 0;
18
+ const assert_1 = __importDefault(require("assert"));
19
+ function getConfigRoutes(opts) {
20
+ const memo = { ret: {}, id: 1 };
21
+ transformRoutes({ routes: opts.routes, parentId: undefined, memo });
22
+ return memo.ret;
23
+ }
24
+ exports.getConfigRoutes = getConfigRoutes;
25
+ function transformRoutes(opts) {
26
+ opts.routes.forEach((route) => {
27
+ transformRoute({ route, parentId: opts.parentId, memo: opts.memo });
28
+ });
29
+ }
30
+ function transformRoute(opts) {
31
+ (0, assert_1.default)(!opts.route.children, 'children is not allowed in route props, use routes instead.');
32
+ const id = String(opts.memo.id++);
33
+ const _a = opts.route, { routes, component } = _a, routeProps = __rest(_a, ["routes", "component"]);
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 });
35
+ if (opts.route.routes) {
36
+ transformRoutes({
37
+ routes: opts.route.routes,
38
+ parentId: id,
39
+ memo: opts.memo,
40
+ });
41
+ }
42
+ }
@@ -1,3 +1,5 @@
1
1
  export declare function getConventionRoutes(opts: {
2
2
  base: string;
3
+ prefix?: string;
4
+ exclude?: RegExp[];
3
5
  }): any;
@@ -1,11 +1,7 @@
1
1
  "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
2
  Object.defineProperty(exports, "__esModule", { value: true });
6
3
  exports.getConventionRoutes = void 0;
7
4
  const utils_1 = require("@umijs/utils");
8
- const assert_1 = __importDefault(require("assert"));
9
5
  const fs_1 = require("fs");
10
6
  const path_1 = require("path");
11
7
  const defineRoutes_1 = require("./defineRoutes");
@@ -13,17 +9,16 @@ const utils_2 = require("./utils");
13
9
  // opts.base: path of pages
14
10
  function getConventionRoutes(opts) {
15
11
  const files = {};
16
- (0, assert_1.default)((0, fs_1.existsSync)(opts.base) && (0, fs_1.statSync)(opts.base).isDirectory(), `Convention routes base not found.`);
12
+ if (!((0, fs_1.existsSync)(opts.base) && (0, fs_1.statSync)(opts.base).isDirectory())) {
13
+ return {};
14
+ }
17
15
  visitFiles({
18
16
  dir: opts.base,
19
17
  visitor: (file) => {
20
18
  const routeId = (0, utils_2.createRouteId)(file);
21
- if ((0, utils_2.isRouteModuleFile)({ file })) {
19
+ if ((0, utils_2.isRouteModuleFile)({ file: (0, utils_1.winPath)(file), exclude: opts.exclude })) {
22
20
  files[routeId] = (0, utils_1.winPath)(file);
23
21
  }
24
- else {
25
- throw new Error(`Invalid route module file: ${(0, path_1.join)(opts.base, file)}`);
26
- }
27
22
  },
28
23
  });
29
24
  const routeIds = Object.keys(files).sort(utils_2.byLongestFirst);
@@ -33,7 +28,7 @@ function getConventionRoutes(opts) {
33
28
  let routePath = createRoutePath(parentId ? routeId.slice(parentId.length + 1) : routeId);
34
29
  defineRoute({
35
30
  path: routePath,
36
- file: files[routeId],
31
+ file: `${opts.prefix || ''}${files[routeId]}`,
37
32
  children() {
38
33
  defineNestedRoutes(defineRoute, routeId);
39
34
  },
@@ -52,13 +47,13 @@ function visitFiles(opts) {
52
47
  visitFiles(Object.assign(Object.assign({}, opts), { dir: file }));
53
48
  }
54
49
  else if (stat.isFile() &&
55
- ['.tsx', '.ts', '.js', '.jsx'].includes((0, path_1.extname)(file))) {
50
+ ['.tsx', '.ts', '.js', '.jsx', '.md', '.mdx'].includes((0, path_1.extname)(file))) {
56
51
  opts.visitor((0, path_1.relative)(opts.baseDir, file));
57
52
  }
58
53
  }
59
54
  }
60
55
  function createRoutePath(routeId) {
61
- const path = routeId
56
+ let path = routeId
62
57
  // routes/$ -> routes/*
63
58
  // routes/nested/$.tsx (with a "routes/nested.tsx" layout)
64
59
  .replace(/^\$$/, '*')
@@ -69,5 +64,7 @@ function createRoutePath(routeId) {
69
64
  .replace(/\$/g, ':')
70
65
  // routes/not.nested -> routes/not/nested
71
66
  .replace(/\./g, '/');
72
- return /\b\/?index$/.test(path) ? path.replace(/\/?index$/, '') : path;
67
+ path = /\b\/?index$/.test(path) ? path.replace(/\/?index$/, '') : path;
68
+ path = /\b\/?README$/.test(path) ? path.replace(/\/?README$/, '') : path;
69
+ return path;
73
70
  }
@@ -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;
@@ -23,15 +23,15 @@ umi generate
23
23
  var _a;
24
24
  return __awaiter(this, void 0, void 0, function* () {
25
25
  const [type] = args._;
26
- const runGenerator = (generator) => {
27
- generator === null || generator === void 0 ? void 0 : generator.fn({
26
+ const runGenerator = (generator) => __awaiter(this, void 0, void 0, function* () {
27
+ yield (generator === null || generator === void 0 ? void 0 : generator.fn({
28
28
  api,
29
29
  args,
30
30
  generateFile: utils_1.generateFile,
31
31
  installDeps: utils_1.installDeps,
32
32
  updatePackageJSON: utils_1.updatePackageJSON,
33
- });
34
- };
33
+ }));
34
+ });
35
35
  if (type) {
36
36
  const generator = api.service.generators[type];
37
37
  if (!generator) {
@@ -46,7 +46,7 @@ umi generate
46
46
  throw new Error(`Generator ${type} is unable.The corresponding function has been turned on or is not available.`);
47
47
  }
48
48
  }
49
- runGenerator(generator);
49
+ yield runGenerator(generator);
50
50
  }
51
51
  else {
52
52
  const getEnableGenerators = (generators) => __awaiter(this, void 0, void 0, function* () {
@@ -83,7 +83,7 @@ umi generate
83
83
  message: 'Pick generator type',
84
84
  choices: questions,
85
85
  });
86
- runGenerator(api.service.generators[gType]);
86
+ yield runGenerator(api.service.generators[gType]);
87
87
  }
88
88
  });
89
89
  },
@@ -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;
@@ -20,12 +20,16 @@ export declare class Plugin {
20
20
  key: string;
21
21
  apply: Function;
22
22
  config: IPluginConfig;
23
- enableBy: EnableBy | (() => boolean);
23
+ enableBy: EnableBy | ((opts: {
24
+ userConfig: any;
25
+ config: any;
26
+ env: Env;
27
+ }) => boolean);
24
28
  constructor(opts: IOpts);
25
29
  merge(opts: {
26
30
  key?: string;
27
31
  config?: IPluginConfig;
28
- enableBy?: EnableBy | (() => boolean);
32
+ enableBy?: any;
29
33
  }): void;
30
34
  getId(opts: {
31
35
  pkg: any;
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.Plugin = void 0;
7
+ const esbuild_1 = __importDefault(require("@umijs/bundler-utils/compiled/esbuild"));
7
8
  const utils_1 = require("@umijs/utils");
8
9
  const assert_1 = __importDefault(require("assert"));
9
10
  const fs_1 = require("fs");
@@ -34,14 +35,23 @@ class Plugin {
34
35
  this.id = this.getId({ pkg, isPkgEntry, pkgJSONPath });
35
36
  this.key = this.getKey({ pkg, isPkgEntry });
36
37
  this.apply = () => {
38
+ utils_1.register.register({
39
+ implementor: esbuild_1.default,
40
+ });
41
+ utils_1.register.clearFiles();
42
+ let ret;
37
43
  try {
38
- const ret = require(this.path);
39
- // use the default member for es modules
40
- return ret.__esModule ? ret.default : ret;
44
+ ret = require(this.path);
41
45
  }
42
46
  catch (e) {
43
47
  throw new Error(`Register ${this.type} ${this.path} failed, since ${e.message}`);
44
48
  }
49
+ for (const file of utils_1.register.getFiles()) {
50
+ delete require.cache[file];
51
+ }
52
+ utils_1.register.restore();
53
+ // use the default member for es modules
54
+ return ret.__esModule ? ret.default : ret;
45
55
  };
46
56
  }
47
57
  merge(opts) {
@@ -104,9 +114,9 @@ class Plugin {
104
114
  .split(',')
105
115
  .filter(Boolean),
106
116
  // dependencies
107
- ...Object.keys(opts.pkg.devDependencies || {})
108
- .concat(Object.keys(opts.pkg.dependencies || {}))
109
- .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)),
110
120
  // user config
111
121
  ...(opts.userConfig[types] || []),
112
122
  ].map((path) => {
@@ -1,12 +1,15 @@
1
- import { EnableBy, IPluginConfig } from '../types';
1
+ import { logger } from '@umijs/utils';
2
+ import { EnableBy, Env, IPluginConfig } from '../types';
2
3
  import { IOpts as ICommandOpts } from './command';
3
4
  import { IGeneratorOpts } from './generator';
4
5
  import { IOpts as IHookOpts } from './hook';
5
6
  import { Plugin } from './plugin';
6
7
  import { Service } from './service';
8
+ declare type Logger = typeof logger;
7
9
  export declare class PluginAPI {
8
10
  service: Service;
9
11
  plugin: Plugin;
12
+ logger: Logger;
10
13
  constructor(opts: {
11
14
  service: Service;
12
15
  plugin: Plugin;
@@ -14,7 +17,10 @@ export declare class PluginAPI {
14
17
  describe(opts: {
15
18
  key?: string;
16
19
  config?: IPluginConfig;
17
- enableBy?: EnableBy | (() => boolean);
20
+ enableBy?: EnableBy | ((enableByOpts: {
21
+ userConfig: any;
22
+ env: Env;
23
+ }) => boolean);
18
24
  }): void;
19
25
  registerCommand(opts: Omit<ICommandOpts, 'plugin'> & {
20
26
  alias?: string | string[];
@@ -35,3 +41,4 @@ export declare class PluginAPI {
35
41
  staticProps: Record<string, any>;
36
42
  }): PluginAPI;
37
43
  }
44
+ export {};
@@ -16,8 +16,24 @@ class PluginAPI {
16
16
  constructor(opts) {
17
17
  this.service = opts.service;
18
18
  this.plugin = opts.plugin;
19
- // TODO
20
19
  // logger
20
+ const loggerKeys = [
21
+ 'wait',
22
+ 'error',
23
+ 'warn',
24
+ 'ready',
25
+ 'info',
26
+ 'event',
27
+ ];
28
+ // @ts-ignore
29
+ this.logger = loggerKeys.reduce((memo, key) => {
30
+ // @ts-ignore
31
+ memo[key] = (...message) => {
32
+ // @ts-ignore
33
+ utils_1.logger[key](utils_1.chalk.green(`[plugin: ${this.plugin.id}]`), ...message);
34
+ };
35
+ return memo;
36
+ }, {});
21
37
  }
22
38
  describe(opts) {
23
39
  this.plugin.merge(opts);
@@ -55,9 +71,12 @@ class PluginAPI {
55
71
  this.service.pluginMethods[opts.name] = {
56
72
  plugin: this.plugin,
57
73
  fn: opts.fn ||
58
- ((fn) => {
74
+ // 这里不能用 arrow function,this 需指向执行此方法的 PluginAPI
75
+ // 否则 pluginId 会不会,导致不能正确 skip plugin
76
+ function (fn) {
77
+ // @ts-ignore
59
78
  this.register(Object.assign({ key: opts.name }, (utils_1.lodash.isPlainObject(fn) ? fn : { fn })));
60
- }),
79
+ },
61
80
  };
62
81
  }
63
82
  registerPresets(source, presets) {
@@ -15,7 +15,14 @@ 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, {
20
+ version: string;
21
+ matches: string[];
22
+ subpaths: string[];
23
+ }>;
24
+ [key: string]: any;
25
+ };
19
26
  args: yParser.Arguments;
20
27
  commands: Record<string, Command>;
21
28
  generators: Record<string, Generator>;
@@ -36,6 +43,7 @@ export declare class Service {
36
43
  absOutputPath?: string;
37
44
  };
38
45
  plugins: Record<string, Plugin>;
46
+ keyToPluginMap: Record<string, Plugin>;
39
47
  pluginMethods: Record<string, {
40
48
  plugin: Plugin;
41
49
  fn: Function;
@@ -44,7 +52,14 @@ export declare class Service {
44
52
  stage: ServiceStage;
45
53
  userConfig: Record<string, any>;
46
54
  configManager: Config | null;
47
- 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;
48
63
  constructor(opts: IOpts);
49
64
  applyPlugins<T>(opts: {
50
65
  key: string;
@@ -66,7 +81,7 @@ export declare class Service {
66
81
  presets?: Plugin[];
67
82
  plugins: Plugin[];
68
83
  }): Promise<any>;
69
- isPluginEnable(hook: Hook): boolean;
84
+ isPluginEnable(hook: Hook | string): boolean;
70
85
  }
71
86
  export interface IServicePluginAPI {
72
87
  appData: typeof Service.prototype.appData;
@@ -76,13 +91,18 @@ export interface IServicePluginAPI {
76
91
  cwd: typeof Service.prototype.cwd;
77
92
  generators: typeof Service.prototype.generators;
78
93
  pkg: typeof Service.prototype.pkg;
94
+ pkgPath: typeof Service.prototype.pkgPath;
79
95
  name: typeof Service.prototype.name;
80
96
  paths: Required<typeof Service.prototype.paths>;
81
97
  userConfig: typeof Service.prototype.userConfig;
98
+ env: typeof Service.prototype.env;
99
+ isPluginEnable: typeof Service.prototype.isPluginEnable;
82
100
  onCheck: IEvent<null>;
83
101
  onStart: IEvent<null>;
84
102
  modifyAppData: IModify<typeof Service.prototype.appData, null>;
85
- modifyConfig: IModify<typeof Service.prototype.config, null>;
103
+ modifyConfig: IModify<typeof Service.prototype.config, {
104
+ paths: Record<string, string>;
105
+ }>;
86
106
  modifyDefaultConfig: IModify<typeof Service.prototype.config, null>;
87
107
  modifyPaths: IModify<typeof Service.prototype.paths, null>;
88
108
  ApplyPluginsType: typeof ApplyPluginsType;
@@ -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];
@@ -192,26 +202,30 @@ class Service {
192
202
  this.configOnChanges[key] = config.onChange || types_1.ConfigChangeType.reload;
193
203
  }
194
204
  // setup api.config from modifyConfig and modifyDefaultConfig
205
+ const paths = (0, path_2.getPaths)({
206
+ cwd: this.cwd,
207
+ env: this.env,
208
+ prefix: this.opts.frameworkName || constants_1.DEFAULT_FRAMEWORK_NAME,
209
+ });
210
+ if (this.config.outputPath) {
211
+ paths.absOutputPath = (0, path_1.join)(this.cwd, this.config.outputPath);
212
+ }
195
213
  this.stage = types_1.ServiceStage.resolveConfig;
196
214
  const config = yield this.applyPlugins({
197
215
  key: 'modifyConfig',
198
- 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({
199
220
  schemas: this.configSchemas,
200
- }).config,
221
+ }).config),
222
+ args: { paths },
201
223
  });
202
224
  const defaultConfig = yield this.applyPlugins({
203
225
  key: 'modifyDefaultConfig',
204
226
  initialValue: this.configDefaults,
205
227
  });
206
228
  this.config = utils_1.lodash.merge(defaultConfig, config);
207
- const paths = (0, path_2.getPaths)({
208
- cwd: this.cwd,
209
- env: this.env,
210
- prefix: this.opts.frameworkName || constants_1.DEFAULT_FRAMEWORK_NAME,
211
- });
212
- if (this.config.outputPath) {
213
- paths.absOutputPath = (0, path_1.join)(this.cwd, this.config.outputPath);
214
- }
215
229
  this.paths = yield this.applyPlugins({
216
230
  key: 'modifyPaths',
217
231
  initialValue: paths,
@@ -225,6 +239,7 @@ class Service {
225
239
  // base
226
240
  cwd: this.cwd,
227
241
  pkg,
242
+ pkgPath,
228
243
  plugins,
229
244
  presets,
230
245
  name,
@@ -298,9 +313,12 @@ class Service {
298
313
  'config',
299
314
  'cwd',
300
315
  'pkg',
316
+ 'pkgPath',
301
317
  'name',
302
318
  'paths',
303
319
  'userConfig',
320
+ 'env',
321
+ 'isPluginEnable',
304
322
  ],
305
323
  staticProps: {
306
324
  ApplyPluginsType: types_1.ApplyPluginsType,
@@ -314,6 +332,9 @@ class Service {
314
332
  if ((0, utils_2.isPromise)(ret)) {
315
333
  ret = yield ret;
316
334
  }
335
+ if (opts.plugin.type === 'plugin') {
336
+ (0, assert_1.default)(!ret, `plugin should return nothing`);
337
+ }
317
338
  if (ret === null || ret === void 0 ? void 0 : ret.presets) {
318
339
  ret.presets = ret.presets.map((preset) => new plugin_1.Plugin({
319
340
  path: preset,
@@ -332,15 +353,30 @@ class Service {
332
353
  });
333
354
  }
334
355
  isPluginEnable(hook) {
335
- 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;
336
364
  if (this.skipPluginIds.has(id))
337
365
  return false;
338
366
  if (this.userConfig[key] === false)
339
367
  return false;
340
- if (enableBy === types_1.EnableBy.config && !(key in this.config))
368
+ if (this.config[key] === false)
341
369
  return false;
370
+ if (enableBy === types_1.EnableBy.config) {
371
+ // TODO: 提供单独的命令用于启用插件
372
+ return key in this.userConfig;
373
+ }
342
374
  if (typeof enableBy === 'function')
343
- return enableBy();
375
+ return enableBy({
376
+ userConfig: this.userConfig,
377
+ config: this.config,
378
+ env: this.env,
379
+ });
344
380
  // EnableBy.register
345
381
  return true;
346
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,13 @@
1
1
  {
2
2
  "name": "@umijs/core",
3
- "version": "4.0.0-beta.6",
3
+ "version": "4.0.0-rc.1",
4
+ "homepage": "https://github.com/umijs/umi-next/tree/master/packages/core#readme",
5
+ "bugs": "https://github.com/umijs/umi-next/issues",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "https://github.com/umijs/umi-next"
9
+ },
10
+ "license": "MIT",
4
11
  "main": "dist/index.js",
5
12
  "types": "dist/index.d.ts",
6
13
  "files": [
@@ -12,22 +19,9 @@
12
19
  "build:deps": "pnpm esno ../../scripts/bundleDeps.ts",
13
20
  "dev": "pnpm build -- --watch"
14
21
  },
15
- "repository": {
16
- "type": "git",
17
- "url": "https://github.com/umijs/umi-next"
18
- },
19
- "authors": [
20
- "chencheng <sorrycc@gmail.com> (https://github.com/sorrycc)"
21
- ],
22
- "license": "MIT",
23
- "bugs": "https://github.com/umijs/umi-next/issues",
24
- "homepage": "https://github.com/umijs/umi-next/tree/master/packages/core#readme",
25
- "publishConfig": {
26
- "access": "public"
27
- },
28
22
  "dependencies": {
29
- "@umijs/bundler-utils": "4.0.0-beta.6",
30
- "@umijs/utils": "4.0.0-beta.6"
23
+ "@umijs/bundler-utils": "4.0.0-rc.1",
24
+ "@umijs/utils": "4.0.0-rc.1"
31
25
  },
32
26
  "devDependencies": {
33
27
  "@hapi/joi": "17.1.1",
@@ -36,6 +30,12 @@
36
30
  "just-diff": "3.1.1",
37
31
  "tapable": "2.2.1"
38
32
  },
33
+ "publishConfig": {
34
+ "access": "public"
35
+ },
36
+ "authors": [
37
+ "chencheng <sorrycc@gmail.com> (https://github.com/sorrycc)"
38
+ ],
39
39
  "compiledConfig": {
40
40
  "deps": [
41
41
  "@hapi/joi",