rspack-plugin-mock 0.2.0 → 0.3.1

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/rsbuild.cjs CHANGED
@@ -3,12 +3,16 @@
3
3
 
4
4
 
5
5
 
6
+ var _chunk4GMRSIINcjs = require('./chunk-4GMRSIIN.cjs');
6
7
 
7
- var _chunk2S4KCTKWcjs = require('./chunk-2S4KCTKW.cjs');
8
+
9
+
10
+ var _chunkZEC4FWWYcjs = require('./chunk-ZEC4FWWY.cjs');
8
11
 
9
12
  // src/rsbuild.ts
10
13
  var _process = require('process'); var _process2 = _interopRequireDefault(_process);
11
14
  var _http = require('http');
15
+ var _path = require('path'); var _path2 = _interopRequireDefault(_path);
12
16
  var _utils = require('@pengzhanbo/utils');
13
17
  var _core = require('@rspack/core'); var _core2 = _interopRequireDefault(_core);
14
18
  var _picocolors = require('picocolors'); var _picocolors2 = _interopRequireDefault(_picocolors);
@@ -17,65 +21,77 @@ function pluginMockServer(options = {}) {
17
21
  return {
18
22
  name: "plugin-mock-server",
19
23
  setup(api) {
20
- if (_process2.default.env.NODE_ENV === "production")
21
- return;
22
- let port = 3079;
23
- api.modifyRsbuildConfig(async (config) => {
24
- const defaultPort = (_optionalChain([config, 'access', _ => _.server, 'optionalAccess', _2 => _2.port]) || port) + 1;
25
- port = await _portfinder.getPortPromise.call(void 0, { port: defaultPort });
24
+ const rsbuildConfig = api.getRsbuildConfig();
25
+ const resolvedOptions = _chunk4GMRSIINcjs.resolvePluginOptions.call(void 0, options, {
26
+ proxies: resolveConfigProxies(rsbuildConfig),
27
+ alias: {},
28
+ context: api.context.rootPath,
29
+ plugins: [new _core2.default.DefinePlugin(_optionalChain([rsbuildConfig, 'access', _ => _.source, 'optionalAccess', _2 => _2.define]) || {})]
26
30
  });
27
- let mockCompiler = null;
28
- let resolvedOptions;
29
- api.modifyRsbuildConfig(updateServerProxyConfig);
31
+ if (_process2.default.env.NODE_ENV === "production") {
32
+ if (resolvedOptions.build) {
33
+ api.onAfterBuild(async () => {
34
+ const config = api.getNormalizedConfig();
35
+ await _chunk4GMRSIINcjs.buildMockServer.call(void 0,
36
+ resolvedOptions,
37
+ _path2.default.resolve(_process2.default.cwd(), config.output.distPath.root || "dist")
38
+ );
39
+ });
40
+ }
41
+ return;
42
+ }
43
+ const mockCompiler = _chunk4GMRSIINcjs.createMockCompiler.call(void 0, resolvedOptions);
30
44
  api.modifyRsbuildConfig((config) => {
31
- config.server ??= {};
32
- resolvedOptions = _chunk2S4KCTKWcjs.resolvePluginOptions.call(void 0, options, {
33
- proxies: resolveConfigProxies(config, options.wsPrefix || [], port),
34
- alias: {},
35
- context: api.context.rootPath,
36
- plugins: [new _core2.default.DefinePlugin(_optionalChain([config, 'access', _3 => _3.source, 'optionalAccess', _4 => _4.define]) || {})]
37
- });
38
- mockCompiler = _chunk2S4KCTKWcjs.createMockCompiler.call(void 0, resolvedOptions);
39
- const mockMiddleware = _chunk2S4KCTKWcjs.createMockMiddleware.call(void 0, mockCompiler, resolvedOptions);
45
+ updateServerProxyConfigByHttpMock(config);
46
+ const mockMiddleware = _chunk4GMRSIINcjs.createMockMiddleware.call(void 0, mockCompiler, resolvedOptions);
40
47
  config.dev ??= {};
41
48
  config.dev.setupMiddlewares ??= [];
42
49
  config.dev.setupMiddlewares.push((middlewares, server2) => {
43
50
  mockMiddleware(middlewares, () => server2.sockWrite("static-changed"));
44
51
  });
45
52
  });
53
+ let port = 3079;
54
+ const shouldMockWs = _utils.toArray.call(void 0, resolvedOptions.wsPrefix).length > 0;
55
+ if (shouldMockWs) {
56
+ api.modifyRsbuildConfig(async (config) => {
57
+ const defaultPort = (_optionalChain([config, 'access', _3 => _3.server, 'optionalAccess', _4 => _4.port]) || port) + 1;
58
+ port = await _portfinder.getPortPromise.call(void 0, { port: defaultPort });
59
+ updateServerProxyConfigByWSMock(config, options.wsPrefix || [], port);
60
+ });
61
+ }
62
+ let server;
63
+ function startMockServer() {
64
+ mockCompiler.run();
65
+ if (shouldMockWs) {
66
+ server = _http.createServer.call(void 0, );
67
+ _chunkZEC4FWWYcjs.mockWebSocket.call(void 0, mockCompiler, server, resolvedOptions);
68
+ server.listen(port);
69
+ }
70
+ }
71
+ function close() {
72
+ mockCompiler.close();
73
+ _optionalChain([server, 'optionalAccess', _5 => _5.close, 'call', _6 => _6()]);
74
+ }
46
75
  api.onAfterCreateCompiler(({ compiler }) => {
47
76
  if ("compilers" in compiler) {
48
77
  compiler.compilers.forEach((compiler2) => {
49
- _optionalChain([mockCompiler, 'optionalAccess', _5 => _5.updateAlias, 'call', _6 => _6(_optionalChain([compiler2, 'access', _7 => _7.options, 'access', _8 => _8.resolve, 'optionalAccess', _9 => _9.alias]) || {})]);
78
+ mockCompiler.updateAlias(_optionalChain([compiler2, 'access', _7 => _7.options, 'access', _8 => _8.resolve, 'optionalAccess', _9 => _9.alias]) || {});
50
79
  });
51
80
  } else {
52
- _optionalChain([mockCompiler, 'optionalAccess', _10 => _10.updateAlias, 'call', _11 => _11(_optionalChain([compiler, 'access', _12 => _12.options, 'access', _13 => _13.resolve, 'optionalAccess', _14 => _14.alias]) || {})]);
81
+ mockCompiler.updateAlias(_optionalChain([compiler, 'access', _10 => _10.options, 'access', _11 => _11.resolve, 'optionalAccess', _12 => _12.alias]) || {});
53
82
  }
54
83
  });
55
- let server;
56
- function startMockServer() {
57
- if (!mockCompiler)
58
- return;
59
- mockCompiler.run();
60
- server = _http.createServer.call(void 0, );
61
- _chunk2S4KCTKWcjs.mockWebSocket.call(void 0, mockCompiler, server, resolvedOptions);
62
- server.listen(port);
63
- }
64
- function close() {
65
- _optionalChain([mockCompiler, 'optionalAccess', _15 => _15.close, 'call', _16 => _16()]);
66
- _optionalChain([server, 'optionalAccess', _17 => _17.close, 'call', _18 => _18()]);
67
- }
68
84
  api.onAfterStartDevServer(startMockServer);
69
85
  api.onAfterStartProdServer(startMockServer);
70
86
  api.onExit(close);
71
87
  }
72
88
  };
73
89
  }
74
- function updateServerProxyConfig(config) {
75
- if (!_optionalChain([config, 'access', _19 => _19.server, 'optionalAccess', _20 => _20.proxy]))
90
+ function updateServerProxyConfigByHttpMock(config) {
91
+ if (!_optionalChain([config, 'access', _13 => _13.server, 'optionalAccess', _14 => _14.proxy]))
76
92
  return;
77
93
  const onProxyError = (err, _req, res) => {
78
- console.error(_picocolors2.default.red(_optionalChain([err, 'optionalAccess', _21 => _21.stack]) || err.message));
94
+ console.error(_picocolors2.default.red(_optionalChain([err, 'optionalAccess', _15 => _15.stack]) || err.message));
79
95
  res.statusCode = 500;
80
96
  res.end();
81
97
  };
@@ -88,8 +104,8 @@ function updateServerProxyConfig(config) {
88
104
  ...item,
89
105
  onError: onError || onProxyError,
90
106
  onProxyReq: (proxyReq, req, ...args) => {
91
- _optionalChain([onProxyReq, 'optionalCall', _22 => _22(proxyReq, req, ...args)]);
92
- _chunk2S4KCTKWcjs.rewriteRequest.call(void 0, proxyReq, req);
107
+ _optionalChain([onProxyReq, 'optionalCall', _16 => _16(proxyReq, req, ...args)]);
108
+ _chunkZEC4FWWYcjs.rewriteRequest.call(void 0, proxyReq, req);
93
109
  }
94
110
  };
95
111
  }
@@ -98,8 +114,8 @@ function updateServerProxyConfig(config) {
98
114
  } else if ("target" in config.server.proxy) {
99
115
  const onProxyReq = config.server.proxy.onProxyReq;
100
116
  config.server.proxy.onProxyReq = (proxyReq, req, ...args) => {
101
- _optionalChain([onProxyReq, 'optionalCall', _23 => _23(proxyReq, req, ...args)]);
102
- _chunk2S4KCTKWcjs.rewriteRequest.call(void 0, proxyReq, req);
117
+ _optionalChain([onProxyReq, 'optionalCall', _17 => _17(proxyReq, req, ...args)]);
118
+ _chunkZEC4FWWYcjs.rewriteRequest.call(void 0, proxyReq, req);
103
119
  };
104
120
  config.server.proxy.onError ??= onProxyError;
105
121
  } else if (config.server.proxy) {
@@ -113,17 +129,17 @@ function updateServerProxyConfig(config) {
113
129
  proxy[key] = {
114
130
  ...rest,
115
131
  onProxyReq: (proxyReq, req, ...args) => {
116
- _optionalChain([onProxyReq, 'optionalCall', _24 => _24(proxyReq, req, ...args)]);
117
- _chunk2S4KCTKWcjs.rewriteRequest.call(void 0, proxyReq, req);
132
+ _optionalChain([onProxyReq, 'optionalCall', _18 => _18(proxyReq, req, ...args)]);
133
+ _chunkZEC4FWWYcjs.rewriteRequest.call(void 0, proxyReq, req);
118
134
  },
119
135
  onError: onError || onProxyError
120
136
  };
121
137
  });
122
138
  }
123
139
  }
124
- function resolveConfigProxies(config, wsPrefix, port) {
140
+ function updateServerProxyConfigByWSMock(config, wsPrefix, port) {
141
+ config.server ??= {};
125
142
  const proxy = config.server.proxy ??= {};
126
- const proxies = [];
127
143
  const wsTarget = `ws://localhost:${port}`;
128
144
  const prefix = _utils.toArray.call(void 0, wsPrefix);
129
145
  const has = (context) => typeof context === "string" && prefix.includes(context);
@@ -138,32 +154,19 @@ function resolveConfigProxies(config, wsPrefix, port) {
138
154
  }
139
155
  if (_utils.isArray.call(void 0, proxy)) {
140
156
  for (const item of proxy) {
141
- if (typeof item !== "function" && item.context) {
142
- if (!item.ws) {
143
- proxies.push(..._utils.toArray.call(void 0, item.context));
144
- } else {
145
- updateProxy(item);
146
- }
157
+ if (typeof item !== "function" && item.context && item.ws) {
158
+ updateProxy(item);
147
159
  }
148
160
  }
149
- prefix.filter((context) => !used.has(context)).forEach((context) => {
150
- proxy.push({ context, target: wsTarget });
151
- });
161
+ prefix.filter((context) => !used.has(context)).forEach((context) => proxy.push({ context, target: wsTarget }));
152
162
  } else if ("target" in proxy) {
153
- if (!proxy.ws) {
154
- proxies.push(..._utils.toArray.call(void 0, proxy.context));
155
- } else {
163
+ if (proxy.ws) {
156
164
  updateProxy(proxy);
157
165
  const list = config.server.proxy = [proxy];
158
- prefix.filter((context) => !used.has(context)).forEach((context) => {
159
- list.push({ context, target: wsTarget });
160
- });
166
+ prefix.filter((context) => !used.has(context)).forEach((context) => list.push({ context, target: wsTarget }));
161
167
  }
162
168
  } else {
163
- Object.entries(proxy).forEach(([context, opt]) => {
164
- if (typeof opt === "string" || !opt.ws) {
165
- proxies.push(context);
166
- }
169
+ Object.entries(proxy).forEach(([, opt]) => {
167
170
  if (typeof opt !== "string" && opt.ws) {
168
171
  updateProxy(opt);
169
172
  }
@@ -172,6 +175,26 @@ function resolveConfigProxies(config, wsPrefix, port) {
172
175
  proxy[context] = { target: wsTarget, ws: true };
173
176
  });
174
177
  }
178
+ }
179
+ function resolveConfigProxies(config) {
180
+ config.server ??= {};
181
+ const proxy = config.server.proxy ??= {};
182
+ const proxies = [];
183
+ if (_utils.isArray.call(void 0, proxy)) {
184
+ for (const item of proxy) {
185
+ if (typeof item !== "function" && item.context && !item.ws) {
186
+ proxies.push(..._utils.toArray.call(void 0, item.context));
187
+ }
188
+ }
189
+ } else if ("target" in proxy) {
190
+ if (!proxy.ws)
191
+ proxies.push(..._utils.toArray.call(void 0, proxy.context));
192
+ } else {
193
+ Object.entries(proxy).forEach(([context, opt]) => {
194
+ if (typeof opt === "string" || !opt.ws)
195
+ proxies.push(context);
196
+ });
197
+ }
175
198
  return proxies;
176
199
  }
177
200
 
@@ -1,5 +1,5 @@
1
1
  import { RsbuildPlugin } from '@rsbuild/core';
2
- import { M as MockServerPluginOptions } from './types-C770q3L0.cjs';
2
+ import { M as MockServerPluginOptions } from './types-BgpcN3jm.cjs';
3
3
  import 'node:buffer';
4
4
  import 'node:http';
5
5
  import 'node:stream';
package/dist/rsbuild.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { RsbuildPlugin } from '@rsbuild/core';
2
- import { M as MockServerPluginOptions } from './types-C770q3L0.js';
2
+ import { M as MockServerPluginOptions } from './types-BgpcN3jm.js';
3
3
  import 'node:buffer';
4
4
  import 'node:http';
5
5
  import 'node:stream';
package/dist/rsbuild.js CHANGED
@@ -1,14 +1,18 @@
1
1
  import {
2
+ buildMockServer,
2
3
  createMockCompiler,
3
4
  createMockMiddleware,
5
+ resolvePluginOptions
6
+ } from "./chunk-B5W3PWCX.js";
7
+ import {
4
8
  mockWebSocket,
5
- resolvePluginOptions,
6
9
  rewriteRequest
7
- } from "./chunk-UJAKORAH.js";
10
+ } from "./chunk-EY46RSAC.js";
8
11
 
9
12
  // src/rsbuild.ts
10
13
  import process from "process";
11
14
  import { createServer } from "http";
15
+ import path from "path";
12
16
  import { isArray, toArray } from "@pengzhanbo/utils";
13
17
  import rspack from "@rspack/core";
14
18
  import color from "picocolors";
@@ -17,25 +21,28 @@ function pluginMockServer(options = {}) {
17
21
  return {
18
22
  name: "plugin-mock-server",
19
23
  setup(api) {
20
- if (process.env.NODE_ENV === "production")
21
- return;
22
- let port = 3079;
23
- api.modifyRsbuildConfig(async (config) => {
24
- const defaultPort = (config.server?.port || port) + 1;
25
- port = await getPortPromise({ port: defaultPort });
24
+ const rsbuildConfig = api.getRsbuildConfig();
25
+ const resolvedOptions = resolvePluginOptions(options, {
26
+ proxies: resolveConfigProxies(rsbuildConfig),
27
+ alias: {},
28
+ context: api.context.rootPath,
29
+ plugins: [new rspack.DefinePlugin(rsbuildConfig.source?.define || {})]
26
30
  });
27
- let mockCompiler = null;
28
- let resolvedOptions;
29
- api.modifyRsbuildConfig(updateServerProxyConfig);
31
+ if (process.env.NODE_ENV === "production") {
32
+ if (resolvedOptions.build) {
33
+ api.onAfterBuild(async () => {
34
+ const config = api.getNormalizedConfig();
35
+ await buildMockServer(
36
+ resolvedOptions,
37
+ path.resolve(process.cwd(), config.output.distPath.root || "dist")
38
+ );
39
+ });
40
+ }
41
+ return;
42
+ }
43
+ const mockCompiler = createMockCompiler(resolvedOptions);
30
44
  api.modifyRsbuildConfig((config) => {
31
- config.server ??= {};
32
- resolvedOptions = resolvePluginOptions(options, {
33
- proxies: resolveConfigProxies(config, options.wsPrefix || [], port),
34
- alias: {},
35
- context: api.context.rootPath,
36
- plugins: [new rspack.DefinePlugin(config.source?.define || {})]
37
- });
38
- mockCompiler = createMockCompiler(resolvedOptions);
45
+ updateServerProxyConfigByHttpMock(config);
39
46
  const mockMiddleware = createMockMiddleware(mockCompiler, resolvedOptions);
40
47
  config.dev ??= {};
41
48
  config.dev.setupMiddlewares ??= [];
@@ -43,35 +50,44 @@ function pluginMockServer(options = {}) {
43
50
  mockMiddleware(middlewares, () => server2.sockWrite("static-changed"));
44
51
  });
45
52
  });
46
- api.onAfterCreateCompiler(({ compiler }) => {
47
- if ("compilers" in compiler) {
48
- compiler.compilers.forEach((compiler2) => {
49
- mockCompiler?.updateAlias(compiler2.options.resolve?.alias || {});
50
- });
51
- } else {
52
- mockCompiler?.updateAlias(compiler.options.resolve?.alias || {});
53
- }
54
- });
53
+ let port = 3079;
54
+ const shouldMockWs = toArray(resolvedOptions.wsPrefix).length > 0;
55
+ if (shouldMockWs) {
56
+ api.modifyRsbuildConfig(async (config) => {
57
+ const defaultPort = (config.server?.port || port) + 1;
58
+ port = await getPortPromise({ port: defaultPort });
59
+ updateServerProxyConfigByWSMock(config, options.wsPrefix || [], port);
60
+ });
61
+ }
55
62
  let server;
56
63
  function startMockServer() {
57
- if (!mockCompiler)
58
- return;
59
64
  mockCompiler.run();
60
- server = createServer();
61
- mockWebSocket(mockCompiler, server, resolvedOptions);
62
- server.listen(port);
65
+ if (shouldMockWs) {
66
+ server = createServer();
67
+ mockWebSocket(mockCompiler, server, resolvedOptions);
68
+ server.listen(port);
69
+ }
63
70
  }
64
71
  function close() {
65
- mockCompiler?.close();
72
+ mockCompiler.close();
66
73
  server?.close();
67
74
  }
75
+ api.onAfterCreateCompiler(({ compiler }) => {
76
+ if ("compilers" in compiler) {
77
+ compiler.compilers.forEach((compiler2) => {
78
+ mockCompiler.updateAlias(compiler2.options.resolve?.alias || {});
79
+ });
80
+ } else {
81
+ mockCompiler.updateAlias(compiler.options.resolve?.alias || {});
82
+ }
83
+ });
68
84
  api.onAfterStartDevServer(startMockServer);
69
85
  api.onAfterStartProdServer(startMockServer);
70
86
  api.onExit(close);
71
87
  }
72
88
  };
73
89
  }
74
- function updateServerProxyConfig(config) {
90
+ function updateServerProxyConfigByHttpMock(config) {
75
91
  if (!config.server?.proxy)
76
92
  return;
77
93
  const onProxyError = (err, _req, res) => {
@@ -121,9 +137,9 @@ function updateServerProxyConfig(config) {
121
137
  });
122
138
  }
123
139
  }
124
- function resolveConfigProxies(config, wsPrefix, port) {
140
+ function updateServerProxyConfigByWSMock(config, wsPrefix, port) {
141
+ config.server ??= {};
125
142
  const proxy = config.server.proxy ??= {};
126
- const proxies = [];
127
143
  const wsTarget = `ws://localhost:${port}`;
128
144
  const prefix = toArray(wsPrefix);
129
145
  const has = (context) => typeof context === "string" && prefix.includes(context);
@@ -138,32 +154,19 @@ function resolveConfigProxies(config, wsPrefix, port) {
138
154
  }
139
155
  if (isArray(proxy)) {
140
156
  for (const item of proxy) {
141
- if (typeof item !== "function" && item.context) {
142
- if (!item.ws) {
143
- proxies.push(...toArray(item.context));
144
- } else {
145
- updateProxy(item);
146
- }
157
+ if (typeof item !== "function" && item.context && item.ws) {
158
+ updateProxy(item);
147
159
  }
148
160
  }
149
- prefix.filter((context) => !used.has(context)).forEach((context) => {
150
- proxy.push({ context, target: wsTarget });
151
- });
161
+ prefix.filter((context) => !used.has(context)).forEach((context) => proxy.push({ context, target: wsTarget }));
152
162
  } else if ("target" in proxy) {
153
- if (!proxy.ws) {
154
- proxies.push(...toArray(proxy.context));
155
- } else {
163
+ if (proxy.ws) {
156
164
  updateProxy(proxy);
157
165
  const list = config.server.proxy = [proxy];
158
- prefix.filter((context) => !used.has(context)).forEach((context) => {
159
- list.push({ context, target: wsTarget });
160
- });
166
+ prefix.filter((context) => !used.has(context)).forEach((context) => list.push({ context, target: wsTarget }));
161
167
  }
162
168
  } else {
163
- Object.entries(proxy).forEach(([context, opt]) => {
164
- if (typeof opt === "string" || !opt.ws) {
165
- proxies.push(context);
166
- }
169
+ Object.entries(proxy).forEach(([, opt]) => {
167
170
  if (typeof opt !== "string" && opt.ws) {
168
171
  updateProxy(opt);
169
172
  }
@@ -172,6 +175,26 @@ function resolveConfigProxies(config, wsPrefix, port) {
172
175
  proxy[context] = { target: wsTarget, ws: true };
173
176
  });
174
177
  }
178
+ }
179
+ function resolveConfigProxies(config) {
180
+ config.server ??= {};
181
+ const proxy = config.server.proxy ??= {};
182
+ const proxies = [];
183
+ if (isArray(proxy)) {
184
+ for (const item of proxy) {
185
+ if (typeof item !== "function" && item.context && !item.ws) {
186
+ proxies.push(...toArray(item.context));
187
+ }
188
+ }
189
+ } else if ("target" in proxy) {
190
+ if (!proxy.ws)
191
+ proxies.push(...toArray(proxy.context));
192
+ } else {
193
+ Object.entries(proxy).forEach(([context, opt]) => {
194
+ if (typeof opt === "string" || !opt.ws)
195
+ proxies.push(context);
196
+ });
197
+ }
175
198
  return proxies;
176
199
  }
177
200
  export {
@@ -0,0 +1,18 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+
4
+
5
+
6
+
7
+
8
+
9
+ var _chunkZEC4FWWYcjs = require('./chunk-ZEC4FWWY.cjs');
10
+
11
+
12
+
13
+
14
+
15
+
16
+
17
+
18
+ exports.baseMiddleware = _chunkZEC4FWWYcjs.baseMiddleware; exports.createLogger = _chunkZEC4FWWYcjs.createLogger; exports.logLevels = _chunkZEC4FWWYcjs.logLevels; exports.mockWebSocket = _chunkZEC4FWWYcjs.mockWebSocket; exports.sortByValidator = _chunkZEC4FWWYcjs.sortByValidator; exports.transformMockData = _chunkZEC4FWWYcjs.transformMockData; exports.transformRawData = _chunkZEC4FWWYcjs.transformRawData;
@@ -0,0 +1,31 @@
1
+ import { M as MockServerPluginOptions, a as MockHttpItem, b as MockWebsocketItem, c as MockOptions } from './types-BgpcN3jm.cjs';
2
+ import { L as Logger, f as MockCompiler, a as Middleware } from './mockWebsocket-Dc9CZBfv.cjs';
3
+ export { b as MockSocketOptions, g as createLogger, l as logLevels, m as mockWebSocket } from './mockWebsocket-Dc9CZBfv.cjs';
4
+ import 'node:buffer';
5
+ import 'node:http';
6
+ import 'node:stream';
7
+ import 'cookies';
8
+ import 'cors';
9
+ import 'formidable';
10
+ import 'ws';
11
+ import 'co-body';
12
+ import '@rspack/core';
13
+ import 'node:http2';
14
+ import 'node:events';
15
+ import 'node:fs';
16
+
17
+ interface BaseMiddlewareOptions {
18
+ formidableOptions: MockServerPluginOptions['formidableOptions'];
19
+ cookiesOptions: MockServerPluginOptions['cookiesOptions'];
20
+ bodyParserOptions: MockServerPluginOptions['bodyParserOptions'];
21
+ proxies: (string | ((pathname: string, req: any) => boolean))[];
22
+ logger: Logger;
23
+ priority: MockServerPluginOptions['priority'];
24
+ }
25
+ declare function baseMiddleware(compiler: MockCompiler, { formidableOptions, bodyParserOptions, proxies, cookiesOptions, logger, priority, }: BaseMiddlewareOptions): Middleware;
26
+
27
+ declare function transformRawData(rawData: (readonly [any, string])[]): (MockHttpItem | MockWebsocketItem | MockOptions)[];
28
+ declare function transformMockData(mockList: (MockHttpItem | MockWebsocketItem | MockOptions)[]): Record<string, MockOptions>;
29
+ declare function sortByValidator(mocks: MockOptions): (MockHttpItem | MockWebsocketItem)[];
30
+
31
+ export { type BaseMiddlewareOptions, Logger, baseMiddleware, sortByValidator, transformMockData, transformRawData };
@@ -0,0 +1,31 @@
1
+ import { M as MockServerPluginOptions, a as MockHttpItem, b as MockWebsocketItem, c as MockOptions } from './types-BgpcN3jm.js';
2
+ import { L as Logger, f as MockCompiler, a as Middleware } from './mockWebsocket-CPuTAvL0.js';
3
+ export { b as MockSocketOptions, g as createLogger, l as logLevels, m as mockWebSocket } from './mockWebsocket-CPuTAvL0.js';
4
+ import 'node:buffer';
5
+ import 'node:http';
6
+ import 'node:stream';
7
+ import 'cookies';
8
+ import 'cors';
9
+ import 'formidable';
10
+ import 'ws';
11
+ import 'co-body';
12
+ import '@rspack/core';
13
+ import 'node:http2';
14
+ import 'node:events';
15
+ import 'node:fs';
16
+
17
+ interface BaseMiddlewareOptions {
18
+ formidableOptions: MockServerPluginOptions['formidableOptions'];
19
+ cookiesOptions: MockServerPluginOptions['cookiesOptions'];
20
+ bodyParserOptions: MockServerPluginOptions['bodyParserOptions'];
21
+ proxies: (string | ((pathname: string, req: any) => boolean))[];
22
+ logger: Logger;
23
+ priority: MockServerPluginOptions['priority'];
24
+ }
25
+ declare function baseMiddleware(compiler: MockCompiler, { formidableOptions, bodyParserOptions, proxies, cookiesOptions, logger, priority, }: BaseMiddlewareOptions): Middleware;
26
+
27
+ declare function transformRawData(rawData: (readonly [any, string])[]): (MockHttpItem | MockWebsocketItem | MockOptions)[];
28
+ declare function transformMockData(mockList: (MockHttpItem | MockWebsocketItem | MockOptions)[]): Record<string, MockOptions>;
29
+ declare function sortByValidator(mocks: MockOptions): (MockHttpItem | MockWebsocketItem)[];
30
+
31
+ export { type BaseMiddlewareOptions, Logger, baseMiddleware, sortByValidator, transformMockData, transformRawData };
package/dist/server.js ADDED
@@ -0,0 +1,18 @@
1
+ import {
2
+ baseMiddleware,
3
+ createLogger,
4
+ logLevels,
5
+ mockWebSocket,
6
+ sortByValidator,
7
+ transformMockData,
8
+ transformRawData
9
+ } from "./chunk-EY46RSAC.js";
10
+ export {
11
+ baseMiddleware,
12
+ createLogger,
13
+ logLevels,
14
+ mockWebSocket,
15
+ sortByValidator,
16
+ transformMockData,
17
+ transformRawData
18
+ };
@@ -567,4 +567,4 @@ type FormidableFile = formidable.File | formidable.File[];
567
567
  type LogType = 'info' | 'warn' | 'error' | 'debug';
568
568
  type LogLevel = LogType | 'silent';
569
569
 
570
- export type { BodyParserOptions as B, ExtraRequest as E, FormidableFile as F, LogLevel as L, MockServerPluginOptions as M, ResponseBody as R, ServerBuildOption as S, WebSocketSetupContext as W, MockOptions as a, MockHttpItem as b, MockWebsocketItem as c, MockMatchPriority as d, MockMatchSpecialPriority as e, Method as f, MockRequest as g, MockResponse as h, LogType as i };
570
+ export type { BodyParserOptions as B, ExtraRequest as E, FormidableFile as F, LogType as L, MockServerPluginOptions as M, ResponseBody as R, ServerBuildOption as S, WebSocketSetupContext as W, MockHttpItem as a, MockWebsocketItem as b, MockOptions as c, MockMatchPriority as d, MockMatchSpecialPriority as e, Method as f, MockRequest as g, MockResponse as h, LogLevel as i };
@@ -567,4 +567,4 @@ type FormidableFile = formidable.File | formidable.File[];
567
567
  type LogType = 'info' | 'warn' | 'error' | 'debug';
568
568
  type LogLevel = LogType | 'silent';
569
569
 
570
- export type { BodyParserOptions as B, ExtraRequest as E, FormidableFile as F, LogLevel as L, MockServerPluginOptions as M, ResponseBody as R, ServerBuildOption as S, WebSocketSetupContext as W, MockOptions as a, MockHttpItem as b, MockWebsocketItem as c, MockMatchPriority as d, MockMatchSpecialPriority as e, Method as f, MockRequest as g, MockResponse as h, LogType as i };
570
+ export type { BodyParserOptions as B, ExtraRequest as E, FormidableFile as F, LogType as L, MockServerPluginOptions as M, ResponseBody as R, ServerBuildOption as S, WebSocketSetupContext as W, MockHttpItem as a, MockWebsocketItem as b, MockOptions as c, MockMatchPriority as d, MockMatchSpecialPriority as e, Method as f, MockRequest as g, MockResponse as h, LogLevel as i };
package/package.json CHANGED
@@ -1,13 +1,15 @@
1
1
  {
2
2
  "name": "rspack-plugin-mock",
3
3
  "type": "module",
4
- "version": "0.2.0",
4
+ "version": "0.3.1",
5
5
  "description": "inject api mock server to development server",
6
6
  "author": "pengzhanbo <q942450674@outlook.com> (https://github.com/pengzhanbo)",
7
7
  "license": "MIT",
8
8
  "keywords": [
9
9
  "mock",
10
10
  "mock server",
11
+ "rspack",
12
+ "rsbuild",
11
13
  "rspack-plugin",
12
14
  "rsbuild-plugin"
13
15
  ],
@@ -22,14 +24,14 @@
22
24
  "default": "./dist/index.cjs"
23
25
  }
24
26
  },
25
- "./rspack": {
27
+ "./server": {
26
28
  "import": {
27
- "types": "./dist/rspack.d.ts",
28
- "default": "./dist/rspack.js"
29
+ "types": "./dist/server.d.ts",
30
+ "default": "./dist/server.js"
29
31
  },
30
32
  "require": {
31
- "types": "./dist/rspack.d.cts",
32
- "default": "./dist/rspack.cjs"
33
+ "types": "./dist/server.d.cts",
34
+ "default": "./dist/server.cjs"
33
35
  }
34
36
  },
35
37
  "./rsbuild": {
@@ -97,8 +99,8 @@
97
99
  },
98
100
  "devDependencies": {
99
101
  "@pengzhanbo/eslint-config": "^1.12.0",
100
- "@rsbuild/core": "1.0.1-beta.8",
101
- "@rspack/core": "1.0.0-beta.1",
102
+ "@rsbuild/core": "1.0.1-beta.9",
103
+ "@rspack/core": "1.0.0-beta.2",
102
104
  "@types/co-body": "^6.1.3",
103
105
  "@types/cookies": "^0.9.0",
104
106
  "@types/cors": "^2.8.17",
@@ -106,15 +108,14 @@
106
108
  "@types/formidable": "2.0.6",
107
109
  "@types/is-core-module": "^2.2.2",
108
110
  "@types/mime-types": "^2.1.4",
109
- "@types/node": "^22.0.2",
111
+ "@types/node": "^22.1.0",
110
112
  "@types/ws": "^8.5.12",
111
- "bumpp": "^9.4.1",
113
+ "bumpp": "^9.4.2",
112
114
  "conventional-changelog-cli": "^5.0.0",
113
- "esbuild": "^0.23.0",
114
115
  "eslint": "^9.8.0",
115
- "husky": "^9.1.1",
116
- "lint-staged": "^15.2.7",
117
- "tsup": "^8.2.3",
116
+ "husky": "^9.1.4",
117
+ "lint-staged": "^15.2.8",
118
+ "tsup": "^8.2.4",
118
119
  "typescript": "^5.5.4"
119
120
  },
120
121
  "lint-staged": {