@umijs/core 4.0.6 → 4.0.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.
@@ -1,148 +1,145 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
4
10
  };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Plugin = void 0;
7
- const esbuild_1 = __importDefault(require("@umijs/bundler-utils/compiled/esbuild"));
8
- const utils_1 = require("@umijs/utils");
9
- const assert_1 = __importDefault(require("assert"));
10
- const fs_1 = require("fs");
11
- const path_1 = require("path");
12
- const types_1 = require("../types");
13
- const RE = {
14
- plugin: /^(@umijs\/|umi-)plugin-/,
15
- preset: /^(@umijs\/|umi-)preset-/,
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
16
18
  };
17
- class Plugin {
18
- constructor(opts) {
19
- this.config = {};
20
- this.time = { hooks: {} };
21
- this.enableBy = types_1.EnableBy.register;
22
- this.type = opts.type;
23
- this.path = (0, utils_1.winPath)(opts.path);
24
- this.cwd = opts.cwd;
25
- (0, assert_1.default)((0, fs_1.existsSync)(this.path), `Invalid ${this.type} ${this.path}, it's not exists.`);
26
- let pkg = null;
27
- // path is the package entry
28
- let isPkgEntry = false;
29
- const pkgJSONPath = utils_1.pkgUp.pkgUpSync({ cwd: this.path });
30
- if (pkgJSONPath) {
31
- pkg = require(pkgJSONPath);
32
- isPkgEntry =
33
- (0, utils_1.winPath)((0, path_1.join)((0, path_1.dirname)(pkgJSONPath), pkg.main || 'index.js')) ===
34
- (0, utils_1.winPath)(this.path);
35
- }
36
- this.id = this.getId({ pkg, isPkgEntry, pkgJSONPath });
37
- this.key = this.getKey({ pkg, isPkgEntry });
38
- this.apply = () => {
39
- utils_1.register.register({
40
- implementor: esbuild_1.default,
41
- exts: ['.ts', '.mjs'],
42
- });
43
- utils_1.register.clearFiles();
44
- let ret;
45
- try {
46
- ret = require(this.path);
47
- }
48
- catch (e) {
49
- throw new Error(`Register ${this.type} ${this.path} failed, since ${e.message}`);
50
- }
51
- finally {
52
- utils_1.register.restore();
53
- }
54
- // use the default member for es modules
55
- return ret.__esModule ? ret.default : ret;
56
- };
57
- }
58
- merge(opts) {
59
- if (opts.key)
60
- this.key = opts.key;
61
- if (opts.config)
62
- this.config = opts.config;
63
- if (opts.enableBy)
64
- this.enableBy = opts.enableBy;
65
- }
66
- getId(opts) {
67
- let id;
68
- if (opts.isPkgEntry) {
69
- id = opts.pkg.name;
70
- }
71
- else if ((0, utils_1.winPath)(this.path).startsWith((0, utils_1.winPath)(this.cwd))) {
72
- id = `./${(0, utils_1.winPath)((0, path_1.relative)(this.cwd, this.path))}`;
73
- }
74
- else if (opts.pkgJSONPath) {
75
- id = (0, utils_1.winPath)((0, path_1.join)(opts.pkg.name, (0, path_1.relative)((0, path_1.dirname)(opts.pkgJSONPath), this.path)));
76
- }
77
- else {
78
- id = (0, utils_1.winPath)(this.path);
79
- }
80
- id = id.replace('@umijs/preset-umi/lib/plugins', '@@');
81
- id = id.replace(/\.js$/, '');
82
- return id;
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
20
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
21
+
22
+ // plugin.ts
23
+ var plugin_exports = {};
24
+ __export(plugin_exports, {
25
+ Plugin: () => Plugin
26
+ });
27
+ module.exports = __toCommonJS(plugin_exports);
28
+ var import_esbuild = __toESM(require("@umijs/bundler-utils/compiled/esbuild"));
29
+ var import_utils = require("@umijs/utils");
30
+ var import_assert = __toESM(require("assert"));
31
+ var import_fs = require("fs");
32
+ var import_path = require("path");
33
+ var import_types = require("../types");
34
+ var RE = {
35
+ plugin: /^(@umijs\/|umi-)plugin-/,
36
+ preset: /^(@umijs\/|umi-)preset-/
37
+ };
38
+ var Plugin = class {
39
+ constructor(opts) {
40
+ this.config = {};
41
+ this.time = { hooks: {} };
42
+ this.enableBy = import_types.EnableBy.register;
43
+ this.type = opts.type;
44
+ this.path = (0, import_utils.winPath)(opts.path);
45
+ this.cwd = opts.cwd;
46
+ (0, import_assert.default)((0, import_fs.existsSync)(this.path), `Invalid ${this.type} ${this.path}, it's not exists.`);
47
+ let pkg = null;
48
+ let isPkgEntry = false;
49
+ const pkgJSONPath = import_utils.pkgUp.pkgUpSync({ cwd: this.path });
50
+ if (pkgJSONPath) {
51
+ pkg = require(pkgJSONPath);
52
+ isPkgEntry = (0, import_utils.winPath)((0, import_path.join)((0, import_path.dirname)(pkgJSONPath), pkg.main || "index.js")) === (0, import_utils.winPath)(this.path);
83
53
  }
84
- getKey(opts) {
85
- // e.g.
86
- // initial-state -> initialState
87
- // webpack.css-loader -> webpack.cssLoader
88
- function nameToKey(name) {
89
- return name
90
- .split('.')
91
- .map((part) => utils_1.lodash.camelCase(part))
92
- .join('.');
93
- }
94
- return nameToKey(opts.isPkgEntry
95
- ? Plugin.stripNoneUmiScope(opts.pkg.name).replace(RE[this.type], '')
96
- : (0, path_1.basename)(this.path, (0, path_1.extname)(this.path)));
54
+ this.id = this.getId({ pkg, isPkgEntry, pkgJSONPath });
55
+ this.key = this.getKey({ pkg, isPkgEntry });
56
+ this.apply = () => {
57
+ import_utils.register.register({
58
+ implementor: import_esbuild.default,
59
+ exts: [".ts", ".mjs"]
60
+ });
61
+ import_utils.register.clearFiles();
62
+ let ret;
63
+ try {
64
+ ret = require(this.path);
65
+ } catch (e) {
66
+ throw new Error(`Register ${this.type} ${this.path} failed, since ${e.message}`);
67
+ } finally {
68
+ import_utils.register.restore();
69
+ }
70
+ return ret.__esModule ? ret.default : ret;
71
+ };
72
+ }
73
+ merge(opts) {
74
+ if (opts.key)
75
+ this.key = opts.key;
76
+ if (opts.config)
77
+ this.config = opts.config;
78
+ if (opts.enableBy)
79
+ this.enableBy = opts.enableBy;
80
+ }
81
+ getId(opts) {
82
+ let id;
83
+ if (opts.isPkgEntry) {
84
+ id = opts.pkg.name;
85
+ } else if ((0, import_utils.winPath)(this.path).startsWith((0, import_utils.winPath)(this.cwd))) {
86
+ id = `./${(0, import_utils.winPath)((0, import_path.relative)(this.cwd, this.path))}`;
87
+ } else if (opts.pkgJSONPath) {
88
+ id = (0, import_utils.winPath)((0, import_path.join)(opts.pkg.name, (0, import_path.relative)((0, import_path.dirname)(opts.pkgJSONPath), this.path)));
89
+ } else {
90
+ id = (0, import_utils.winPath)(this.path);
97
91
  }
98
- static isPluginOrPreset(type, name) {
99
- return RE[type].test(Plugin.stripNoneUmiScope(name));
92
+ id = id.replace("@umijs/preset-umi/lib/plugins", "@@");
93
+ id = id.replace(/\.js$/, "");
94
+ return id;
95
+ }
96
+ getKey(opts) {
97
+ function nameToKey(name) {
98
+ return name.split(".").map((part) => import_utils.lodash.camelCase(part)).join(".");
100
99
  }
101
- static stripNoneUmiScope(name) {
102
- if (name.charAt(0) === '@' && !name.startsWith('@umijs/')) {
103
- name = name.split('/')[1];
104
- }
105
- return name;
100
+ return nameToKey(opts.isPkgEntry ? Plugin.stripNoneUmiScope(opts.pkg.name).replace(RE[this.type], "") : (0, import_path.basename)(this.path, (0, import_path.extname)(this.path)));
101
+ }
102
+ static isPluginOrPreset(type, name) {
103
+ return RE[type].test(Plugin.stripNoneUmiScope(name));
104
+ }
105
+ static stripNoneUmiScope(name) {
106
+ if (name.charAt(0) === "@" && !name.startsWith("@umijs/")) {
107
+ name = name.split("/")[1];
106
108
  }
107
- static getPluginsAndPresets(opts) {
108
- function get(type) {
109
- const types = `${type}s`;
110
- return [
111
- // opts
112
- ...(opts[types] || []),
113
- // env
114
- ...(process.env[`${opts.prefix}_${types}`.toUpperCase()] || '')
115
- .split(',')
116
- .filter(Boolean),
117
- // dependencies
118
- // ...Object.keys(opts.pkg.devDependencies || {})
119
- // .concat(Object.keys(opts.pkg.dependencies || {}))
120
- // .filter(Plugin.isPluginOrPreset.bind(null, type)),
121
- // user config
122
- ...(opts.userConfig[types] || []),
123
- ].map((path) => {
124
- (0, assert_1.default)(typeof path === 'string', `Invalid plugin ${path}, it must be string.`);
125
- let resolved;
126
- try {
127
- resolved = utils_1.resolve.sync(path, {
128
- basedir: opts.cwd,
129
- extensions: ['.tsx', '.ts', '.mjs', '.jsx', '.js'],
130
- });
131
- }
132
- catch (_e) {
133
- throw new Error(`Invalid plugin ${path}, can not be resolved.`);
134
- }
135
- return new Plugin({
136
- path: resolved,
137
- type,
138
- cwd: opts.cwd,
139
- });
140
- });
109
+ return name;
110
+ }
111
+ static getPluginsAndPresets(opts) {
112
+ function get(type) {
113
+ const types = `${type}s`;
114
+ return [
115
+ ...opts[types] || [],
116
+ ...(process.env[`${opts.prefix}_${types}`.toUpperCase()] || "").split(",").filter(Boolean),
117
+ ...opts.userConfig[types] || []
118
+ ].map((path) => {
119
+ (0, import_assert.default)(typeof path === "string", `Invalid plugin ${path}, it must be string.`);
120
+ let resolved;
121
+ try {
122
+ resolved = import_utils.resolve.sync(path, {
123
+ basedir: opts.cwd,
124
+ extensions: [".tsx", ".ts", ".mjs", ".jsx", ".js"]
125
+ });
126
+ } catch (_e) {
127
+ throw new Error(`Invalid plugin ${path}, can not be resolved.`);
141
128
  }
142
- return {
143
- presets: get('preset'),
144
- plugins: get('plugin'),
145
- };
129
+ return new Plugin({
130
+ path: resolved,
131
+ type,
132
+ cwd: opts.cwd
133
+ });
134
+ });
146
135
  }
147
- }
148
- exports.Plugin = Plugin;
136
+ return {
137
+ presets: get("preset"),
138
+ plugins: get("plugin")
139
+ };
140
+ }
141
+ };
142
+ // Annotate the CommonJS export names for ESM import in node:
143
+ 0 && (module.exports = {
144
+ Plugin
145
+ });
@@ -1,168 +1,190 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __defProps = Object.defineProperties;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
6
+ var __getOwnPropNames = Object.getOwnPropertyNames;
7
+ var __getOwnPropSymbols = Object.getOwnPropertySymbols;
8
+ var __getProtoOf = Object.getPrototypeOf;
9
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
10
+ var __propIsEnum = Object.prototype.propertyIsEnumerable;
11
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
12
+ var __spreadValues = (a, b) => {
13
+ for (var prop in b || (b = {}))
14
+ if (__hasOwnProp.call(b, prop))
15
+ __defNormalProp(a, prop, b[prop]);
16
+ if (__getOwnPropSymbols)
17
+ for (var prop of __getOwnPropSymbols(b)) {
18
+ if (__propIsEnum.call(b, prop))
19
+ __defNormalProp(a, prop, b[prop]);
20
+ }
21
+ return a;
22
+ };
23
+ var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
24
+ var __export = (target, all) => {
25
+ for (var name in all)
26
+ __defProp(target, name, { get: all[name], enumerable: true });
27
+ };
28
+ var __copyProps = (to, from, except, desc) => {
29
+ if (from && typeof from === "object" || typeof from === "function") {
30
+ for (let key of __getOwnPropNames(from))
31
+ if (!__hasOwnProp.call(to, key) && key !== except)
32
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
33
+ }
34
+ return to;
4
35
  };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.PluginAPI = void 0;
7
- const utils_1 = require("@umijs/utils");
8
- const assert_1 = __importDefault(require("assert"));
9
- const types_1 = require("../types");
10
- const command_1 = require("./command");
11
- const generator_1 = require("./generator");
12
- const hook_1 = require("./hook");
13
- const plugin_1 = require("./plugin");
14
- const utils_2 = require("./utils");
15
- const resolveConfigModes = ['strict', 'loose'];
16
- class PluginAPI {
17
- constructor(opts) {
18
- this.service = opts.service;
19
- this.plugin = opts.plugin;
20
- // logger
21
- const loggerKeys = [
22
- 'wait',
23
- 'error',
24
- 'warn',
25
- 'ready',
26
- 'info',
27
- 'event',
28
- 'debug',
29
- ];
30
- // @ts-ignore
31
- this.logger = loggerKeys.reduce((memo, key) => {
32
- // @ts-ignore
33
- memo[key] = (...message) => {
34
- // @ts-ignore
35
- utils_1.logger[key](utils_1.chalk.green(`[plugin: ${this.plugin.id}]`), ...message);
36
- };
37
- return memo;
38
- }, {});
36
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target, mod));
37
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
38
+
39
+ // pluginAPI.ts
40
+ var pluginAPI_exports = {};
41
+ __export(pluginAPI_exports, {
42
+ PluginAPI: () => PluginAPI
43
+ });
44
+ module.exports = __toCommonJS(pluginAPI_exports);
45
+ var import_utils = require("@umijs/utils");
46
+ var import_assert = __toESM(require("assert"));
47
+ var import_types = require("../types");
48
+ var import_command = require("./command");
49
+ var import_generator = require("./generator");
50
+ var import_hook = require("./hook");
51
+ var import_plugin = require("./plugin");
52
+ var import_utils2 = require("./utils");
53
+ var resolveConfigModes = ["strict", "loose"];
54
+ var PluginAPI = class {
55
+ constructor(opts) {
56
+ this.service = opts.service;
57
+ this.plugin = opts.plugin;
58
+ const loggerKeys = [
59
+ "wait",
60
+ "error",
61
+ "warn",
62
+ "ready",
63
+ "info",
64
+ "event",
65
+ "debug"
66
+ ];
67
+ this.logger = loggerKeys.reduce((memo, key) => {
68
+ memo[key] = (...message) => {
69
+ import_utils.logger[key](import_utils.chalk.green(`[plugin: ${this.plugin.id}]`), ...message);
70
+ };
71
+ return memo;
72
+ }, {});
73
+ }
74
+ describe(opts) {
75
+ var _a;
76
+ if (opts.enableBy === import_types.EnableBy.config && ((_a = opts.config) == null ? void 0 : _a.default)) {
77
+ throw new Error(`[plugin: ${this.plugin.id}] The config.default is not allowed when enableBy is EnableBy.config.`);
39
78
  }
40
- describe(opts) {
41
- var _a;
42
- // default 值 + 配置开启冲突,会导致就算用户没有配 key,插件也会生效
43
- if (opts.enableBy === types_1.EnableBy.config && ((_a = opts.config) === null || _a === void 0 ? void 0 : _a.default)) {
44
- throw new Error(`[plugin: ${this.plugin.id}] The config.default is not allowed when enableBy is EnableBy.config.`);
45
- }
46
- this.plugin.merge(opts);
79
+ this.plugin.merge(opts);
80
+ }
81
+ registerCommand(opts) {
82
+ const { alias } = opts;
83
+ delete opts.alias;
84
+ const registerCommand = (commandOpts) => {
85
+ var _a;
86
+ const { name, configResolveMode } = commandOpts;
87
+ (0, import_assert.default)(!configResolveMode || resolveConfigModes.indexOf(configResolveMode) >= 0, `configResolveMode must be one of ${resolveConfigModes.join(",")}, but got ${configResolveMode}`);
88
+ (0, import_assert.default)(!this.service.commands[name], `api.registerCommand() failed, the command ${name} is exists from ${(_a = this.service.commands[name]) == null ? void 0 : _a.plugin.id}.`);
89
+ this.service.commands[name] = new import_command.Command(__spreadProps(__spreadValues({}, commandOpts), {
90
+ plugin: this.plugin
91
+ }));
92
+ };
93
+ registerCommand(opts);
94
+ if (alias) {
95
+ const aliases = (0, import_utils2.makeArray)(alias);
96
+ aliases.forEach((alias2) => {
97
+ registerCommand(__spreadProps(__spreadValues({}, opts), { name: alias2 }));
98
+ });
47
99
  }
48
- registerCommand(opts) {
49
- const { alias } = opts;
50
- delete opts.alias;
51
- const registerCommand = (commandOpts) => {
52
- var _a;
53
- const { name, configResolveMode } = commandOpts;
54
- (0, assert_1.default)(!configResolveMode ||
55
- resolveConfigModes.indexOf(configResolveMode) >= 0, `configResolveMode must be one of ${resolveConfigModes.join(',')}, but got ${configResolveMode}`);
56
- (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}.`);
57
- this.service.commands[name] = new command_1.Command({
58
- ...commandOpts,
59
- plugin: this.plugin,
60
- });
61
- };
62
- registerCommand(opts);
63
- if (alias) {
64
- const aliases = (0, utils_2.makeArray)(alias);
65
- aliases.forEach((alias) => {
66
- registerCommand({ ...opts, name: alias });
67
- });
68
- }
69
- }
70
- registerGenerator(opts) {
71
- var _a;
72
- const { key } = opts;
73
- (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}.`);
74
- this.service.generators[key] = (0, generator_1.makeGenerator)({
75
- ...opts,
76
- plugin: this.plugin,
100
+ }
101
+ registerGenerator(opts) {
102
+ var _a;
103
+ const { key } = opts;
104
+ (0, import_assert.default)(!this.service.generators[key], `api.registerGenerator() failed, the generator ${key} is exists from ${(_a = this.service.generators[key]) == null ? void 0 : _a.plugin.id}.`);
105
+ this.service.generators[key] = (0, import_generator.makeGenerator)(__spreadProps(__spreadValues({}, opts), {
106
+ plugin: this.plugin
107
+ }));
108
+ }
109
+ register(opts) {
110
+ var _a, _b;
111
+ (0, import_assert.default)(this.service.stage <= import_types.ServiceStage.initPlugins, "api.register() should not be called after plugin register stage.");
112
+ (_a = this.service.hooks)[_b = opts.key] || (_a[_b] = []);
113
+ this.service.hooks[opts.key].push(new import_hook.Hook(__spreadProps(__spreadValues({}, opts), { plugin: this.plugin })));
114
+ }
115
+ registerMethod(opts) {
116
+ (0, import_assert.default)(!this.service.pluginMethods[opts.name], `api.registerMethod() failed, method ${opts.name} is already exist.`);
117
+ this.service.pluginMethods[opts.name] = {
118
+ plugin: this.plugin,
119
+ fn: opts.fn || function(fn) {
120
+ this.register(__spreadValues({
121
+ key: opts.name
122
+ }, import_utils.lodash.isPlainObject(fn) ? fn : { fn }));
123
+ }
124
+ };
125
+ }
126
+ registerPresets(source, presets) {
127
+ (0, import_assert.default)(this.service.stage === import_types.ServiceStage.initPresets, `api.registerPresets() failed, it should only used in presets.`);
128
+ source.splice(0, 0, ...presets.map((preset) => {
129
+ return new import_plugin.Plugin({
130
+ path: preset,
131
+ cwd: this.service.cwd,
132
+ type: import_types.PluginType.preset
133
+ });
134
+ }));
135
+ }
136
+ registerPlugins(source, plugins) {
137
+ (0, import_assert.default)(this.service.stage === import_types.ServiceStage.initPresets || this.service.stage === import_types.ServiceStage.initPlugins, `api.registerPlugins() failed, it should only be used in registering stage.`);
138
+ const mappedPlugins = plugins.map((plugin) => {
139
+ if (import_utils.lodash.isPlainObject(plugin)) {
140
+ (0, import_assert.default)(plugin.id && plugin.key, `Invalid plugin object, id and key must supplied.`);
141
+ plugin.type = import_types.PluginType.plugin;
142
+ plugin.enableBy = plugin.enableBy || import_types.EnableBy.register;
143
+ plugin.apply = plugin.apply || (() => () => {
77
144
  });
78
- }
79
- register(opts) {
80
- var _a, _b;
81
- (0, assert_1.default)(this.service.stage <= types_1.ServiceStage.initPlugins, 'api.register() should not be called after plugin register stage.');
82
- (_a = this.service.hooks)[_b = opts.key] || (_a[_b] = []);
83
- this.service.hooks[opts.key].push(new hook_1.Hook({ ...opts, plugin: this.plugin }));
84
- }
85
- registerMethod(opts) {
86
- (0, assert_1.default)(!this.service.pluginMethods[opts.name], `api.registerMethod() failed, method ${opts.name} is already exist.`);
87
- this.service.pluginMethods[opts.name] = {
88
- plugin: this.plugin,
89
- fn: opts.fn ||
90
- // 这里不能用 arrow function,this 需指向执行此方法的 PluginAPI
91
- // 否则 pluginId 会不会,导致不能正确 skip plugin
92
- function (fn) {
93
- // @ts-ignore
94
- this.register({
95
- key: opts.name,
96
- ...(utils_1.lodash.isPlainObject(fn) ? fn : { fn }),
97
- });
98
- },
99
- };
100
- }
101
- registerPresets(source, presets) {
102
- (0, assert_1.default)(this.service.stage === types_1.ServiceStage.initPresets, `api.registerPresets() failed, it should only used in presets.`);
103
- source.splice(0, 0, ...presets.map((preset) => {
104
- return new plugin_1.Plugin({
105
- path: preset,
106
- cwd: this.service.cwd,
107
- type: types_1.PluginType.preset,
108
- });
109
- }));
110
- }
111
- registerPlugins(source, plugins) {
112
- (0, assert_1.default)(this.service.stage === types_1.ServiceStage.initPresets ||
113
- this.service.stage === types_1.ServiceStage.initPlugins, `api.registerPlugins() failed, it should only be used in registering stage.`);
114
- const mappedPlugins = plugins.map((plugin) => {
115
- if (utils_1.lodash.isPlainObject(plugin)) {
116
- (0, assert_1.default)(plugin.id && plugin.key, `Invalid plugin object, id and key must supplied.`);
117
- plugin.type = types_1.PluginType.plugin;
118
- plugin.enableBy = plugin.enableBy || types_1.EnableBy.register;
119
- plugin.apply = plugin.apply || (() => () => { });
120
- plugin.config = plugin.config || {};
121
- plugin.time = { hooks: {} };
122
- return plugin;
123
- }
124
- else {
125
- return new plugin_1.Plugin({
126
- path: plugin,
127
- cwd: this.service.cwd,
128
- type: types_1.PluginType.plugin,
129
- });
130
- }
145
+ plugin.config = plugin.config || {};
146
+ plugin.time = { hooks: {} };
147
+ return plugin;
148
+ } else {
149
+ return new import_plugin.Plugin({
150
+ path: plugin,
151
+ cwd: this.service.cwd,
152
+ type: import_types.PluginType.plugin
131
153
  });
132
- if (this.service.stage === types_1.ServiceStage.initPresets) {
133
- source.push(...mappedPlugins);
154
+ }
155
+ });
156
+ if (this.service.stage === import_types.ServiceStage.initPresets) {
157
+ source.push(...mappedPlugins);
158
+ } else {
159
+ source.splice(0, 0, ...mappedPlugins);
160
+ }
161
+ }
162
+ skipPlugins(keys) {
163
+ keys.forEach((key) => {
164
+ (0, import_assert.default)(!(this.plugin.key === key), `plugin ${key} can't skip itself!`);
165
+ (0, import_assert.default)(this.service.keyToPluginMap[key], `key: ${key} is not be registered by any plugin. You can't skip it!`);
166
+ this.service.skipPluginIds.add(this.service.keyToPluginMap[key].id);
167
+ });
168
+ }
169
+ static proxyPluginAPI(opts) {
170
+ return new Proxy(opts.pluginAPI, {
171
+ get: (target, prop) => {
172
+ if (opts.service.pluginMethods[prop]) {
173
+ return opts.service.pluginMethods[prop].fn;
134
174
  }
135
- else {
136
- source.splice(0, 0, ...mappedPlugins);
175
+ if (opts.serviceProps.includes(prop)) {
176
+ const serviceProp = opts.service[prop];
177
+ return typeof serviceProp === "function" ? serviceProp.bind(opts.service) : serviceProp;
137
178
  }
138
- }
139
- skipPlugins(keys) {
140
- keys.forEach((key) => {
141
- (0, assert_1.default)(!(this.plugin.key === key), `plugin ${key} can't skip itself!`);
142
- (0, assert_1.default)(this.service.keyToPluginMap[key], `key: ${key} is not be registered by any plugin. You can't skip it!`);
143
- this.service.skipPluginIds.add(this.service.keyToPluginMap[key].id);
144
- });
145
- }
146
- static proxyPluginAPI(opts) {
147
- return new Proxy(opts.pluginAPI, {
148
- get: (target, prop) => {
149
- if (opts.service.pluginMethods[prop]) {
150
- return opts.service.pluginMethods[prop].fn;
151
- }
152
- if (opts.serviceProps.includes(prop)) {
153
- // @ts-ignore
154
- const serviceProp = opts.service[prop];
155
- return typeof serviceProp === 'function'
156
- ? serviceProp.bind(opts.service)
157
- : serviceProp;
158
- }
159
- if (prop in opts.staticProps) {
160
- return opts.staticProps[prop];
161
- }
162
- // @ts-ignore
163
- return target[prop];
164
- },
165
- });
166
- }
167
- }
168
- exports.PluginAPI = PluginAPI;
179
+ if (prop in opts.staticProps) {
180
+ return opts.staticProps[prop];
181
+ }
182
+ return target[prop];
183
+ }
184
+ });
185
+ }
186
+ };
187
+ // Annotate the CommonJS export names for ESM import in node:
188
+ 0 && (module.exports = {
189
+ PluginAPI
190
+ });