@modern-js/core 1.4.6 → 1.5.0

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.
package/.eslintrc.js ADDED
@@ -0,0 +1,10 @@
1
+ module.exports = {
2
+ root: true,
3
+ extends: ['@modern-js'],
4
+ parserOptions: {
5
+ // the base path used to search for `tsconfig.json`
6
+ tsconfigRootDir: __dirname,
7
+ // the relative path to sub-package's `tsconfig.json`
8
+ project: ['./tsconfig.json'],
9
+ },
10
+ };
package/CHANGELOG.md CHANGED
@@ -1,5 +1,25 @@
1
1
  # @modern-js/core
2
2
 
3
+ ## 1.5.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 80d8ddfe: feat: add `CliPlugin` type to define new plugin
8
+
9
+ ### Patch Changes
10
+
11
+ - 80d3cfb7: fix: server.metrics type
12
+ - 42c6b136: feat: support api.setAppContext
13
+ - 4e7dcbd5: fix: server.logger type
14
+ - 9e8bc4ab: fix: server.routes type
15
+ - 0c556e59: fix: tools.less type
16
+ - 2008fdbd: convert two packages server part, support server load plugin itself
17
+ - Updated dependencies [5bf5868d]
18
+ - Updated dependencies [80d8ddfe]
19
+ - Updated dependencies [491145e3]
20
+ - @modern-js/utils@1.3.5
21
+ - @modern-js/plugin@1.3.0
22
+
3
23
  ## 1.4.6
4
24
 
5
25
  ### Patch Changes
@@ -4,8 +4,26 @@ import address from 'address';
4
4
  export const AppContext = createContext({});
5
5
  export const ConfigContext = createContext({});
6
6
  export const ResolvedConfigContext = createContext({});
7
+ /**
8
+ * Set app context.
9
+ * @param value new app context. It will override previous app context.
10
+ */
11
+
12
+ export const setAppContext = value => AppContext.set(value);
13
+ /**
14
+ * Get app context, including directories, plugins and some static infos.
15
+ */
16
+
7
17
  export const useAppContext = () => AppContext.use().value;
18
+ /**
19
+ * Get original content of user config.
20
+ */
21
+
8
22
  export const useConfigContext = () => ConfigContext.use().value;
23
+ /**
24
+ * Get normalized content of user config.
25
+ */
26
+
9
27
  export const useResolvedConfigContext = () => ResolvedConfigContext.use().value;
10
28
  export const initAppContext = (appDirectory, plugins, configFile, options) => {
11
29
  const {
@@ -5,41 +5,23 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
5
5
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
6
 
7
7
  import path from 'path';
8
- import { compatRequire, pkgUp, ensureAbsolutePath, logger } from '@modern-js/utils';
9
- import { createAsyncManager, createAsyncWorkflow, createParallelWorkflow, createAsyncWaterfall } from '@modern-js/plugin';
8
+ import { pkgUp, ensureAbsolutePath, logger } from '@modern-js/utils';
10
9
  import { enable } from '@modern-js/plugin/node';
11
10
  import { program } from "./utils/commander";
12
11
  import { resolveConfig, loadUserConfig } from "./config";
13
12
  import { loadPlugins } from "./loadPlugins";
14
- import { AppContext, ConfigContext, initAppContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext } from "./context";
13
+ import { AppContext, ConfigContext, initAppContext, ResolvedConfigContext } from "./context";
15
14
  import { initWatcher } from "./initWatcher";
16
15
  import { loadEnv } from "./loadEnv";
16
+ import { manager } from "./manager";
17
17
  export * from "./config";
18
18
  export * from '@modern-js/plugin';
19
- export * from '@modern-js/plugin/node';
19
+ export * from '@modern-js/plugin/node'; // TODO: remove export after refactor all plugins
20
+
21
+ export { manager, mountHook, usePlugins, createPlugin, registerHook } from "./manager";
22
+ // TODO: remove export after refactor all plugins
23
+ export { AppContext, ConfigContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext } from "./pluginAPI";
20
24
  program.name('modern').usage('<command> [options]').version(process.env.MODERN_JS_VERSION || '0.1.0');
21
- const hooksMap = {
22
- config: createParallelWorkflow(),
23
- resolvedConfig: createAsyncWaterfall(),
24
- validateSchema: createParallelWorkflow(),
25
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
26
- prepare: createAsyncWorkflow(),
27
- commands: createAsyncWorkflow(),
28
- watchFiles: createParallelWorkflow(),
29
- fileChange: createAsyncWorkflow(),
30
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
31
- beforeExit: createAsyncWorkflow(),
32
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
33
- beforeRestart: createAsyncWorkflow()
34
- };
35
- export const manager = createAsyncManager(hooksMap);
36
- export const {
37
- createPlugin,
38
- registe: registerHook,
39
- useRunner: mountHook
40
- } = manager;
41
- export const usePlugins = plugins => plugins.forEach(plugin => manager.usePlugin(compatRequire(require.resolve(plugin))));
42
- export { AppContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext, ConfigContext };
43
25
 
44
26
  const initAppDir = async cwd => {
45
27
  if (!cwd) {
@@ -75,8 +57,7 @@ const createCli = () => {
75
57
  loadEnv(appDirectory, process.env[`${metaName.toUpperCase()}_ENV`]);
76
58
  const loaded = await loadUserConfig(appDirectory, options === null || options === void 0 ? void 0 : options.configFile, options === null || options === void 0 ? void 0 : options.packageJsonConfig);
77
59
  const plugins = loadPlugins(appDirectory, loaded.config, {
78
- internalPlugins: options === null || options === void 0 ? void 0 : options.plugins,
79
- transformPlugin: options === null || options === void 0 ? void 0 : options.transformPlugin
60
+ internalPlugins: options === null || options === void 0 ? void 0 : options.plugins
80
61
  });
81
62
  plugins.forEach(plugin => plugin.cli && manager.usePlugin(plugin.cli));
82
63
  const appContext = initAppContext(appDirectory, plugins, loaded.filePath, options === null || options === void 0 ? void 0 : options.options);
@@ -5,6 +5,7 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
5
5
  function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
6
6
 
7
7
  import { isDepExists, createDebugger, compatRequire, INTERNAL_PLUGINS } from '@modern-js/utils';
8
+ import { createPlugin } from "./manager";
8
9
  const debug = createDebugger('load-plugins');
9
10
 
10
11
  /**
@@ -51,14 +52,12 @@ export function getAppPlugins(appDirectory, pluginConfig, internalPlugins) {
51
52
  * @param appDirectory - Application root directory.
52
53
  * @param userConfig - Resolved user config.
53
54
  * @param options.internalPlugins - Internal plugins.
54
- * @param options.transformPlugin - transform plugin before using it.
55
55
  * @returns Plugin Objects has been required.
56
56
  */
57
57
 
58
58
  export const loadPlugins = (appDirectory, userConfig, options = {}) => {
59
59
  const {
60
- internalPlugins,
61
- transformPlugin
60
+ internalPlugins
62
61
  } = options;
63
62
 
64
63
  const resolvePlugin = p => {
@@ -67,10 +66,9 @@ export const loadPlugins = (appDirectory, userConfig, options = {}) => {
67
66
  let module = compatRequire(path);
68
67
  const pluginOptions = Array.isArray(p) ? p[1] : undefined;
69
68
 
70
- if (transformPlugin) {
71
- module = transformPlugin(module, userConfig, pluginOptions);
72
- } else {
73
- module = typeof module === 'function' ? module(pluginOptions) : module;
69
+ if (typeof module === 'function') {
70
+ const plugin = module(pluginOptions);
71
+ module = createPlugin(plugin.setup, plugin);
74
72
  }
75
73
 
76
74
  return {
@@ -106,10 +104,7 @@ export const loadPlugins = (appDirectory, userConfig, options = {}) => {
106
104
 
107
105
 
108
106
  if (server && typeof server === 'string') {
109
- const path = tryResolve(server, appDirectory);
110
- loadedPlugin.server = {
111
- pluginPath: path
112
- };
107
+ loadedPlugin.server = server;
113
108
  loadedPlugin.serverPkg = server;
114
109
  }
115
110
 
@@ -0,0 +1,28 @@
1
+ import { createAsyncManager, createAsyncWorkflow, createAsyncWaterfall, createParallelWorkflow } from '@modern-js/plugin';
2
+ import { compatRequire } from '@modern-js/utils';
3
+ import { pluginAPI } from "./pluginAPI";
4
+ const baseHooks = {
5
+ config: createParallelWorkflow(),
6
+ resolvedConfig: createAsyncWaterfall(),
7
+ validateSchema: createParallelWorkflow(),
8
+ prepare: createAsyncWorkflow(),
9
+ commands: createAsyncWorkflow(),
10
+ watchFiles: createParallelWorkflow(),
11
+ fileChange: createAsyncWorkflow(),
12
+ beforeExit: createAsyncWorkflow(),
13
+ beforeRestart: createAsyncWorkflow()
14
+ };
15
+ /** All hooks of cli plugin. */
16
+
17
+ export const manager = createAsyncManager(baseHooks, pluginAPI);
18
+ /** Plugin options of a cli plugin. */
19
+
20
+ export const {
21
+ createPlugin,
22
+ registerHook,
23
+ useRunner: mountHook
24
+ } = manager;
25
+ export const usePlugins = plugins => plugins.forEach(pluginPath => {
26
+ const module = compatRequire(require.resolve(pluginPath));
27
+ manager.usePlugin(module);
28
+ });
@@ -0,0 +1,11 @@
1
+ import { AppContext, ConfigContext, setAppContext, useAppContext, useConfigContext, ResolvedConfigContext, useResolvedConfigContext } from "./context";
2
+ export const pluginAPI = {
3
+ setAppContext,
4
+ useAppContext,
5
+ useConfigContext,
6
+ useResolvedConfigContext
7
+ };
8
+ /** all apis for cli plugin */
9
+
10
+ // TODO: only export types after refactor all plugins
11
+ export { AppContext, ConfigContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext };
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.useResolvedConfigContext = exports.useConfigContext = exports.useAppContext = exports.initAppContext = exports.ResolvedConfigContext = exports.ConfigContext = exports.AppContext = void 0;
6
+ exports.useResolvedConfigContext = exports.useConfigContext = exports.useAppContext = exports.setAppContext = exports.initAppContext = exports.ResolvedConfigContext = exports.ConfigContext = exports.AppContext = void 0;
7
7
 
8
8
  var _path = _interopRequireDefault(require("path"));
9
9
 
@@ -18,13 +18,34 @@ exports.AppContext = AppContext;
18
18
  const ConfigContext = (0, _plugin.createContext)({});
19
19
  exports.ConfigContext = ConfigContext;
20
20
  const ResolvedConfigContext = (0, _plugin.createContext)({});
21
+ /**
22
+ * Set app context.
23
+ * @param value new app context. It will override previous app context.
24
+ */
25
+
21
26
  exports.ResolvedConfigContext = ResolvedConfigContext;
22
27
 
28
+ const setAppContext = value => AppContext.set(value);
29
+ /**
30
+ * Get app context, including directories, plugins and some static infos.
31
+ */
32
+
33
+
34
+ exports.setAppContext = setAppContext;
35
+
23
36
  const useAppContext = () => AppContext.use().value;
37
+ /**
38
+ * Get original content of user config.
39
+ */
40
+
24
41
 
25
42
  exports.useAppContext = useAppContext;
26
43
 
27
44
  const useConfigContext = () => ConfigContext.use().value;
45
+ /**
46
+ * Get normalized content of user config.
47
+ */
48
+
28
49
 
29
50
  exports.useConfigContext = useConfigContext;
30
51
 
@@ -4,16 +4,16 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  var _exportNames = {
7
+ cli: true,
8
+ initAppDir: true,
9
+ initAppContext: true,
7
10
  manager: true,
8
- createPlugin: true,
9
- registerHook: true,
10
11
  mountHook: true,
11
12
  usePlugins: true,
12
- cli: true,
13
- initAppDir: true,
13
+ createPlugin: true,
14
+ registerHook: true,
14
15
  AppContext: true,
15
16
  ConfigContext: true,
16
- initAppContext: true,
17
17
  ResolvedConfigContext: true,
18
18
  useAppContext: true,
19
19
  useConfigContext: true,
@@ -22,46 +22,75 @@ var _exportNames = {
22
22
  Object.defineProperty(exports, "AppContext", {
23
23
  enumerable: true,
24
24
  get: function () {
25
- return _context.AppContext;
25
+ return _pluginAPI.AppContext;
26
26
  }
27
27
  });
28
28
  Object.defineProperty(exports, "ConfigContext", {
29
29
  enumerable: true,
30
30
  get: function () {
31
- return _context.ConfigContext;
31
+ return _pluginAPI.ConfigContext;
32
32
  }
33
33
  });
34
34
  Object.defineProperty(exports, "ResolvedConfigContext", {
35
35
  enumerable: true,
36
36
  get: function () {
37
- return _context.ResolvedConfigContext;
37
+ return _pluginAPI.ResolvedConfigContext;
38
+ }
39
+ });
40
+ exports.cli = void 0;
41
+ Object.defineProperty(exports, "createPlugin", {
42
+ enumerable: true,
43
+ get: function () {
44
+ return _manager.createPlugin;
38
45
  }
39
46
  });
40
- exports.createPlugin = exports.cli = void 0;
41
47
  Object.defineProperty(exports, "initAppContext", {
42
48
  enumerable: true,
43
49
  get: function () {
44
50
  return _context.initAppContext;
45
51
  }
46
52
  });
47
- exports.registerHook = exports.mountHook = exports.manager = exports.initAppDir = void 0;
53
+ exports.initAppDir = void 0;
54
+ Object.defineProperty(exports, "manager", {
55
+ enumerable: true,
56
+ get: function () {
57
+ return _manager.manager;
58
+ }
59
+ });
60
+ Object.defineProperty(exports, "mountHook", {
61
+ enumerable: true,
62
+ get: function () {
63
+ return _manager.mountHook;
64
+ }
65
+ });
66
+ Object.defineProperty(exports, "registerHook", {
67
+ enumerable: true,
68
+ get: function () {
69
+ return _manager.registerHook;
70
+ }
71
+ });
48
72
  Object.defineProperty(exports, "useAppContext", {
49
73
  enumerable: true,
50
74
  get: function () {
51
- return _context.useAppContext;
75
+ return _pluginAPI.useAppContext;
52
76
  }
53
77
  });
54
78
  Object.defineProperty(exports, "useConfigContext", {
55
79
  enumerable: true,
56
80
  get: function () {
57
- return _context.useConfigContext;
81
+ return _pluginAPI.useConfigContext;
82
+ }
83
+ });
84
+ Object.defineProperty(exports, "usePlugins", {
85
+ enumerable: true,
86
+ get: function () {
87
+ return _manager.usePlugins;
58
88
  }
59
89
  });
60
- exports.usePlugins = void 0;
61
90
  Object.defineProperty(exports, "useResolvedConfigContext", {
62
91
  enumerable: true,
63
92
  get: function () {
64
- return _context.useResolvedConfigContext;
93
+ return _pluginAPI.useResolvedConfigContext;
65
94
  }
66
95
  });
67
96
 
@@ -69,20 +98,6 @@ var _path = _interopRequireDefault(require("path"));
69
98
 
70
99
  var _utils = require("@modern-js/utils");
71
100
 
72
- var _plugin = require("@modern-js/plugin");
73
-
74
- Object.keys(_plugin).forEach(function (key) {
75
- if (key === "default" || key === "__esModule") return;
76
- if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
77
- if (key in exports && exports[key] === _plugin[key]) return;
78
- Object.defineProperty(exports, key, {
79
- enumerable: true,
80
- get: function () {
81
- return _plugin[key];
82
- }
83
- });
84
- });
85
-
86
101
  var _node = require("@modern-js/plugin/node");
87
102
 
88
103
  Object.keys(_node).forEach(function (key) {
@@ -121,6 +136,24 @@ var _initWatcher = require("./initWatcher");
121
136
 
122
137
  var _loadEnv = require("./loadEnv");
123
138
 
139
+ var _manager = require("./manager");
140
+
141
+ var _plugin = require("@modern-js/plugin");
142
+
143
+ Object.keys(_plugin).forEach(function (key) {
144
+ if (key === "default" || key === "__esModule") return;
145
+ if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
146
+ if (key in exports && exports[key] === _plugin[key]) return;
147
+ Object.defineProperty(exports, key, {
148
+ enumerable: true,
149
+ get: function () {
150
+ return _plugin[key];
151
+ }
152
+ });
153
+ });
154
+
155
+ var _pluginAPI = require("./pluginAPI");
156
+
124
157
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
125
158
 
126
159
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
@@ -131,35 +164,6 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
131
164
 
132
165
  _commander.program.name('modern').usage('<command> [options]').version(process.env.MODERN_JS_VERSION || '0.1.0');
133
166
 
134
- const hooksMap = {
135
- config: (0, _plugin.createParallelWorkflow)(),
136
- resolvedConfig: (0, _plugin.createAsyncWaterfall)(),
137
- validateSchema: (0, _plugin.createParallelWorkflow)(),
138
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
139
- prepare: (0, _plugin.createAsyncWorkflow)(),
140
- commands: (0, _plugin.createAsyncWorkflow)(),
141
- watchFiles: (0, _plugin.createParallelWorkflow)(),
142
- fileChange: (0, _plugin.createAsyncWorkflow)(),
143
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
144
- beforeExit: (0, _plugin.createAsyncWorkflow)(),
145
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
146
- beforeRestart: (0, _plugin.createAsyncWorkflow)()
147
- };
148
- const manager = (0, _plugin.createAsyncManager)(hooksMap);
149
- exports.manager = manager;
150
- const {
151
- createPlugin,
152
- registe: registerHook,
153
- useRunner: mountHook
154
- } = manager;
155
- exports.mountHook = mountHook;
156
- exports.registerHook = registerHook;
157
- exports.createPlugin = createPlugin;
158
-
159
- const usePlugins = plugins => plugins.forEach(plugin => manager.usePlugin((0, _utils.compatRequire)(require.resolve(plugin))));
160
-
161
- exports.usePlugins = usePlugins;
162
-
163
167
  const initAppDir = async cwd => {
164
168
  if (!cwd) {
165
169
  // eslint-disable-next-line no-param-reassign
@@ -189,24 +193,27 @@ const createCli = () => {
189
193
  var _options$options$meta, _options$options;
190
194
 
191
195
  (0, _node.enable)();
192
- manager.clear();
196
+
197
+ _manager.manager.clear();
198
+
193
199
  restartOptions = options;
194
200
  const appDirectory = await initAppDir();
195
201
  const metaName = (_options$options$meta = options === null || options === void 0 ? void 0 : (_options$options = options.options) === null || _options$options === void 0 ? void 0 : _options$options.metaName) !== null && _options$options$meta !== void 0 ? _options$options$meta : 'MODERN';
196
202
  (0, _loadEnv.loadEnv)(appDirectory, process.env[`${metaName.toUpperCase()}_ENV`]);
197
203
  const loaded = await (0, _config.loadUserConfig)(appDirectory, options === null || options === void 0 ? void 0 : options.configFile, options === null || options === void 0 ? void 0 : options.packageJsonConfig);
198
204
  const plugins = (0, _loadPlugins.loadPlugins)(appDirectory, loaded.config, {
199
- internalPlugins: options === null || options === void 0 ? void 0 : options.plugins,
200
- transformPlugin: options === null || options === void 0 ? void 0 : options.transformPlugin
205
+ internalPlugins: options === null || options === void 0 ? void 0 : options.plugins
201
206
  });
202
- plugins.forEach(plugin => plugin.cli && manager.usePlugin(plugin.cli));
207
+ plugins.forEach(plugin => plugin.cli && _manager.manager.usePlugin(plugin.cli));
203
208
  const appContext = (0, _context.initAppContext)(appDirectory, plugins, loaded.filePath, options === null || options === void 0 ? void 0 : options.options);
204
- manager.run(() => {
209
+
210
+ _manager.manager.run(() => {
205
211
  _context.ConfigContext.set(loaded.config);
206
212
 
207
213
  _context.AppContext.set(appContext);
208
214
  });
209
- hooksRunner = await manager.init();
215
+
216
+ hooksRunner = await _manager.manager.init();
210
217
  ['SIGINT', 'SIGTERM', 'unhandledRejection', 'uncaughtException'].forEach(event => {
211
218
  process.on(event, async err => {
212
219
  await hooksRunner.beforeExit();
@@ -230,7 +237,7 @@ const createCli = () => {
230
237
  resolved: config
231
238
  }); // update context value
232
239
 
233
- manager.run(() => {
240
+ _manager.manager.run(() => {
234
241
  _context.ConfigContext.set(loaded.config);
235
242
 
236
243
  _context.ResolvedConfigContext.set(resolved);
@@ -240,6 +247,7 @@ const createCli = () => {
240
247
  distDirectory: (0, _utils.ensureAbsolutePath)(appDirectory, resolved.output.path)
241
248
  }));
242
249
  });
250
+
243
251
  await hooksRunner.prepare();
244
252
  return {
245
253
  loadedConfig: loaded,
@@ -258,7 +266,8 @@ const createCli = () => {
258
266
  program: _commander.program
259
267
  });
260
268
  (0, _initWatcher.initWatcher)(loadedConfig, appContext.appDirectory, resolved.source.configDir, hooksRunner, argv);
261
- manager.run(() => _commander.program.parse(process.argv));
269
+
270
+ _manager.manager.run(() => _commander.program.parse(process.argv));
262
271
  }
263
272
 
264
273
  async function restart() {
@@ -270,7 +279,9 @@ const createCli = () => {
270
279
  _utils.logger.info('Restart...\n');
271
280
 
272
281
  let hasGetError = false;
273
- const runner = manager.useRunner();
282
+
283
+ const runner = _manager.manager.useRunner();
284
+
274
285
  await runner.beforeRestart();
275
286
 
276
287
  try {
@@ -280,7 +291,7 @@ const createCli = () => {
280
291
  hasGetError = true;
281
292
  } finally {
282
293
  if (!hasGetError) {
283
- manager.run(() => _commander.program.parse(process.argv));
294
+ _manager.manager.run(() => _commander.program.parse(process.argv));
284
295
  }
285
296
  }
286
297
  }
@@ -8,6 +8,8 @@ exports.loadPlugins = void 0;
8
8
 
9
9
  var _utils = require("@modern-js/utils");
10
10
 
11
+ var _manager = require("./manager");
12
+
11
13
  function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
12
14
 
13
15
  function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
@@ -60,15 +62,13 @@ function getAppPlugins(appDirectory, pluginConfig, internalPlugins) {
60
62
  * @param appDirectory - Application root directory.
61
63
  * @param userConfig - Resolved user config.
62
64
  * @param options.internalPlugins - Internal plugins.
63
- * @param options.transformPlugin - transform plugin before using it.
64
65
  * @returns Plugin Objects has been required.
65
66
  */
66
67
 
67
68
 
68
69
  const loadPlugins = (appDirectory, userConfig, options = {}) => {
69
70
  const {
70
- internalPlugins,
71
- transformPlugin
71
+ internalPlugins
72
72
  } = options;
73
73
 
74
74
  const resolvePlugin = p => {
@@ -77,10 +77,9 @@ const loadPlugins = (appDirectory, userConfig, options = {}) => {
77
77
  let module = (0, _utils.compatRequire)(path);
78
78
  const pluginOptions = Array.isArray(p) ? p[1] : undefined;
79
79
 
80
- if (transformPlugin) {
81
- module = transformPlugin(module, userConfig, pluginOptions);
82
- } else {
83
- module = typeof module === 'function' ? module(pluginOptions) : module;
80
+ if (typeof module === 'function') {
81
+ const plugin = module(pluginOptions);
82
+ module = (0, _manager.createPlugin)(plugin.setup, plugin);
84
83
  }
85
84
 
86
85
  return {
@@ -116,10 +115,7 @@ const loadPlugins = (appDirectory, userConfig, options = {}) => {
116
115
 
117
116
 
118
117
  if (server && typeof server === 'string') {
119
- const path = tryResolve(server, appDirectory);
120
- loadedPlugin.server = {
121
- pluginPath: path
122
- };
118
+ loadedPlugin.server = server;
123
119
  loadedPlugin.serverPkg = server;
124
120
  }
125
121
 
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.usePlugins = exports.registerHook = exports.mountHook = exports.manager = exports.createPlugin = void 0;
7
+
8
+ var _plugin = require("@modern-js/plugin");
9
+
10
+ var _utils = require("@modern-js/utils");
11
+
12
+ var _pluginAPI = require("./pluginAPI");
13
+
14
+ const baseHooks = {
15
+ config: (0, _plugin.createParallelWorkflow)(),
16
+ resolvedConfig: (0, _plugin.createAsyncWaterfall)(),
17
+ validateSchema: (0, _plugin.createParallelWorkflow)(),
18
+ prepare: (0, _plugin.createAsyncWorkflow)(),
19
+ commands: (0, _plugin.createAsyncWorkflow)(),
20
+ watchFiles: (0, _plugin.createParallelWorkflow)(),
21
+ fileChange: (0, _plugin.createAsyncWorkflow)(),
22
+ beforeExit: (0, _plugin.createAsyncWorkflow)(),
23
+ beforeRestart: (0, _plugin.createAsyncWorkflow)()
24
+ };
25
+ /** All hooks of cli plugin. */
26
+
27
+ const manager = (0, _plugin.createAsyncManager)(baseHooks, _pluginAPI.pluginAPI);
28
+ /** Plugin options of a cli plugin. */
29
+
30
+ exports.manager = manager;
31
+ const {
32
+ createPlugin,
33
+ registerHook,
34
+ useRunner: mountHook
35
+ } = manager;
36
+ exports.mountHook = mountHook;
37
+ exports.registerHook = registerHook;
38
+ exports.createPlugin = createPlugin;
39
+
40
+ const usePlugins = plugins => plugins.forEach(pluginPath => {
41
+ const module = (0, _utils.compatRequire)(require.resolve(pluginPath));
42
+ manager.usePlugin(module);
43
+ });
44
+
45
+ exports.usePlugins = usePlugins;
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "AppContext", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _context.AppContext;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "ConfigContext", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _context.ConfigContext;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "ResolvedConfigContext", {
19
+ enumerable: true,
20
+ get: function () {
21
+ return _context.ResolvedConfigContext;
22
+ }
23
+ });
24
+ exports.pluginAPI = void 0;
25
+ Object.defineProperty(exports, "useAppContext", {
26
+ enumerable: true,
27
+ get: function () {
28
+ return _context.useAppContext;
29
+ }
30
+ });
31
+ Object.defineProperty(exports, "useConfigContext", {
32
+ enumerable: true,
33
+ get: function () {
34
+ return _context.useConfigContext;
35
+ }
36
+ });
37
+ Object.defineProperty(exports, "useResolvedConfigContext", {
38
+ enumerable: true,
39
+ get: function () {
40
+ return _context.useResolvedConfigContext;
41
+ }
42
+ });
43
+
44
+ var _context = require("./context");
45
+
46
+ const pluginAPI = {
47
+ setAppContext: _context.setAppContext,
48
+ useAppContext: _context.useAppContext,
49
+ useConfigContext: _context.useConfigContext,
50
+ useResolvedConfigContext: _context.useResolvedConfigContext
51
+ };
52
+ /** all apis for cli plugin */
53
+
54
+ exports.pluginAPI = pluginAPI;
@@ -65,7 +65,7 @@ interface OutputConfig {
65
65
  enableTsLoader?: boolean;
66
66
  }
67
67
  interface ServerConfig {
68
- routes?: Record<string, string | {
68
+ routes?: Record<string, string | string[] | {
69
69
  route: string | string[];
70
70
  disableSpa?: boolean;
71
71
  }>;
@@ -76,8 +76,8 @@ interface ServerConfig {
76
76
  ssrByEntries?: Record<string, boolean | Record<string, unknown>>;
77
77
  baseUrl?: string | Array<string>;
78
78
  port?: number;
79
- logger?: Record<string, any>;
80
- metrics?: Record<string, any>;
79
+ logger?: boolean | Record<string, any>;
80
+ metrics?: boolean | Record<string, any>;
81
81
  enableMicroFrontendDebug?: boolean;
82
82
  }
83
83
  interface DevConfig {
@@ -6,8 +6,26 @@ export type { IAppContext };
6
6
  export declare const AppContext: import("@modern-js/plugin").Context<IAppContext>;
7
7
  export declare const ConfigContext: import("@modern-js/plugin").Context<UserConfig>;
8
8
  export declare const ResolvedConfigContext: import("@modern-js/plugin").Context<NormalizedConfig>;
9
+ /**
10
+ * Set app context.
11
+ * @param value new app context. It will override previous app context.
12
+ */
13
+
14
+ export declare const setAppContext: (value: IAppContext) => void;
15
+ /**
16
+ * Get app context, including directories, plugins and some static infos.
17
+ */
18
+
9
19
  export declare const useAppContext: () => IAppContext;
20
+ /**
21
+ * Get original content of user config.
22
+ */
23
+
10
24
  export declare const useConfigContext: () => UserConfig;
25
+ /**
26
+ * Get normalized content of user config.
27
+ */
28
+
11
29
  export declare const useResolvedConfigContext: () => NormalizedConfig;
12
30
  export declare const initAppContext: (appDirectory: string, plugins: Array<LoadedPlugin>, configFile: string | false, options?: {
13
31
  metaName?: string | undefined;
@@ -1,111 +1,22 @@
1
1
  import { INTERNAL_PLUGINS } from '@modern-js/utils';
2
- import { ParallelWorkflow, AsyncWorkflow, Progresses2Runners, AsyncWaterfall } from '@modern-js/plugin';
3
2
  import type { Hooks } from '@modern-js/types';
4
3
  import { ErrorObject } from 'ajv';
5
- import { Command } from './utils/commander';
6
- import { TransformPlugin } from './loadPlugins';
7
- import { AppContext, ConfigContext, IAppContext, initAppContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext } from './context';
8
- import { NormalizedConfig } from './config/mergeConfig';
4
+ import { IAppContext, initAppContext } from './context';
5
+ import type { NormalizedConfig } from './config/mergeConfig';
9
6
  export type { Hooks };
10
7
  export * from './config';
11
8
  export * from '@modern-js/plugin';
12
9
  export * from '@modern-js/plugin/node';
13
- export declare type HooksRunner = Progresses2Runners<{
14
- config: ParallelWorkflow<void>;
15
- resolvedConfig: AsyncWaterfall<{
16
- resolved: NormalizedConfig;
17
- }>;
18
- validateSchema: ParallelWorkflow<void>;
19
- prepare: AsyncWorkflow<void, void>;
20
- commands: AsyncWorkflow<{
21
- program: Command;
22
- }, void>;
23
- watchFiles: ParallelWorkflow<void>;
24
- fileChange: AsyncWorkflow<{
25
- filename: string;
26
- eventType: 'add' | 'change' | 'unlink';
27
- }, void>;
28
- beforeExit: AsyncWorkflow<void, void>;
29
- }>;
30
- export declare const manager: import("@modern-js/plugin").AsyncManager<Hooks, {
31
- config: ParallelWorkflow<void, unknown>;
32
- resolvedConfig: AsyncWaterfall<{
33
- resolved: NormalizedConfig;
34
- }>;
35
- validateSchema: ParallelWorkflow<void, unknown>;
36
- prepare: AsyncWorkflow<void, void>;
37
- commands: AsyncWorkflow<{
38
- program: Command;
39
- }, void>;
40
- watchFiles: ParallelWorkflow<void, unknown>;
41
- fileChange: AsyncWorkflow<{
42
- filename: string;
43
- eventType: 'add' | 'change' | 'unlink';
44
- }, void>;
45
- beforeExit: AsyncWorkflow<void, void>;
46
- beforeRestart: AsyncWorkflow<void, void>;
47
- }>;
48
- export declare const createPlugin: (initializer: import("@modern-js/plugin").AsyncInitializer<Partial<import("@modern-js/plugin").Progresses2Threads<{
49
- config: ParallelWorkflow<void, unknown>;
50
- resolvedConfig: AsyncWaterfall<{
51
- resolved: NormalizedConfig;
52
- }>;
53
- validateSchema: ParallelWorkflow<void, unknown>;
54
- prepare: AsyncWorkflow<void, void>;
55
- commands: AsyncWorkflow<{
56
- program: Command;
57
- }, void>;
58
- watchFiles: ParallelWorkflow<void, unknown>;
59
- fileChange: AsyncWorkflow<{
60
- filename: string;
61
- eventType: 'add' | 'change' | 'unlink';
62
- }, void>;
63
- beforeExit: AsyncWorkflow<void, void>;
64
- beforeRestart: AsyncWorkflow<void, void>;
65
- } & import("@modern-js/plugin").ClearDraftProgress<Hooks>>>>, options?: import("@modern-js/plugin").PluginOptions | undefined) => import("@modern-js/plugin").AsyncPlugin<Partial<import("@modern-js/plugin").Progresses2Threads<{
66
- config: ParallelWorkflow<void, unknown>;
67
- resolvedConfig: AsyncWaterfall<{
68
- resolved: NormalizedConfig;
69
- }>;
70
- validateSchema: ParallelWorkflow<void, unknown>;
71
- prepare: AsyncWorkflow<void, void>;
72
- commands: AsyncWorkflow<{
73
- program: Command;
74
- }, void>;
75
- watchFiles: ParallelWorkflow<void, unknown>;
76
- fileChange: AsyncWorkflow<{
77
- filename: string;
78
- eventType: 'add' | 'change' | 'unlink';
79
- }, void>;
80
- beforeExit: AsyncWorkflow<void, void>;
81
- beforeRestart: AsyncWorkflow<void, void>;
82
- } & import("@modern-js/plugin").ClearDraftProgress<Hooks>>>>, registerHook: (newShape: Partial<Hooks>) => void, mountHook: () => Progresses2Runners<{
83
- config: ParallelWorkflow<void, unknown>;
84
- resolvedConfig: AsyncWaterfall<{
85
- resolved: NormalizedConfig;
86
- }>;
87
- validateSchema: ParallelWorkflow<void, unknown>;
88
- prepare: AsyncWorkflow<void, void>;
89
- commands: AsyncWorkflow<{
90
- program: Command;
91
- }, void>;
92
- watchFiles: ParallelWorkflow<void, unknown>;
93
- fileChange: AsyncWorkflow<{
94
- filename: string;
95
- eventType: 'add' | 'change' | 'unlink';
96
- }, void>;
97
- beforeExit: AsyncWorkflow<void, void>;
98
- beforeRestart: AsyncWorkflow<void, void>;
99
- } & import("@modern-js/plugin").ClearDraftProgress<Hooks>>;
100
- export declare const usePlugins: (plugins: string[]) => void;
101
- export { AppContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext, ConfigContext };
10
+ export { manager, mountHook, usePlugins, createPlugin, registerHook } from './manager';
11
+ export type { CliHooks, CliPlugin, CliHookCallbacks } from './manager';
12
+ export { AppContext, ConfigContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext } from './pluginAPI';
13
+ export type { PluginAPI } from './pluginAPI';
102
14
  export type { NormalizedConfig, IAppContext };
103
15
  declare const initAppDir: (cwd?: string | undefined) => Promise<string>;
104
16
  export interface CoreOptions {
105
17
  configFile?: string;
106
18
  packageJsonConfig?: string;
107
19
  plugins?: typeof INTERNAL_PLUGINS;
108
- transformPlugin?: TransformPlugin;
109
20
  onSchemaError?: (error: ErrorObject) => void;
110
21
  options?: {
111
22
  metaName?: string;
@@ -1,4 +1,4 @@
1
1
  import chokidar from 'chokidar';
2
2
  import { LoadedConfig } from './config';
3
- import { HooksRunner } from '.';
3
+ import { HooksRunner } from './manager';
4
4
  export declare const initWatcher: (loaded: LoadedConfig, appDirectory: string, configDir: string | undefined, hooksRunner: HooksRunner, argv: string[]) => Promise<chokidar.FSWatcher | undefined>;
@@ -12,19 +12,16 @@ export declare type PluginConfigItem = {
12
12
  server?: Plugin;
13
13
  } | Plugin;
14
14
  export declare type PluginConfig = Array<PluginConfigItem>;
15
- export declare type TransformPlugin = (plugin: PluginConfig, resolvedConfig: UserConfig, pluginOptions?: any) => PluginConfig;
16
15
  export declare function getAppPlugins(appDirectory: string, pluginConfig: PluginConfig, internalPlugins?: typeof INTERNAL_PLUGINS): PluginConfigItem[];
17
16
  /**
18
17
  * Load internal plugins which in @modern-js scope and user's custom plugins.
19
18
  * @param appDirectory - Application root directory.
20
19
  * @param userConfig - Resolved user config.
21
20
  * @param options.internalPlugins - Internal plugins.
22
- * @param options.transformPlugin - transform plugin before using it.
23
21
  * @returns Plugin Objects has been required.
24
22
  */
25
23
 
26
24
  export declare const loadPlugins: (appDirectory: string, userConfig: UserConfig, options?: {
27
25
  internalPlugins?: typeof INTERNAL_PLUGINS;
28
- transformPlugin?: TransformPlugin;
29
26
  }) => LoadedPlugin[];
30
27
  export {};
@@ -0,0 +1,74 @@
1
+ import { ToThreads, ToRunners, AsyncSetup, PluginOptions, AsyncWorkflow, AsyncWaterfall, ParallelWorkflow } from '@modern-js/plugin';
2
+ import type { Hooks } from '@modern-js/types';
3
+ import type { Command } from './utils/commander';
4
+ import type { NormalizedConfig } from './config/mergeConfig';
5
+ import { pluginAPI } from './pluginAPI';
6
+ export declare type HooksRunner = ToRunners<{
7
+ config: ParallelWorkflow<void>;
8
+ resolvedConfig: AsyncWaterfall<{
9
+ resolved: NormalizedConfig;
10
+ }>;
11
+ validateSchema: ParallelWorkflow<void>;
12
+ prepare: AsyncWorkflow<void, void>;
13
+ commands: AsyncWorkflow<{
14
+ program: Command;
15
+ }, void>;
16
+ watchFiles: ParallelWorkflow<void>;
17
+ fileChange: AsyncWorkflow<{
18
+ filename: string;
19
+ eventType: 'add' | 'change' | 'unlink';
20
+ }, void>;
21
+ beforeExit: AsyncWorkflow<void, void>;
22
+ beforeRestart: AsyncWorkflow<void, void>;
23
+ }>;
24
+ declare const baseHooks: {
25
+ config: ParallelWorkflow<void, unknown>;
26
+ resolvedConfig: AsyncWaterfall<{
27
+ resolved: NormalizedConfig;
28
+ }>;
29
+ validateSchema: ParallelWorkflow<void, unknown>;
30
+ prepare: AsyncWorkflow<void, void>;
31
+ commands: AsyncWorkflow<{
32
+ program: Command;
33
+ }, void>;
34
+ watchFiles: ParallelWorkflow<void, unknown>;
35
+ fileChange: AsyncWorkflow<{
36
+ filename: string;
37
+ eventType: 'add' | 'change' | 'unlink';
38
+ }, void>;
39
+ beforeExit: AsyncWorkflow<void, void>;
40
+ beforeRestart: AsyncWorkflow<void, void>;
41
+ };
42
+ /** All hooks of cli plugin. */
43
+
44
+ export declare type CliHooks = typeof baseHooks & Hooks;
45
+ /** All hook callbacks of cli plugin. */
46
+
47
+ export declare type CliHookCallbacks = ToThreads<CliHooks>;
48
+ export declare const manager: import("@modern-js/plugin").AsyncManager<CliHooks, {
49
+ setAppContext: (value: import("@modern-js/types").IAppContext) => void;
50
+ useAppContext: () => import("@modern-js/types").IAppContext;
51
+ useConfigContext: () => import("./config").UserConfig;
52
+ useResolvedConfigContext: () => NormalizedConfig;
53
+ }>;
54
+ /** Plugin options of a cli plugin. */
55
+
56
+ export declare type CliPlugin = PluginOptions<CliHooks, AsyncSetup<CliHooks, typeof pluginAPI>>;
57
+ export declare const createPlugin: (setup?: AsyncSetup<CliHooks, {
58
+ setAppContext: (value: import("@modern-js/types").IAppContext) => void;
59
+ useAppContext: () => import("@modern-js/types").IAppContext;
60
+ useConfigContext: () => import("./config").UserConfig;
61
+ useResolvedConfigContext: () => NormalizedConfig;
62
+ }> | undefined, options?: PluginOptions<CliHooks, AsyncSetup<CliHooks, {
63
+ setAppContext: (value: import("@modern-js/types").IAppContext) => void;
64
+ useAppContext: () => import("@modern-js/types").IAppContext;
65
+ useConfigContext: () => import("./config").UserConfig;
66
+ useResolvedConfigContext: () => NormalizedConfig;
67
+ }>> | undefined) => import("@modern-js/plugin").AsyncPlugin<CliHooks, {
68
+ setAppContext: (value: import("@modern-js/types").IAppContext) => void;
69
+ useAppContext: () => import("@modern-js/types").IAppContext;
70
+ useConfigContext: () => import("./config").UserConfig;
71
+ useResolvedConfigContext: () => NormalizedConfig;
72
+ }>, registerHook: (newHooks: Partial<CliHooks>) => void, mountHook: () => ToRunners<CliHooks>;
73
+ export declare const usePlugins: (plugins: string[]) => void;
74
+ export {};
@@ -0,0 +1,13 @@
1
+ import type { CommonAPI } from '@modern-js/plugin';
2
+ import type { CliHooks } from './manager';
3
+ import { AppContext, ConfigContext, useAppContext, useConfigContext, ResolvedConfigContext, useResolvedConfigContext } from './context';
4
+ export declare const pluginAPI: {
5
+ setAppContext: (value: import("@modern-js/types/cli").IAppContext) => void;
6
+ useAppContext: () => import("@modern-js/types/cli").IAppContext;
7
+ useConfigContext: () => import("./config").UserConfig;
8
+ useResolvedConfigContext: () => import(".").NormalizedConfig;
9
+ };
10
+ /** all apis for cli plugin */
11
+
12
+ export declare type PluginAPI = typeof pluginAPI & CommonAPI<CliHooks>;
13
+ export { AppContext, ConfigContext, ResolvedConfigContext, useAppContext, useConfigContext, useResolvedConfigContext };
package/package.json CHANGED
@@ -11,7 +11,7 @@
11
11
  "modern",
12
12
  "modern.js"
13
13
  ],
14
- "version": "1.4.6",
14
+ "version": "1.5.0",
15
15
  "jsnext:source": "./src/index.ts",
16
16
  "types": "./dist/types/index.d.ts",
17
17
  "main": "./dist/js/node/index.js",
@@ -43,8 +43,8 @@
43
43
  "@babel/code-frame": "^7.14.5",
44
44
  "@babel/runtime": "^7",
45
45
  "@modern-js/load-config": "^1.2.2",
46
- "@modern-js/plugin": "^1.2.1",
47
- "@modern-js/utils": "^1.3.4",
46
+ "@modern-js/plugin": "^1.3.0",
47
+ "@modern-js/utils": "^1.3.5",
48
48
  "address": "^1.1.2",
49
49
  "ajv": "^8.6.2",
50
50
  "ajv-keywords": "^5.0.0",
@@ -61,7 +61,7 @@
61
61
  "devDependencies": {
62
62
  "btsm": "2.2.2",
63
63
  "@types/babel__code-frame": "^7.0.3",
64
- "@modern-js/types": "^1.3.4",
64
+ "@modern-js/types": "^1.3.5",
65
65
  "@types/jest": "^26",
66
66
  "@types/lodash.clonedeep": "^4.5.6",
67
67
  "@types/lodash.mergewith": "^4.6.6",
@@ -38,9 +38,7 @@ describe('load plugins', () => {
38
38
  cliPkg: path.join(fixture, './test-plugin-a.js'),
39
39
  },
40
40
  {
41
- server: {
42
- pluginPath: path.join(fixture, './test-plugin-b.js'),
43
- },
41
+ server: './test-plugin-b',
44
42
  serverPkg: './test-plugin-b',
45
43
  },
46
44
  ]);
@@ -56,22 +54,8 @@ describe('load plugins', () => {
56
54
  plugins: [{ cli: ['./test-plugin-c', 'c'] }, ['./test-plugin-c', 'c2']],
57
55
  });
58
56
 
59
- expect(plugins).toEqual([
60
- {
61
- cli: {
62
- name: 'c',
63
- pluginPath: path.join(fixture, './test-plugin-c.js'),
64
- },
65
- cliPkg: './test-plugin-c',
66
- },
67
- {
68
- cli: {
69
- name: 'c2',
70
- pluginPath: path.join(fixture, './test-plugin-c.js'),
71
- },
72
- cliPkg: './test-plugin-c',
73
- },
74
- ]);
57
+ expect(plugins[0].cli.name).toEqual('c');
58
+ expect(plugins[1].cli.name).toEqual('c2');
75
59
  });
76
60
 
77
61
  test('should load user string plugin successfully', () => {
@@ -95,26 +79,6 @@ describe('load plugins', () => {
95
79
  ]);
96
80
  });
97
81
 
98
- test('should call transformPlugin', () => {
99
- const fixture = path.resolve(
100
- __dirname,
101
- './fixtures/load-plugin/user-plugins',
102
- );
103
-
104
- const options = {
105
- transformPlugin: jest.fn(),
106
- };
107
- options.transformPlugin.mockImplementation((plugins, _) => plugins);
108
-
109
- loadPlugins(
110
- fixture,
111
- { plugins: [{ cli: path.join(fixture, './test-plugin-a.js') }] },
112
- options,
113
- );
114
-
115
- expect(options.transformPlugin).toHaveBeenCalled();
116
- });
117
-
118
82
  test(`should throw error when plugin not found `, () => {
119
83
  const fixture = path.resolve(__dirname, './fixtures/load-plugin/not-found');
120
84
 
@@ -0,0 +1,19 @@
1
+ import { CliPlugin, manager } from '../src';
2
+
3
+ describe('pluginAPI', () => {
4
+ it('api.setAppContext', done => {
5
+ const plugin = (): CliPlugin => ({
6
+ setup(api) {
7
+ api.setAppContext({
8
+ ...api.useAppContext(),
9
+ packageName: 'foo',
10
+ });
11
+
12
+ expect(api.useAppContext().packageName).toEqual('foo');
13
+ done();
14
+ },
15
+ });
16
+
17
+ manager.clone().usePlugin(plugin).init();
18
+ });
19
+ });