rspack-plugin-mock 0.4.0 → 0.4.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.
@@ -9,121 +9,23 @@
9
9
 
10
10
 
11
11
 
12
- var _chunkT2VK464Wcjs = require('./chunk-T2VK464W.cjs');
13
-
14
- // src/core/mockMiddleware.ts
15
- var _cors = require('cors'); var _cors2 = _interopRequireDefault(_cors);
16
- var _pathtoregexp = require('path-to-regexp');
17
- function createMockMiddleware(compiler, options) {
18
- function mockMiddleware(middlewares, reload) {
19
- middlewares.unshift(_chunkT2VK464Wcjs.baseMiddleware.call(void 0, compiler, options));
20
- const corsMiddleware = createCorsMiddleware(compiler, options);
21
- if (corsMiddleware) {
22
- middlewares.unshift(corsMiddleware);
23
- }
24
- if (options.reload) {
25
- compiler.on("update", () => _optionalChain([reload, 'optionalCall', _ => _()]));
26
- }
27
- return middlewares;
28
- }
29
- return mockMiddleware;
30
- }
31
- function createCorsMiddleware(compiler, options) {
32
- let corsOptions = {};
33
- const enabled = options.cors !== false;
34
- if (enabled) {
35
- corsOptions = {
36
- ...corsOptions,
37
- ...typeof options.cors === "boolean" ? {} : options.cors
38
- };
39
- }
40
- const proxies = options.proxies;
41
- return !enabled ? void 0 : function(req, res, next) {
42
- const { pathname } = _chunkT2VK464Wcjs.urlParse.call(void 0, req.url);
43
- if (!pathname || proxies.length === 0 || !proxies.some(
44
- (context) => _chunkT2VK464Wcjs.doesProxyContextMatchUrl.call(void 0, context, req.url, req)
45
- )) {
46
- return next();
47
- }
48
- const mockData = compiler.mockData;
49
- const mockUrl = Object.keys(mockData).find(
50
- (key) => _pathtoregexp.pathToRegexp.call(void 0, key).test(pathname)
51
- );
52
- if (!mockUrl)
53
- return next();
54
- _cors2.default.call(void 0, corsOptions)(req, res, next);
55
- };
56
- }
57
-
58
- // src/core/resolvePluginOptions.ts
59
- var _process = require('process'); var _process2 = _interopRequireDefault(_process);
60
- var _utils = require('@pengzhanbo/utils');
61
- function resolvePluginOptions({
62
- prefix = [],
63
- wsPrefix = [],
64
- cwd,
65
- include = ["mock/**/*.mock.{js,ts,cjs,mjs,json,json5}"],
66
- exclude = ["**/node_modules/**", "**/.vscode/**", "**/.git/**"],
67
- reload = false,
68
- log = "info",
69
- cors: cors2 = true,
70
- formidableOptions = {},
71
- build = false,
72
- cookiesOptions = {},
73
- bodyParserOptions = {},
74
- priority = {}
75
- } = {}, { alias, context, plugins, proxies }) {
76
- const logger = _chunkT2VK464Wcjs.createLogger.call(void 0,
77
- "rspack:mock",
78
- _utils.isBoolean.call(void 0, log) ? log ? "info" : "error" : log
79
- );
80
- return {
81
- prefix,
82
- wsPrefix,
83
- cwd: cwd || context || _process2.default.cwd(),
84
- include,
85
- exclude,
86
- reload,
87
- cors: cors2,
88
- cookiesOptions,
89
- log,
90
- formidableOptions: {
91
- multiples: true,
92
- ...formidableOptions
93
- },
94
- bodyParserOptions,
95
- priority,
96
- build: build ? Object.assign(
97
- {
98
- serverPort: 8080,
99
- dist: "mockServer",
100
- log: "error"
101
- },
102
- typeof build === "object" ? build : {}
103
- ) : false,
104
- alias,
105
- plugins,
106
- proxies,
107
- wsProxies: _utils.toArray.call(void 0, wsPrefix),
108
- logger
109
- };
110
- }
12
+ var _chunkTKSRTJX5cjs = require('./chunk-TKSRTJX5.cjs');
111
13
 
112
14
  // src/core/build.ts
113
15
  var _fs = require('fs'); var _fs2 = _interopRequireDefault(_fs);
114
16
  var _promises = require('fs/promises'); var _promises2 = _interopRequireDefault(_promises);
115
17
  var _path = require('path'); var _path2 = _interopRequireDefault(_path);
116
-
117
- var _fastglob = require('fast-glob'); var _fastglob2 = _interopRequireDefault(_fastglob);
18
+ var _process = require('process'); var _process2 = _interopRequireDefault(_process);
19
+ var _utils = require('@pengzhanbo/utils');
118
20
  var _pluginutils = require('@rollup/pluginutils');
21
+ var _fastglob = require('fast-glob'); var _fastglob2 = _interopRequireDefault(_fastglob);
119
22
  var _picocolors = require('picocolors'); var _picocolors2 = _interopRequireDefault(_picocolors);
120
23
 
121
-
122
24
  // src/core/createRspackCompiler.ts
123
25
  var _module = require('module');
124
26
  var _core = require('@rspack/core'); var rspackCore = _interopRequireWildcard(_core);
125
-
126
27
  var _iscoremodule = require('is-core-module'); var _iscoremodule2 = _interopRequireDefault(_iscoremodule);
28
+
127
29
  var require2 = _module.createRequire.call(void 0, import.meta.url);
128
30
  function createCompiler(options, callback) {
129
31
  const rspackOptions = resolveRspackOptions(options);
@@ -144,17 +46,17 @@ function createCompiler(options, callback) {
144
46
  }
145
47
  return;
146
48
  }
147
- if (_optionalChain([stats, 'optionalAccess', _2 => _2.hasErrors, 'call', _3 => _3()])) {
49
+ if (_optionalChain([stats, 'optionalAccess', _ => _.hasErrors, 'call', _2 => _2()])) {
148
50
  const info = stats.toJson();
149
51
  logError(info.errors);
150
52
  }
151
- const code = _chunkT2VK464Wcjs.vfs.readFileSync("/output.js", "utf-8");
53
+ const code = _chunkTKSRTJX5cjs.vfs.readFileSync("/output.js", "utf-8");
152
54
  const externals = [];
153
55
  if (!isWatch) {
154
- const modules = _optionalChain([stats, 'optionalAccess', _4 => _4.toJson, 'call', _5 => _5(), 'access', _6 => _6.modules]) || [];
56
+ const modules = _optionalChain([stats, 'optionalAccess', _3 => _3.toJson, 'call', _4 => _4(), 'access', _5 => _5.modules]) || [];
155
57
  const aliasList = Object.keys(options.alias || {}).map((key) => key.replace(/\$$/g, ""));
156
58
  for (const { name: name2 } of modules) {
157
- if (_optionalChain([name2, 'optionalAccess', _7 => _7.startsWith, 'call', _8 => _8("external")])) {
59
+ if (_optionalChain([name2, 'optionalAccess', _6 => _6.startsWith, 'call', _7 => _7("external")])) {
158
60
  const packageName = normalizePackageName(name2);
159
61
  if (!_iscoremodule2.default.call(void 0, packageName) && !aliasList.includes(packageName))
160
62
  externals.push(normalizePackageName(name2));
@@ -165,9 +67,9 @@ function createCompiler(options, callback) {
165
67
  }
166
68
  const compiler = rspackCore.rspack(rspackOptions, isWatch ? handler : void 0);
167
69
  if (compiler)
168
- compiler.outputFileSystem = _chunkT2VK464Wcjs.vfs;
70
+ compiler.outputFileSystem = _chunkTKSRTJX5cjs.vfs;
169
71
  if (!isWatch) {
170
- _optionalChain([compiler, 'optionalAccess', _9 => _9.run, 'call', _10 => _10(async (...args) => {
72
+ _optionalChain([compiler, 'optionalAccess', _8 => _8.run, 'call', _9 => _9(async (...args) => {
171
73
  await handler(...args);
172
74
  compiler.close(() => {
173
75
  });
@@ -368,7 +270,7 @@ async function writeMockEntryFile(entryFile, files, cwd) {
368
270
  const importers = [];
369
271
  const exporters = [];
370
272
  for (const [index, filepath] of files.entries()) {
371
- const file = _chunkT2VK464Wcjs.normalizePath.call(void 0, _path2.default.join(cwd, filepath));
273
+ const file = _chunkTKSRTJX5cjs.normalizePath.call(void 0, _path2.default.join(cwd, filepath));
372
274
  importers.push(`import * as m${index} from '${file}'`);
373
275
  exporters.push(`[m${index}, '${filepath}']`);
374
276
  }
@@ -386,7 +288,7 @@ export default [
386
288
  function getPluginPackageInfo() {
387
289
  let pkg = {};
388
290
  try {
389
- const filepath = _path2.default.join(_chunkT2VK464Wcjs.packageDir, "../package.json");
291
+ const filepath = _path2.default.join(_chunkTKSRTJX5cjs.packageDir, "../package.json");
390
292
  if (_fs2.default.existsSync(filepath)) {
391
293
  pkg = JSON.parse(_fs2.default.readFileSync(filepath, "utf8"));
392
294
  }
@@ -400,7 +302,7 @@ function getPluginPackageInfo() {
400
302
  function getHostDependencies(context) {
401
303
  let pkg = {};
402
304
  try {
403
- const content = _chunkT2VK464Wcjs.lookupFile.call(void 0, context, ["package.json"]);
305
+ const content = _chunkTKSRTJX5cjs.lookupFile.call(void 0, context, ["package.json"]);
404
306
  if (content)
405
307
  pkg = JSON.parse(content);
406
308
  } catch (e3) {
@@ -413,8 +315,8 @@ var _events = require('events'); var _events2 = _interopRequireDefault(_events);
413
315
 
414
316
 
415
317
 
416
- var _chokidar = require('chokidar'); var _chokidar2 = _interopRequireDefault(_chokidar);
417
318
 
319
+ var _chokidar = require('chokidar'); var _chokidar2 = _interopRequireDefault(_chokidar);
418
320
 
419
321
 
420
322
  // src/core/loadFromCode.ts
@@ -455,7 +357,7 @@ var MockCompiler = (_class = class extends _events2.default {
455
357
  const { include, exclude } = this.options;
456
358
  this.fileFilter = _pluginutils.createFilter.call(void 0, include, exclude, { resolve: false });
457
359
  try {
458
- const pkg = _chunkT2VK464Wcjs.lookupFile.call(void 0, this.cwd, ["package.json"]);
360
+ const pkg = _chunkTKSRTJX5cjs.lookupFile.call(void 0, this.cwd, ["package.json"]);
459
361
  this.moduleType = !!pkg && JSON.parse(pkg).type === "module" ? "esm" : "cjs";
460
362
  } catch (e5) {
461
363
  }
@@ -492,7 +394,7 @@ var MockCompiler = (_class = class extends _events2.default {
492
394
  isESM: this.moduleType === "esm",
493
395
  cwd: this.cwd
494
396
  });
495
- this._mockData = _chunkT2VK464Wcjs.transformMockData.call(void 0, _chunkT2VK464Wcjs.transformRawData.call(void 0, result));
397
+ this._mockData = _chunkTKSRTJX5cjs.transformMockData.call(void 0, _chunkTKSRTJX5cjs.transformRawData.call(void 0, result));
496
398
  this.emit("update", this.watchInfo || {});
497
399
  } catch (e) {
498
400
  this.options.logger.error(e.stack || e.message);
@@ -501,7 +403,7 @@ var MockCompiler = (_class = class extends _events2.default {
501
403
  }
502
404
  close() {
503
405
  this.mockWatcher.close();
504
- _optionalChain([this, 'access', _11 => _11.compiler, 'optionalAccess', _12 => _12.close, 'call', _13 => _13(() => {
406
+ _optionalChain([this, 'access', _10 => _10.compiler, 'optionalAccess', _11 => _11.close, 'call', _12 => _12(() => {
505
407
  })]);
506
408
  this.emit("close");
507
409
  }
@@ -547,10 +449,109 @@ var MockCompiler = (_class = class extends _events2.default {
547
449
  }
548
450
  }, _class);
549
451
 
452
+ // src/core/mockMiddleware.ts
453
+ var _cors = require('cors'); var _cors2 = _interopRequireDefault(_cors);
454
+ var _pathtoregexp = require('path-to-regexp');
455
+ function createMockMiddleware(compiler, options) {
456
+ function mockMiddleware(middlewares, reload) {
457
+ middlewares.unshift(_chunkTKSRTJX5cjs.baseMiddleware.call(void 0, compiler, options));
458
+ const corsMiddleware = createCorsMiddleware(compiler, options);
459
+ if (corsMiddleware) {
460
+ middlewares.unshift(corsMiddleware);
461
+ }
462
+ if (options.reload) {
463
+ compiler.on("update", () => _optionalChain([reload, 'optionalCall', _13 => _13()]));
464
+ }
465
+ return middlewares;
466
+ }
467
+ return mockMiddleware;
468
+ }
469
+ function createCorsMiddleware(compiler, options) {
470
+ let corsOptions = {};
471
+ const enabled = options.cors !== false;
472
+ if (enabled) {
473
+ corsOptions = {
474
+ ...corsOptions,
475
+ ...typeof options.cors === "boolean" ? {} : options.cors
476
+ };
477
+ }
478
+ const proxies = options.proxies;
479
+ return !enabled ? void 0 : function(req, res, next) {
480
+ const { pathname } = _chunkTKSRTJX5cjs.urlParse.call(void 0, req.url);
481
+ if (!pathname || proxies.length === 0 || !proxies.some(
482
+ (context) => _chunkTKSRTJX5cjs.doesProxyContextMatchUrl.call(void 0, context, req.url, req)
483
+ )) {
484
+ return next();
485
+ }
486
+ const mockData = compiler.mockData;
487
+ const mockUrl = Object.keys(mockData).find(
488
+ (key) => _pathtoregexp.pathToRegexp.call(void 0, key).test(pathname)
489
+ );
490
+ if (!mockUrl)
491
+ return next();
492
+ _cors2.default.call(void 0, corsOptions)(req, res, next);
493
+ };
494
+ }
495
+
496
+ // src/core/resolvePluginOptions.ts
497
+
498
+
499
+ function resolvePluginOptions({
500
+ prefix = [],
501
+ wsPrefix = [],
502
+ cwd,
503
+ include = ["mock/**/*.mock.{js,ts,cjs,mjs,json,json5}"],
504
+ exclude = ["**/node_modules/**", "**/.vscode/**", "**/.git/**"],
505
+ reload = false,
506
+ log = "info",
507
+ cors: cors2 = true,
508
+ formidableOptions = {},
509
+ build = false,
510
+ cookiesOptions = {},
511
+ bodyParserOptions = {},
512
+ priority = {}
513
+ } = {}, { alias, context, plugins, proxies }) {
514
+ const logger = _chunkTKSRTJX5cjs.createLogger.call(void 0,
515
+ "rspack:mock",
516
+ _utils.isBoolean.call(void 0, log) ? log ? "info" : "error" : log
517
+ );
518
+ prefix = _utils.toArray.call(void 0, prefix);
519
+ return {
520
+ prefix,
521
+ wsPrefix,
522
+ cwd: cwd || context || _process2.default.cwd(),
523
+ include,
524
+ exclude,
525
+ reload,
526
+ cors: cors2,
527
+ cookiesOptions,
528
+ log,
529
+ formidableOptions: {
530
+ multiples: true,
531
+ ...formidableOptions
532
+ },
533
+ bodyParserOptions,
534
+ priority,
535
+ build: build ? Object.assign(
536
+ {
537
+ serverPort: 8080,
538
+ dist: "mockServer",
539
+ log: "error"
540
+ },
541
+ typeof build === "object" ? build : {}
542
+ ) : false,
543
+ alias,
544
+ plugins,
545
+ proxies: [...proxies, ...prefix],
546
+ wsProxies: _utils.toArray.call(void 0, wsPrefix),
547
+ logger
548
+ };
549
+ }
550
+
550
551
 
551
552
 
552
553
 
553
554
 
554
555
 
555
556
 
556
- exports.createMockMiddleware = createMockMiddleware; exports.resolvePluginOptions = resolvePluginOptions; exports.buildMockServer = buildMockServer; exports.createMockCompiler = createMockCompiler; exports.MockCompiler = MockCompiler;
557
+ exports.buildMockServer = buildMockServer; exports.createMockCompiler = createMockCompiler; exports.MockCompiler = MockCompiler; exports.createMockMiddleware = createMockMiddleware; exports.resolvePluginOptions = resolvePluginOptions;
package/dist/helper.d.cts CHANGED
@@ -1,13 +1,13 @@
1
- import { a as MockHttpItem, b as MockWebsocketItem, c as MockOptions } from './types-BgpcN3jm.cjs';
2
- export { B as BodyParserOptions, E as ExtraRequest, F as FormidableFile, i as LogLevel, L as LogType, f as Method, d as MockMatchPriority, e as MockMatchSpecialPriority, g as MockRequest, h as MockResponse, M as MockServerPluginOptions, R as ResponseBody, S as ServerBuildOption, W as WebSocketSetupContext } from './types-BgpcN3jm.cjs';
3
- import 'node:buffer';
4
- import 'node:http';
5
- import 'node:stream';
1
+ import { M as MockHttpItem, a as MockWebsocketItem, b as MockOptions } from './types-Aw0AciTG.cjs';
2
+ export { B as BodyParserOptions, E as ExtraRequest, F as FormidableFile, i as LogLevel, L as LogType, f as Method, d as MockMatchPriority, e as MockMatchSpecialPriority, g as MockRequest, h as MockResponse, c as MockServerPluginOptions, R as ResponseBody, S as ServerBuildOption, W as WebSocketSetupContext } from './types-Aw0AciTG.cjs';
3
+ import 'co-body';
6
4
  import 'cookies';
7
5
  import 'cors';
8
6
  import 'formidable';
7
+ import 'node:buffer';
8
+ import 'node:http';
9
+ import 'node:stream';
9
10
  import 'ws';
10
- import 'co-body';
11
11
 
12
12
  /**
13
13
  * mock config Type helper
package/dist/helper.d.ts CHANGED
@@ -1,13 +1,13 @@
1
- import { a as MockHttpItem, b as MockWebsocketItem, c as MockOptions } from './types-BgpcN3jm.js';
2
- export { B as BodyParserOptions, E as ExtraRequest, F as FormidableFile, i as LogLevel, L as LogType, f as Method, d as MockMatchPriority, e as MockMatchSpecialPriority, g as MockRequest, h as MockResponse, M as MockServerPluginOptions, R as ResponseBody, S as ServerBuildOption, W as WebSocketSetupContext } from './types-BgpcN3jm.js';
3
- import 'node:buffer';
4
- import 'node:http';
5
- import 'node:stream';
1
+ import { M as MockHttpItem, a as MockWebsocketItem, b as MockOptions } from './types-Aw0AciTG.js';
2
+ export { B as BodyParserOptions, E as ExtraRequest, F as FormidableFile, i as LogLevel, L as LogType, f as Method, d as MockMatchPriority, e as MockMatchSpecialPriority, g as MockRequest, h as MockResponse, c as MockServerPluginOptions, R as ResponseBody, S as ServerBuildOption, W as WebSocketSetupContext } from './types-Aw0AciTG.js';
3
+ import 'co-body';
6
4
  import 'cookies';
7
5
  import 'cors';
8
6
  import 'formidable';
7
+ import 'node:buffer';
8
+ import 'node:http';
9
+ import 'node:stream';
9
10
  import 'ws';
10
- import 'co-body';
11
11
 
12
12
  /**
13
13
  * mock config Type helper
package/dist/index.cjs CHANGED
@@ -4,18 +4,18 @@
4
4
 
5
5
 
6
6
 
7
- var _chunkZUCMHLWWcjs = require('./chunk-ZUCMHLWW.cjs');
7
+ var _chunkYAHWW6TXcjs = require('./chunk-YAHWW6TX.cjs');
8
8
 
9
9
 
10
10
 
11
11
 
12
- var _chunkT2VK464Wcjs = require('./chunk-T2VK464W.cjs');
12
+ var _chunkTKSRTJX5cjs = require('./chunk-TKSRTJX5.cjs');
13
13
 
14
14
  // src/rspack.ts
15
- var _process = require('process'); var _process2 = _interopRequireDefault(_process);
16
15
  var _path = require('path'); var _path2 = _interopRequireDefault(_path);
17
- var _core = require('@rspack/core'); var _core2 = _interopRequireDefault(_core);
16
+ var _process = require('process'); var _process2 = _interopRequireDefault(_process);
18
17
  var _utils = require('@pengzhanbo/utils');
18
+ var _core = require('@rspack/core'); var _core2 = _interopRequireDefault(_core);
19
19
  var PLUGIN_NAME = "rspack-plugin-mock";
20
20
  var MockServerPlugin = class {
21
21
  constructor(options = {}) {
@@ -25,11 +25,11 @@ var MockServerPlugin = class {
25
25
  const compilerOptions = compiler.options;
26
26
  const options = resolvePluginOptions2(compiler, this.options);
27
27
  if (_process2.default.env.NODE_ENV !== "production") {
28
- const mockCompiler = _chunkZUCMHLWWcjs.createMockCompiler.call(void 0, options);
29
- const mockMiddleware = _chunkZUCMHLWWcjs.createMockMiddleware.call(void 0, mockCompiler, options);
28
+ const mockCompiler = _chunkYAHWW6TXcjs.createMockCompiler.call(void 0, options);
29
+ const mockMiddleware = _chunkYAHWW6TXcjs.createMockMiddleware.call(void 0, mockCompiler, options);
30
30
  const setupMiddlewares = _optionalChain([compilerOptions, 'access', _ => _.devServer, 'optionalAccess', _2 => _2.setupMiddlewares]);
31
- const waitServer = _chunkT2VK464Wcjs.waitingFor.call(void 0, (server) => {
32
- _chunkT2VK464Wcjs.mockWebSocket.call(void 0, mockCompiler, server, options);
31
+ const waitServerForMockWebSocket = _chunkTKSRTJX5cjs.waitingFor.call(void 0, (server) => {
32
+ _chunkTKSRTJX5cjs.mockWebSocket.call(void 0, mockCompiler, server, options);
33
33
  });
34
34
  compilerOptions.devServer = {
35
35
  ...compilerOptions.devServer,
@@ -40,33 +40,33 @@ var MockServerPlugin = class {
40
40
  devServer.sendMessage(devServer.webSocketServer.clients, "static-changed");
41
41
  };
42
42
  middlewares = mockMiddleware(middlewares, reload) || middlewares;
43
- waitServer(() => devServer.server);
43
+ waitServerForMockWebSocket(() => devServer.server);
44
44
  return middlewares;
45
45
  }
46
46
  };
47
- const proxy = _optionalChain([compilerOptions, 'access', _6 => _6.devServer, 'optionalAccess', _7 => _7.proxy]) || [];
48
47
  const wsPrefix = _utils.toArray.call(void 0, options.wsPrefix);
49
- if (proxy.length) {
50
- compilerOptions.devServer.proxy = proxy.map((item) => {
48
+ if (_optionalChain([compilerOptions, 'access', _6 => _6.devServer, 'optionalAccess', _7 => _7.proxy, 'optionalAccess', _8 => _8.length])) {
49
+ const proxy = compilerOptions.devServer.proxy;
50
+ compilerOptions.devServer.proxy = proxy.filter((item) => {
51
+ if (typeof item !== "function" && item.ws === true && wsPrefix.length) {
52
+ return !_utils.toArray.call(void 0, item.context).filter(_utils.isString).some((context) => wsPrefix.includes(context));
53
+ }
54
+ return true;
55
+ }).map((item) => {
51
56
  if (typeof item !== "function" && !item.ws) {
52
57
  const onProxyReq = item.onProxyReq;
53
58
  item.onProxyReq = (proxyReq, req, ...args) => {
54
- _optionalChain([onProxyReq, 'optionalCall', _8 => _8(proxyReq, req, ...args)]);
55
- _chunkT2VK464Wcjs.rewriteRequest.call(void 0, proxyReq, req);
59
+ _optionalChain([onProxyReq, 'optionalCall', _9 => _9(proxyReq, req, ...args)]);
60
+ _chunkTKSRTJX5cjs.rewriteRequest.call(void 0, proxyReq, req);
56
61
  };
57
62
  }
58
63
  return item;
59
- }).filter((item) => {
60
- if (typeof item !== "function" && item.ws === true && wsPrefix.length) {
61
- return !_utils.toArray.call(void 0, item.context).filter(_utils.isString).some((context) => wsPrefix.includes(context));
62
- }
63
- return true;
64
64
  });
65
65
  }
66
66
  compiler.hooks.watchRun.tap(PLUGIN_NAME, () => mockCompiler.run());
67
67
  compiler.hooks.watchClose.tap(PLUGIN_NAME, () => mockCompiler.close());
68
68
  } else if (options.build !== false) {
69
- compiler.hooks.afterEmit.tap(PLUGIN_NAME, () => _chunkZUCMHLWWcjs.buildMockServer.call(void 0,
69
+ compiler.hooks.afterEmit.tap(PLUGIN_NAME, () => _chunkYAHWW6TXcjs.buildMockServer.call(void 0,
70
70
  options,
71
71
  compilerOptions.output.path || _path2.default.resolve(_process2.default.cwd(), "dist")
72
72
  ));
@@ -75,18 +75,18 @@ var MockServerPlugin = class {
75
75
  };
76
76
  function resolvePluginOptions2(compiler, options) {
77
77
  const compilerOptions = compiler.options;
78
- const alias = _optionalChain([compilerOptions, 'access', _9 => _9.resolve, 'optionalAccess', _10 => _10.alias]) || {};
78
+ const alias = _optionalChain([compilerOptions, 'access', _10 => _10.resolve, 'optionalAccess', _11 => _11.alias]) || {};
79
79
  const context = compilerOptions.context;
80
- const definePluginInstance = _optionalChain([compilerOptions, 'access', _11 => _11.plugins, 'optionalAccess', _12 => _12.find, 'call', _13 => _13(
80
+ const definePluginInstance = _optionalChain([compilerOptions, 'access', _12 => _12.plugins, 'optionalAccess', _13 => _13.find, 'call', _14 => _14(
81
81
  (plugin) => plugin instanceof _core2.default.DefinePlugin
82
82
  )]);
83
- const proxies = (_optionalChain([compilerOptions, 'access', _14 => _14.devServer, 'optionalAccess', _15 => _15.proxy]) || []).flatMap((item) => {
83
+ const proxies = (_optionalChain([compilerOptions, 'access', _15 => _15.devServer, 'optionalAccess', _16 => _16.proxy]) || []).flatMap((item) => {
84
84
  if (typeof item !== "function" && item.context && !item.ws) {
85
85
  return item.context;
86
86
  }
87
87
  return [];
88
88
  });
89
- return _chunkZUCMHLWWcjs.resolvePluginOptions.call(void 0, options, {
89
+ return _chunkYAHWW6TXcjs.resolvePluginOptions.call(void 0, options, {
90
90
  alias,
91
91
  context,
92
92
  plugins: _utils.toArray.call(void 0, definePluginInstance),
@@ -100,4 +100,4 @@ function resolvePluginOptions2(compiler, options) {
100
100
 
101
101
 
102
102
 
103
- exports.MockCompiler = _chunkZUCMHLWWcjs.MockCompiler; exports.MockServerPlugin = MockServerPlugin; exports.createMockCompiler = _chunkZUCMHLWWcjs.createMockCompiler; exports.createMockMiddleware = _chunkZUCMHLWWcjs.createMockMiddleware; exports.mockWebSocket = _chunkT2VK464Wcjs.mockWebSocket; exports.resolvePluginOptions = resolvePluginOptions2;
103
+ exports.MockCompiler = _chunkYAHWW6TXcjs.MockCompiler; exports.MockServerPlugin = MockServerPlugin; exports.createMockCompiler = _chunkYAHWW6TXcjs.createMockCompiler; exports.createMockMiddleware = _chunkYAHWW6TXcjs.createMockMiddleware; exports.mockWebSocket = _chunkTKSRTJX5cjs.mockWebSocket; exports.resolvePluginOptions = resolvePluginOptions2;
package/dist/index.d.cts CHANGED
@@ -1,18 +1,18 @@
1
+ import { R as ResolvePluginOptions } from './mockWebsocket-Ki_cShTv.cjs';
2
+ export { e as Middleware, b as MiddlewareOptions, a as MockCompiler, M as MockCompilerOptions, f as MockSocketOptions, S as Server, c as createMockCompiler, d as createMockMiddleware, m as mockWebSocket } from './mockWebsocket-Ki_cShTv.cjs';
1
3
  import { RspackPluginInstance, Compiler } from '@rspack/core';
2
- import { M as MockServerPluginOptions } from './types-BgpcN3jm.cjs';
3
- import { R as ResolvePluginOptions } from './mockWebsocket-Dc9CZBfv.cjs';
4
- export { a as Middleware, M as MiddlewareOptions, f as MockCompiler, d as MockCompilerOptions, b as MockSocketOptions, S as Server, e as createMockCompiler, c as createMockMiddleware, m as mockWebSocket } from './mockWebsocket-Dc9CZBfv.cjs';
5
- import 'node:buffer';
4
+ import { c as MockServerPluginOptions } from './types-Aw0AciTG.cjs';
6
5
  import 'node:http';
7
- import 'node:stream';
6
+ import 'node:http2';
7
+ import 'node:fs';
8
+ import 'node:events';
9
+ import 'co-body';
8
10
  import 'cookies';
9
11
  import 'cors';
10
12
  import 'formidable';
13
+ import 'node:buffer';
14
+ import 'node:stream';
11
15
  import 'ws';
12
- import 'co-body';
13
- import 'node:http2';
14
- import 'node:events';
15
- import 'node:fs';
16
16
 
17
17
  declare class MockServerPlugin implements RspackPluginInstance {
18
18
  options: MockServerPluginOptions;
package/dist/index.d.ts CHANGED
@@ -1,18 +1,18 @@
1
+ import { R as ResolvePluginOptions } from './mockWebsocket-DBgZBsdo.js';
2
+ export { e as Middleware, b as MiddlewareOptions, a as MockCompiler, M as MockCompilerOptions, f as MockSocketOptions, S as Server, c as createMockCompiler, d as createMockMiddleware, m as mockWebSocket } from './mockWebsocket-DBgZBsdo.js';
1
3
  import { RspackPluginInstance, Compiler } from '@rspack/core';
2
- import { M as MockServerPluginOptions } from './types-BgpcN3jm.js';
3
- import { R as ResolvePluginOptions } from './mockWebsocket-CPuTAvL0.js';
4
- export { a as Middleware, M as MiddlewareOptions, f as MockCompiler, d as MockCompilerOptions, b as MockSocketOptions, S as Server, e as createMockCompiler, c as createMockMiddleware, m as mockWebSocket } from './mockWebsocket-CPuTAvL0.js';
5
- import 'node:buffer';
4
+ import { c as MockServerPluginOptions } from './types-Aw0AciTG.js';
6
5
  import 'node:http';
7
- import 'node:stream';
6
+ import 'node:http2';
7
+ import 'node:fs';
8
+ import 'node:events';
9
+ import 'co-body';
8
10
  import 'cookies';
9
11
  import 'cors';
10
12
  import 'formidable';
13
+ import 'node:buffer';
14
+ import 'node:stream';
11
15
  import 'ws';
12
- import 'co-body';
13
- import 'node:http2';
14
- import 'node:events';
15
- import 'node:fs';
16
16
 
17
17
  declare class MockServerPlugin implements RspackPluginInstance {
18
18
  options: MockServerPluginOptions;
package/dist/index.js CHANGED
@@ -4,18 +4,18 @@ import {
4
4
  createMockCompiler,
5
5
  createMockMiddleware,
6
6
  resolvePluginOptions
7
- } from "./chunk-VKD4VKFQ.js";
7
+ } from "./chunk-4XOUX6CL.js";
8
8
  import {
9
9
  mockWebSocket,
10
10
  rewriteRequest,
11
11
  waitingFor
12
- } from "./chunk-2SSCBSV5.js";
12
+ } from "./chunk-JMXFIX5Q.js";
13
13
 
14
14
  // src/rspack.ts
15
- import process from "process";
16
15
  import path from "path";
17
- import rspack from "@rspack/core";
16
+ import process from "process";
18
17
  import { isString, toArray } from "@pengzhanbo/utils";
18
+ import rspack from "@rspack/core";
19
19
  var PLUGIN_NAME = "rspack-plugin-mock";
20
20
  var MockServerPlugin = class {
21
21
  constructor(options = {}) {
@@ -28,7 +28,7 @@ var MockServerPlugin = class {
28
28
  const mockCompiler = createMockCompiler(options);
29
29
  const mockMiddleware = createMockMiddleware(mockCompiler, options);
30
30
  const setupMiddlewares = compilerOptions.devServer?.setupMiddlewares;
31
- const waitServer = waitingFor((server) => {
31
+ const waitServerForMockWebSocket = waitingFor((server) => {
32
32
  mockWebSocket(mockCompiler, server, options);
33
33
  });
34
34
  compilerOptions.devServer = {
@@ -40,14 +40,19 @@ var MockServerPlugin = class {
40
40
  devServer.sendMessage(devServer.webSocketServer.clients, "static-changed");
41
41
  };
42
42
  middlewares = mockMiddleware(middlewares, reload) || middlewares;
43
- waitServer(() => devServer.server);
43
+ waitServerForMockWebSocket(() => devServer.server);
44
44
  return middlewares;
45
45
  }
46
46
  };
47
- const proxy = compilerOptions.devServer?.proxy || [];
48
47
  const wsPrefix = toArray(options.wsPrefix);
49
- if (proxy.length) {
50
- compilerOptions.devServer.proxy = proxy.map((item) => {
48
+ if (compilerOptions.devServer?.proxy?.length) {
49
+ const proxy = compilerOptions.devServer.proxy;
50
+ compilerOptions.devServer.proxy = proxy.filter((item) => {
51
+ if (typeof item !== "function" && item.ws === true && wsPrefix.length) {
52
+ return !toArray(item.context).filter(isString).some((context) => wsPrefix.includes(context));
53
+ }
54
+ return true;
55
+ }).map((item) => {
51
56
  if (typeof item !== "function" && !item.ws) {
52
57
  const onProxyReq = item.onProxyReq;
53
58
  item.onProxyReq = (proxyReq, req, ...args) => {
@@ -56,11 +61,6 @@ var MockServerPlugin = class {
56
61
  };
57
62
  }
58
63
  return item;
59
- }).filter((item) => {
60
- if (typeof item !== "function" && item.ws === true && wsPrefix.length) {
61
- return !toArray(item.context).filter(isString).some((context) => wsPrefix.includes(context));
62
- }
63
- return true;
64
64
  });
65
65
  }
66
66
  compiler.hooks.watchRun.tap(PLUGIN_NAME, () => mockCompiler.run());