rspack-plugin-mock 0.1.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,68 @@
1
+ import {
2
+ createManuallyMockMiddleware,
3
+ rewriteRequest
4
+ } from "./chunk-5MGZAMDI.js";
5
+
6
+ // src/rspack.ts
7
+ import process from "process";
8
+ import rspack from "@rspack/core";
9
+ import { toArray } from "@pengzhanbo/utils";
10
+ var PLUGIN_NAME = "rspack-plugin-mock";
11
+ var MockServerPlugin = class {
12
+ constructor(options = {}) {
13
+ this.options = options;
14
+ }
15
+ apply(compiler) {
16
+ const compilerOptions = compiler.options;
17
+ if (process.env.NODE_ENV !== "production") {
18
+ const { mockMiddleware, run, close } = createManuallyMockMiddleware(
19
+ resolveMiddleOptions(compiler),
20
+ this.options
21
+ );
22
+ const setupMiddlewares = compilerOptions.devServer?.setupMiddlewares;
23
+ compilerOptions.devServer = {
24
+ ...compilerOptions.devServer,
25
+ setupMiddlewares: (middlewares, devServer) => {
26
+ middlewares = setupMiddlewares?.(middlewares, devServer) || middlewares;
27
+ middlewares = mockMiddleware(middlewares) || middlewares;
28
+ return middlewares;
29
+ }
30
+ };
31
+ const proxy = compilerOptions.devServer?.proxy || [];
32
+ if (proxy.length) {
33
+ compilerOptions.devServer.proxy = proxy.map((item) => {
34
+ if (typeof item !== "function" && !item.ws) {
35
+ const onProxyReq = item.onProxyReq;
36
+ item.onProxyReq = (proxyReq, req, ...args) => {
37
+ onProxyReq?.(proxyReq, req, ...args);
38
+ rewriteRequest(proxyReq, req);
39
+ };
40
+ }
41
+ return item;
42
+ });
43
+ }
44
+ compiler.hooks.watchRun.tap(PLUGIN_NAME, () => run());
45
+ compiler.hooks.watchClose.tap(PLUGIN_NAME, () => close());
46
+ }
47
+ }
48
+ };
49
+ function resolveMiddleOptions(compiler) {
50
+ const compilerOptions = compiler.options;
51
+ const alias = compilerOptions.resolve?.alias || {};
52
+ const context = compilerOptions.context;
53
+ const definePluginInstance = compilerOptions.plugins?.find(
54
+ (plugin) => plugin instanceof rspack.DefinePlugin
55
+ );
56
+ const proxies = (compilerOptions.devServer?.proxy || []).map((item) => {
57
+ if (typeof item !== "function" && !item.ws && item.context) {
58
+ return item.context;
59
+ }
60
+ return [];
61
+ }).flat();
62
+ return { alias, context, plugins: toArray(definePluginInstance), proxies };
63
+ }
64
+
65
+ export {
66
+ MockServerPlugin,
67
+ resolveMiddleOptions
68
+ };