mokup 2.3.0 → 2.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/dist/bundle.cjs CHANGED
@@ -1,11 +1,7 @@
1
1
  'use strict';
2
2
 
3
- const bundle = require('./shared/mokup.BXPIIxtS.cjs');
4
- require('./shared/mokup.Dy9VDphS.cjs');
5
- require('@mokup/shared/pathe');
6
- require('@mokup/shared/path-utils');
7
- require('@mokup/shared/timing');
3
+ const core = require('@mokup/core');
8
4
 
9
5
 
10
6
 
11
- exports.buildBundleModule = bundle.buildBundleModule;
7
+ exports.buildBundleModule = core.buildBundleModule;
package/dist/bundle.d.cts CHANGED
@@ -1,27 +1 @@
1
- import { R as RouteTable } from './shared/mokup.Dkqu10Hk.cjs';
2
- export { a as ResolvedRoute } from './shared/mokup.Dkqu10Hk.cjs';
3
- import '@mokup/runtime';
4
- import '@mokup/shared';
5
- import '@mokup/shared/hono';
6
-
7
- /**
8
- * Build the source for a virtual mokup bundle module.
9
- *
10
- * @param params - Bundle build parameters.
11
- * @param params.routes - Resolved routes to serialize.
12
- * @param params.root - Workspace root path.
13
- * @param params.resolveModulePath - Optional module resolver.
14
- * @returns JavaScript source string.
15
- *
16
- * @example
17
- * import { buildBundleModule } from 'mokup/bundle'
18
- *
19
- * const source = buildBundleModule({ routes: [], root: '/project' })
20
- */
21
- declare function buildBundleModule(params: {
22
- routes: RouteTable;
23
- root: string;
24
- resolveModulePath?: (file: string, root: string) => string;
25
- }): string;
26
-
27
- export { RouteTable, buildBundleModule };
1
+ export { ResolvedRoute, RouteTable, buildBundleModule } from '@mokup/core';
package/dist/bundle.d.mts CHANGED
@@ -1,27 +1 @@
1
- import { R as RouteTable } from './shared/mokup.Dkqu10Hk.mjs';
2
- export { a as ResolvedRoute } from './shared/mokup.Dkqu10Hk.mjs';
3
- import '@mokup/runtime';
4
- import '@mokup/shared';
5
- import '@mokup/shared/hono';
6
-
7
- /**
8
- * Build the source for a virtual mokup bundle module.
9
- *
10
- * @param params - Bundle build parameters.
11
- * @param params.routes - Resolved routes to serialize.
12
- * @param params.root - Workspace root path.
13
- * @param params.resolveModulePath - Optional module resolver.
14
- * @returns JavaScript source string.
15
- *
16
- * @example
17
- * import { buildBundleModule } from 'mokup/bundle'
18
- *
19
- * const source = buildBundleModule({ routes: [], root: '/project' })
20
- */
21
- declare function buildBundleModule(params: {
22
- routes: RouteTable;
23
- root: string;
24
- resolveModulePath?: (file: string, root: string) => string;
25
- }): string;
26
-
27
- export { RouteTable, buildBundleModule };
1
+ export { ResolvedRoute, RouteTable, buildBundleModule } from '@mokup/core';
package/dist/bundle.d.ts CHANGED
@@ -1,27 +1 @@
1
- import { R as RouteTable } from './shared/mokup.Dkqu10Hk.js';
2
- export { a as ResolvedRoute } from './shared/mokup.Dkqu10Hk.js';
3
- import '@mokup/runtime';
4
- import '@mokup/shared';
5
- import '@mokup/shared/hono';
6
-
7
- /**
8
- * Build the source for a virtual mokup bundle module.
9
- *
10
- * @param params - Bundle build parameters.
11
- * @param params.routes - Resolved routes to serialize.
12
- * @param params.root - Workspace root path.
13
- * @param params.resolveModulePath - Optional module resolver.
14
- * @returns JavaScript source string.
15
- *
16
- * @example
17
- * import { buildBundleModule } from 'mokup/bundle'
18
- *
19
- * const source = buildBundleModule({ routes: [], root: '/project' })
20
- */
21
- declare function buildBundleModule(params: {
22
- routes: RouteTable;
23
- root: string;
24
- resolveModulePath?: (file: string, root: string) => string;
25
- }): string;
26
-
27
- export { RouteTable, buildBundleModule };
1
+ export { ResolvedRoute, RouteTable, buildBundleModule } from '@mokup/core';
package/dist/bundle.mjs CHANGED
@@ -1,5 +1 @@
1
- export { b as buildBundleModule } from './shared/mokup.CsBTglhs.mjs';
2
- import './shared/mokup.Iqw32OxC.mjs';
3
- import '@mokup/shared/pathe';
4
- import '@mokup/shared/path-utils';
5
- import '@mokup/shared/timing';
1
+ export { buildBundleModule } from '@mokup/core';
@@ -1,5 +1,6 @@
1
1
 
2
2
  import '@mokup/cli';
3
+ import '@mokup/core';
3
4
  import '@mokup/runtime';
4
5
  import '@mokup/server';
5
6
  import '@mokup/server/fetch';
@@ -1,5 +1,6 @@
1
1
 
2
2
  import '@mokup/cli';
3
+ import '@mokup/core';
3
4
  import '@mokup/runtime';
4
5
  import '@mokup/server';
5
6
  import '@mokup/server/fetch';
package/dist/cli-bin.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
 
2
2
  import '@mokup/cli';
3
+ import '@mokup/core';
3
4
  import '@mokup/runtime';
4
5
  import '@mokup/server';
5
6
  import '@mokup/server/fetch';
package/dist/index.cjs CHANGED
@@ -1,164 +1,13 @@
1
1
  'use strict';
2
2
 
3
- const middlewareSymbol = Symbol.for("mokup.config.middlewares");
4
- const contextStack = [];
5
- function getActiveContext() {
6
- const context = contextStack[contextStack.length - 1];
7
- if (!context) {
8
- throw new Error("onBeforeAll/onAfterAll must be called inside defineConfig()");
9
- }
10
- return context;
11
- }
12
- function runWithContext(context, fn) {
13
- contextStack.push(context);
14
- try {
15
- const result = fn();
16
- if (isPromise(result)) {
17
- return result.finally(() => {
18
- contextStack.pop();
19
- });
20
- }
21
- contextStack.pop();
22
- return result;
23
- } catch (error) {
24
- contextStack.pop();
25
- throw error;
26
- }
27
- }
28
- function isPromise(value) {
29
- return !!value && typeof value.then === "function";
30
- }
31
- function normalizeHookError(policy) {
32
- if (policy === "throw" || policy === "silent") {
33
- return policy;
34
- }
35
- return "warn";
36
- }
37
- function reportHookError(error, policy) {
38
- if (policy === "silent") {
39
- return;
40
- }
41
- if (policy === "warn") {
42
- console.warn("[mokup] defineConfig hook failed:", error);
43
- }
44
- }
45
- function runHookSequence(stage, hooks, policy, setStage) {
46
- if (hooks.length === 0) {
47
- return;
48
- }
49
- setStage(stage);
50
- let chain = null;
51
- const runHook = (hook) => {
52
- try {
53
- const result = hook();
54
- if (isPromise(result)) {
55
- return result.catch((error) => {
56
- if (policy === "throw") {
57
- throw error;
58
- }
59
- reportHookError(error, policy);
60
- });
61
- }
62
- return void 0;
63
- } catch (error) {
64
- if (policy === "throw") {
65
- throw error;
66
- }
67
- reportHookError(error, policy);
68
- return void 0;
69
- }
70
- };
71
- for (const hook of hooks) {
72
- if (chain) {
73
- chain = chain.then(() => runHook(hook));
74
- continue;
75
- }
76
- const result = runHook(hook);
77
- if (isPromise(result)) {
78
- chain = result;
79
- }
80
- }
81
- if (!chain) {
82
- setStage("normal");
83
- return;
84
- }
85
- return chain.finally(() => {
86
- setStage("normal");
87
- });
88
- }
89
- function attachMetadata(config, meta) {
90
- Object.defineProperty(config, middlewareSymbol, {
91
- value: meta,
92
- enumerable: false
93
- });
94
- return config;
95
- }
96
- function normalizeConfig(value) {
97
- return value && typeof value === "object" ? value : {};
98
- }
99
- function onBeforeAll(handler) {
100
- if (typeof handler !== "function") {
101
- throw new TypeError("onBeforeAll expects a function");
102
- }
103
- const context = getActiveContext();
104
- context.hooks.pre.push(handler);
105
- }
106
- function onAfterAll(handler) {
107
- if (typeof handler !== "function") {
108
- throw new TypeError("onAfterAll expects a function");
109
- }
110
- const context = getActiveContext();
111
- context.hooks.post.push(handler);
112
- }
113
- function defineConfig(input) {
114
- if (typeof input === "function") {
115
- const pre = [];
116
- const normal = [];
117
- const post = [];
118
- let stage = "normal";
119
- const app = {
120
- use: (...handlers) => {
121
- if (stage === "pre") {
122
- pre.push(...handlers);
123
- return;
124
- }
125
- if (stage === "post") {
126
- post.push(...handlers);
127
- return;
128
- }
129
- normal.push(...handlers);
130
- }
131
- };
132
- const context = {
133
- app,
134
- hooks: { pre: [], post: [] },
135
- setStage: (next) => {
136
- stage = next;
137
- }
138
- };
139
- const result = runWithContext(context, () => input({ app }));
140
- const finalize = (value) => {
141
- const config2 = normalizeConfig(value);
142
- const policy = normalizeHookError(config2.hookError);
143
- const preResult = runHookSequence("pre", context.hooks.pre, policy, context.setStage);
144
- const runPost = () => runHookSequence("post", context.hooks.post, policy, context.setStage);
145
- if (isPromise(preResult)) {
146
- return preResult.then(runPost).then(() => attachMetadata(config2, { pre, normal, post }));
147
- }
148
- const postResult = runPost();
149
- if (isPromise(postResult)) {
150
- return postResult.then(() => attachMetadata(config2, { pre, normal, post }));
151
- }
152
- return attachMetadata(config2, { pre, normal, post });
153
- };
154
- if (isPromise(result)) {
155
- return result.then(finalize);
156
- }
157
- return finalize(result);
158
- }
159
- const config = normalizeConfig(input);
160
- return attachMetadata(config, { pre: [], normal: [], post: [] });
161
- }
3
+ const defineConfig$1 = require('@mokup/shared/define-config');
4
+
5
+ const shared = defineConfig$1.createDefineConfig({
6
+ logPrefix: "[mokup]"
7
+ });
8
+ const defineConfig = shared.defineConfig;
9
+ const onBeforeAll = shared.onBeforeAll;
10
+ const onAfterAll = shared.onAfterAll;
162
11
 
163
12
  function defineHandler(input) {
164
13
  return input;
package/dist/index.d.cts CHANGED
@@ -1,51 +1,10 @@
1
- import { e as RouteDirectoryConfig, d as RequestHandler, g as RouteRule } from './shared/mokup.Dkqu10Hk.cjs';
2
- export { k as HookErrorPolicy, H as HttpMethod, b as MiddlewarePosition, c as MiddlewareRegistry, M as MokupPluginOptions, f as RouteResponse, h as RuntimeMode, S as ServiceWorkerOptions, V as VitePluginOptions, i as VitePluginOptionsInput } from './shared/mokup.Dkqu10Hk.cjs';
3
- import { MiddlewareHandler } from '@mokup/shared/hono';
4
- export { Context, MiddlewareHandler } from '@mokup/shared/hono';
5
- export { PlaygroundOptionsInput } from '@mokup/shared';
6
- import '@mokup/runtime';
1
+ import * as _mokup_shared_define_config from '@mokup/shared/define-config';
2
+ import { RouteDirectoryConfig, MiddlewareHandler, RequestHandler, RouteRule } from '@mokup/core';
3
+ export { Context, HookErrorPolicy, HttpMethod, MiddlewareHandler, MiddlewarePosition, MiddlewareRegistry, MokupPluginOptions, PlaygroundOptionsInput, RequestHandler, RouteDirectoryConfig, RouteResponse, RouteRule, RuntimeMode, ServiceWorkerOptions, VitePluginOptions, VitePluginOptionsInput } from '@mokup/core';
7
4
 
8
- type HookHandler = () => void | Promise<void>;
9
- interface ConfigApp {
10
- use: (...handlers: MiddlewareHandler[]) => void;
11
- }
12
- type DefineConfigFactory = (context: {
13
- app: ConfigApp;
14
- }) => RouteDirectoryConfig | void | Promise<RouteDirectoryConfig | void>;
15
- declare function onBeforeAll(handler: HookHandler): void;
16
- declare function onAfterAll(handler: HookHandler): void;
17
- /**
18
- * Define a directory config with hook-based middleware registration.
19
- *
20
- * @param input - Config object or factory callback.
21
- * @returns Route directory config with middleware metadata.
22
- *
23
- * @example
24
- * import { defineConfig, onBeforeAll, onAfterAll } from 'mokup'
25
- *
26
- * export default defineConfig(({ app }) => {
27
- * onBeforeAll(() => {
28
- * app.use(async (c, next) => {
29
- * c.header('x-before', '1')
30
- * await next()
31
- * })
32
- * })
33
- *
34
- * app.use(async (_c, next) => {
35
- * await next()
36
- * })
37
- *
38
- * onAfterAll(() => {
39
- * app.use(async (c, next) => {
40
- * await next()
41
- * c.header('x-after', '1')
42
- * })
43
- * })
44
- *
45
- * return { delay: 120 }
46
- * })
47
- */
48
- declare function defineConfig(input: RouteDirectoryConfig | DefineConfigFactory): RouteDirectoryConfig | Promise<RouteDirectoryConfig>;
5
+ declare const defineConfig: (input: RouteDirectoryConfig | _mokup_shared_define_config.DefineConfigFactory<RouteDirectoryConfig, MiddlewareHandler>) => RouteDirectoryConfig | Promise<RouteDirectoryConfig>;
6
+ declare const onBeforeAll: (handler: _mokup_shared_define_config.HookHandler) => void;
7
+ declare const onAfterAll: (handler: _mokup_shared_define_config.HookHandler) => void;
49
8
 
50
9
  /**
51
10
  * Define a mock route handler with type hints.
@@ -73,4 +32,4 @@ declare function defineConfig(input: RouteDirectoryConfig | DefineConfigFactory)
73
32
  declare function defineHandler(input: RequestHandler): RequestHandler;
74
33
  declare function defineHandler(input: RouteRule): RouteRule;
75
34
 
76
- export { RequestHandler, RouteDirectoryConfig, RouteRule, defineConfig, defineHandler, onAfterAll, onBeforeAll };
35
+ export { defineConfig, defineHandler, onAfterAll, onBeforeAll };
package/dist/index.d.mts CHANGED
@@ -1,51 +1,10 @@
1
- import { e as RouteDirectoryConfig, d as RequestHandler, g as RouteRule } from './shared/mokup.Dkqu10Hk.mjs';
2
- export { k as HookErrorPolicy, H as HttpMethod, b as MiddlewarePosition, c as MiddlewareRegistry, M as MokupPluginOptions, f as RouteResponse, h as RuntimeMode, S as ServiceWorkerOptions, V as VitePluginOptions, i as VitePluginOptionsInput } from './shared/mokup.Dkqu10Hk.mjs';
3
- import { MiddlewareHandler } from '@mokup/shared/hono';
4
- export { Context, MiddlewareHandler } from '@mokup/shared/hono';
5
- export { PlaygroundOptionsInput } from '@mokup/shared';
6
- import '@mokup/runtime';
1
+ import * as _mokup_shared_define_config from '@mokup/shared/define-config';
2
+ import { RouteDirectoryConfig, MiddlewareHandler, RequestHandler, RouteRule } from '@mokup/core';
3
+ export { Context, HookErrorPolicy, HttpMethod, MiddlewareHandler, MiddlewarePosition, MiddlewareRegistry, MokupPluginOptions, PlaygroundOptionsInput, RequestHandler, RouteDirectoryConfig, RouteResponse, RouteRule, RuntimeMode, ServiceWorkerOptions, VitePluginOptions, VitePluginOptionsInput } from '@mokup/core';
7
4
 
8
- type HookHandler = () => void | Promise<void>;
9
- interface ConfigApp {
10
- use: (...handlers: MiddlewareHandler[]) => void;
11
- }
12
- type DefineConfigFactory = (context: {
13
- app: ConfigApp;
14
- }) => RouteDirectoryConfig | void | Promise<RouteDirectoryConfig | void>;
15
- declare function onBeforeAll(handler: HookHandler): void;
16
- declare function onAfterAll(handler: HookHandler): void;
17
- /**
18
- * Define a directory config with hook-based middleware registration.
19
- *
20
- * @param input - Config object or factory callback.
21
- * @returns Route directory config with middleware metadata.
22
- *
23
- * @example
24
- * import { defineConfig, onBeforeAll, onAfterAll } from 'mokup'
25
- *
26
- * export default defineConfig(({ app }) => {
27
- * onBeforeAll(() => {
28
- * app.use(async (c, next) => {
29
- * c.header('x-before', '1')
30
- * await next()
31
- * })
32
- * })
33
- *
34
- * app.use(async (_c, next) => {
35
- * await next()
36
- * })
37
- *
38
- * onAfterAll(() => {
39
- * app.use(async (c, next) => {
40
- * await next()
41
- * c.header('x-after', '1')
42
- * })
43
- * })
44
- *
45
- * return { delay: 120 }
46
- * })
47
- */
48
- declare function defineConfig(input: RouteDirectoryConfig | DefineConfigFactory): RouteDirectoryConfig | Promise<RouteDirectoryConfig>;
5
+ declare const defineConfig: (input: RouteDirectoryConfig | _mokup_shared_define_config.DefineConfigFactory<RouteDirectoryConfig, MiddlewareHandler>) => RouteDirectoryConfig | Promise<RouteDirectoryConfig>;
6
+ declare const onBeforeAll: (handler: _mokup_shared_define_config.HookHandler) => void;
7
+ declare const onAfterAll: (handler: _mokup_shared_define_config.HookHandler) => void;
49
8
 
50
9
  /**
51
10
  * Define a mock route handler with type hints.
@@ -73,4 +32,4 @@ declare function defineConfig(input: RouteDirectoryConfig | DefineConfigFactory)
73
32
  declare function defineHandler(input: RequestHandler): RequestHandler;
74
33
  declare function defineHandler(input: RouteRule): RouteRule;
75
34
 
76
- export { RequestHandler, RouteDirectoryConfig, RouteRule, defineConfig, defineHandler, onAfterAll, onBeforeAll };
35
+ export { defineConfig, defineHandler, onAfterAll, onBeforeAll };
package/dist/index.d.ts CHANGED
@@ -1,53 +1,12 @@
1
1
  /// <reference path="./types/virtual.d.ts" />
2
2
 
3
- import { e as RouteDirectoryConfig, d as RequestHandler, g as RouteRule } from './shared/mokup.Dkqu10Hk.js';
4
- export { k as HookErrorPolicy, H as HttpMethod, b as MiddlewarePosition, c as MiddlewareRegistry, M as MokupPluginOptions, f as RouteResponse, h as RuntimeMode, S as ServiceWorkerOptions, V as VitePluginOptions, i as VitePluginOptionsInput } from './shared/mokup.Dkqu10Hk.js';
5
- import { MiddlewareHandler } from '@mokup/shared/hono';
6
- export { Context, MiddlewareHandler } from '@mokup/shared/hono';
7
- export { PlaygroundOptionsInput } from '@mokup/shared';
8
- import '@mokup/runtime';
3
+ import * as _mokup_shared_define_config from '@mokup/shared/define-config';
4
+ import { RouteDirectoryConfig, MiddlewareHandler, RequestHandler, RouteRule } from '@mokup/core';
5
+ export { Context, HookErrorPolicy, HttpMethod, MiddlewareHandler, MiddlewarePosition, MiddlewareRegistry, MokupPluginOptions, PlaygroundOptionsInput, RequestHandler, RouteDirectoryConfig, RouteResponse, RouteRule, RuntimeMode, ServiceWorkerOptions, VitePluginOptions, VitePluginOptionsInput } from '@mokup/core';
9
6
 
10
- type HookHandler = () => void | Promise<void>;
11
- interface ConfigApp {
12
- use: (...handlers: MiddlewareHandler[]) => void;
13
- }
14
- type DefineConfigFactory = (context: {
15
- app: ConfigApp;
16
- }) => RouteDirectoryConfig | void | Promise<RouteDirectoryConfig | void>;
17
- declare function onBeforeAll(handler: HookHandler): void;
18
- declare function onAfterAll(handler: HookHandler): void;
19
- /**
20
- * Define a directory config with hook-based middleware registration.
21
- *
22
- * @param input - Config object or factory callback.
23
- * @returns Route directory config with middleware metadata.
24
- *
25
- * @example
26
- * import { defineConfig, onBeforeAll, onAfterAll } from 'mokup'
27
- *
28
- * export default defineConfig(({ app }) => {
29
- * onBeforeAll(() => {
30
- * app.use(async (c, next) => {
31
- * c.header('x-before', '1')
32
- * await next()
33
- * })
34
- * })
35
- *
36
- * app.use(async (_c, next) => {
37
- * await next()
38
- * })
39
- *
40
- * onAfterAll(() => {
41
- * app.use(async (c, next) => {
42
- * await next()
43
- * c.header('x-after', '1')
44
- * })
45
- * })
46
- *
47
- * return { delay: 120 }
48
- * })
49
- */
50
- declare function defineConfig(input: RouteDirectoryConfig | DefineConfigFactory): RouteDirectoryConfig | Promise<RouteDirectoryConfig>;
7
+ declare const defineConfig: (input: RouteDirectoryConfig | _mokup_shared_define_config.DefineConfigFactory<RouteDirectoryConfig, MiddlewareHandler>) => RouteDirectoryConfig | Promise<RouteDirectoryConfig>;
8
+ declare const onBeforeAll: (handler: _mokup_shared_define_config.HookHandler) => void;
9
+ declare const onAfterAll: (handler: _mokup_shared_define_config.HookHandler) => void;
51
10
 
52
11
  /**
53
12
  * Define a mock route handler with type hints.
@@ -75,4 +34,4 @@ declare function defineConfig(input: RouteDirectoryConfig | DefineConfigFactory)
75
34
  declare function defineHandler(input: RequestHandler): RequestHandler;
76
35
  declare function defineHandler(input: RouteRule): RouteRule;
77
36
 
78
- export { RequestHandler, RouteDirectoryConfig, RouteRule, defineConfig, defineHandler, onAfterAll, onBeforeAll };
37
+ export { defineConfig, defineHandler, onAfterAll, onBeforeAll };
package/dist/index.mjs CHANGED
@@ -1,162 +1,11 @@
1
- const middlewareSymbol = Symbol.for("mokup.config.middlewares");
2
- const contextStack = [];
3
- function getActiveContext() {
4
- const context = contextStack[contextStack.length - 1];
5
- if (!context) {
6
- throw new Error("onBeforeAll/onAfterAll must be called inside defineConfig()");
7
- }
8
- return context;
9
- }
10
- function runWithContext(context, fn) {
11
- contextStack.push(context);
12
- try {
13
- const result = fn();
14
- if (isPromise(result)) {
15
- return result.finally(() => {
16
- contextStack.pop();
17
- });
18
- }
19
- contextStack.pop();
20
- return result;
21
- } catch (error) {
22
- contextStack.pop();
23
- throw error;
24
- }
25
- }
26
- function isPromise(value) {
27
- return !!value && typeof value.then === "function";
28
- }
29
- function normalizeHookError(policy) {
30
- if (policy === "throw" || policy === "silent") {
31
- return policy;
32
- }
33
- return "warn";
34
- }
35
- function reportHookError(error, policy) {
36
- if (policy === "silent") {
37
- return;
38
- }
39
- if (policy === "warn") {
40
- console.warn("[mokup] defineConfig hook failed:", error);
41
- }
42
- }
43
- function runHookSequence(stage, hooks, policy, setStage) {
44
- if (hooks.length === 0) {
45
- return;
46
- }
47
- setStage(stage);
48
- let chain = null;
49
- const runHook = (hook) => {
50
- try {
51
- const result = hook();
52
- if (isPromise(result)) {
53
- return result.catch((error) => {
54
- if (policy === "throw") {
55
- throw error;
56
- }
57
- reportHookError(error, policy);
58
- });
59
- }
60
- return void 0;
61
- } catch (error) {
62
- if (policy === "throw") {
63
- throw error;
64
- }
65
- reportHookError(error, policy);
66
- return void 0;
67
- }
68
- };
69
- for (const hook of hooks) {
70
- if (chain) {
71
- chain = chain.then(() => runHook(hook));
72
- continue;
73
- }
74
- const result = runHook(hook);
75
- if (isPromise(result)) {
76
- chain = result;
77
- }
78
- }
79
- if (!chain) {
80
- setStage("normal");
81
- return;
82
- }
83
- return chain.finally(() => {
84
- setStage("normal");
85
- });
86
- }
87
- function attachMetadata(config, meta) {
88
- Object.defineProperty(config, middlewareSymbol, {
89
- value: meta,
90
- enumerable: false
91
- });
92
- return config;
93
- }
94
- function normalizeConfig(value) {
95
- return value && typeof value === "object" ? value : {};
96
- }
97
- function onBeforeAll(handler) {
98
- if (typeof handler !== "function") {
99
- throw new TypeError("onBeforeAll expects a function");
100
- }
101
- const context = getActiveContext();
102
- context.hooks.pre.push(handler);
103
- }
104
- function onAfterAll(handler) {
105
- if (typeof handler !== "function") {
106
- throw new TypeError("onAfterAll expects a function");
107
- }
108
- const context = getActiveContext();
109
- context.hooks.post.push(handler);
110
- }
111
- function defineConfig(input) {
112
- if (typeof input === "function") {
113
- const pre = [];
114
- const normal = [];
115
- const post = [];
116
- let stage = "normal";
117
- const app = {
118
- use: (...handlers) => {
119
- if (stage === "pre") {
120
- pre.push(...handlers);
121
- return;
122
- }
123
- if (stage === "post") {
124
- post.push(...handlers);
125
- return;
126
- }
127
- normal.push(...handlers);
128
- }
129
- };
130
- const context = {
131
- app,
132
- hooks: { pre: [], post: [] },
133
- setStage: (next) => {
134
- stage = next;
135
- }
136
- };
137
- const result = runWithContext(context, () => input({ app }));
138
- const finalize = (value) => {
139
- const config2 = normalizeConfig(value);
140
- const policy = normalizeHookError(config2.hookError);
141
- const preResult = runHookSequence("pre", context.hooks.pre, policy, context.setStage);
142
- const runPost = () => runHookSequence("post", context.hooks.post, policy, context.setStage);
143
- if (isPromise(preResult)) {
144
- return preResult.then(runPost).then(() => attachMetadata(config2, { pre, normal, post }));
145
- }
146
- const postResult = runPost();
147
- if (isPromise(postResult)) {
148
- return postResult.then(() => attachMetadata(config2, { pre, normal, post }));
149
- }
150
- return attachMetadata(config2, { pre, normal, post });
151
- };
152
- if (isPromise(result)) {
153
- return result.then(finalize);
154
- }
155
- return finalize(result);
156
- }
157
- const config = normalizeConfig(input);
158
- return attachMetadata(config, { pre: [], normal: [], post: [] });
159
- }
1
+ import { createDefineConfig } from '@mokup/shared/define-config';
2
+
3
+ const shared = createDefineConfig({
4
+ logPrefix: "[mokup]"
5
+ });
6
+ const defineConfig = shared.defineConfig;
7
+ const onBeforeAll = shared.onBeforeAll;
8
+ const onAfterAll = shared.onAfterAll;
160
9
 
161
10
  function defineHandler(input) {
162
11
  return input;