rspack-plugin-mock 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,90 @@
1
+ import {
2
+ createMockCompiler,
3
+ createMockMiddleware,
4
+ mockWebSocket,
5
+ resolvePluginOptions,
6
+ rewriteRequest,
7
+ waitingFor
8
+ } from "./chunk-UJAKORAH.js";
9
+
10
+ // src/rspack.ts
11
+ import process from "process";
12
+ import rspack from "@rspack/core";
13
+ import { isString, toArray } from "@pengzhanbo/utils";
14
+ var PLUGIN_NAME = "rspack-plugin-mock";
15
+ var MockServerPlugin = class {
16
+ constructor(options = {}) {
17
+ this.options = options;
18
+ }
19
+ apply(compiler) {
20
+ const compilerOptions = compiler.options;
21
+ if (process.env.NODE_ENV !== "production") {
22
+ const options = resolvePluginOptions2(compiler, this.options);
23
+ const mockCompiler = createMockCompiler(options);
24
+ const mockMiddleware = createMockMiddleware(mockCompiler, options);
25
+ const setupMiddlewares = compilerOptions.devServer?.setupMiddlewares;
26
+ const waitServer = waitingFor((server) => {
27
+ mockWebSocket(mockCompiler, server, options);
28
+ });
29
+ compilerOptions.devServer = {
30
+ ...compilerOptions.devServer,
31
+ setupMiddlewares: (middlewares, devServer) => {
32
+ middlewares = setupMiddlewares?.(middlewares, devServer) || middlewares;
33
+ const reload = () => {
34
+ if (devServer.webSocketServer?.clients)
35
+ devServer.sendMessage(devServer.webSocketServer.clients, "static-changed");
36
+ };
37
+ middlewares = mockMiddleware(middlewares, reload) || middlewares;
38
+ waitServer(() => devServer.server);
39
+ return middlewares;
40
+ }
41
+ };
42
+ const proxy = compilerOptions.devServer?.proxy || [];
43
+ const wsPrefix = toArray(options.wsPrefix);
44
+ if (proxy.length) {
45
+ compilerOptions.devServer.proxy = proxy.map((item) => {
46
+ if (typeof item !== "function" && !item.ws) {
47
+ const onProxyReq = item.onProxyReq;
48
+ item.onProxyReq = (proxyReq, req, ...args) => {
49
+ onProxyReq?.(proxyReq, req, ...args);
50
+ rewriteRequest(proxyReq, req);
51
+ };
52
+ }
53
+ return item;
54
+ }).filter((item) => {
55
+ if (typeof item !== "function" && item.ws === true && wsPrefix.length) {
56
+ return !toArray(item.context).filter(isString).some((context) => wsPrefix.includes(context));
57
+ }
58
+ return true;
59
+ });
60
+ }
61
+ compiler.hooks.watchRun.tap(PLUGIN_NAME, () => mockCompiler.run());
62
+ compiler.hooks.watchClose.tap(PLUGIN_NAME, () => mockCompiler.close());
63
+ }
64
+ }
65
+ };
66
+ function resolvePluginOptions2(compiler, options) {
67
+ const compilerOptions = compiler.options;
68
+ const alias = compilerOptions.resolve?.alias || {};
69
+ const context = compilerOptions.context;
70
+ const definePluginInstance = compilerOptions.plugins?.find(
71
+ (plugin) => plugin instanceof rspack.DefinePlugin
72
+ );
73
+ const proxies = (compilerOptions.devServer?.proxy || []).flatMap((item) => {
74
+ if (typeof item !== "function" && item.context && !item.ws) {
75
+ return item.context;
76
+ }
77
+ return [];
78
+ });
79
+ return resolvePluginOptions(options, {
80
+ alias,
81
+ context,
82
+ plugins: toArray(definePluginInstance),
83
+ proxies
84
+ });
85
+ }
86
+
87
+ export {
88
+ MockServerPlugin,
89
+ resolvePluginOptions2 as resolvePluginOptions
90
+ };
@@ -1,7 +1,11 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
2
2
 
3
3
 
4
- var _chunkG53QRHGVcjs = require('./chunk-G53QRHGV.cjs');
4
+
5
+
6
+
7
+
8
+ var _chunk2S4KCTKWcjs = require('./chunk-2S4KCTKW.cjs');
5
9
 
6
10
  // src/rspack.ts
7
11
  var _process = require('process'); var _process2 = _interopRequireDefault(_process);
@@ -15,54 +19,72 @@ var MockServerPlugin = class {
15
19
  apply(compiler) {
16
20
  const compilerOptions = compiler.options;
17
21
  if (_process2.default.env.NODE_ENV !== "production") {
18
- const { mockMiddleware, run, close } = _chunkG53QRHGVcjs.createManuallyMockMiddleware.call(void 0,
19
- resolveMiddleOptions(compiler),
20
- this.options
21
- );
22
+ const options = resolvePluginOptions2(compiler, this.options);
23
+ const mockCompiler = _chunk2S4KCTKWcjs.createMockCompiler.call(void 0, options);
24
+ const mockMiddleware = _chunk2S4KCTKWcjs.createMockMiddleware.call(void 0, mockCompiler, options);
22
25
  const setupMiddlewares = _optionalChain([compilerOptions, 'access', _ => _.devServer, 'optionalAccess', _2 => _2.setupMiddlewares]);
26
+ const waitServer = _chunk2S4KCTKWcjs.waitingFor.call(void 0, (server) => {
27
+ _chunk2S4KCTKWcjs.mockWebSocket.call(void 0, mockCompiler, server, options);
28
+ });
23
29
  compilerOptions.devServer = {
24
30
  ...compilerOptions.devServer,
25
31
  setupMiddlewares: (middlewares, devServer) => {
26
32
  middlewares = _optionalChain([setupMiddlewares, 'optionalCall', _3 => _3(middlewares, devServer)]) || middlewares;
27
- middlewares = mockMiddleware(middlewares) || middlewares;
33
+ const reload = () => {
34
+ if (_optionalChain([devServer, 'access', _4 => _4.webSocketServer, 'optionalAccess', _5 => _5.clients]))
35
+ devServer.sendMessage(devServer.webSocketServer.clients, "static-changed");
36
+ };
37
+ middlewares = mockMiddleware(middlewares, reload) || middlewares;
38
+ waitServer(() => devServer.server);
28
39
  return middlewares;
29
40
  }
30
41
  };
31
- const proxy = _optionalChain([compilerOptions, 'access', _4 => _4.devServer, 'optionalAccess', _5 => _5.proxy]) || [];
42
+ const proxy = _optionalChain([compilerOptions, 'access', _6 => _6.devServer, 'optionalAccess', _7 => _7.proxy]) || [];
43
+ const wsPrefix = _utils.toArray.call(void 0, options.wsPrefix);
32
44
  if (proxy.length) {
33
45
  compilerOptions.devServer.proxy = proxy.map((item) => {
34
46
  if (typeof item !== "function" && !item.ws) {
35
47
  const onProxyReq = item.onProxyReq;
36
48
  item.onProxyReq = (proxyReq, req, ...args) => {
37
- _optionalChain([onProxyReq, 'optionalCall', _6 => _6(proxyReq, req, ...args)]);
38
- _chunkG53QRHGVcjs.rewriteRequest.call(void 0, proxyReq, req);
49
+ _optionalChain([onProxyReq, 'optionalCall', _8 => _8(proxyReq, req, ...args)]);
50
+ _chunk2S4KCTKWcjs.rewriteRequest.call(void 0, proxyReq, req);
39
51
  };
40
52
  }
41
53
  return item;
54
+ }).filter((item) => {
55
+ if (typeof item !== "function" && item.ws === true && wsPrefix.length) {
56
+ return !_utils.toArray.call(void 0, item.context).filter(_utils.isString).some((context) => wsPrefix.includes(context));
57
+ }
58
+ return true;
42
59
  });
43
60
  }
44
- compiler.hooks.watchRun.tap(PLUGIN_NAME, () => run());
45
- compiler.hooks.watchClose.tap(PLUGIN_NAME, () => close());
61
+ compiler.hooks.watchRun.tap(PLUGIN_NAME, () => mockCompiler.run());
62
+ compiler.hooks.watchClose.tap(PLUGIN_NAME, () => mockCompiler.close());
46
63
  }
47
64
  }
48
65
  };
49
- function resolveMiddleOptions(compiler) {
66
+ function resolvePluginOptions2(compiler, options) {
50
67
  const compilerOptions = compiler.options;
51
- const alias = _optionalChain([compilerOptions, 'access', _7 => _7.resolve, 'optionalAccess', _8 => _8.alias]) || {};
68
+ const alias = _optionalChain([compilerOptions, 'access', _9 => _9.resolve, 'optionalAccess', _10 => _10.alias]) || {};
52
69
  const context = compilerOptions.context;
53
- const definePluginInstance = _optionalChain([compilerOptions, 'access', _9 => _9.plugins, 'optionalAccess', _10 => _10.find, 'call', _11 => _11(
70
+ const definePluginInstance = _optionalChain([compilerOptions, 'access', _11 => _11.plugins, 'optionalAccess', _12 => _12.find, 'call', _13 => _13(
54
71
  (plugin) => plugin instanceof _core2.default.DefinePlugin
55
72
  )]);
56
- const proxies = (_optionalChain([compilerOptions, 'access', _12 => _12.devServer, 'optionalAccess', _13 => _13.proxy]) || []).map((item) => {
57
- if (typeof item !== "function" && !item.ws && item.context) {
73
+ const proxies = (_optionalChain([compilerOptions, 'access', _14 => _14.devServer, 'optionalAccess', _15 => _15.proxy]) || []).flatMap((item) => {
74
+ if (typeof item !== "function" && item.context && !item.ws) {
58
75
  return item.context;
59
76
  }
60
77
  return [];
61
- }).flat();
62
- return { alias, context, plugins: _utils.toArray.call(void 0, definePluginInstance), proxies };
78
+ });
79
+ return _chunk2S4KCTKWcjs.resolvePluginOptions.call(void 0, options, {
80
+ alias,
81
+ context,
82
+ plugins: _utils.toArray.call(void 0, definePluginInstance),
83
+ proxies
84
+ });
63
85
  }
64
86
 
65
87
 
66
88
 
67
89
 
68
- exports.MockServerPlugin = MockServerPlugin; exports.resolveMiddleOptions = resolveMiddleOptions;
90
+ exports.MockServerPlugin = MockServerPlugin; exports.resolvePluginOptions = resolvePluginOptions2;