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