@powerlines/plugin-webpack 0.5.133 → 0.5.135

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 (35) hide show
  1. package/dist/powerlines/src/internal/helpers/hooks.cjs +15 -12
  2. package/dist/powerlines/src/internal/helpers/hooks.d.cts +47 -0
  3. package/dist/powerlines/src/internal/helpers/hooks.d.mts +49 -0
  4. package/dist/powerlines/src/internal/helpers/hooks.mjs +15 -12
  5. package/dist/powerlines/src/lib/contexts/environment-context.cjs +108 -73
  6. package/dist/powerlines/src/lib/contexts/environment-context.mjs +109 -74
  7. package/dist/powerlines/src/lib/unplugin/helpers.cjs +13 -1
  8. package/dist/powerlines/src/lib/unplugin/helpers.mjs +12 -1
  9. package/dist/powerlines/src/lib/unplugin/index.cjs +1 -1
  10. package/dist/powerlines/src/lib/unplugin/index.mjs +1 -1
  11. package/dist/powerlines/src/lib/unplugin/plugin.cjs +3 -3
  12. package/dist/powerlines/src/lib/unplugin/plugin.mjs +3 -3
  13. package/dist/powerlines/src/plugin-utils/helpers.cjs +34 -15
  14. package/dist/powerlines/src/plugin-utils/helpers.mjs +33 -15
  15. package/dist/powerlines/src/types/api.d.cts +104 -0
  16. package/dist/powerlines/src/types/api.d.mts +104 -0
  17. package/dist/powerlines/src/types/build.cjs +11 -3
  18. package/dist/powerlines/src/types/build.d.cts +39 -3
  19. package/dist/powerlines/src/types/build.d.mts +39 -3
  20. package/dist/powerlines/src/types/build.mjs +10 -3
  21. package/dist/powerlines/src/types/config.d.cts +60 -5
  22. package/dist/powerlines/src/types/config.d.mts +60 -5
  23. package/dist/powerlines/src/types/context.d.cts +113 -1
  24. package/dist/powerlines/src/types/context.d.mts +113 -3
  25. package/dist/powerlines/src/types/hooks.d.cts +32 -0
  26. package/dist/powerlines/src/types/hooks.d.mts +32 -2
  27. package/dist/powerlines/src/types/plugin.cjs +5 -4
  28. package/dist/powerlines/src/types/plugin.d.cts +35 -61
  29. package/dist/powerlines/src/types/plugin.d.mts +35 -61
  30. package/dist/powerlines/src/types/plugin.mjs +6 -5
  31. package/dist/powerlines/src/types/resolved.d.cts +15 -4
  32. package/dist/powerlines/src/types/resolved.d.mts +15 -5
  33. package/dist/powerlines/src/types/unplugin.d.cts +22 -0
  34. package/dist/powerlines/src/types/unplugin.d.mts +23 -0
  35. package/package.json +5 -5
@@ -20,29 +20,32 @@ const mergeResults = (0, defu.createDefu)((obj, key, value) => {
20
20
  * Calls a hook with the given context, options, and arguments.
21
21
  *
22
22
  * @param context - The context to use when calling the hook.
23
- * @param hook - The hook to call.
23
+ * @param key - The hook to call.
24
24
  * @param options - Options for calling the hook.
25
25
  * @param args - Arguments to pass to the hook.
26
26
  * @returns The return value of the hook.
27
27
  */
28
- async function callHook(context, hook, options, ...args) {
29
- const handlers = context.selectHooks(hook, options);
30
- if (handlers.length > 0) {
31
- context.log(__storm_software_config_tools_types.LogLevelLabel.DEBUG, ` 🧩 Calling plugin hook: ${chalk.default.bold.cyanBright(`${hook}${options?.order ? ` (${options.order})` : ""}`)}`);
28
+ async function callHook(context, key, options, ...args) {
29
+ const hooks = context.selectHooks(key, options);
30
+ if (hooks.length > 0) {
31
+ context.log(__storm_software_config_tools_types.LogLevelLabel.DEBUG, ` 🧩 Calling plugin hook: ${chalk.default.bold.cyanBright(`${key}${options?.order ? ` (${options.order})` : ""}`)}`);
32
+ const invokeHook = async (hook, hookArgs) => {
33
+ return Reflect.apply(hook.handler, hook.context, hookArgs);
34
+ };
32
35
  let results = [];
33
- if (options?.sequential === false) results = await Promise.all(handlers.map(async (handler) => {
34
- if (!(0, __stryke_type_checks_is_function.isFunction)(handler.handle)) throw new Error(`Plugin hook handler for hook "${hook}" is not a function.`);
35
- return Promise.resolve(handler.handle.apply(handler.context, [...args]));
36
+ if (options?.sequential === false) results = await Promise.all(hooks.map(async (hook) => {
37
+ if (!(0, __stryke_type_checks_is_function.isFunction)(hook.handler)) throw new Error(`Plugin hook handler for hook "${key}" is not a function.`);
38
+ return invokeHook(hook, [...args]);
36
39
  }));
37
- else for (const handler of handlers) {
38
- if (!(0, __stryke_type_checks_is_function.isFunction)(handler.handle)) throw new Error(`Plugin hook handler for hook "${hook}" is not a function.`);
40
+ else for (const hook of hooks) {
41
+ if (!(0, __stryke_type_checks_is_function.isFunction)(hook.handler)) throw new Error(`Plugin hook handler for hook "${key}" is not a function.`);
39
42
  if (options?.result === "first" || options?.asNextParam === false) {
40
- results.push(await Promise.resolve(handler.handle.apply(handler.context, [...args])));
43
+ results.push(await Promise.resolve(invokeHook(hook, [...args])));
41
44
  if (options?.result === "first" && (0, __stryke_type_checks_is_set.isSet)(results[results.length - 1])) break;
42
45
  } else {
43
46
  const sequenceArgs = [...args];
44
47
  if (results.length > 0 && sequenceArgs.length > 0) sequenceArgs[0] = (0, __stryke_type_checks_is_function.isFunction)(options.asNextParam) ? await Promise.resolve(options.asNextParam(results[0])) : results[0];
45
- const result = await Promise.resolve(handler.handle.apply(handler.context, [...sequenceArgs]));
48
+ const result = await Promise.resolve(invokeHook(hook, [...sequenceArgs]));
46
49
  if (result) {
47
50
  if (options?.result === "last") results = [result];
48
51
  else if ((0, __stryke_type_checks_is_string.isString)(result)) results = [`${(0, __stryke_type_checks_is_string.isString)(results[0]) ? results[0] || "" : ""}\n${result || ""}`.trim()];
@@ -0,0 +1,47 @@
1
+ import { SelectHooksOptions } from "../../types/context.cjs";
2
+ import { MaybePromise } from "@stryke/types/base";
3
+
4
+ //#region ../powerlines/src/internal/helpers/hooks.d.ts
5
+ type CallHookOptions = SelectHooksOptions & (({
6
+ /**
7
+ * Whether to call the hooks sequentially or in parallel.
8
+ *
9
+ * @defaultValue true
10
+ */
11
+ sequential?: true;
12
+ } & ({
13
+ /**
14
+ * How to handle multiple return values from hooks.
15
+ * - "merge": Merge all non-undefined return values (if they are objects).
16
+ * - "first": Return the first non-undefined value.
17
+ *
18
+ * @remarks
19
+ * Merging only works if the return values are objects.
20
+ *
21
+ * @defaultValue "merge"
22
+ */
23
+ result: "first";
24
+ } | {
25
+ /**
26
+ * How to handle multiple return values from hooks.
27
+ * - "merge": Merge all non-undefined return values (if they are objects).
28
+ * - "first": Return the first non-undefined value.
29
+ *
30
+ * @remarks
31
+ * Merging only works if the return values are objects.
32
+ *
33
+ * @defaultValue "merge"
34
+ */
35
+ result?: "merge" | "last";
36
+ /**
37
+ * An indicator specifying if the results of the previous hook should be provided as the **first** parameter of the next hook function, or a function to process the result of the previous hook function and pass the returned value as the next hook's **first** parameter
38
+ */
39
+ asNextParam?: false | ((previousResult: any) => MaybePromise<any>);
40
+ })) | {
41
+ /**
42
+ * Whether to call the hooks sequentially or in parallel.
43
+ */
44
+ sequential: false;
45
+ });
46
+ //#endregion
47
+ export { CallHookOptions };
@@ -0,0 +1,49 @@
1
+ import "../../types/resolved.mjs";
2
+ import "../../types/hooks.mjs";
3
+ import { SelectHooksOptions } from "../../types/context.mjs";
4
+ import { MaybePromise } from "@stryke/types/base";
5
+
6
+ //#region ../powerlines/src/internal/helpers/hooks.d.ts
7
+ type CallHookOptions = SelectHooksOptions & (({
8
+ /**
9
+ * Whether to call the hooks sequentially or in parallel.
10
+ *
11
+ * @defaultValue true
12
+ */
13
+ sequential?: true;
14
+ } & ({
15
+ /**
16
+ * How to handle multiple return values from hooks.
17
+ * - "merge": Merge all non-undefined return values (if they are objects).
18
+ * - "first": Return the first non-undefined value.
19
+ *
20
+ * @remarks
21
+ * Merging only works if the return values are objects.
22
+ *
23
+ * @defaultValue "merge"
24
+ */
25
+ result: "first";
26
+ } | {
27
+ /**
28
+ * How to handle multiple return values from hooks.
29
+ * - "merge": Merge all non-undefined return values (if they are objects).
30
+ * - "first": Return the first non-undefined value.
31
+ *
32
+ * @remarks
33
+ * Merging only works if the return values are objects.
34
+ *
35
+ * @defaultValue "merge"
36
+ */
37
+ result?: "merge" | "last";
38
+ /**
39
+ * An indicator specifying if the results of the previous hook should be provided as the **first** parameter of the next hook function, or a function to process the result of the previous hook function and pass the returned value as the next hook's **first** parameter
40
+ */
41
+ asNextParam?: false | ((previousResult: any) => MaybePromise<any>);
42
+ })) | {
43
+ /**
44
+ * Whether to call the hooks sequentially or in parallel.
45
+ */
46
+ sequential: false;
47
+ });
48
+ //#endregion
49
+ export { CallHookOptions };
@@ -18,29 +18,32 @@ const mergeResults = createDefu((obj, key, value) => {
18
18
  * Calls a hook with the given context, options, and arguments.
19
19
  *
20
20
  * @param context - The context to use when calling the hook.
21
- * @param hook - The hook to call.
21
+ * @param key - The hook to call.
22
22
  * @param options - Options for calling the hook.
23
23
  * @param args - Arguments to pass to the hook.
24
24
  * @returns The return value of the hook.
25
25
  */
26
- async function callHook(context, hook, options, ...args) {
27
- const handlers = context.selectHooks(hook, options);
28
- if (handlers.length > 0) {
29
- context.log(LogLevelLabel.DEBUG, ` 🧩 Calling plugin hook: ${chalk.bold.cyanBright(`${hook}${options?.order ? ` (${options.order})` : ""}`)}`);
26
+ async function callHook(context, key, options, ...args) {
27
+ const hooks = context.selectHooks(key, options);
28
+ if (hooks.length > 0) {
29
+ context.log(LogLevelLabel.DEBUG, ` 🧩 Calling plugin hook: ${chalk.bold.cyanBright(`${key}${options?.order ? ` (${options.order})` : ""}`)}`);
30
+ const invokeHook = async (hook, hookArgs) => {
31
+ return Reflect.apply(hook.handler, hook.context, hookArgs);
32
+ };
30
33
  let results = [];
31
- if (options?.sequential === false) results = await Promise.all(handlers.map(async (handler) => {
32
- if (!isFunction(handler.handle)) throw new Error(`Plugin hook handler for hook "${hook}" is not a function.`);
33
- return Promise.resolve(handler.handle.apply(handler.context, [...args]));
34
+ if (options?.sequential === false) results = await Promise.all(hooks.map(async (hook) => {
35
+ if (!isFunction(hook.handler)) throw new Error(`Plugin hook handler for hook "${key}" is not a function.`);
36
+ return invokeHook(hook, [...args]);
34
37
  }));
35
- else for (const handler of handlers) {
36
- if (!isFunction(handler.handle)) throw new Error(`Plugin hook handler for hook "${hook}" is not a function.`);
38
+ else for (const hook of hooks) {
39
+ if (!isFunction(hook.handler)) throw new Error(`Plugin hook handler for hook "${key}" is not a function.`);
37
40
  if (options?.result === "first" || options?.asNextParam === false) {
38
- results.push(await Promise.resolve(handler.handle.apply(handler.context, [...args])));
41
+ results.push(await Promise.resolve(invokeHook(hook, [...args])));
39
42
  if (options?.result === "first" && isSet(results[results.length - 1])) break;
40
43
  } else {
41
44
  const sequenceArgs = [...args];
42
45
  if (results.length > 0 && sequenceArgs.length > 0) sequenceArgs[0] = isFunction(options.asNextParam) ? await Promise.resolve(options.asNextParam(results[0])) : results[0];
43
- const result = await Promise.resolve(handler.handle.apply(handler.context, [...sequenceArgs]));
46
+ const result = await Promise.resolve(invokeHook(hook, [...sequenceArgs]));
44
47
  if (result) {
45
48
  if (options?.result === "last") results = [result];
46
49
  else if (isString(result)) results = [`${isString(results[0]) ? results[0] || "" : ""}\n${result || ""}`.trim()];
@@ -2,10 +2,12 @@ const require_rolldown_runtime = require('../../../../_virtual/rolldown_runtime.
2
2
  const require_plugin = require('../../types/plugin.cjs');
3
3
  const require_helpers = require('../../plugin-utils/helpers.cjs');
4
4
  const require_context = require('./context.cjs');
5
+ const require_helpers$1 = require('../unplugin/helpers.cjs');
5
6
  const require_plugin_context = require('./plugin-context.cjs');
6
7
  let __stryke_fs_resolve = require("@stryke/fs/resolve");
7
8
  let __stryke_type_checks_is_function = require("@stryke/type-checks/is-function");
8
9
  let __stryke_type_checks_is_object = require("@stryke/type-checks/is-object");
10
+ let __stryke_type_checks_is_set_object = require("@stryke/type-checks/is-set-object");
9
11
 
10
12
  //#region ../powerlines/src/lib/contexts/environment-context.ts
11
13
  var PowerlinesEnvironmentContext = class PowerlinesEnvironmentContext extends require_context.PowerlinesContext {
@@ -60,94 +62,127 @@ var PowerlinesEnvironmentContext = class PowerlinesEnvironmentContext extends re
60
62
  });
61
63
  this.#hooks = Object.keys(resolvedPlugin).filter((key) => !require_plugin.PLUGIN_NON_HOOK_FIELDS.includes(key)).reduce((ret, key) => {
62
64
  const hook = key;
63
- const pluginHook = resolvedPlugin[hook];
64
- if (!require_helpers.isPluginHook(pluginHook)) return ret;
65
- if (!require_helpers.isHookExternal(hook)) {
66
- ret[hook] ??= {};
65
+ if (require_helpers.isPluginHookField(hook)) {
66
+ const pluginHook = resolvedPlugin[hook];
67
+ if (!require_helpers.isPluginHook(pluginHook)) return ret;
68
+ ret[hook] ??= {
69
+ preEnforced: [],
70
+ preOrdered: [],
71
+ normal: [],
72
+ postEnforced: [],
73
+ postOrdered: []
74
+ };
67
75
  if (resolvedPlugin.enforce) {
68
- ret[hook][`${resolvedPlugin.enforce}Enforced`] ??= [];
69
- require_helpers.addPluginHook(context, resolvedPlugin, pluginHook, ret[hook][`${resolvedPlugin.enforce}Enforced`]);
76
+ const hookListOrder$1 = `${resolvedPlugin.enforce}Enforced`;
77
+ ret[hook][hookListOrder$1] ??= [];
78
+ const bucket = ret[hook][hookListOrder$1];
79
+ require_helpers.addPluginHook(context, resolvedPlugin, pluginHook, bucket);
70
80
  return ret;
71
81
  }
72
82
  if ((0, __stryke_type_checks_is_function.isFunction)(pluginHook) || !pluginHook.order) {
73
83
  ret[hook].normal ??= [];
74
- require_helpers.addPluginHook(context, resolvedPlugin, pluginHook, ret[hook].normal);
84
+ const bucket = ret[hook].normal;
85
+ require_helpers.addPluginHook(context, resolvedPlugin, pluginHook, bucket);
75
86
  return ret;
76
87
  }
77
- ret[hook][`${pluginHook.order}Ordered`] ??= [];
78
- require_helpers.addPluginHook(context, resolvedPlugin, pluginHook, ret[hook][`${pluginHook.order}Ordered`]);
79
- } else {
80
- ret[hook] ??= [];
81
- ret[hook].push({
82
- plugin: resolvedPlugin,
83
- hook: require_helpers.getHookHandler(pluginHook).bind(context)
84
- });
85
- }
88
+ const hookListOrder = `${pluginHook.order}Ordered`;
89
+ ret[hook][hookListOrder] ??= [];
90
+ require_helpers.addPluginHook(context, resolvedPlugin, pluginHook, ret[hook][hookListOrder]);
91
+ return ret;
92
+ } else if (require_helpers.isUnpluginHookField(hook)) {
93
+ const unpluginPlugin = resolvedPlugin[hook];
94
+ if (!(0, __stryke_type_checks_is_set_object.isSetObject)(unpluginPlugin)) return ret;
95
+ for (const field of Object.keys(unpluginPlugin)) {
96
+ const variantField = field;
97
+ const pluginHook = unpluginPlugin[variantField];
98
+ if (!require_helpers.isPluginHook(pluginHook)) continue;
99
+ ret[hook] ??= {};
100
+ ret[hook][variantField] ??= {
101
+ preEnforced: [],
102
+ preOrdered: [],
103
+ normal: [],
104
+ postEnforced: [],
105
+ postOrdered: []
106
+ };
107
+ if (resolvedPlugin.enforce) {
108
+ require_helpers.addPluginHook(context, resolvedPlugin, pluginHook, ret[hook][variantField][`${resolvedPlugin.enforce}Enforced`]);
109
+ return ret;
110
+ }
111
+ if ((0, __stryke_type_checks_is_function.isFunction)(pluginHook) || !pluginHook.order) {
112
+ require_helpers.addPluginHook(context, resolvedPlugin, pluginHook, ret[hook][variantField].normal);
113
+ return ret;
114
+ }
115
+ require_helpers.addPluginHook(context, resolvedPlugin, pluginHook, ret[hook][variantField][`${pluginHook.order}Ordered`]);
116
+ }
117
+ } else this.warn(`Unknown plugin hook field: ${String(hook)}`);
86
118
  return ret;
87
119
  }, this.hooks);
88
120
  }
89
121
  /**
90
122
  * Retrieves the hook handlers for a specific hook name
91
123
  */
92
- selectHooks(hook, options) {
124
+ selectHooks(key, options) {
93
125
  const result = [];
94
- if (this.hooks[hook]) if (!require_helpers.isHookExternal(hook)) {
95
- const hooks = this.hooks[hook];
96
- if (options?.order) if (options?.order === "pre") {
97
- result.push(...(hooks.preOrdered ?? []).map((h) => {
98
- const plugin = this.plugins.find((p) => p.plugin.name === h.plugin.name);
99
- if (!plugin) throw new Error(`Could not find plugin context for plugin "${h.plugin.name}".`);
100
- return {
101
- handle: h.handler,
102
- context: plugin.context
103
- };
104
- }));
105
- result.push(...(hooks.preEnforced ?? []).map((h) => {
106
- const plugin = this.plugins.find((p) => p.plugin.name === h.plugin.name);
107
- if (!plugin) throw new Error(`Could not find plugin context for plugin "${h.plugin.name}".`);
108
- return {
109
- handle: h.handler,
110
- context: plugin.context
111
- };
112
- }));
113
- } else if (options?.order === "post") {
114
- result.push(...(hooks.postOrdered ?? []).map((h) => {
115
- const plugin = this.plugins.find((p) => p.plugin.name === h.plugin.name);
116
- if (!plugin) throw new Error(`Could not find plugin context for plugin "${h.plugin.name}".`);
117
- return {
118
- handle: h.handler,
119
- context: plugin.context
120
- };
121
- }));
122
- result.push(...(hooks.postEnforced ?? []).map((h) => {
123
- const plugin = this.plugins.find((p) => p.plugin.name === h.plugin.name);
124
- if (!plugin) throw new Error(`Could not find plugin context for plugin "${h.plugin.name}".`);
125
- return {
126
- handle: h.handler,
127
- context: plugin.context
128
- };
129
- }));
130
- } else result.push(...(hooks.normal ?? []).map((h) => {
131
- const plugin = this.plugins.find((p) => p.plugin.name === h.plugin.name);
132
- if (!plugin) throw new Error(`Could not find plugin context for plugin "${h.plugin.name}".`);
133
- return {
134
- handle: h.handler,
135
- context: plugin.context
136
- };
137
- }));
138
- else {
139
- result.push(...this.selectHooks(hook, { order: "pre" }));
140
- result.push(...this.selectHooks(hook, { order: "normal" }));
141
- result.push(...this.selectHooks(hook, { order: "post" }));
126
+ if (require_helpers.isUnpluginHookKey(key)) {
127
+ const variant = String(key).split(":")[0];
128
+ if (require_helpers$1.isUnpluginBuilderVariant(variant)) {
129
+ const hooks = this.hooks[variant];
130
+ if (hooks) {
131
+ const field = String(key).split(":")[1];
132
+ if (field && hooks[field]) {
133
+ const fieldHooks = hooks[field];
134
+ if (options?.order) {
135
+ const mapHooksToResult = (hooksList) => hooksList.map((hook) => {
136
+ const plugin = this.plugins.find((p) => p.plugin.name === hook.plugin.name);
137
+ if (!plugin) throw new Error(`Could not find plugin context for plugin "${hook.plugin.name}".`);
138
+ return {
139
+ handler: hook.handler,
140
+ plugin: hook.plugin,
141
+ context: plugin.context
142
+ };
143
+ });
144
+ if (options?.order === "pre") {
145
+ result.push(...mapHooksToResult(fieldHooks.preOrdered ?? []));
146
+ result.push(...mapHooksToResult(fieldHooks.preEnforced ?? []));
147
+ } else if (options?.order === "post") {
148
+ result.push(...mapHooksToResult(fieldHooks.postOrdered ?? []));
149
+ result.push(...mapHooksToResult(fieldHooks.postEnforced ?? []));
150
+ } else result.push(...mapHooksToResult(fieldHooks.normal ?? []));
151
+ } else {
152
+ result.push(...this.selectHooks(key, { order: "pre" }));
153
+ result.push(...this.selectHooks(key, { order: "normal" }));
154
+ result.push(...this.selectHooks(key, { order: "post" }));
155
+ }
156
+ }
157
+ }
158
+ }
159
+ } else if (require_helpers.isPluginHookField(key)) {
160
+ if (this.hooks[key]) {
161
+ const fieldHooks = this.hooks[key];
162
+ if (options?.order) {
163
+ const mapHooksToResult = (hooksList) => hooksList.map((hook) => {
164
+ const plugin = this.plugins.find((p) => p.plugin.name === hook.plugin.name);
165
+ if (!plugin) throw new Error(`Could not find plugin context for plugin "${hook.plugin.name}".`);
166
+ return {
167
+ handler: hook.handler,
168
+ plugin: hook.plugin,
169
+ context: plugin.context
170
+ };
171
+ });
172
+ if (options?.order === "pre") {
173
+ result.push(...mapHooksToResult(fieldHooks.preOrdered ?? []));
174
+ result.push(...mapHooksToResult(fieldHooks.preEnforced ?? []));
175
+ } else if (options?.order === "post") {
176
+ result.push(...mapHooksToResult(fieldHooks.postOrdered ?? []));
177
+ result.push(...mapHooksToResult(fieldHooks.postEnforced ?? []));
178
+ } else result.push(...mapHooksToResult(fieldHooks.normal ?? []));
179
+ } else {
180
+ result.push(...this.selectHooks(key, { order: "pre" }));
181
+ result.push(...this.selectHooks(key, { order: "normal" }));
182
+ result.push(...this.selectHooks(key, { order: "post" }));
183
+ }
142
184
  }
143
- } else result.push(...this.hooks[hook].map((h) => {
144
- const plugin = this.plugins.find((p) => p.plugin.name === h.plugin.name);
145
- if (!plugin) throw new Error(`Could not find plugin context for plugin "${h.plugin.name}".`);
146
- return {
147
- handle: h.handler,
148
- context: plugin.context
149
- };
150
- }));
185
+ } else throw new Error(`Unknown plugin hook key: ${String(key)}`);
151
186
  return result;
152
187
  }
153
188
  constructor(config, workspaceConfig) {
@@ -1,10 +1,12 @@
1
1
  import { PLUGIN_NON_HOOK_FIELDS } from "../../types/plugin.mjs";
2
- import { addPluginHook, getHookHandler, isHookExternal, isPlugin, isPluginConfig, isPluginHook } from "../../plugin-utils/helpers.mjs";
2
+ import { addPluginHook, isPlugin, isPluginConfig, isPluginHook, isPluginHookField, isUnpluginHookField, isUnpluginHookKey } from "../../plugin-utils/helpers.mjs";
3
3
  import { PowerlinesContext } from "./context.mjs";
4
+ import { isUnpluginBuilderVariant } from "../unplugin/helpers.mjs";
4
5
  import { createPluginContext } from "./plugin-context.mjs";
5
6
  import { resolvePackage } from "@stryke/fs/resolve";
6
7
  import { isFunction } from "@stryke/type-checks/is-function";
7
8
  import { isObject } from "@stryke/type-checks/is-object";
9
+ import { isSetObject } from "@stryke/type-checks/is-set-object";
8
10
 
9
11
  //#region ../powerlines/src/lib/contexts/environment-context.ts
10
12
  var PowerlinesEnvironmentContext = class PowerlinesEnvironmentContext extends PowerlinesContext {
@@ -59,94 +61,127 @@ var PowerlinesEnvironmentContext = class PowerlinesEnvironmentContext extends Po
59
61
  });
60
62
  this.#hooks = Object.keys(resolvedPlugin).filter((key) => !PLUGIN_NON_HOOK_FIELDS.includes(key)).reduce((ret, key) => {
61
63
  const hook = key;
62
- const pluginHook = resolvedPlugin[hook];
63
- if (!isPluginHook(pluginHook)) return ret;
64
- if (!isHookExternal(hook)) {
65
- ret[hook] ??= {};
64
+ if (isPluginHookField(hook)) {
65
+ const pluginHook = resolvedPlugin[hook];
66
+ if (!isPluginHook(pluginHook)) return ret;
67
+ ret[hook] ??= {
68
+ preEnforced: [],
69
+ preOrdered: [],
70
+ normal: [],
71
+ postEnforced: [],
72
+ postOrdered: []
73
+ };
66
74
  if (resolvedPlugin.enforce) {
67
- ret[hook][`${resolvedPlugin.enforce}Enforced`] ??= [];
68
- addPluginHook(context, resolvedPlugin, pluginHook, ret[hook][`${resolvedPlugin.enforce}Enforced`]);
75
+ const hookListOrder$1 = `${resolvedPlugin.enforce}Enforced`;
76
+ ret[hook][hookListOrder$1] ??= [];
77
+ const bucket = ret[hook][hookListOrder$1];
78
+ addPluginHook(context, resolvedPlugin, pluginHook, bucket);
69
79
  return ret;
70
80
  }
71
81
  if (isFunction(pluginHook) || !pluginHook.order) {
72
82
  ret[hook].normal ??= [];
73
- addPluginHook(context, resolvedPlugin, pluginHook, ret[hook].normal);
83
+ const bucket = ret[hook].normal;
84
+ addPluginHook(context, resolvedPlugin, pluginHook, bucket);
74
85
  return ret;
75
86
  }
76
- ret[hook][`${pluginHook.order}Ordered`] ??= [];
77
- addPluginHook(context, resolvedPlugin, pluginHook, ret[hook][`${pluginHook.order}Ordered`]);
78
- } else {
79
- ret[hook] ??= [];
80
- ret[hook].push({
81
- plugin: resolvedPlugin,
82
- hook: getHookHandler(pluginHook).bind(context)
83
- });
84
- }
87
+ const hookListOrder = `${pluginHook.order}Ordered`;
88
+ ret[hook][hookListOrder] ??= [];
89
+ addPluginHook(context, resolvedPlugin, pluginHook, ret[hook][hookListOrder]);
90
+ return ret;
91
+ } else if (isUnpluginHookField(hook)) {
92
+ const unpluginPlugin = resolvedPlugin[hook];
93
+ if (!isSetObject(unpluginPlugin)) return ret;
94
+ for (const field of Object.keys(unpluginPlugin)) {
95
+ const variantField = field;
96
+ const pluginHook = unpluginPlugin[variantField];
97
+ if (!isPluginHook(pluginHook)) continue;
98
+ ret[hook] ??= {};
99
+ ret[hook][variantField] ??= {
100
+ preEnforced: [],
101
+ preOrdered: [],
102
+ normal: [],
103
+ postEnforced: [],
104
+ postOrdered: []
105
+ };
106
+ if (resolvedPlugin.enforce) {
107
+ addPluginHook(context, resolvedPlugin, pluginHook, ret[hook][variantField][`${resolvedPlugin.enforce}Enforced`]);
108
+ return ret;
109
+ }
110
+ if (isFunction(pluginHook) || !pluginHook.order) {
111
+ addPluginHook(context, resolvedPlugin, pluginHook, ret[hook][variantField].normal);
112
+ return ret;
113
+ }
114
+ addPluginHook(context, resolvedPlugin, pluginHook, ret[hook][variantField][`${pluginHook.order}Ordered`]);
115
+ }
116
+ } else this.warn(`Unknown plugin hook field: ${String(hook)}`);
85
117
  return ret;
86
118
  }, this.hooks);
87
119
  }
88
120
  /**
89
121
  * Retrieves the hook handlers for a specific hook name
90
122
  */
91
- selectHooks(hook, options) {
123
+ selectHooks(key, options) {
92
124
  const result = [];
93
- if (this.hooks[hook]) if (!isHookExternal(hook)) {
94
- const hooks = this.hooks[hook];
95
- if (options?.order) if (options?.order === "pre") {
96
- result.push(...(hooks.preOrdered ?? []).map((h) => {
97
- const plugin = this.plugins.find((p) => p.plugin.name === h.plugin.name);
98
- if (!plugin) throw new Error(`Could not find plugin context for plugin "${h.plugin.name}".`);
99
- return {
100
- handle: h.handler,
101
- context: plugin.context
102
- };
103
- }));
104
- result.push(...(hooks.preEnforced ?? []).map((h) => {
105
- const plugin = this.plugins.find((p) => p.plugin.name === h.plugin.name);
106
- if (!plugin) throw new Error(`Could not find plugin context for plugin "${h.plugin.name}".`);
107
- return {
108
- handle: h.handler,
109
- context: plugin.context
110
- };
111
- }));
112
- } else if (options?.order === "post") {
113
- result.push(...(hooks.postOrdered ?? []).map((h) => {
114
- const plugin = this.plugins.find((p) => p.plugin.name === h.plugin.name);
115
- if (!plugin) throw new Error(`Could not find plugin context for plugin "${h.plugin.name}".`);
116
- return {
117
- handle: h.handler,
118
- context: plugin.context
119
- };
120
- }));
121
- result.push(...(hooks.postEnforced ?? []).map((h) => {
122
- const plugin = this.plugins.find((p) => p.plugin.name === h.plugin.name);
123
- if (!plugin) throw new Error(`Could not find plugin context for plugin "${h.plugin.name}".`);
124
- return {
125
- handle: h.handler,
126
- context: plugin.context
127
- };
128
- }));
129
- } else result.push(...(hooks.normal ?? []).map((h) => {
130
- const plugin = this.plugins.find((p) => p.plugin.name === h.plugin.name);
131
- if (!plugin) throw new Error(`Could not find plugin context for plugin "${h.plugin.name}".`);
132
- return {
133
- handle: h.handler,
134
- context: plugin.context
135
- };
136
- }));
137
- else {
138
- result.push(...this.selectHooks(hook, { order: "pre" }));
139
- result.push(...this.selectHooks(hook, { order: "normal" }));
140
- result.push(...this.selectHooks(hook, { order: "post" }));
125
+ if (isUnpluginHookKey(key)) {
126
+ const variant = String(key).split(":")[0];
127
+ if (isUnpluginBuilderVariant(variant)) {
128
+ const hooks = this.hooks[variant];
129
+ if (hooks) {
130
+ const field = String(key).split(":")[1];
131
+ if (field && hooks[field]) {
132
+ const fieldHooks = hooks[field];
133
+ if (options?.order) {
134
+ const mapHooksToResult = (hooksList) => hooksList.map((hook) => {
135
+ const plugin = this.plugins.find((p) => p.plugin.name === hook.plugin.name);
136
+ if (!plugin) throw new Error(`Could not find plugin context for plugin "${hook.plugin.name}".`);
137
+ return {
138
+ handler: hook.handler,
139
+ plugin: hook.plugin,
140
+ context: plugin.context
141
+ };
142
+ });
143
+ if (options?.order === "pre") {
144
+ result.push(...mapHooksToResult(fieldHooks.preOrdered ?? []));
145
+ result.push(...mapHooksToResult(fieldHooks.preEnforced ?? []));
146
+ } else if (options?.order === "post") {
147
+ result.push(...mapHooksToResult(fieldHooks.postOrdered ?? []));
148
+ result.push(...mapHooksToResult(fieldHooks.postEnforced ?? []));
149
+ } else result.push(...mapHooksToResult(fieldHooks.normal ?? []));
150
+ } else {
151
+ result.push(...this.selectHooks(key, { order: "pre" }));
152
+ result.push(...this.selectHooks(key, { order: "normal" }));
153
+ result.push(...this.selectHooks(key, { order: "post" }));
154
+ }
155
+ }
156
+ }
157
+ }
158
+ } else if (isPluginHookField(key)) {
159
+ if (this.hooks[key]) {
160
+ const fieldHooks = this.hooks[key];
161
+ if (options?.order) {
162
+ const mapHooksToResult = (hooksList) => hooksList.map((hook) => {
163
+ const plugin = this.plugins.find((p) => p.plugin.name === hook.plugin.name);
164
+ if (!plugin) throw new Error(`Could not find plugin context for plugin "${hook.plugin.name}".`);
165
+ return {
166
+ handler: hook.handler,
167
+ plugin: hook.plugin,
168
+ context: plugin.context
169
+ };
170
+ });
171
+ if (options?.order === "pre") {
172
+ result.push(...mapHooksToResult(fieldHooks.preOrdered ?? []));
173
+ result.push(...mapHooksToResult(fieldHooks.preEnforced ?? []));
174
+ } else if (options?.order === "post") {
175
+ result.push(...mapHooksToResult(fieldHooks.postOrdered ?? []));
176
+ result.push(...mapHooksToResult(fieldHooks.postEnforced ?? []));
177
+ } else result.push(...mapHooksToResult(fieldHooks.normal ?? []));
178
+ } else {
179
+ result.push(...this.selectHooks(key, { order: "pre" }));
180
+ result.push(...this.selectHooks(key, { order: "normal" }));
181
+ result.push(...this.selectHooks(key, { order: "post" }));
182
+ }
141
183
  }
142
- } else result.push(...this.hooks[hook].map((h) => {
143
- const plugin = this.plugins.find((p) => p.plugin.name === h.plugin.name);
144
- if (!plugin) throw new Error(`Could not find plugin context for plugin "${h.plugin.name}".`);
145
- return {
146
- handle: h.handler,
147
- context: plugin.context
148
- };
149
- }));
184
+ } else throw new Error(`Unknown plugin hook key: ${String(key)}`);
150
185
  return result;
151
186
  }
152
187
  constructor(config, workspaceConfig) {