@modern-js/plugin 1.2.1 → 1.3.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.
Files changed (36) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/js/modern/manager/async.js +51 -24
  3. package/dist/js/modern/manager/index.js +2 -1
  4. package/dist/js/modern/manager/sync.js +71 -56
  5. package/dist/js/modern/manager/types.js +0 -0
  6. package/dist/js/modern/waterfall/async.js +0 -1
  7. package/dist/js/modern/waterfall/sync.js +0 -1
  8. package/dist/js/modern/workflow/async.js +1 -2
  9. package/dist/js/modern/workflow/sync.js +0 -1
  10. package/dist/js/node/manager/async.js +49 -21
  11. package/dist/js/node/manager/index.js +13 -0
  12. package/dist/js/node/manager/sync.js +76 -54
  13. package/dist/js/node/manager/types.js +0 -0
  14. package/dist/js/node/waterfall/async.js +0 -1
  15. package/dist/js/node/waterfall/sync.js +0 -1
  16. package/dist/js/node/workflow/async.js +1 -2
  17. package/dist/js/node/workflow/sync.js +0 -1
  18. package/dist/js/treeshaking/manager/async.js +55 -30
  19. package/dist/js/treeshaking/manager/index.js +2 -1
  20. package/dist/js/treeshaking/manager/sync.js +77 -58
  21. package/dist/js/treeshaking/manager/types.js +0 -0
  22. package/dist/js/treeshaking/waterfall/async.js +0 -1
  23. package/dist/js/treeshaking/waterfall/sync.js +0 -1
  24. package/dist/js/treeshaking/workflow/async.js +1 -2
  25. package/dist/js/treeshaking/workflow/sync.js +0 -1
  26. package/dist/types/manager/async.d.ts +60 -21
  27. package/dist/types/manager/index.d.ts +2 -1
  28. package/dist/types/manager/sync.d.ts +74 -43
  29. package/dist/types/manager/types.d.ts +41 -0
  30. package/package.json +3 -3
  31. package/tests/async.test.ts +125 -7
  32. package/tests/fixtures/async/core/index.ts +12 -4
  33. package/tests/fixtures/async/dynamic/foo.ts +2 -2
  34. package/tests/fixtures/sync/core/index.ts +9 -4
  35. package/tests/fixtures/sync/dynamic/foo.ts +2 -2
  36. package/tests/sync.test.ts +119 -6
@@ -3,7 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
- exports.hasOwnProperty = exports.generateRunner = exports.createManager = exports.cloneProgressRecord = exports.cloneProgress = exports.DEFAULT_OPTIONS = void 0;
6
+ exports.isObject = exports.hasOwnProperty = exports.generateRunner = exports.createManager = exports.closeHooksMap = exports.cloneHook = exports.DEFAULT_OPTIONS = void 0;
7
7
 
8
8
  var _farrowPipeline = require("farrow-pipeline");
9
9
 
@@ -25,38 +25,43 @@ const DEFAULT_OPTIONS = {
25
25
  pre: [],
26
26
  post: [],
27
27
  rivals: [],
28
- required: []
28
+ required: [],
29
+ usePlugins: [],
30
+ registerHook: {}
29
31
  };
30
32
  exports.DEFAULT_OPTIONS = DEFAULT_OPTIONS;
31
33
 
32
- const createManager = processes => {
34
+ const createManager = (hooks, api) => {
33
35
  let index = 0;
34
36
 
35
- const createPlugin = (initializer, options = {}) => _objectSpread(_objectSpread(_objectSpread({}, DEFAULT_OPTIONS), {}, {
36
- name: `No.${index++} plugin`
37
- }, options), {}, {
38
- SYNC_PLUGIN_SYMBOL,
39
- initializer
40
- });
41
-
42
- const isPlugin = input => hasOwnProperty(input, SYNC_PLUGIN_SYMBOL) && input[SYNC_PLUGIN_SYMBOL] === SYNC_PLUGIN_SYMBOL;
37
+ let currentHooks = _objectSpread({}, hooks);
43
38
 
44
- const registe = extraProcesses => {
45
- // eslint-disable-next-line no-param-reassign
46
- processes = _objectSpread(_objectSpread({}, extraProcesses), processes);
39
+ const registerHook = extraHooks => {
40
+ currentHooks = _objectSpread(_objectSpread({}, extraHooks), currentHooks);
47
41
  };
48
42
 
49
- const clone = () => {
43
+ const isPlugin = input => isObject(input) && hasOwnProperty(input, SYNC_PLUGIN_SYMBOL) && input[SYNC_PLUGIN_SYMBOL] === SYNC_PLUGIN_SYMBOL;
44
+
45
+ const pluginAPI = _objectSpread(_objectSpread({}, api), {}, {
46
+ useHookRunners: _runner.useRunner
47
+ });
48
+
49
+ const clone = overrideAPI => {
50
50
  let plugins = [];
51
51
 
52
+ const addPlugin = plugin => {
53
+ if (!includePlugin(plugins, plugin)) {
54
+ plugins.push(_objectSpread({}, plugin));
55
+ }
56
+ };
57
+
52
58
  const usePlugin = (...input) => {
53
59
  for (const plugin of input) {
54
60
  if (isPlugin(plugin)) {
55
- if (!includePlugin(plugins, plugin)) {
56
- plugins.push(_objectSpread(_objectSpread({}, plugin), {}, {
57
- index: plugins.length
58
- }));
59
- }
61
+ addPlugin(plugin);
62
+ } else if (typeof plugin === 'function') {
63
+ const options = plugin();
64
+ addPlugin(createPlugin(options.setup, options));
60
65
  } else {
61
66
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
62
67
  // @ts-expect-error
@@ -64,17 +69,29 @@ const createManager = processes => {
64
69
  }
65
70
  }
66
71
 
67
- return {
68
- createPlugin,
69
- isPlugin,
70
- usePlugin,
71
- init,
72
- run,
73
- clear,
74
- registe,
75
- useRunner: _runner.useRunner,
76
- clone
77
- };
72
+ return manager;
73
+ };
74
+
75
+ const createPlugin = ( // eslint-disable-next-line @typescript-eslint/no-empty-function
76
+ setup = () => {}, options = {}) => {
77
+ var _options$usePlugins;
78
+
79
+ if ((_options$usePlugins = options.usePlugins) !== null && _options$usePlugins !== void 0 && _options$usePlugins.length) {
80
+ options.usePlugins.forEach(plugin => {
81
+ usePlugin(createPlugin(plugin.setup, plugin));
82
+ });
83
+ }
84
+
85
+ if (options.registerHook) {
86
+ registerHook(options.registerHook);
87
+ }
88
+
89
+ return _objectSpread(_objectSpread(_objectSpread({}, DEFAULT_OPTIONS), {}, {
90
+ name: `No.${index++} plugin`
91
+ }, options), {}, {
92
+ SYNC_PLUGIN_SYMBOL,
93
+ setup
94
+ });
78
95
  };
79
96
 
80
97
  const clear = () => {
@@ -86,9 +103,12 @@ const createManager = processes => {
86
103
  const init = options => {
87
104
  const container = (options === null || options === void 0 ? void 0 : options.container) || currentContainer;
88
105
  const sortedPlugins = sortPlugins(plugins);
106
+
107
+ const mergedPluginAPI = _objectSpread(_objectSpread({}, pluginAPI), overrideAPI);
108
+
89
109
  checkPlugins(sortedPlugins);
90
- const hooksList = sortedPlugins.map(plugin => (0, _farrowPipeline.runWithContainer)(() => plugin.initializer(), container));
91
- return generateRunner(hooksList, container, processes);
110
+ const hooksList = sortedPlugins.map(plugin => (0, _farrowPipeline.runWithContainer)(() => plugin.setup(mergedPluginAPI), container));
111
+ return generateRunner(hooksList, container, currentHooks);
92
112
  };
93
113
 
94
114
  const run = (cb, options) => {
@@ -96,17 +116,18 @@ const createManager = processes => {
96
116
  return (0, _farrowPipeline.runWithContainer)(cb, container);
97
117
  };
98
118
 
99
- return {
119
+ const manager = {
100
120
  createPlugin,
101
121
  isPlugin,
102
122
  usePlugin,
103
123
  init,
104
124
  clear,
105
125
  run,
106
- registe,
126
+ registerHook,
107
127
  useRunner: _runner.useRunner,
108
128
  clone
109
129
  };
130
+ return manager;
110
131
  };
111
132
 
112
133
  return clone();
@@ -114,11 +135,11 @@ const createManager = processes => {
114
135
 
115
136
  exports.createManager = createManager;
116
137
 
117
- const generateRunner = (hooksList, container, processes) => {
138
+ const generateRunner = (hooksList, container, hooksMap) => {
118
139
  const runner = {};
119
- const cloneShape = cloneProgressRecord(processes);
140
+ const cloneShape = closeHooksMap(hooksMap);
120
141
 
121
- if (processes) {
142
+ if (hooksMap) {
122
143
  for (const key in cloneShape) {
123
144
  for (const hooks of hooksList) {
124
145
  if (!hooks) {
@@ -126,8 +147,6 @@ const generateRunner = (hooksList, container, processes) => {
126
147
  }
127
148
 
128
149
  if (hooks[key]) {
129
- // eslint-disable-next-line @typescript-eslint/ban-ts-comment
130
- // @ts-expect-error
131
150
  cloneShape[key].use(hooks[key]);
132
151
  }
133
152
  } // eslint-disable-next-line @typescript-eslint/ban-ts-comment
@@ -146,39 +165,38 @@ const generateRunner = (hooksList, container, processes) => {
146
165
 
147
166
  exports.generateRunner = generateRunner;
148
167
 
149
- const cloneProgress = progress => {
150
- if ((0, _waterfall.isWaterfall)(progress)) {
168
+ const cloneHook = hook => {
169
+ if ((0, _waterfall.isWaterfall)(hook)) {
151
170
  return (0, _waterfall.createWaterfall)();
152
171
  }
153
172
 
154
- if ((0, _waterfall.isAsyncWaterfall)(progress)) {
173
+ if ((0, _waterfall.isAsyncWaterfall)(hook)) {
155
174
  return (0, _waterfall.createAsyncWaterfall)();
156
175
  }
157
176
 
158
- if ((0, _workflow.isWorkflow)(progress)) {
177
+ if ((0, _workflow.isWorkflow)(hook)) {
159
178
  return (0, _workflow.createWorkflow)();
160
179
  }
161
180
 
162
- if ((0, _workflow.isAsyncWorkflow)(progress)) {
181
+ if ((0, _workflow.isAsyncWorkflow)(hook)) {
163
182
  return (0, _workflow.createAsyncWorkflow)();
164
183
  }
165
184
 
166
- if ((0, _workflow.isParallelWorkflow)(progress)) {
185
+ if ((0, _workflow.isParallelWorkflow)(hook)) {
167
186
  return (0, _workflow.createParallelWorkflow)();
168
187
  }
169
188
 
170
- if ((0, _farrowPipeline.isPipeline)(progress)) {
189
+ if ((0, _farrowPipeline.isPipeline)(hook)) {
171
190
  return (0, _farrowPipeline.createPipeline)();
172
191
  } // eslint-disable-next-line @typescript-eslint/restrict-template-expressions
173
192
 
174
193
 
175
- throw new Error(`Unknown progress: ${progress}`);
176
- }; // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
177
-
194
+ throw new Error(`Unknown hook: ${hook}`);
195
+ };
178
196
 
179
- exports.cloneProgress = cloneProgress;
197
+ exports.cloneHook = cloneHook;
180
198
 
181
- const cloneProgressRecord = record => {
199
+ const closeHooksMap = record => {
182
200
  if (!record) {
183
201
  return record;
184
202
  }
@@ -188,13 +206,13 @@ const cloneProgressRecord = record => {
188
206
  for (const key in record) {
189
207
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
190
208
  // @ts-expect-error
191
- result[key] = cloneProgress(record[key]);
209
+ result[key] = cloneHook(record[key]);
192
210
  }
193
211
 
194
212
  return result;
195
213
  };
196
214
 
197
- exports.cloneProgressRecord = cloneProgressRecord;
215
+ exports.closeHooksMap = closeHooksMap;
198
216
 
199
217
  const includePlugin = (plugins, input) => {
200
218
  for (const plugin of plugins) {
@@ -250,6 +268,10 @@ const checkPlugins = plugins => {
250
268
  }
251
269
  };
252
270
 
271
+ const isObject = obj => obj !== null && typeof obj === 'object';
272
+
273
+ exports.isObject = isObject;
274
+
253
275
  const hasOwnProperty = (obj, prop) => obj.hasOwnProperty(prop);
254
276
 
255
277
  exports.hasOwnProperty = hasOwnProperty;
File without changes
@@ -28,7 +28,6 @@ const getAsyncBrook = input => {
28
28
 
29
29
  exports.getAsyncBrook = getAsyncBrook;
30
30
 
31
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
32
31
  const createAsyncWaterfall = () => {
33
32
  const pipeline = (0, _farrowPipeline.createAsyncPipeline)();
34
33
 
@@ -28,7 +28,6 @@ const getBrook = input => {
28
28
 
29
29
  exports.getBrook = getBrook;
30
30
 
31
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
32
31
  const createWaterfall = () => {
33
32
  const pipeline = (0, _farrowPipeline.createPipeline)();
34
33
 
@@ -15,8 +15,7 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
15
15
 
16
16
  const ASYNC_WORKFLOW_SYMBOL = Symbol('ASYNC_WORKFLOW_SYMBOL');
17
17
 
18
- const isAsyncWorkflow = input => Boolean(input === null || input === void 0 ? void 0 : input[ASYNC_WORKFLOW_SYMBOL]); // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
19
-
18
+ const isAsyncWorkflow = input => Boolean(input === null || input === void 0 ? void 0 : input[ASYNC_WORKFLOW_SYMBOL]);
20
19
 
21
20
  exports.isAsyncWorkflow = isAsyncWorkflow;
22
21
 
@@ -15,7 +15,6 @@ function _defineProperty(obj, key, value) { if (key in obj) { Object.definePrope
15
15
 
16
16
  const WORKFLOW_SYMBOL = Symbol('WORKFLOW_SYMBOL');
17
17
 
18
- // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
19
18
  const createWorkflow = () => {
20
19
  const pipeline = (0, _farrowPipeline.createPipeline)();
21
20
 
@@ -25,35 +25,35 @@ function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { va
25
25
  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; }
26
26
 
27
27
  import { runWithContainer, createContainer } from 'farrow-pipeline';
28
- import { generateRunner, hasOwnProperty, DEFAULT_OPTIONS } from "./sync";
29
- import { useRunner } from "./runner"; // eslint-disable-next-line @typescript-eslint/no-invalid-void-type
30
-
28
+ import { isObject, generateRunner, hasOwnProperty, DEFAULT_OPTIONS } from "./sync";
29
+ import { useRunner } from "./runner";
31
30
  var ASYNC_PLUGIN_SYMBOL = 'ASYNC_PLUGIN_SYMBOL';
32
- export var createAsyncManager = function createAsyncManager(processes) {
31
+ export var createAsyncManager = function createAsyncManager(hooks, api) {
33
32
  var index = 0;
34
33
 
35
- var createPlugin = function createPlugin(initializer) {
36
- var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
37
- return _objectSpread(_objectSpread(_objectSpread({}, DEFAULT_OPTIONS), {}, {
38
- name: "No.".concat(index++, " plugin")
39
- }, options), {}, {
40
- ASYNC_PLUGIN_SYMBOL: ASYNC_PLUGIN_SYMBOL,
41
- initializer: initializer
42
- });
34
+ var currentHooks = _objectSpread({}, hooks);
35
+
36
+ var registerHook = function registerHook(extraHooks) {
37
+ currentHooks = _objectSpread(_objectSpread({}, extraHooks), currentHooks);
43
38
  };
44
39
 
45
40
  var isPlugin = function isPlugin(input) {
46
- return hasOwnProperty(input, ASYNC_PLUGIN_SYMBOL) && input[ASYNC_PLUGIN_SYMBOL] === ASYNC_PLUGIN_SYMBOL;
41
+ return isObject(input) && hasOwnProperty(input, ASYNC_PLUGIN_SYMBOL) && input[ASYNC_PLUGIN_SYMBOL] === ASYNC_PLUGIN_SYMBOL;
47
42
  };
48
43
 
49
- var registe = function registe(extraProcesses) {
50
- // eslint-disable-next-line no-param-reassign
51
- processes = _objectSpread(_objectSpread({}, extraProcesses), processes);
52
- };
44
+ var pluginAPI = _objectSpread(_objectSpread({}, api), {}, {
45
+ useHookRunners: useRunner
46
+ });
53
47
 
54
- var clone = function clone() {
48
+ var clone = function clone(overrideAPI) {
55
49
  var plugins = [];
56
50
 
51
+ var addPlugin = function addPlugin(plugin) {
52
+ if (!includeAsyncPlugin(plugins, plugin)) {
53
+ plugins.push(_objectSpread({}, plugin));
54
+ }
55
+ };
56
+
57
57
  var usePlugin = function usePlugin() {
58
58
  for (var _len = arguments.length, input = new Array(_len), _key = 0; _key < _len; _key++) {
59
59
  input[_key] = arguments[_key];
@@ -63,11 +63,11 @@ export var createAsyncManager = function createAsyncManager(processes) {
63
63
  var plugin = _input[_i];
64
64
 
65
65
  if (isPlugin(plugin)) {
66
- if (!includeAsyncPlugin(plugins, plugin)) {
67
- plugins.push(_objectSpread(_objectSpread({}, plugin), {}, {
68
- index: plugins.length
69
- }));
70
- }
66
+ addPlugin(plugin);
67
+ } else if (typeof plugin === 'function') {
68
+ var _options = plugin();
69
+
70
+ addPlugin(createPlugin(_options.setup, _options));
71
71
  } else {
72
72
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
73
73
  // @ts-expect-error
@@ -78,6 +78,30 @@ export var createAsyncManager = function createAsyncManager(processes) {
78
78
  return manager;
79
79
  };
80
80
 
81
+ var createPlugin = function createPlugin() {
82
+ var _options$usePlugins;
83
+
84
+ var setup = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : function () {};
85
+ var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
86
+
87
+ if ((_options$usePlugins = options.usePlugins) !== null && _options$usePlugins !== void 0 && _options$usePlugins.length) {
88
+ options.usePlugins.forEach(function (plugin) {
89
+ usePlugin(createPlugin(plugin.setup, plugin));
90
+ });
91
+ }
92
+
93
+ if (options.registerHook) {
94
+ registerHook(options.registerHook);
95
+ }
96
+
97
+ return _objectSpread(_objectSpread(_objectSpread({}, DEFAULT_OPTIONS), {}, {
98
+ name: "No.".concat(index++, " plugin")
99
+ }, options), {}, {
100
+ ASYNC_PLUGIN_SYMBOL: ASYNC_PLUGIN_SYMBOL,
101
+ setup: setup
102
+ });
103
+ };
104
+
81
105
  var clear = function clear() {
82
106
  plugins = [];
83
107
  };
@@ -86,26 +110,27 @@ export var createAsyncManager = function createAsyncManager(processes) {
86
110
 
87
111
  var init = /*#__PURE__*/function () {
88
112
  var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(options) {
89
- var container, sortedPlugins, hooksList;
113
+ var container, sortedPlugins, mergedPluginAPI, hooksList;
90
114
  return _regeneratorRuntime.wrap(function _callee$(_context) {
91
115
  while (1) {
92
116
  switch (_context.prev = _context.next) {
93
117
  case 0:
94
118
  container = (options === null || options === void 0 ? void 0 : options.container) || currentContainer;
95
119
  sortedPlugins = sortAsyncPlugins(plugins);
120
+ mergedPluginAPI = _objectSpread(_objectSpread({}, pluginAPI), overrideAPI);
96
121
  checkAsyncPlugins(sortedPlugins);
97
- _context.next = 5;
122
+ _context.next = 6;
98
123
  return Promise.all(sortedPlugins.map(function (plugin) {
99
124
  return runWithContainer(function () {
100
- return plugin.initializer();
125
+ return plugin.setup(mergedPluginAPI);
101
126
  }, container);
102
127
  }));
103
128
 
104
- case 5:
129
+ case 6:
105
130
  hooksList = _context.sent;
106
- return _context.abrupt("return", generateRunner(hooksList, container, processes));
131
+ return _context.abrupt("return", generateRunner(hooksList, container, currentHooks));
107
132
 
108
- case 7:
133
+ case 8:
109
134
  case "end":
110
135
  return _context.stop();
111
136
  }
@@ -131,7 +156,7 @@ export var createAsyncManager = function createAsyncManager(processes) {
131
156
  run: run,
132
157
  clear: clear,
133
158
  clone: clone,
134
- registe: registe,
159
+ registerHook: registerHook,
135
160
  useRunner: useRunner
136
161
  };
137
162
  return manager;
@@ -1,3 +1,4 @@
1
1
  export * from "./sync";
2
2
  export * from "./async";
3
- export * from "./runner";
3
+ export * from "./runner";
4
+ export * from "./types";