rspack-plugin-mock 0.3.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.
@@ -0,0 +1,76 @@
1
+ import { RspackPluginInstance, Compiler, RspackOptionsNormalized } from '@rspack/core';
2
+ import { i as LogLevel, M as MockServerPluginOptions, S as ServerBuildOption, c as MockOptions } from './types-BgpcN3jm.cjs';
3
+ import { Server as Server$1 } from 'node:http';
4
+ import { Http2SecureServer } from 'node:http2';
5
+ import EventEmitter from 'node:events';
6
+ import { FSWatcher } from 'node:fs';
7
+
8
+ interface Logger {
9
+ debug: (msg: string, level?: boolean | LogLevel) => void;
10
+ info: (msg: string, level?: boolean | LogLevel) => void;
11
+ warn: (msg: string, level?: boolean | LogLevel) => void;
12
+ error: (msg: string, level?: boolean | LogLevel) => void;
13
+ }
14
+ declare const logLevels: Record<LogLevel, number>;
15
+ declare function createLogger(prefix: string, defaultLevel?: LogLevel): Logger;
16
+
17
+ interface ResolvedCompilerOptions {
18
+ alias: Record<string, false | string | (string | false)[]>;
19
+ proxies: (string | ((pathname: string, req: any) => boolean))[];
20
+ wsProxies: (string | ((pathname: string, req: any) => boolean))[];
21
+ plugins: RspackPluginInstance[];
22
+ context?: string;
23
+ }
24
+ type ResolvePluginOptions = Required<Omit<MockServerPluginOptions, 'build'>> & ResolvedCompilerOptions & {
25
+ logger: Logger;
26
+ build: false | ServerBuildOption;
27
+ };
28
+
29
+ interface MockCompilerOptions {
30
+ alias?: Record<string, false | string | (string | false)[]>;
31
+ plugins: RspackPluginInstance[];
32
+ cwd?: string;
33
+ include: string | string[];
34
+ exclude: string | string[];
35
+ logger: Logger;
36
+ }
37
+ declare function createMockCompiler(options: MockCompilerOptions): MockCompiler;
38
+ declare class MockCompiler extends EventEmitter {
39
+ options: MockCompilerOptions;
40
+ cwd: string;
41
+ mockWatcher: FSWatcher;
42
+ moduleType: 'cjs' | 'esm';
43
+ entryFile: string;
44
+ private _mockData;
45
+ private fileFilter;
46
+ private watchInfo?;
47
+ compiler?: Compiler | null;
48
+ constructor(options: MockCompilerOptions);
49
+ get mockData(): Record<string, MockOptions>;
50
+ run(): Promise<void>;
51
+ close(): void;
52
+ updateAlias(alias: Record<string, false | string | (string | false)[]>): void;
53
+ updateMockEntry(): Promise<void>;
54
+ getMockFiles(): Promise<string[]>;
55
+ watchMockFiles(): void;
56
+ }
57
+
58
+ interface MiddlewareOptions {
59
+ alias: Record<string, false | string | (string | false)[]>;
60
+ proxies: (string | ((pathname: string, req: any) => boolean))[];
61
+ context?: string;
62
+ plugins: RspackPluginInstance[];
63
+ }
64
+ declare function createMockMiddleware(compiler: MockCompiler, options: ResolvePluginOptions): (middlewares: Middleware[], reload?: () => void) => Middleware[];
65
+ type SetupMiddlewaresFn = NonNullable<NonNullable<RspackOptionsNormalized['devServer']>['setupMiddlewares']>;
66
+ type Middleware = SetupMiddlewaresFn extends (middlewares: (infer T)[], devServer: any) => void ? T : never;
67
+ type Server = SetupMiddlewaresFn extends (middlewares: any, devServer: infer T) => void ? T : never;
68
+
69
+ interface MockSocketOptions {
70
+ wsProxies: (string | ((pathname: string, req: any) => boolean))[];
71
+ cookiesOptions: MockServerPluginOptions['cookiesOptions'];
72
+ logger: Logger;
73
+ }
74
+ declare function mockWebSocket(compiler: MockCompiler, httpServer: Server$1 | Http2SecureServer, { wsProxies: proxies, cookiesOptions, logger, }: MockSocketOptions): void;
75
+
76
+ export { type Logger as L, type MiddlewareOptions as M, type ResolvePluginOptions as R, type Server as S, type Middleware as a, type MockSocketOptions as b, createMockMiddleware as c, type MockCompilerOptions as d, createMockCompiler as e, MockCompiler as f, createLogger as g, logLevels as l, mockWebSocket as m };
package/dist/rsbuild.cjs CHANGED
@@ -3,9 +3,11 @@
3
3
 
4
4
 
5
5
 
6
+ var _chunk4GMRSIINcjs = require('./chunk-4GMRSIIN.cjs');
6
7
 
7
8
 
8
- var _chunkI54ZNZWLcjs = require('./chunk-I54ZNZWL.cjs');
9
+
10
+ var _chunkZEC4FWWYcjs = require('./chunk-ZEC4FWWY.cjs');
9
11
 
10
12
  // src/rsbuild.ts
11
13
  var _process = require('process'); var _process2 = _interopRequireDefault(_process);
@@ -20,7 +22,7 @@ function pluginMockServer(options = {}) {
20
22
  name: "plugin-mock-server",
21
23
  setup(api) {
22
24
  const rsbuildConfig = api.getRsbuildConfig();
23
- const resolvedOptions = _chunkI54ZNZWLcjs.resolvePluginOptions.call(void 0, options, {
25
+ const resolvedOptions = _chunk4GMRSIINcjs.resolvePluginOptions.call(void 0, options, {
24
26
  proxies: resolveConfigProxies(rsbuildConfig),
25
27
  alias: {},
26
28
  context: api.context.rootPath,
@@ -30,7 +32,7 @@ function pluginMockServer(options = {}) {
30
32
  if (resolvedOptions.build) {
31
33
  api.onAfterBuild(async () => {
32
34
  const config = api.getNormalizedConfig();
33
- await _chunkI54ZNZWLcjs.buildMockServer.call(void 0,
35
+ await _chunk4GMRSIINcjs.buildMockServer.call(void 0,
34
36
  resolvedOptions,
35
37
  _path2.default.resolve(_process2.default.cwd(), config.output.distPath.root || "dist")
36
38
  );
@@ -38,10 +40,10 @@ function pluginMockServer(options = {}) {
38
40
  }
39
41
  return;
40
42
  }
41
- const mockCompiler = _chunkI54ZNZWLcjs.createMockCompiler.call(void 0, resolvedOptions);
43
+ const mockCompiler = _chunk4GMRSIINcjs.createMockCompiler.call(void 0, resolvedOptions);
42
44
  api.modifyRsbuildConfig((config) => {
43
45
  updateServerProxyConfigByHttpMock(config);
44
- const mockMiddleware = _chunkI54ZNZWLcjs.createMockMiddleware.call(void 0, mockCompiler, resolvedOptions);
46
+ const mockMiddleware = _chunk4GMRSIINcjs.createMockMiddleware.call(void 0, mockCompiler, resolvedOptions);
45
47
  config.dev ??= {};
46
48
  config.dev.setupMiddlewares ??= [];
47
49
  config.dev.setupMiddlewares.push((middlewares, server2) => {
@@ -62,7 +64,7 @@ function pluginMockServer(options = {}) {
62
64
  mockCompiler.run();
63
65
  if (shouldMockWs) {
64
66
  server = _http.createServer.call(void 0, );
65
- _chunkI54ZNZWLcjs.mockWebSocket.call(void 0, mockCompiler, server, resolvedOptions);
67
+ _chunkZEC4FWWYcjs.mockWebSocket.call(void 0, mockCompiler, server, resolvedOptions);
66
68
  server.listen(port);
67
69
  }
68
70
  }
@@ -103,7 +105,7 @@ function updateServerProxyConfigByHttpMock(config) {
103
105
  onError: onError || onProxyError,
104
106
  onProxyReq: (proxyReq, req, ...args) => {
105
107
  _optionalChain([onProxyReq, 'optionalCall', _16 => _16(proxyReq, req, ...args)]);
106
- _chunkI54ZNZWLcjs.rewriteRequest.call(void 0, proxyReq, req);
108
+ _chunkZEC4FWWYcjs.rewriteRequest.call(void 0, proxyReq, req);
107
109
  }
108
110
  };
109
111
  }
@@ -113,7 +115,7 @@ function updateServerProxyConfigByHttpMock(config) {
113
115
  const onProxyReq = config.server.proxy.onProxyReq;
114
116
  config.server.proxy.onProxyReq = (proxyReq, req, ...args) => {
115
117
  _optionalChain([onProxyReq, 'optionalCall', _17 => _17(proxyReq, req, ...args)]);
116
- _chunkI54ZNZWLcjs.rewriteRequest.call(void 0, proxyReq, req);
118
+ _chunkZEC4FWWYcjs.rewriteRequest.call(void 0, proxyReq, req);
117
119
  };
118
120
  config.server.proxy.onError ??= onProxyError;
119
121
  } else if (config.server.proxy) {
@@ -128,7 +130,7 @@ function updateServerProxyConfigByHttpMock(config) {
128
130
  ...rest,
129
131
  onProxyReq: (proxyReq, req, ...args) => {
130
132
  _optionalChain([onProxyReq, 'optionalCall', _18 => _18(proxyReq, req, ...args)]);
131
- _chunkI54ZNZWLcjs.rewriteRequest.call(void 0, proxyReq, req);
133
+ _chunkZEC4FWWYcjs.rewriteRequest.call(void 0, proxyReq, req);
132
134
  },
133
135
  onError: onError || onProxyError
134
136
  };
@@ -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
@@ -2,10 +2,12 @@ import {
2
2
  buildMockServer,
3
3
  createMockCompiler,
4
4
  createMockMiddleware,
5
+ resolvePluginOptions
6
+ } from "./chunk-B5W3PWCX.js";
7
+ import {
5
8
  mockWebSocket,
6
- resolvePluginOptions,
7
9
  rewriteRequest
8
- } from "./chunk-YSJVV4SH.js";
10
+ } from "./chunk-EY46RSAC.js";
9
11
 
10
12
  // src/rsbuild.ts
11
13
  import process from "process";
@@ -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,7 +1,7 @@
1
1
  {
2
2
  "name": "rspack-plugin-mock",
3
3
  "type": "module",
4
- "version": "0.3.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",
@@ -24,14 +24,14 @@
24
24
  "default": "./dist/index.cjs"
25
25
  }
26
26
  },
27
- "./rspack": {
27
+ "./server": {
28
28
  "import": {
29
- "types": "./dist/rspack.d.ts",
30
- "default": "./dist/rspack.js"
29
+ "types": "./dist/server.d.ts",
30
+ "default": "./dist/server.js"
31
31
  },
32
32
  "require": {
33
- "types": "./dist/rspack.d.cts",
34
- "default": "./dist/rspack.cjs"
33
+ "types": "./dist/server.d.cts",
34
+ "default": "./dist/server.cjs"
35
35
  }
36
36
  },
37
37
  "./rsbuild": {
@@ -1,20 +0,0 @@
1
- // src/core/defineMock.ts
2
- import { isArray } from "@pengzhanbo/utils";
3
- function defineMock(config) {
4
- return config;
5
- }
6
- function createDefineMock(transformer) {
7
- const define = (config) => {
8
- if (isArray(config))
9
- config = config.map((item) => transformer(item) || item);
10
- else
11
- config = transformer(config) || config;
12
- return config;
13
- };
14
- return define;
15
- }
16
-
17
- export {
18
- defineMock,
19
- createDefineMock
20
- };
@@ -1,20 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});// src/core/defineMock.ts
2
- var _utils = require('@pengzhanbo/utils');
3
- function defineMock(config) {
4
- return config;
5
- }
6
- function createDefineMock(transformer) {
7
- const define = (config) => {
8
- if (_utils.isArray.call(void 0, config))
9
- config = config.map((item) => transformer(item) || item);
10
- else
11
- config = transformer(config) || config;
12
- return config;
13
- };
14
- return define;
15
- }
16
-
17
-
18
-
19
-
20
- exports.defineMock = defineMock; exports.createDefineMock = createDefineMock;
@@ -1,97 +0,0 @@
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
-
3
-
4
-
5
-
6
-
7
-
8
-
9
- var _chunkI54ZNZWLcjs = require('./chunk-I54ZNZWL.cjs');
10
-
11
- // src/rspack.ts
12
- var _process = require('process'); var _process2 = _interopRequireDefault(_process);
13
- var _path = require('path'); var _path2 = _interopRequireDefault(_path);
14
- var _core = require('@rspack/core'); var _core2 = _interopRequireDefault(_core);
15
- var _utils = require('@pengzhanbo/utils');
16
- var PLUGIN_NAME = "rspack-plugin-mock";
17
- var MockServerPlugin = class {
18
- constructor(options = {}) {
19
- this.options = options;
20
- }
21
- apply(compiler) {
22
- const compilerOptions = compiler.options;
23
- const options = resolvePluginOptions2(compiler, this.options);
24
- if (_process2.default.env.NODE_ENV !== "production") {
25
- const mockCompiler = _chunkI54ZNZWLcjs.createMockCompiler.call(void 0, options);
26
- const mockMiddleware = _chunkI54ZNZWLcjs.createMockMiddleware.call(void 0, mockCompiler, options);
27
- const setupMiddlewares = _optionalChain([compilerOptions, 'access', _ => _.devServer, 'optionalAccess', _2 => _2.setupMiddlewares]);
28
- const waitServer = _chunkI54ZNZWLcjs.waitingFor.call(void 0, (server) => {
29
- _chunkI54ZNZWLcjs.mockWebSocket.call(void 0, mockCompiler, server, options);
30
- });
31
- compilerOptions.devServer = {
32
- ...compilerOptions.devServer,
33
- setupMiddlewares: (middlewares, devServer) => {
34
- middlewares = _optionalChain([setupMiddlewares, 'optionalCall', _3 => _3(middlewares, devServer)]) || middlewares;
35
- const reload = () => {
36
- if (_optionalChain([devServer, 'access', _4 => _4.webSocketServer, 'optionalAccess', _5 => _5.clients]))
37
- devServer.sendMessage(devServer.webSocketServer.clients, "static-changed");
38
- };
39
- middlewares = mockMiddleware(middlewares, reload) || middlewares;
40
- waitServer(() => devServer.server);
41
- return middlewares;
42
- }
43
- };
44
- const proxy = _optionalChain([compilerOptions, 'access', _6 => _6.devServer, 'optionalAccess', _7 => _7.proxy]) || [];
45
- const wsPrefix = _utils.toArray.call(void 0, options.wsPrefix);
46
- if (proxy.length) {
47
- compilerOptions.devServer.proxy = proxy.map((item) => {
48
- if (typeof item !== "function" && !item.ws) {
49
- const onProxyReq = item.onProxyReq;
50
- item.onProxyReq = (proxyReq, req, ...args) => {
51
- _optionalChain([onProxyReq, 'optionalCall', _8 => _8(proxyReq, req, ...args)]);
52
- _chunkI54ZNZWLcjs.rewriteRequest.call(void 0, proxyReq, req);
53
- };
54
- }
55
- return item;
56
- }).filter((item) => {
57
- if (typeof item !== "function" && item.ws === true && wsPrefix.length) {
58
- return !_utils.toArray.call(void 0, item.context).filter(_utils.isString).some((context) => wsPrefix.includes(context));
59
- }
60
- return true;
61
- });
62
- }
63
- compiler.hooks.watchRun.tap(PLUGIN_NAME, () => mockCompiler.run());
64
- compiler.hooks.watchClose.tap(PLUGIN_NAME, () => mockCompiler.close());
65
- } else if (options.build !== false) {
66
- compiler.hooks.afterEmit.tap(PLUGIN_NAME, () => _chunkI54ZNZWLcjs.buildMockServer.call(void 0,
67
- options,
68
- compilerOptions.output.path || _path2.default.resolve(_process2.default.cwd(), "dist")
69
- ));
70
- }
71
- }
72
- };
73
- function resolvePluginOptions2(compiler, options) {
74
- const compilerOptions = compiler.options;
75
- const alias = _optionalChain([compilerOptions, 'access', _9 => _9.resolve, 'optionalAccess', _10 => _10.alias]) || {};
76
- const context = compilerOptions.context;
77
- const definePluginInstance = _optionalChain([compilerOptions, 'access', _11 => _11.plugins, 'optionalAccess', _12 => _12.find, 'call', _13 => _13(
78
- (plugin) => plugin instanceof _core2.default.DefinePlugin
79
- )]);
80
- const proxies = (_optionalChain([compilerOptions, 'access', _14 => _14.devServer, 'optionalAccess', _15 => _15.proxy]) || []).flatMap((item) => {
81
- if (typeof item !== "function" && item.context && !item.ws) {
82
- return item.context;
83
- }
84
- return [];
85
- });
86
- return _chunkI54ZNZWLcjs.resolvePluginOptions.call(void 0, options, {
87
- alias,
88
- context,
89
- plugins: _utils.toArray.call(void 0, definePluginInstance),
90
- proxies
91
- });
92
- }
93
-
94
-
95
-
96
-
97
- exports.MockServerPlugin = MockServerPlugin; exports.resolvePluginOptions = resolvePluginOptions2;
@@ -1,97 +0,0 @@
1
- import {
2
- buildMockServer,
3
- createMockCompiler,
4
- createMockMiddleware,
5
- mockWebSocket,
6
- resolvePluginOptions,
7
- rewriteRequest,
8
- waitingFor
9
- } from "./chunk-YSJVV4SH.js";
10
-
11
- // src/rspack.ts
12
- import process from "process";
13
- import path from "path";
14
- import rspack from "@rspack/core";
15
- import { isString, toArray } from "@pengzhanbo/utils";
16
- var PLUGIN_NAME = "rspack-plugin-mock";
17
- var MockServerPlugin = class {
18
- constructor(options = {}) {
19
- this.options = options;
20
- }
21
- apply(compiler) {
22
- const compilerOptions = compiler.options;
23
- const options = resolvePluginOptions2(compiler, this.options);
24
- if (process.env.NODE_ENV !== "production") {
25
- const mockCompiler = createMockCompiler(options);
26
- const mockMiddleware = createMockMiddleware(mockCompiler, options);
27
- const setupMiddlewares = compilerOptions.devServer?.setupMiddlewares;
28
- const waitServer = waitingFor((server) => {
29
- mockWebSocket(mockCompiler, server, options);
30
- });
31
- compilerOptions.devServer = {
32
- ...compilerOptions.devServer,
33
- setupMiddlewares: (middlewares, devServer) => {
34
- middlewares = setupMiddlewares?.(middlewares, devServer) || middlewares;
35
- const reload = () => {
36
- if (devServer.webSocketServer?.clients)
37
- devServer.sendMessage(devServer.webSocketServer.clients, "static-changed");
38
- };
39
- middlewares = mockMiddleware(middlewares, reload) || middlewares;
40
- waitServer(() => devServer.server);
41
- return middlewares;
42
- }
43
- };
44
- const proxy = compilerOptions.devServer?.proxy || [];
45
- const wsPrefix = toArray(options.wsPrefix);
46
- if (proxy.length) {
47
- compilerOptions.devServer.proxy = proxy.map((item) => {
48
- if (typeof item !== "function" && !item.ws) {
49
- const onProxyReq = item.onProxyReq;
50
- item.onProxyReq = (proxyReq, req, ...args) => {
51
- onProxyReq?.(proxyReq, req, ...args);
52
- rewriteRequest(proxyReq, req);
53
- };
54
- }
55
- return item;
56
- }).filter((item) => {
57
- if (typeof item !== "function" && item.ws === true && wsPrefix.length) {
58
- return !toArray(item.context).filter(isString).some((context) => wsPrefix.includes(context));
59
- }
60
- return true;
61
- });
62
- }
63
- compiler.hooks.watchRun.tap(PLUGIN_NAME, () => mockCompiler.run());
64
- compiler.hooks.watchClose.tap(PLUGIN_NAME, () => mockCompiler.close());
65
- } else if (options.build !== false) {
66
- compiler.hooks.afterEmit.tap(PLUGIN_NAME, () => buildMockServer(
67
- options,
68
- compilerOptions.output.path || path.resolve(process.cwd(), "dist")
69
- ));
70
- }
71
- }
72
- };
73
- function resolvePluginOptions2(compiler, options) {
74
- const compilerOptions = compiler.options;
75
- const alias = compilerOptions.resolve?.alias || {};
76
- const context = compilerOptions.context;
77
- const definePluginInstance = compilerOptions.plugins?.find(
78
- (plugin) => plugin instanceof rspack.DefinePlugin
79
- );
80
- const proxies = (compilerOptions.devServer?.proxy || []).flatMap((item) => {
81
- if (typeof item !== "function" && item.context && !item.ws) {
82
- return item.context;
83
- }
84
- return [];
85
- });
86
- return resolvePluginOptions(options, {
87
- alias,
88
- context,
89
- plugins: toArray(definePluginInstance),
90
- proxies
91
- });
92
- }
93
-
94
- export {
95
- MockServerPlugin,
96
- resolvePluginOptions2 as resolvePluginOptions
97
- };
@@ -1,32 +0,0 @@
1
- import { RspackPluginInstance, Compiler } from '@rspack/core';
2
- import { L as LogLevel, M as MockServerPluginOptions, S as ServerBuildOption } from './types-C770q3L0.cjs';
3
-
4
- interface Logger {
5
- debug: (msg: string, level?: boolean | LogLevel) => void;
6
- info: (msg: string, level?: boolean | LogLevel) => void;
7
- warn: (msg: string, level?: boolean | LogLevel) => void;
8
- error: (msg: string, level?: boolean | LogLevel) => void;
9
- }
10
- declare const logLevels: Record<LogLevel, number>;
11
- declare function createLogger(prefix: string, defaultLevel?: LogLevel): Logger;
12
-
13
- interface ResolvedCompilerOptions {
14
- alias: Record<string, false | string | (string | false)[]>;
15
- proxies: (string | ((pathname: string, req: any) => boolean))[];
16
- wsProxies: (string | ((pathname: string, req: any) => boolean))[];
17
- plugins: RspackPluginInstance[];
18
- context?: string;
19
- }
20
- type ResolvePluginOptions = Required<Omit<MockServerPluginOptions, 'build'>> & ResolvedCompilerOptions & {
21
- logger: Logger;
22
- build: false | ServerBuildOption;
23
- };
24
-
25
- declare class MockServerPlugin implements RspackPluginInstance {
26
- options: MockServerPluginOptions;
27
- constructor(options?: MockServerPluginOptions);
28
- apply(compiler: Compiler): void;
29
- }
30
- declare function resolvePluginOptions(compiler: Compiler, options: MockServerPluginOptions): ResolvePluginOptions;
31
-
32
- export { type Logger as L, MockServerPlugin as M, type ResolvePluginOptions as R, createLogger as c, logLevels as l, resolvePluginOptions as r };
@@ -1,32 +0,0 @@
1
- import { RspackPluginInstance, Compiler } from '@rspack/core';
2
- import { L as LogLevel, M as MockServerPluginOptions, S as ServerBuildOption } from './types-C770q3L0.js';
3
-
4
- interface Logger {
5
- debug: (msg: string, level?: boolean | LogLevel) => void;
6
- info: (msg: string, level?: boolean | LogLevel) => void;
7
- warn: (msg: string, level?: boolean | LogLevel) => void;
8
- error: (msg: string, level?: boolean | LogLevel) => void;
9
- }
10
- declare const logLevels: Record<LogLevel, number>;
11
- declare function createLogger(prefix: string, defaultLevel?: LogLevel): Logger;
12
-
13
- interface ResolvedCompilerOptions {
14
- alias: Record<string, false | string | (string | false)[]>;
15
- proxies: (string | ((pathname: string, req: any) => boolean))[];
16
- wsProxies: (string | ((pathname: string, req: any) => boolean))[];
17
- plugins: RspackPluginInstance[];
18
- context?: string;
19
- }
20
- type ResolvePluginOptions = Required<Omit<MockServerPluginOptions, 'build'>> & ResolvedCompilerOptions & {
21
- logger: Logger;
22
- build: false | ServerBuildOption;
23
- };
24
-
25
- declare class MockServerPlugin implements RspackPluginInstance {
26
- options: MockServerPluginOptions;
27
- constructor(options?: MockServerPluginOptions);
28
- apply(compiler: Compiler): void;
29
- }
30
- declare function resolvePluginOptions(compiler: Compiler, options: MockServerPluginOptions): ResolvePluginOptions;
31
-
32
- export { type Logger as L, MockServerPlugin as M, type ResolvePluginOptions as R, createLogger as c, logLevels as l, resolvePluginOptions as r };
package/dist/rspack.cjs DELETED
@@ -1,9 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
-
3
-
4
- var _chunkOYBMX3GQcjs = require('./chunk-OYBMX3GQ.cjs');
5
- require('./chunk-I54ZNZWL.cjs');
6
-
7
-
8
-
9
- exports.MockServerPlugin = _chunkOYBMX3GQcjs.MockServerPlugin; exports.resolvePluginOptions = _chunkOYBMX3GQcjs.resolvePluginOptions;
package/dist/rspack.d.cts DELETED
@@ -1,11 +0,0 @@
1
- import '@rspack/core';
2
- import './types-C770q3L0.cjs';
3
- export { M as MockServerPlugin, r as resolvePluginOptions } from './rspack-BB-Jtq4f.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';