@modern-js/plugin 2.11.0 → 2.13.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 (43) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/dist/cjs/farrow-pipeline/context.js +7 -16
  3. package/dist/cjs/farrow-pipeline/counter.js +1 -2
  4. package/dist/cjs/farrow-pipeline/index.js +71 -2
  5. package/dist/cjs/manager/async.js +9 -8
  6. package/dist/cjs/manager/shared.js +16 -15
  7. package/dist/cjs/manager/sync.js +4 -9
  8. package/dist/cjs/waterfall/async.js +3 -2
  9. package/dist/cjs/workflow/async.js +6 -5
  10. package/dist/cjs/workflow/parallel.js +1 -1
  11. package/dist/esm/index.js +743 -4
  12. package/dist/esm-node/farrow-pipeline/context.js +7 -16
  13. package/dist/esm-node/farrow-pipeline/counter.js +1 -2
  14. package/dist/esm-node/farrow-pipeline/index.js +60 -1
  15. package/dist/esm-node/manager/async.js +9 -8
  16. package/dist/esm-node/manager/shared.js +16 -15
  17. package/dist/esm-node/manager/sync.js +4 -9
  18. package/dist/esm-node/waterfall/async.js +3 -2
  19. package/dist/esm-node/workflow/async.js +6 -5
  20. package/dist/esm-node/workflow/parallel.js +1 -1
  21. package/dist/types/farrow-pipeline/context.d.ts +0 -1
  22. package/dist/types/farrow-pipeline/index.d.ts +25 -1
  23. package/package.json +3 -3
  24. package/dist/cjs/farrow-pipeline/pipeline.js +0 -86
  25. package/dist/esm/farrow-pipeline/context.js +0 -31
  26. package/dist/esm/farrow-pipeline/counter.js +0 -17
  27. package/dist/esm/farrow-pipeline/index.js +0 -1
  28. package/dist/esm/farrow-pipeline/pipeline.js +0 -112
  29. package/dist/esm/manager/async.js +0 -306
  30. package/dist/esm/manager/index.js +0 -3
  31. package/dist/esm/manager/shared.js +0 -35
  32. package/dist/esm/manager/sync.js +0 -226
  33. package/dist/esm/manager/types.js +0 -1
  34. package/dist/esm/utils/pluginDagSort.js +0 -70
  35. package/dist/esm/waterfall/async.js +0 -271
  36. package/dist/esm/waterfall/index.js +0 -2
  37. package/dist/esm/waterfall/sync.js +0 -127
  38. package/dist/esm/workflow/async.js +0 -293
  39. package/dist/esm/workflow/index.js +0 -3
  40. package/dist/esm/workflow/parallel.js +0 -249
  41. package/dist/esm/workflow/sync.js +0 -114
  42. package/dist/esm-node/farrow-pipeline/pipeline.js +0 -60
  43. package/dist/types/farrow-pipeline/pipeline.d.ts +0 -29
package/CHANGELOG.md CHANGED
@@ -1,5 +1,15 @@
1
1
  # @modern-js/plugin
2
2
 
3
+ ## 2.13.0
4
+
5
+ ### Patch Changes
6
+
7
+ - 78431f4: perf(plugin): reduce bundle size
8
+
9
+ perf(plugin): 优化包体积
10
+
11
+ ## 2.12.0
12
+
3
13
  ## 2.11.0
4
14
 
5
15
  ## 2.10.0
@@ -21,30 +21,21 @@ __export(context_exports, {
21
21
  });
22
22
  module.exports = __toCommonJS(context_exports);
23
23
  const createContext = (value) => {
24
- let currentValue;
25
- const create = (value2) => {
26
- currentValue = value2;
27
- const use = () => ({
24
+ let currentValue = value;
25
+ return {
26
+ use: () => ({
28
27
  get value() {
29
28
  return currentValue;
30
29
  },
31
30
  set value(v) {
32
31
  currentValue = v;
33
32
  }
34
- });
35
- const get = () => currentValue;
36
- const set = (v) => {
33
+ }),
34
+ get: () => currentValue,
35
+ set: (v) => {
37
36
  currentValue = v;
38
- };
39
- const Context = {
40
- create,
41
- use,
42
- get,
43
- set
44
- };
45
- return Context;
37
+ }
46
38
  };
47
- return create(value);
48
39
  };
49
40
  // Annotate the CommonJS export names for ESM import in node:
50
41
  0 && (module.exports = {
@@ -25,9 +25,8 @@ const createCounter = (callback) => {
25
25
  const next = (nextInput = input) => dispatch(index + 1, nextInput);
26
26
  return callback(index, input, next);
27
27
  };
28
- const start = (input) => dispatch(0, input);
29
28
  return {
30
- start,
29
+ start: (input) => dispatch(0, input),
31
30
  dispatch
32
31
  };
33
32
  };
@@ -2,6 +2,10 @@ var __defProp = Object.defineProperty;
2
2
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
3
3
  var __getOwnPropNames = Object.getOwnPropertyNames;
4
4
  var __hasOwnProp = Object.prototype.hasOwnProperty;
5
+ var __export = (target, all) => {
6
+ for (var name in all)
7
+ __defProp(target, name, { get: all[name], enumerable: true });
8
+ };
5
9
  var __copyProps = (to, from, except, desc) => {
6
10
  if (from && typeof from === "object" || typeof from === "function") {
7
11
  for (let key of __getOwnPropNames(from))
@@ -10,8 +14,73 @@ var __copyProps = (to, from, except, desc) => {
10
14
  }
11
15
  return to;
12
16
  };
13
- var __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
14
17
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
18
  var farrow_pipeline_exports = {};
19
+ __export(farrow_pipeline_exports, {
20
+ createAsyncPipeline: () => createAsyncPipeline,
21
+ createContext: () => import_context.createContext,
22
+ createPipeline: () => createPipeline,
23
+ isPipeline: () => isPipeline
24
+ });
16
25
  module.exports = __toCommonJS(farrow_pipeline_exports);
17
- __reExport(farrow_pipeline_exports, require("./pipeline"), module.exports);
26
+ var import_context = require("./context");
27
+ var import_counter = require("./counter");
28
+ const isPipeline = (input) => Boolean(input == null ? void 0 : input[PipelineSymbol]);
29
+ const PipelineSymbol = Symbol.for("MODERN_PIPELINE");
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
+ throw new Error(`${input} is not a Middleware`);
37
+ };
38
+ const createPipeline = () => {
39
+ const middlewares = [];
40
+ const use = (...inputs) => {
41
+ middlewares.push(...inputs.map(getMiddleware));
42
+ return pipeline;
43
+ };
44
+ const createCurrentCounter = (onLast) => {
45
+ return (0, import_counter.createCounter)((index, input, next) => {
46
+ if (index >= middlewares.length) {
47
+ if (onLast) {
48
+ return onLast(input);
49
+ }
50
+ throw new Error(
51
+ `Expect returning a value, but all middlewares just calling next()`
52
+ );
53
+ }
54
+ return middlewares[index](input, next);
55
+ });
56
+ };
57
+ const currentCounter = createCurrentCounter();
58
+ const getCounter = (options) => {
59
+ if (!options) {
60
+ return currentCounter;
61
+ }
62
+ return createCurrentCounter(options == null ? void 0 : options.onLast);
63
+ };
64
+ const run = (input, options) => getCounter(options).start(input);
65
+ const middleware = (input, next) => run(input, {
66
+ onLast: next
67
+ });
68
+ const pipeline = {
69
+ [PipelineSymbol]: true,
70
+ use,
71
+ run,
72
+ middleware
73
+ };
74
+ return pipeline;
75
+ };
76
+ const createAsyncPipeline = () => {
77
+ const pipeline = createPipeline();
78
+ return { ...pipeline };
79
+ };
80
+ // Annotate the CommonJS export names for ESM import in node:
81
+ 0 && (module.exports = {
82
+ createAsyncPipeline,
83
+ createContext,
84
+ createPipeline,
85
+ isPipeline
86
+ });
@@ -47,7 +47,7 @@ const createAsyncManager = (hooks, api) => {
47
47
  }
48
48
  };
49
49
  const usePlugin = (...input) => {
50
- for (const plugin of input) {
50
+ input.forEach((plugin) => {
51
51
  if (isPlugin(plugin)) {
52
52
  addPlugin(plugin);
53
53
  } else if (typeof plugin === "function") {
@@ -55,10 +55,10 @@ const createAsyncManager = (hooks, api) => {
55
55
  addPlugin(createPlugin(options.setup, options));
56
56
  } else if ((0, import_shared.isObject)(plugin)) {
57
57
  addPlugin(createPlugin(plugin.setup, plugin));
58
- } else {
58
+ } else if (process.env.NODE_ENV !== "production") {
59
59
  console.warn(`Unknown plugin: ${JSON.stringify(plugin)}`);
60
60
  }
61
- }
61
+ });
62
62
  return manager;
63
63
  };
64
64
  const createPlugin = (setup = () => {
@@ -83,18 +83,19 @@ const createAsyncManager = (hooks, api) => {
83
83
  const clear = () => {
84
84
  plugins = [];
85
85
  };
86
- const init = async () => {
86
+ const init = () => {
87
87
  const sortedPlugins = (0, import_shared.sortPlugins)(plugins);
88
88
  const mergedPluginAPI = {
89
89
  ...pluginAPI,
90
90
  ...overrideAPI
91
91
  };
92
92
  (0, import_shared.checkPlugins)(sortedPlugins);
93
- const hooksList = await Promise.all(
93
+ return Promise.all(
94
94
  sortedPlugins.map((plugin) => plugin.setup(mergedPluginAPI))
95
- );
96
- runners = (0, import_sync.generateRunner)(hooksList, currentHooks);
97
- return runners;
95
+ ).then((hooksList) => {
96
+ runners = (0, import_sync.generateRunner)(hooksList, currentHooks);
97
+ return runners;
98
+ });
98
99
  };
99
100
  const run = (cb) => cb();
100
101
  const manager = {
@@ -26,26 +26,27 @@ __export(shared_exports, {
26
26
  module.exports = __toCommonJS(shared_exports);
27
27
  var import_pluginDagSort = require("../utils/pluginDagSort");
28
28
  const checkPlugins = (plugins) => {
29
- plugins.forEach((origin) => {
30
- origin.rivals.forEach((rival) => {
31
- plugins.forEach((plugin) => {
32
- if (rival === plugin.name) {
33
- throw new Error(`${origin.name} has rival ${plugin.name}`);
29
+ if (process.env.NODE_ENV !== "production") {
30
+ plugins.forEach((origin) => {
31
+ origin.rivals.forEach((rival) => {
32
+ plugins.forEach((plugin) => {
33
+ if (rival === plugin.name) {
34
+ throw new Error(`${origin.name} has rival ${plugin.name}`);
35
+ }
36
+ });
37
+ });
38
+ origin.required.forEach((required) => {
39
+ if (!plugins.some((plugin) => plugin.name === required)) {
40
+ throw new Error(
41
+ `The plugin: ${required} is required when plugin: ${origin.name} is exist.`
42
+ );
34
43
  }
35
44
  });
36
45
  });
37
- origin.required.forEach((required) => {
38
- if (!plugins.some((plugin) => plugin.name === required)) {
39
- throw new Error(
40
- `The plugin: ${required} is required when plugin: ${origin.name} is exist.`
41
- );
42
- }
43
- });
44
- });
46
+ }
45
47
  };
46
48
  function sortPlugins(input) {
47
- const plugins = input.slice();
48
- return (0, import_pluginDagSort.dagSort)(plugins);
49
+ return (0, import_pluginDagSort.dagSort)(input.slice());
49
50
  }
50
51
  const includePlugin = (plugins, input) => plugins.some((plugin) => plugin.name === input.name);
51
52
  const isObject = (obj) => obj !== null && typeof obj === "object";
@@ -70,7 +70,7 @@ const createManager = (hooks, api) => {
70
70
  addPlugin(createPlugin(options.setup, options));
71
71
  } else if ((0, import_shared.isObject)(plugin)) {
72
72
  addPlugin(createPlugin(plugin.setup, plugin));
73
- } else {
73
+ } else if (process.env.NODE_ENV !== "production") {
74
74
  console.warn(`Unknown plugin: ${JSON.stringify(plugin)}`);
75
75
  }
76
76
  });
@@ -132,17 +132,12 @@ const generateRunner = (hooksList, hooksMap) => {
132
132
  const cloneShape = cloneHooksMap(hooksMap);
133
133
  if (hooksMap) {
134
134
  for (const key in cloneShape) {
135
- for (const hooks of hooksList) {
136
- if (!hooks) {
137
- continue;
138
- }
139
- if (hooks[key]) {
135
+ hooksList.forEach((hooks) => {
136
+ if (hooks == null ? void 0 : hooks[key]) {
140
137
  cloneShape[key].use(hooks[key]);
141
138
  }
142
- }
143
- runner[key] = (input, options) => cloneShape[key].run(input, {
144
- ...options
145
139
  });
140
+ runner[key] = (input, options) => cloneShape[key].run(input, { ...options });
146
141
  }
147
142
  }
148
143
  return runner;
@@ -27,7 +27,8 @@ const ASYNC_WATERFALL_SYMBOL = Symbol.for("MODERN_ASYNC_WATERFALL");
27
27
  const getAsyncBrook = (input) => {
28
28
  if (typeof input === "function") {
29
29
  return input;
30
- } else if (input && typeof input.middleware === "function") {
30
+ }
31
+ if (input && typeof input.middleware === "function") {
31
32
  return input.middleware;
32
33
  }
33
34
  throw new Error(`${input} is not a AsyncBrook or { brook: AsyncBrook }`);
@@ -54,7 +55,7 @@ const createAsyncWaterfall = () => {
54
55
  return waterfall;
55
56
  };
56
57
  const isAsyncWaterfall = (input) => Boolean(input == null ? void 0 : input[ASYNC_WATERFALL_SYMBOL]);
57
- const mapAsyncBrookToAsyncMiddleware = (brook) => async (input, next) => next(await brook(input));
58
+ const mapAsyncBrookToAsyncMiddleware = (brook) => (input, next) => Promise.resolve(brook(input)).then((result) => next(result));
58
59
  // Annotate the CommonJS export names for ESM import in node:
59
60
  0 && (module.exports = {
60
61
  createAsyncWaterfall,
@@ -30,13 +30,12 @@ const createAsyncWorkflow = () => {
30
30
  pipeline.use(...input.map(mapAsyncWorkerToAsyncMiddleware));
31
31
  return workflow;
32
32
  };
33
- const run = async (input) => {
33
+ const run = (input) => {
34
34
  const result = pipeline.run(input, { onLast: () => [] });
35
35
  if (isPromise(result)) {
36
36
  return result.then((result2) => result2.filter(Boolean));
37
- } else {
38
- return result.filter(Boolean);
39
37
  }
38
+ return result.filter(Boolean);
40
39
  };
41
40
  const workflow = {
42
41
  ...pipeline,
@@ -46,9 +45,11 @@ const createAsyncWorkflow = () => {
46
45
  };
47
46
  return workflow;
48
47
  };
49
- const mapAsyncWorkerToAsyncMiddleware = (worker) => async (input, next) => [await worker(input), ...await next(input)];
48
+ const mapAsyncWorkerToAsyncMiddleware = (worker) => (input, next) => Promise.resolve(worker(input)).then(
49
+ (result) => Promise.resolve(next(input)).then((nextResult) => [result, ...nextResult])
50
+ );
50
51
  function isPromise(obj) {
51
- return Boolean(obj) && (typeof obj === "object" || typeof obj === "function") && typeof obj.then === "function";
52
+ return obj && typeof obj.then === "function";
52
53
  }
53
54
  // Annotate the CommonJS export names for ESM import in node:
54
55
  0 && (module.exports = {
@@ -30,7 +30,7 @@ const createParallelWorkflow = () => {
30
30
  pipeline.use(...input.map(mapParallelWorkerToAsyncMiddleware));
31
31
  return workflow;
32
32
  };
33
- const run = async (input) => Promise.all(pipeline.run(input, { onLast: () => [] })).then(
33
+ const run = (input) => Promise.all(pipeline.run(input, { onLast: () => [] })).then(
34
34
  (result) => result.filter(Boolean)
35
35
  );
36
36
  const workflow = {