@modern-js/plugin 1.21.2-beta.1 → 1.21.2-beta.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.
Files changed (69) hide show
  1. package/dist/js/modern/farrow-pipeline/context.js +38 -0
  2. package/dist/js/modern/farrow-pipeline/counter.js +18 -0
  3. package/dist/js/modern/farrow-pipeline/index.js +5 -0
  4. package/dist/js/modern/farrow-pipeline/pipeline.js +76 -0
  5. package/dist/js/modern/index.js +4 -0
  6. package/dist/js/modern/manager/async.js +112 -0
  7. package/dist/js/modern/manager/index.js +3 -0
  8. package/dist/js/modern/manager/shared.js +44 -0
  9. package/dist/js/modern/manager/sync.js +188 -0
  10. package/dist/js/modern/manager/types.js +0 -0
  11. package/dist/js/modern/waterfall/async.js +47 -0
  12. package/dist/js/modern/waterfall/index.js +2 -0
  13. package/dist/js/modern/waterfall/sync.js +47 -0
  14. package/dist/js/modern/workflow/async.js +43 -0
  15. package/dist/js/modern/workflow/index.js +3 -0
  16. package/dist/js/modern/workflow/parallel.js +31 -0
  17. package/dist/js/modern/workflow/sync.js +34 -0
  18. package/dist/js/node/farrow-pipeline/context.js +47 -0
  19. package/dist/js/node/farrow-pipeline/counter.js +27 -0
  20. package/dist/js/node/farrow-pipeline/index.js +18 -0
  21. package/dist/js/node/farrow-pipeline/pipeline.js +94 -0
  22. package/dist/js/node/index.js +57 -0
  23. package/dist/js/node/manager/async.js +124 -0
  24. package/dist/js/node/manager/index.js +44 -0
  25. package/dist/js/node/manager/shared.js +64 -0
  26. package/dist/js/node/manager/sync.js +212 -0
  27. package/dist/js/node/manager/types.js +0 -0
  28. package/dist/js/node/waterfall/async.js +64 -0
  29. package/dist/js/node/waterfall/index.js +31 -0
  30. package/dist/js/node/waterfall/sync.js +64 -0
  31. package/dist/js/node/workflow/async.js +57 -0
  32. package/dist/js/node/workflow/index.js +44 -0
  33. package/dist/js/node/workflow/parallel.js +45 -0
  34. package/dist/js/node/workflow/sync.js +48 -0
  35. package/dist/js/treeshaking/farrow-pipeline/context.js +42 -0
  36. package/dist/js/treeshaking/farrow-pipeline/counter.js +23 -0
  37. package/dist/js/treeshaking/farrow-pipeline/index.js +5 -0
  38. package/dist/js/treeshaking/farrow-pipeline/pipeline.js +81 -0
  39. package/dist/js/treeshaking/index.js +4 -0
  40. package/dist/js/treeshaking/manager/async.js +146 -0
  41. package/dist/js/treeshaking/manager/index.js +3 -0
  42. package/dist/js/treeshaking/manager/shared.js +79 -0
  43. package/dist/js/treeshaking/manager/sync.js +216 -0
  44. package/dist/js/treeshaking/manager/types.js +0 -0
  45. package/dist/js/treeshaking/waterfall/async.js +84 -0
  46. package/dist/js/treeshaking/waterfall/index.js +2 -0
  47. package/dist/js/treeshaking/waterfall/sync.js +59 -0
  48. package/dist/js/treeshaking/workflow/async.js +108 -0
  49. package/dist/js/treeshaking/workflow/index.js +3 -0
  50. package/dist/js/treeshaking/workflow/parallel.js +62 -0
  51. package/dist/js/treeshaking/workflow/sync.js +42 -0
  52. package/dist/types/farrow-pipeline/context.d.ts +13 -0
  53. package/dist/types/farrow-pipeline/counter.d.ts +11 -0
  54. package/dist/types/farrow-pipeline/index.d.ts +5 -0
  55. package/dist/types/farrow-pipeline/pipeline.d.ts +29 -0
  56. package/dist/types/index.d.ts +4 -0
  57. package/dist/types/manager/async.d.ts +63 -0
  58. package/dist/types/manager/index.d.ts +3 -0
  59. package/dist/types/manager/shared.d.ts +12 -0
  60. package/dist/types/manager/sync.d.ts +75 -0
  61. package/dist/types/manager/types.d.ts +36 -0
  62. package/dist/types/waterfall/async.d.ts +21 -0
  63. package/dist/types/waterfall/index.d.ts +2 -0
  64. package/dist/types/waterfall/sync.d.ts +20 -0
  65. package/dist/types/workflow/async.d.ts +12 -0
  66. package/dist/types/workflow/index.d.ts +3 -0
  67. package/dist/types/workflow/parallel.d.ts +10 -0
  68. package/dist/types/workflow/sync.d.ts +11 -0
  69. package/package.json +1 -1
@@ -0,0 +1,34 @@
1
+ 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; }
2
+
3
+ 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; }
4
+
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
+
7
+ import { createPipeline } from "../farrow-pipeline";
8
+ const WORKFLOW_SYMBOL = Symbol.for('MODERN_WORKFLOW');
9
+ export const createWorkflow = () => {
10
+ const pipeline = createPipeline();
11
+
12
+ const use = (...input) => {
13
+ pipeline.use(...input.map(mapWorkerToMiddleware));
14
+ return workflow;
15
+ };
16
+
17
+ const run = input => {
18
+ const result = pipeline.run(input, {
19
+ onLast: () => []
20
+ });
21
+ return result.filter(Boolean);
22
+ };
23
+
24
+ const workflow = _objectSpread(_objectSpread({}, pipeline), {}, {
25
+ use,
26
+ run,
27
+ [WORKFLOW_SYMBOL]: true
28
+ });
29
+
30
+ return workflow;
31
+ };
32
+ export const isWorkflow = input => Boolean(input === null || input === void 0 ? void 0 : input[WORKFLOW_SYMBOL]);
33
+
34
+ const mapWorkerToMiddleware = worker => (input, next) => [worker(input), ...next(input)];
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createContext = void 0;
7
+
8
+ /**
9
+ * modified from https://github.com/farrow-js/farrow/tree/master/packages/farrow-pipeline
10
+ * license at https://github.com/farrow-js/farrow/blob/master/LICENSE
11
+ */
12
+ const createContext = value => {
13
+ let currentValue;
14
+
15
+ const create = value => {
16
+ currentValue = value;
17
+
18
+ const use = () => ({
19
+ get value() {
20
+ return currentValue;
21
+ },
22
+
23
+ set value(v) {
24
+ currentValue = v;
25
+ }
26
+
27
+ });
28
+
29
+ const get = () => currentValue;
30
+
31
+ const set = v => {
32
+ currentValue = v;
33
+ };
34
+
35
+ const Context = {
36
+ create,
37
+ use,
38
+ get,
39
+ set
40
+ };
41
+ return Context;
42
+ };
43
+
44
+ return create(value);
45
+ };
46
+
47
+ exports.createContext = createContext;
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createCounter = void 0;
7
+
8
+ /**
9
+ * modified from https://github.com/farrow-js/farrow/tree/master/packages/farrow-pipeline
10
+ * license at https://github.com/farrow-js/farrow/blob/master/LICENSE
11
+ */
12
+ const createCounter = callback => {
13
+ const dispatch = (index, input) => {
14
+ const next = (nextInput = input) => dispatch(index + 1, nextInput);
15
+
16
+ return callback(index, input, next);
17
+ };
18
+
19
+ const start = input => dispatch(0, input);
20
+
21
+ return {
22
+ start,
23
+ dispatch
24
+ };
25
+ };
26
+
27
+ exports.createCounter = createCounter;
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+
7
+ var _pipeline = require("./pipeline");
8
+
9
+ Object.keys(_pipeline).forEach(function (key) {
10
+ if (key === "default" || key === "__esModule") return;
11
+ if (key in exports && exports[key] === _pipeline[key]) return;
12
+ Object.defineProperty(exports, key, {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _pipeline[key];
16
+ }
17
+ });
18
+ });
@@ -0,0 +1,94 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createAsyncPipeline = void 0;
7
+ Object.defineProperty(exports, "createContext", {
8
+ enumerable: true,
9
+ get: function () {
10
+ return _context.createContext;
11
+ }
12
+ });
13
+ exports.isPipeline = exports.createPipeline = void 0;
14
+
15
+ var _context = require("./context");
16
+
17
+ var _counter = require("./counter");
18
+
19
+ 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; }
20
+
21
+ 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; }
22
+
23
+ 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; }
24
+
25
+ const isPipeline = input => Boolean(input === null || input === void 0 ? void 0 : input[PipelineSymbol]);
26
+
27
+ exports.isPipeline = isPipeline;
28
+ const PipelineSymbol = Symbol.for('MODERN_PIPELINE');
29
+
30
+ const getMiddleware = input => {
31
+ if (typeof input === 'function') {
32
+ return input;
33
+ } else if (input && typeof input.middleware === 'function') {
34
+ return input.middleware;
35
+ }
36
+
37
+ throw new Error(`${input} is not a Middleware`);
38
+ };
39
+
40
+ const createPipeline = () => {
41
+ const middlewares = [];
42
+
43
+ const use = (...inputs) => {
44
+ middlewares.push(...inputs.map(getMiddleware));
45
+ return pipeline;
46
+ };
47
+
48
+ const createCurrentCounter = onLast => {
49
+ return (0, _counter.createCounter)((index, input, next) => {
50
+ if (index >= middlewares.length) {
51
+ if (onLast) {
52
+ return onLast(input);
53
+ }
54
+
55
+ throw new Error(`Expect returning a value, but all middlewares just calling next()`);
56
+ }
57
+
58
+ return middlewares[index](input, next);
59
+ });
60
+ };
61
+
62
+ const currentCounter = createCurrentCounter();
63
+
64
+ const getCounter = options => {
65
+ if (!options) {
66
+ return currentCounter;
67
+ }
68
+
69
+ return createCurrentCounter(options === null || options === void 0 ? void 0 : options.onLast);
70
+ };
71
+
72
+ const run = (input, options) => getCounter(options).start(input);
73
+
74
+ const middleware = (input, next) => run(input, {
75
+ onLast: next
76
+ });
77
+
78
+ const pipeline = {
79
+ [PipelineSymbol]: true,
80
+ use,
81
+ run,
82
+ middleware
83
+ };
84
+ return pipeline;
85
+ };
86
+
87
+ exports.createPipeline = createPipeline;
88
+
89
+ const createAsyncPipeline = () => {
90
+ const pipeline = createPipeline();
91
+ return _objectSpread({}, pipeline);
92
+ };
93
+
94
+ exports.createAsyncPipeline = createAsyncPipeline;
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+
7
+ var _farrowPipeline = require("./farrow-pipeline");
8
+
9
+ Object.keys(_farrowPipeline).forEach(function (key) {
10
+ if (key === "default" || key === "__esModule") return;
11
+ if (key in exports && exports[key] === _farrowPipeline[key]) return;
12
+ Object.defineProperty(exports, key, {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _farrowPipeline[key];
16
+ }
17
+ });
18
+ });
19
+
20
+ var _waterfall = require("./waterfall");
21
+
22
+ Object.keys(_waterfall).forEach(function (key) {
23
+ if (key === "default" || key === "__esModule") return;
24
+ if (key in exports && exports[key] === _waterfall[key]) return;
25
+ Object.defineProperty(exports, key, {
26
+ enumerable: true,
27
+ get: function () {
28
+ return _waterfall[key];
29
+ }
30
+ });
31
+ });
32
+
33
+ var _workflow = require("./workflow");
34
+
35
+ Object.keys(_workflow).forEach(function (key) {
36
+ if (key === "default" || key === "__esModule") return;
37
+ if (key in exports && exports[key] === _workflow[key]) return;
38
+ Object.defineProperty(exports, key, {
39
+ enumerable: true,
40
+ get: function () {
41
+ return _workflow[key];
42
+ }
43
+ });
44
+ });
45
+
46
+ var _manager = require("./manager");
47
+
48
+ Object.keys(_manager).forEach(function (key) {
49
+ if (key === "default" || key === "__esModule") return;
50
+ if (key in exports && exports[key] === _manager[key]) return;
51
+ Object.defineProperty(exports, key, {
52
+ enumerable: true,
53
+ get: function () {
54
+ return _manager[key];
55
+ }
56
+ });
57
+ });
@@ -0,0 +1,124 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.createAsyncManager = void 0;
7
+
8
+ var _sync = require("./sync");
9
+
10
+ var _shared = require("./shared");
11
+
12
+ 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; }
13
+
14
+ 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; }
15
+
16
+ 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; }
17
+
18
+ const ASYNC_PLUGIN_SYMBOL = 'ASYNC_PLUGIN_SYMBOL';
19
+
20
+ const createAsyncManager = (hooks, api) => {
21
+ let index = 0;
22
+ let runners;
23
+
24
+ let currentHooks = _objectSpread({}, hooks);
25
+
26
+ const useRunner = () => runners;
27
+
28
+ const registerHook = extraHooks => {
29
+ currentHooks = _objectSpread(_objectSpread({}, extraHooks), currentHooks);
30
+ };
31
+
32
+ const isPlugin = input => (0, _shared.isObject)(input) && (0, _shared.hasOwnProperty)(input, ASYNC_PLUGIN_SYMBOL) && input[ASYNC_PLUGIN_SYMBOL] === ASYNC_PLUGIN_SYMBOL;
33
+
34
+ const pluginAPI = _objectSpread(_objectSpread({}, api), {}, {
35
+ useHookRunners: useRunner
36
+ });
37
+
38
+ const clone = overrideAPI => {
39
+ let plugins = [];
40
+
41
+ const addPlugin = plugin => {
42
+ if (!(0, _shared.includePlugin)(plugins, plugin)) {
43
+ plugins.push(_objectSpread({}, plugin));
44
+ }
45
+ };
46
+
47
+ const usePlugin = (...input) => {
48
+ for (const plugin of input) {
49
+ // already created by createPlugin
50
+ if (isPlugin(plugin)) {
51
+ addPlugin(plugin);
52
+ } // using function to return plugin options
53
+ else if (typeof plugin === 'function') {
54
+ const options = plugin();
55
+ addPlugin(createPlugin(options.setup, options));
56
+ } // plain plugin object
57
+ else if ((0, _shared.isObject)(plugin)) {
58
+ addPlugin(createPlugin(plugin.setup, plugin));
59
+ } // unknown plugin
60
+ else {
61
+ console.warn(`Unknown plugin: ${JSON.stringify(plugin)}`);
62
+ }
63
+ }
64
+
65
+ return manager;
66
+ };
67
+
68
+ const createPlugin = ( // eslint-disable-next-line @typescript-eslint/no-empty-function
69
+ setup = () => {}, options = {}) => {
70
+ var _options$usePlugins;
71
+
72
+ if ((_options$usePlugins = options.usePlugins) !== null && _options$usePlugins !== void 0 && _options$usePlugins.length) {
73
+ options.usePlugins.forEach(plugin => {
74
+ usePlugin(createPlugin(plugin.setup, plugin));
75
+ });
76
+ }
77
+
78
+ if (options.registerHook) {
79
+ registerHook(options.registerHook);
80
+ }
81
+
82
+ return _objectSpread(_objectSpread(_objectSpread({}, _sync.DEFAULT_OPTIONS), {}, {
83
+ name: `No.${index++} plugin`
84
+ }, options), {}, {
85
+ ASYNC_PLUGIN_SYMBOL,
86
+ setup
87
+ });
88
+ };
89
+
90
+ const clear = () => {
91
+ plugins = [];
92
+ };
93
+
94
+ const init = async () => {
95
+ const sortedPlugins = (0, _shared.sortPlugins)(plugins);
96
+
97
+ const mergedPluginAPI = _objectSpread(_objectSpread({}, pluginAPI), overrideAPI);
98
+
99
+ (0, _shared.checkPlugins)(sortedPlugins);
100
+ const hooksList = await Promise.all(sortedPlugins.map(plugin => plugin.setup(mergedPluginAPI)));
101
+ runners = (0, _sync.generateRunner)(hooksList, currentHooks);
102
+ return runners;
103
+ };
104
+
105
+ const run = cb => cb();
106
+
107
+ const manager = {
108
+ createPlugin,
109
+ isPlugin,
110
+ usePlugin,
111
+ init,
112
+ run,
113
+ clear,
114
+ clone,
115
+ registerHook,
116
+ useRunner
117
+ };
118
+ return manager;
119
+ };
120
+
121
+ return clone();
122
+ };
123
+
124
+ exports.createAsyncManager = createAsyncManager;
@@ -0,0 +1,44 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+
7
+ var _sync = require("./sync");
8
+
9
+ Object.keys(_sync).forEach(function (key) {
10
+ if (key === "default" || key === "__esModule") return;
11
+ if (key in exports && exports[key] === _sync[key]) return;
12
+ Object.defineProperty(exports, key, {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _sync[key];
16
+ }
17
+ });
18
+ });
19
+
20
+ var _async = require("./async");
21
+
22
+ Object.keys(_async).forEach(function (key) {
23
+ if (key === "default" || key === "__esModule") return;
24
+ if (key in exports && exports[key] === _async[key]) return;
25
+ Object.defineProperty(exports, key, {
26
+ enumerable: true,
27
+ get: function () {
28
+ return _async[key];
29
+ }
30
+ });
31
+ });
32
+
33
+ var _types = require("./types");
34
+
35
+ Object.keys(_types).forEach(function (key) {
36
+ if (key === "default" || key === "__esModule") return;
37
+ if (key in exports && exports[key] === _types[key]) return;
38
+ Object.defineProperty(exports, key, {
39
+ enumerable: true,
40
+ get: function () {
41
+ return _types[key];
42
+ }
43
+ });
44
+ });
@@ -0,0 +1,64 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.isObject = exports.includePlugin = exports.hasOwnProperty = exports.checkPlugins = void 0;
7
+ exports.sortPlugins = sortPlugins;
8
+
9
+ const checkPlugins = plugins => {
10
+ plugins.forEach(origin => {
11
+ origin.rivals.forEach(rival => {
12
+ plugins.forEach(plugin => {
13
+ if (rival === plugin.name) {
14
+ throw new Error(`${origin.name} has rival ${plugin.name}`);
15
+ }
16
+ });
17
+ });
18
+ origin.required.forEach(required => {
19
+ if (!plugins.some(plugin => plugin.name === required)) {
20
+ throw new Error(`The plugin: ${required} is required when plugin: ${origin.name} is exist.`);
21
+ }
22
+ });
23
+ });
24
+ };
25
+
26
+ exports.checkPlugins = checkPlugins;
27
+
28
+ function sortPlugins(input) {
29
+ let plugins = input.slice();
30
+
31
+ for (let i = 0; i < plugins.length; i++) {
32
+ const plugin = plugins[i];
33
+
34
+ for (const pre of plugin.pre) {
35
+ for (let j = i + 1; j < plugins.length; j++) {
36
+ if (plugins[j].name === pre) {
37
+ plugins = [...plugins.slice(0, i), plugins[j], ...plugins.slice(i, j), ...plugins.slice(j + 1, plugins.length)];
38
+ }
39
+ }
40
+ }
41
+
42
+ for (const post of plugin.post) {
43
+ for (let j = 0; j < i; j++) {
44
+ if (plugins[j].name === post) {
45
+ plugins = [...plugins.slice(0, j), ...plugins.slice(j + 1, i + 1), plugins[j], ...plugins.slice(i + 1, plugins.length)];
46
+ }
47
+ }
48
+ }
49
+ }
50
+
51
+ return plugins;
52
+ }
53
+
54
+ const includePlugin = (plugins, input) => plugins.some(plugin => plugin.name === input.name);
55
+
56
+ exports.includePlugin = includePlugin;
57
+
58
+ const isObject = obj => obj !== null && typeof obj === 'object';
59
+
60
+ exports.isObject = isObject;
61
+
62
+ const hasOwnProperty = (obj, prop) => obj.hasOwnProperty(prop);
63
+
64
+ exports.hasOwnProperty = hasOwnProperty;