rspack-plugin-mock 1.0.1 → 1.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.
package/dist/index.d.cts CHANGED
@@ -1,25 +1,13 @@
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';
3
- import { RspackPluginInstance, Compiler } from '@rspack/core';
4
- import { c as MockServerPluginOptions } from './types-Aw0AciTG.cjs';
5
- export { B as BodyParserOptions, E as ExtraRequest, F as FormidableFile, i as LogLevel, L as LogType, f as Method, M as MockHttpItem, d as MockMatchPriority, e as MockMatchSpecialPriority, b as MockOptions, g as MockRequest, h as MockResponse, a as MockWebsocketItem, R as ResponseBody, S as ServerBuildOption, W as WebSocketSetupContext } from './types-Aw0AciTG.cjs';
6
- import 'node:http';
7
- import 'node:http2';
8
- import 'node:fs';
9
- import 'node:events';
10
- import 'co-body';
11
- import 'cookies';
12
- import 'cors';
13
- import 'formidable';
14
- import 'node:buffer';
15
- import 'node:stream';
16
- import 'ws';
1
+ import { BodyParserOptions, ExtraRequest, FormidableFile, LogLevel, LogType, Method, MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, MockOptions, MockRequest, MockResponse, MockServerPluginOptions, MockWebsocketItem, ResponseBody, ServerBuildOption, WebSocketSetupContext } from "./types-6lajtJPx.cjs";
2
+ import { Middleware, MiddlewareOptions, MockCompiler, MockCompilerOptions, MockSocketOptions, ResolvePluginOptions, Server, createMockCompiler, createMockMiddleware, mockWebSocket } from "./mockWebsocket-DkVHpZCx.cjs";
3
+ import { Compiler, RspackPluginInstance } from "@rspack/core";
17
4
 
5
+ //#region src/rspack.d.ts
18
6
  declare class MockServerPlugin implements RspackPluginInstance {
19
- options: MockServerPluginOptions;
20
- constructor(options?: MockServerPluginOptions);
21
- apply(compiler: Compiler): void;
7
+ options: MockServerPluginOptions;
8
+ constructor(options?: MockServerPluginOptions);
9
+ apply(compiler: Compiler): void;
22
10
  }
23
- declare function resolvePluginOptions(compiler: Compiler, options: MockServerPluginOptions): ResolvePluginOptions;
24
-
25
- export { MockServerPlugin, MockServerPluginOptions, resolvePluginOptions };
11
+ declare function resolvePluginOptions(compiler: Compiler, options?: MockServerPluginOptions): ResolvePluginOptions;
12
+ //#endregion
13
+ export { BodyParserOptions, ExtraRequest, FormidableFile, LogLevel, LogType, Method, Middleware, MiddlewareOptions, MockCompiler, MockCompilerOptions, MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, MockOptions, MockRequest, MockResponse, MockServerPlugin, MockServerPluginOptions, MockSocketOptions, MockWebsocketItem, ResponseBody, Server, ServerBuildOption, WebSocketSetupContext, createMockCompiler, createMockMiddleware, mockWebSocket, resolvePluginOptions };
package/dist/index.d.ts CHANGED
@@ -1,25 +1,13 @@
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';
3
- import { RspackPluginInstance, Compiler } from '@rspack/core';
4
- import { c as MockServerPluginOptions } from './types-Aw0AciTG.js';
5
- export { B as BodyParserOptions, E as ExtraRequest, F as FormidableFile, i as LogLevel, L as LogType, f as Method, M as MockHttpItem, d as MockMatchPriority, e as MockMatchSpecialPriority, b as MockOptions, g as MockRequest, h as MockResponse, a as MockWebsocketItem, R as ResponseBody, S as ServerBuildOption, W as WebSocketSetupContext } from './types-Aw0AciTG.js';
6
- import 'node:http';
7
- import 'node:http2';
8
- import 'node:fs';
9
- import 'node:events';
10
- import 'co-body';
11
- import 'cookies';
12
- import 'cors';
13
- import 'formidable';
14
- import 'node:buffer';
15
- import 'node:stream';
16
- import 'ws';
1
+ import { BodyParserOptions, ExtraRequest, FormidableFile, LogLevel, LogType, Method, MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, MockOptions, MockRequest, MockResponse, MockServerPluginOptions, MockWebsocketItem, ResponseBody, ServerBuildOption, WebSocketSetupContext } from "./types-DPzh7nJq.js";
2
+ import { Middleware, MiddlewareOptions, MockCompiler, MockCompilerOptions, MockSocketOptions, ResolvePluginOptions, Server, createMockCompiler, createMockMiddleware, mockWebSocket } from "./mockWebsocket-qLVAe-RI.js";
3
+ import { Compiler, RspackPluginInstance } from "@rspack/core";
17
4
 
5
+ //#region src/rspack.d.ts
18
6
  declare class MockServerPlugin implements RspackPluginInstance {
19
- options: MockServerPluginOptions;
20
- constructor(options?: MockServerPluginOptions);
21
- apply(compiler: Compiler): void;
7
+ options: MockServerPluginOptions;
8
+ constructor(options?: MockServerPluginOptions);
9
+ apply(compiler: Compiler): void;
22
10
  }
23
- declare function resolvePluginOptions(compiler: Compiler, options: MockServerPluginOptions): ResolvePluginOptions;
24
-
25
- export { MockServerPlugin, MockServerPluginOptions, resolvePluginOptions };
11
+ declare function resolvePluginOptions(compiler: Compiler, options?: MockServerPluginOptions): ResolvePluginOptions;
12
+ //#endregion
13
+ export { BodyParserOptions, ExtraRequest, FormidableFile, LogLevel, LogType, Method, Middleware, MiddlewareOptions, MockCompiler, MockCompilerOptions, MockHttpItem, MockMatchPriority, MockMatchSpecialPriority, MockOptions, MockRequest, MockResponse, MockServerPlugin, MockServerPluginOptions, MockSocketOptions, MockWebsocketItem, ResponseBody, Server, ServerBuildOption, WebSocketSetupContext, createMockCompiler, createMockMiddleware, mockWebSocket, resolvePluginOptions };
package/dist/index.js CHANGED
@@ -1,103 +1,80 @@
1
- import {
2
- MockCompiler,
3
- buildMockServer,
4
- createMockCompiler,
5
- createMockMiddleware,
6
- resolvePluginOptions
7
- } from "./chunk-HV5L72CY.js";
8
- import {
9
- mockWebSocket,
10
- rewriteRequest,
11
- waitingFor
12
- } from "./chunk-OGWV5ZGG.js";
13
-
14
- // src/rspack.ts
1
+ import { mockWebSocket, rewriteRequest, waitingFor } from "./logger-C48_LmdS.js";
2
+ import { MockCompiler, buildMockServer, createMockCompiler, createMockMiddleware, resolvePluginOptions } from "./resolvePluginOptions-DlkIkykz.js";
3
+ import { isString, toArray } from "@pengzhanbo/utils";
15
4
  import path from "node:path";
16
5
  import process from "node:process";
17
- import { isString, toArray } from "@pengzhanbo/utils";
18
6
  import rspack from "@rspack/core";
19
- var PLUGIN_NAME = "rspack-plugin-mock";
7
+
8
+ //#region src/rspack.ts
9
+ const PLUGIN_NAME = "rspack-plugin-mock";
20
10
  var MockServerPlugin = class {
21
- constructor(options = {}) {
22
- this.options = options;
23
- }
24
- apply(compiler) {
25
- const compilerOptions = compiler.options;
26
- const options = resolvePluginOptions2(compiler, this.options);
27
- if (process.env.NODE_ENV !== "production") {
28
- const mockCompiler = createMockCompiler(options);
29
- const mockMiddleware = createMockMiddleware(mockCompiler, options);
30
- const setupMiddlewares = compilerOptions.devServer?.setupMiddlewares;
31
- const waitServerForMockWebSocket = waitingFor((server) => {
32
- mockWebSocket(mockCompiler, server, options);
33
- });
34
- compilerOptions.devServer = {
35
- ...compilerOptions.devServer,
36
- setupMiddlewares: (middlewares, devServer) => {
37
- middlewares = setupMiddlewares?.(middlewares, devServer) || middlewares;
38
- const reload = () => {
39
- if (devServer.webSocketServer?.clients)
40
- devServer.sendMessage(devServer.webSocketServer.clients, "static-changed");
41
- };
42
- middlewares = mockMiddleware(middlewares, reload) || middlewares;
43
- waitServerForMockWebSocket(() => devServer.server);
44
- return middlewares;
45
- }
46
- };
47
- const wsPrefix = toArray(options.wsPrefix);
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) => {
56
- if (typeof item !== "function" && !item.ws) {
57
- const onProxyReq = item.onProxyReq;
58
- item.onProxyReq = (proxyReq, req, ...args) => {
59
- onProxyReq?.(proxyReq, req, ...args);
60
- rewriteRequest(proxyReq, req);
61
- };
62
- }
63
- return item;
64
- });
65
- }
66
- compiler.hooks.watchRun.tap(PLUGIN_NAME, () => mockCompiler.run());
67
- compiler.hooks.watchClose.tap(PLUGIN_NAME, () => mockCompiler.close());
68
- } else if (options.build !== false) {
69
- compiler.hooks.afterEmit.tap(PLUGIN_NAME, () => buildMockServer(
70
- options,
71
- compilerOptions.output.path || path.resolve(process.cwd(), "dist")
72
- ));
73
- }
74
- }
11
+ constructor(options = {}) {
12
+ this.options = options;
13
+ }
14
+ apply(compiler) {
15
+ const compilerOptions = compiler.options;
16
+ const options = resolvePluginOptions$1(compiler, this.options);
17
+ if (process.env.NODE_ENV !== "production") {
18
+ const mockCompiler = createMockCompiler(options);
19
+ const mockMiddleware = createMockMiddleware(mockCompiler, options);
20
+ const setupMiddlewares = compilerOptions.devServer?.setupMiddlewares;
21
+ const waitServerForMockWebSocket = waitingFor((server) => {
22
+ mockWebSocket(mockCompiler, server, options);
23
+ });
24
+ compilerOptions.devServer = {
25
+ ...compilerOptions.devServer,
26
+ setupMiddlewares: (middlewares, devServer) => {
27
+ middlewares = setupMiddlewares?.(middlewares, devServer) || middlewares;
28
+ const reload = () => {
29
+ if (devServer.webSocketServer?.clients) devServer.sendMessage(devServer.webSocketServer.clients, "static-changed");
30
+ };
31
+ middlewares = mockMiddleware(middlewares, reload) || middlewares;
32
+ /**
33
+ * @rspack/dev-server -> webpack-dev-server 中, setupMiddlewares 优先于 createServer
34
+ * 执行,需要等待 server 启动后再注入 mock websocket
35
+ */
36
+ waitServerForMockWebSocket(() => devServer.server);
37
+ return middlewares;
38
+ }
39
+ };
40
+ const wsPrefix = toArray(options.wsPrefix);
41
+ if (compilerOptions.devServer?.proxy?.length) {
42
+ const proxy = compilerOptions.devServer.proxy;
43
+ compilerOptions.devServer.proxy = proxy.filter((item) => {
44
+ if (typeof item !== "function" && item.ws === true && wsPrefix.length) return !toArray(item.context).filter(isString).some((context) => wsPrefix.includes(context));
45
+ return true;
46
+ }).map((item) => {
47
+ if (typeof item !== "function" && !item.ws) {
48
+ const onProxyReq = item.onProxyReq;
49
+ item.onProxyReq = (proxyReq, req, ...args) => {
50
+ onProxyReq?.(proxyReq, req, ...args);
51
+ rewriteRequest(proxyReq, req);
52
+ };
53
+ }
54
+ return item;
55
+ });
56
+ }
57
+ compiler.hooks.watchRun.tap(PLUGIN_NAME, () => mockCompiler.run());
58
+ compiler.hooks.watchClose.tap(PLUGIN_NAME, () => mockCompiler.close());
59
+ } else if (options.build !== false) compiler.hooks.afterEmit.tap(PLUGIN_NAME, () => buildMockServer(options, compilerOptions.output.path || path.resolve(process.cwd(), "dist")));
60
+ }
75
61
  };
76
- function resolvePluginOptions2(compiler, options) {
77
- const compilerOptions = compiler.options;
78
- const alias = compilerOptions.resolve?.alias || {};
79
- const context = compilerOptions.context;
80
- const definePluginInstance = compilerOptions.plugins?.find(
81
- (plugin) => plugin instanceof rspack.DefinePlugin
82
- );
83
- const proxies = (compilerOptions.devServer?.proxy || []).flatMap((item) => {
84
- if (typeof item !== "function" && item.context && !item.ws) {
85
- return item.context;
86
- }
87
- return [];
88
- });
89
- return resolvePluginOptions(options, {
90
- alias,
91
- context,
92
- plugins: toArray(definePluginInstance),
93
- proxies
94
- });
62
+ function resolvePluginOptions$1(compiler, options = {}) {
63
+ const compilerOptions = compiler.options;
64
+ const alias = compilerOptions.resolve?.alias || {};
65
+ const context = compilerOptions.context;
66
+ const definePluginInstance = compilerOptions.plugins?.find((plugin) => plugin instanceof rspack.DefinePlugin);
67
+ const proxies = (compilerOptions.devServer?.proxy || []).flatMap((item) => {
68
+ if (typeof item !== "function" && item.context && !item.ws) return item.context;
69
+ return [];
70
+ });
71
+ return resolvePluginOptions(options, {
72
+ alias,
73
+ context,
74
+ plugins: toArray(definePluginInstance),
75
+ proxies
76
+ });
95
77
  }
96
- export {
97
- MockCompiler,
98
- MockServerPlugin,
99
- createMockCompiler,
100
- createMockMiddleware,
101
- mockWebSocket,
102
- resolvePluginOptions2 as resolvePluginOptions
103
- };
78
+
79
+ //#endregion
80
+ export { MockCompiler, MockServerPlugin, createMockCompiler, createMockMiddleware, mockWebSocket, resolvePluginOptions$1 as resolvePluginOptions };
@@ -1,17 +1,32 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var __getOwnPropNames = Object.getOwnPropertyNames;
2
- var __commonJS = (cb, mod) => function __require() {
3
- return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
4
17
  };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
5
22
 
6
- // src/json5-loader.cts
7
- var _json5 = require('json5'); var _json52 = _interopRequireDefault(_json5);
8
- var require_json5_loader = __commonJS({
9
- "src/json5-loader.cts"(exports, module) {
10
- module.exports = function(content) {
11
- if (!content)
12
- return "export default {}";
13
- return `export default ${JSON.stringify(_json52.default.parse(content))}`;
14
- };
15
- }
16
- });
17
- exports. default = require_json5_loader();
23
+ //#endregion
24
+ const json5 = __toESM(require("json5"));
25
+
26
+ //#region src/json5-loader.cts
27
+ module.exports = function(content) {
28
+ if (!content) return "export default {}";
29
+ return `export default ${JSON.stringify(json5.default.parse(content))}`;
30
+ };
31
+
32
+ //#endregion