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.
- package/README.md +117 -10
- package/dist/{chunk-G53QRHGV.cjs → chunk-2S4KCTKW.cjs} +1096 -938
- package/dist/chunk-P6DRAHHI.js +90 -0
- package/dist/{chunk-4BGDHRTO.cjs → chunk-T4AI3L6R.cjs} +41 -19
- package/dist/{chunk-5MGZAMDI.js → chunk-UJAKORAH.js} +1100 -942
- package/dist/helper.d.cts +2 -2
- package/dist/helper.d.ts +2 -2
- package/dist/index.cjs +3 -3
- package/dist/index.d.cts +51 -3
- package/dist/index.d.ts +51 -3
- package/dist/index.js +6 -6
- package/dist/rsbuild.cjs +149 -91
- package/dist/rsbuild.d.cts +1 -1
- package/dist/rsbuild.d.ts +1 -1
- package/dist/rsbuild.js +150 -92
- package/dist/rspack-BcdszmoI.d.cts +29 -0
- package/dist/rspack-E_yBzR5v.d.ts +29 -0
- package/dist/rspack.cjs +3 -3
- package/dist/rspack.d.cts +2 -2
- package/dist/rspack.d.ts +2 -2
- package/dist/rspack.js +4 -4
- package/dist/{types-DhT3pRJ3.d.cts → types-C770q3L0.d.cts} +4 -1
- package/dist/{types-DhT3pRJ3.d.ts → types-C770q3L0.d.ts} +4 -1
- package/package.json +2 -1
- package/dist/chunk-B56QNVSS.js +0 -68
- package/dist/rspack-BI-Ifj4a.d.cts +0 -28
- package/dist/rspack-Db7drzDm.d.ts +0 -28
package/dist/helper.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { B as BodyParserOptions, E as ExtraRequest, F as FormidableFile,
|
|
1
|
+
import { b as MockHttpItem, c as MockWebsocketItem, a as MockOptions } from './types-C770q3L0.cjs';
|
|
2
|
+
export { B as BodyParserOptions, E as ExtraRequest, F as FormidableFile, L as LogLevel, i 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-C770q3L0.cjs';
|
|
3
3
|
import 'node:buffer';
|
|
4
4
|
import 'node:http';
|
|
5
5
|
import 'node:stream';
|
package/dist/helper.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export { B as BodyParserOptions, E as ExtraRequest, F as FormidableFile,
|
|
1
|
+
import { b as MockHttpItem, c as MockWebsocketItem, a as MockOptions } from './types-C770q3L0.js';
|
|
2
|
+
export { B as BodyParserOptions, E as ExtraRequest, F as FormidableFile, L as LogLevel, i 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-C770q3L0.js';
|
|
3
3
|
import 'node:buffer';
|
|
4
4
|
import 'node:http';
|
|
5
5
|
import 'node:stream';
|
package/dist/index.cjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";Object.defineProperty(exports, "__esModule", {value: true});
|
|
2
2
|
|
|
3
3
|
|
|
4
|
-
var
|
|
4
|
+
var _chunkT4AI3L6Rcjs = require('./chunk-T4AI3L6R.cjs');
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
@@ -9,7 +9,7 @@ var _chunkKE2LVHUPcjs = require('./chunk-KE2LVHUP.cjs');
|
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
var
|
|
12
|
+
var _chunk2S4KCTKWcjs = require('./chunk-2S4KCTKW.cjs');
|
|
13
13
|
|
|
14
14
|
|
|
15
15
|
|
|
@@ -17,4 +17,4 @@ var _chunkG53QRHGVcjs = require('./chunk-G53QRHGV.cjs');
|
|
|
17
17
|
|
|
18
18
|
|
|
19
19
|
|
|
20
|
-
exports.MockServerPlugin =
|
|
20
|
+
exports.MockServerPlugin = _chunkT4AI3L6Rcjs.MockServerPlugin; exports.createDefineMock = _chunkKE2LVHUPcjs.createDefineMock; exports.createMockCompiler = _chunk2S4KCTKWcjs.createMockCompiler; exports.createMockMiddleware = _chunk2S4KCTKWcjs.createMockMiddleware; exports.defineMock = _chunkKE2LVHUPcjs.defineMock; exports.resolvePluginOptions = _chunkT4AI3L6Rcjs.resolvePluginOptions;
|
package/dist/index.d.cts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
import { R as ResolvePluginOptions } from './rspack-BcdszmoI.cjs';
|
|
2
|
+
export { M as MockServerPlugin, r as resolvePluginOptions } from './rspack-BcdszmoI.cjs';
|
|
2
3
|
export { createDefineMock, defineMock } from './helper.cjs';
|
|
3
|
-
import '@rspack/core';
|
|
4
|
-
import '
|
|
4
|
+
import * as rspackCore from '@rspack/core';
|
|
5
|
+
import { Compiler, RspackPluginInstance, RspackOptionsNormalized } from '@rspack/core';
|
|
6
|
+
import EventEmitter from 'node:events';
|
|
7
|
+
import { FSWatcher } from 'node:fs';
|
|
8
|
+
import { a as MockOptions } from './types-C770q3L0.cjs';
|
|
5
9
|
import 'node:buffer';
|
|
6
10
|
import 'node:http';
|
|
7
11
|
import 'node:stream';
|
|
@@ -10,3 +14,47 @@ import 'cors';
|
|
|
10
14
|
import 'formidable';
|
|
11
15
|
import 'ws';
|
|
12
16
|
import 'co-body';
|
|
17
|
+
|
|
18
|
+
interface MockCompilerOptions {
|
|
19
|
+
alias?: Record<string, false | string | (string | false)[]>;
|
|
20
|
+
plugins: RspackPluginInstance[];
|
|
21
|
+
cwd?: string;
|
|
22
|
+
include: string | string[];
|
|
23
|
+
exclude: string | string[];
|
|
24
|
+
}
|
|
25
|
+
declare function createMockCompiler(options: MockCompilerOptions): MockCompiler;
|
|
26
|
+
declare class MockCompiler extends EventEmitter {
|
|
27
|
+
options: MockCompilerOptions;
|
|
28
|
+
cwd: string;
|
|
29
|
+
mockWatcher: FSWatcher;
|
|
30
|
+
moduleType: 'cjs' | 'esm';
|
|
31
|
+
entryFile: string;
|
|
32
|
+
outputFile: string;
|
|
33
|
+
private _mockData;
|
|
34
|
+
private fileFilter;
|
|
35
|
+
private watchInfo?;
|
|
36
|
+
compiler?: Compiler | null;
|
|
37
|
+
constructor(options: MockCompilerOptions);
|
|
38
|
+
get mockData(): Record<string, MockOptions>;
|
|
39
|
+
run(): Promise<void>;
|
|
40
|
+
close(): void;
|
|
41
|
+
updateAlias(alias: Record<string, false | string | (string | false)[]>): void;
|
|
42
|
+
updateMockEntry(): Promise<void>;
|
|
43
|
+
getMockFiles(): Promise<string[]>;
|
|
44
|
+
watchMockFiles(): void;
|
|
45
|
+
resolveEntryFile(fileList: string[]): Promise<void>;
|
|
46
|
+
createCompiler(callback: (e: Error | null, stats?: rspackCore.Stats) => void): void;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
interface MiddlewareOptions {
|
|
50
|
+
alias: Record<string, false | string | (string | false)[]>;
|
|
51
|
+
proxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
52
|
+
context?: string;
|
|
53
|
+
plugins: RspackPluginInstance[];
|
|
54
|
+
}
|
|
55
|
+
declare function createMockMiddleware(compiler: MockCompiler, options: ResolvePluginOptions): (middlewares: Middleware[], reload?: () => void) => Middleware[];
|
|
56
|
+
type SetupMiddlewaresFn = NonNullable<NonNullable<RspackOptionsNormalized['devServer']>['setupMiddlewares']>;
|
|
57
|
+
type Middleware = SetupMiddlewaresFn extends (middlewares: (infer T)[], devServer: any) => void ? T : never;
|
|
58
|
+
type Server = SetupMiddlewaresFn extends (middlewares: any, devServer: infer T) => void ? T : never;
|
|
59
|
+
|
|
60
|
+
export { type Middleware, type MiddlewareOptions, type Server, createMockCompiler, createMockMiddleware };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
import { R as ResolvePluginOptions } from './rspack-E_yBzR5v.js';
|
|
2
|
+
export { M as MockServerPlugin, r as resolvePluginOptions } from './rspack-E_yBzR5v.js';
|
|
2
3
|
export { createDefineMock, defineMock } from './helper.js';
|
|
3
|
-
import '@rspack/core';
|
|
4
|
-
import '
|
|
4
|
+
import * as rspackCore from '@rspack/core';
|
|
5
|
+
import { Compiler, RspackPluginInstance, RspackOptionsNormalized } from '@rspack/core';
|
|
6
|
+
import EventEmitter from 'node:events';
|
|
7
|
+
import { FSWatcher } from 'node:fs';
|
|
8
|
+
import { a as MockOptions } from './types-C770q3L0.js';
|
|
5
9
|
import 'node:buffer';
|
|
6
10
|
import 'node:http';
|
|
7
11
|
import 'node:stream';
|
|
@@ -10,3 +14,47 @@ import 'cors';
|
|
|
10
14
|
import 'formidable';
|
|
11
15
|
import 'ws';
|
|
12
16
|
import 'co-body';
|
|
17
|
+
|
|
18
|
+
interface MockCompilerOptions {
|
|
19
|
+
alias?: Record<string, false | string | (string | false)[]>;
|
|
20
|
+
plugins: RspackPluginInstance[];
|
|
21
|
+
cwd?: string;
|
|
22
|
+
include: string | string[];
|
|
23
|
+
exclude: string | string[];
|
|
24
|
+
}
|
|
25
|
+
declare function createMockCompiler(options: MockCompilerOptions): MockCompiler;
|
|
26
|
+
declare class MockCompiler extends EventEmitter {
|
|
27
|
+
options: MockCompilerOptions;
|
|
28
|
+
cwd: string;
|
|
29
|
+
mockWatcher: FSWatcher;
|
|
30
|
+
moduleType: 'cjs' | 'esm';
|
|
31
|
+
entryFile: string;
|
|
32
|
+
outputFile: string;
|
|
33
|
+
private _mockData;
|
|
34
|
+
private fileFilter;
|
|
35
|
+
private watchInfo?;
|
|
36
|
+
compiler?: Compiler | null;
|
|
37
|
+
constructor(options: MockCompilerOptions);
|
|
38
|
+
get mockData(): Record<string, MockOptions>;
|
|
39
|
+
run(): Promise<void>;
|
|
40
|
+
close(): void;
|
|
41
|
+
updateAlias(alias: Record<string, false | string | (string | false)[]>): void;
|
|
42
|
+
updateMockEntry(): Promise<void>;
|
|
43
|
+
getMockFiles(): Promise<string[]>;
|
|
44
|
+
watchMockFiles(): void;
|
|
45
|
+
resolveEntryFile(fileList: string[]): Promise<void>;
|
|
46
|
+
createCompiler(callback: (e: Error | null, stats?: rspackCore.Stats) => void): void;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
interface MiddlewareOptions {
|
|
50
|
+
alias: Record<string, false | string | (string | false)[]>;
|
|
51
|
+
proxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
52
|
+
context?: string;
|
|
53
|
+
plugins: RspackPluginInstance[];
|
|
54
|
+
}
|
|
55
|
+
declare function createMockMiddleware(compiler: MockCompiler, options: ResolvePluginOptions): (middlewares: Middleware[], reload?: () => void) => Middleware[];
|
|
56
|
+
type SetupMiddlewaresFn = NonNullable<NonNullable<RspackOptionsNormalized['devServer']>['setupMiddlewares']>;
|
|
57
|
+
type Middleware = SetupMiddlewaresFn extends (middlewares: (infer T)[], devServer: any) => void ? T : never;
|
|
58
|
+
type Server = SetupMiddlewaresFn extends (middlewares: any, devServer: infer T) => void ? T : never;
|
|
59
|
+
|
|
60
|
+
export { type Middleware, type MiddlewareOptions, type Server, createMockCompiler, createMockMiddleware };
|
package/dist/index.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
1
|
import {
|
|
2
2
|
MockServerPlugin,
|
|
3
|
-
|
|
4
|
-
} from "./chunk-
|
|
3
|
+
resolvePluginOptions
|
|
4
|
+
} from "./chunk-P6DRAHHI.js";
|
|
5
5
|
import {
|
|
6
6
|
createDefineMock,
|
|
7
7
|
defineMock
|
|
8
8
|
} from "./chunk-HTOWPFQ7.js";
|
|
9
9
|
import {
|
|
10
|
-
|
|
10
|
+
createMockCompiler,
|
|
11
11
|
createMockMiddleware
|
|
12
|
-
} from "./chunk-
|
|
12
|
+
} from "./chunk-UJAKORAH.js";
|
|
13
13
|
export {
|
|
14
14
|
MockServerPlugin,
|
|
15
15
|
createDefineMock,
|
|
16
|
-
|
|
16
|
+
createMockCompiler,
|
|
17
17
|
createMockMiddleware,
|
|
18
18
|
defineMock,
|
|
19
|
-
|
|
19
|
+
resolvePluginOptions
|
|
20
20
|
};
|
package/dist/rsbuild.cjs
CHANGED
|
@@ -1,121 +1,179 @@
|
|
|
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
|
-
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
var _chunk2S4KCTKWcjs = require('./chunk-2S4KCTKW.cjs');
|
|
5
8
|
|
|
6
9
|
// src/rsbuild.ts
|
|
7
10
|
var _process = require('process'); var _process2 = _interopRequireDefault(_process);
|
|
11
|
+
var _http = require('http');
|
|
8
12
|
var _utils = require('@pengzhanbo/utils');
|
|
9
13
|
var _core = require('@rspack/core'); var _core2 = _interopRequireDefault(_core);
|
|
14
|
+
var _picocolors = require('picocolors'); var _picocolors2 = _interopRequireDefault(_picocolors);
|
|
15
|
+
var _portfinder = require('portfinder');
|
|
10
16
|
function pluginMockServer(options = {}) {
|
|
11
17
|
return {
|
|
12
18
|
name: "plugin-mock-server",
|
|
13
19
|
setup(api) {
|
|
14
20
|
if (_process2.default.env.NODE_ENV === "production")
|
|
15
21
|
return;
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
console.error(_optionalChain([err, 'optionalAccess', _3 => _3.stack]) || err);
|
|
21
|
-
res.statusCode = 500;
|
|
22
|
-
res.end();
|
|
23
|
-
};
|
|
24
|
-
if (_utils.isArray.call(void 0, config.server.proxy)) {
|
|
25
|
-
config.server.proxy = config.server.proxy.map((item) => {
|
|
26
|
-
if (typeof item !== "function" && !item.ws) {
|
|
27
|
-
const onProxyReq = item.onProxyReq;
|
|
28
|
-
const onError = item.onError;
|
|
29
|
-
return {
|
|
30
|
-
...item,
|
|
31
|
-
onError: onError || onProxyError,
|
|
32
|
-
onProxyReq: (proxyReq, req, ...args) => {
|
|
33
|
-
_optionalChain([onProxyReq, 'optionalCall', _4 => _4(proxyReq, req, ...args)]);
|
|
34
|
-
_chunkG53QRHGVcjs.rewriteRequest.call(void 0, proxyReq, req);
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
return item;
|
|
39
|
-
});
|
|
40
|
-
} else if ("target" in config.server.proxy) {
|
|
41
|
-
const onProxyReq = config.server.proxy.onProxyReq;
|
|
42
|
-
config.server.proxy.onProxyReq = (proxyReq, req, ...args) => {
|
|
43
|
-
_optionalChain([onProxyReq, 'optionalCall', _5 => _5(proxyReq, req, ...args)]);
|
|
44
|
-
_chunkG53QRHGVcjs.rewriteRequest.call(void 0, proxyReq, req);
|
|
45
|
-
};
|
|
46
|
-
config.server.proxy.onError ??= onProxyError;
|
|
47
|
-
} else if (config.server.proxy) {
|
|
48
|
-
const proxy = config.server.proxy;
|
|
49
|
-
Object.keys(proxy).forEach((key) => {
|
|
50
|
-
const target = proxy[key];
|
|
51
|
-
const options2 = typeof target === "string" ? { target } : target;
|
|
52
|
-
if (options2.ws)
|
|
53
|
-
return;
|
|
54
|
-
const { onProxyReq, onError, ...rest } = options2;
|
|
55
|
-
proxy[key] = {
|
|
56
|
-
...rest,
|
|
57
|
-
onProxyReq: (proxyReq, req, ...args) => {
|
|
58
|
-
_optionalChain([onProxyReq, 'optionalCall', _6 => _6(proxyReq, req, ...args)]);
|
|
59
|
-
_chunkG53QRHGVcjs.rewriteRequest.call(void 0, proxyReq, req);
|
|
60
|
-
},
|
|
61
|
-
onError: onError || onProxyError
|
|
62
|
-
};
|
|
63
|
-
});
|
|
64
|
-
}
|
|
22
|
+
let port = 3079;
|
|
23
|
+
api.modifyRsbuildConfig(async (config) => {
|
|
24
|
+
const defaultPort = (_optionalChain([config, 'access', _ => _.server, 'optionalAccess', _2 => _2.port]) || port) + 1;
|
|
25
|
+
port = await _portfinder.getPortPromise.call(void 0, { port: defaultPort });
|
|
65
26
|
});
|
|
66
|
-
let
|
|
67
|
-
let
|
|
68
|
-
|
|
27
|
+
let mockCompiler = null;
|
|
28
|
+
let resolvedOptions;
|
|
29
|
+
api.modifyRsbuildConfig(updateServerProxyConfig);
|
|
69
30
|
api.modifyRsbuildConfig((config) => {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
for (const item of proxy) {
|
|
74
|
-
if (typeof item !== "function" && !item.ws && item.context) {
|
|
75
|
-
proxies.push(..._utils.toArray.call(void 0, item.context));
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
} else if ("target" in proxy) {
|
|
79
|
-
proxies.push(..._utils.toArray.call(void 0, proxy.context));
|
|
80
|
-
} else {
|
|
81
|
-
Object.entries(proxy).forEach(([context, opt]) => {
|
|
82
|
-
if (typeof opt === "string" || !opt.ws) {
|
|
83
|
-
proxies.push(context);
|
|
84
|
-
}
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
const { mockMiddleware, run, close, updateAlias } = _chunkG53QRHGVcjs.createManuallyMockMiddleware.call(void 0, {
|
|
88
|
-
plugins: [new _core2.default.DefinePlugin(_optionalChain([config, 'access', _9 => _9.source, 'optionalAccess', _10 => _10.define]) || {})],
|
|
31
|
+
config.server ??= {};
|
|
32
|
+
resolvedOptions = _chunk2S4KCTKWcjs.resolvePluginOptions.call(void 0, options, {
|
|
33
|
+
proxies: resolveConfigProxies(config, options.wsPrefix || [], port),
|
|
89
34
|
alias: {},
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
compilerUpdateAlias = updateAlias;
|
|
35
|
+
context: api.context.rootPath,
|
|
36
|
+
plugins: [new _core2.default.DefinePlugin(_optionalChain([config, 'access', _3 => _3.source, 'optionalAccess', _4 => _4.define]) || {})]
|
|
37
|
+
});
|
|
38
|
+
mockCompiler = _chunk2S4KCTKWcjs.createMockCompiler.call(void 0, resolvedOptions);
|
|
39
|
+
const mockMiddleware = _chunk2S4KCTKWcjs.createMockMiddleware.call(void 0, mockCompiler, resolvedOptions);
|
|
96
40
|
config.dev ??= {};
|
|
97
41
|
config.dev.setupMiddlewares ??= [];
|
|
98
|
-
config.dev.setupMiddlewares.push((middlewares) => {
|
|
99
|
-
mockMiddleware(middlewares);
|
|
42
|
+
config.dev.setupMiddlewares.push((middlewares, server2) => {
|
|
43
|
+
mockMiddleware(middlewares, () => server2.sockWrite("static-changed"));
|
|
100
44
|
});
|
|
101
45
|
});
|
|
102
46
|
api.onAfterCreateCompiler(({ compiler }) => {
|
|
103
|
-
if (
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
_optionalChain([compilerUpdateAlias, 'optionalCall', _15 => _15(_optionalChain([compiler, 'access', _16 => _16.options, 'access', _17 => _17.resolve, 'optionalAccess', _18 => _18.alias]) || {})]);
|
|
110
|
-
}
|
|
47
|
+
if ("compilers" in compiler) {
|
|
48
|
+
compiler.compilers.forEach((compiler2) => {
|
|
49
|
+
_optionalChain([mockCompiler, 'optionalAccess', _5 => _5.updateAlias, 'call', _6 => _6(_optionalChain([compiler2, 'access', _7 => _7.options, 'access', _8 => _8.resolve, 'optionalAccess', _9 => _9.alias]) || {})]);
|
|
50
|
+
});
|
|
51
|
+
} else {
|
|
52
|
+
_optionalChain([mockCompiler, 'optionalAccess', _10 => _10.updateAlias, 'call', _11 => _11(_optionalChain([compiler, 'access', _12 => _12.options, 'access', _13 => _13.resolve, 'optionalAccess', _14 => _14.alias]) || {})]);
|
|
111
53
|
}
|
|
112
54
|
});
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
55
|
+
let server;
|
|
56
|
+
function startMockServer() {
|
|
57
|
+
if (!mockCompiler)
|
|
58
|
+
return;
|
|
59
|
+
mockCompiler.run();
|
|
60
|
+
server = _http.createServer.call(void 0, );
|
|
61
|
+
_chunk2S4KCTKWcjs.mockWebSocket.call(void 0, mockCompiler, server, resolvedOptions);
|
|
62
|
+
server.listen(port);
|
|
63
|
+
}
|
|
64
|
+
function close() {
|
|
65
|
+
_optionalChain([mockCompiler, 'optionalAccess', _15 => _15.close, 'call', _16 => _16()]);
|
|
66
|
+
_optionalChain([server, 'optionalAccess', _17 => _17.close, 'call', _18 => _18()]);
|
|
67
|
+
}
|
|
68
|
+
api.onAfterStartDevServer(startMockServer);
|
|
69
|
+
api.onAfterStartProdServer(startMockServer);
|
|
70
|
+
api.onExit(close);
|
|
116
71
|
}
|
|
117
72
|
};
|
|
118
73
|
}
|
|
74
|
+
function updateServerProxyConfig(config) {
|
|
75
|
+
if (!_optionalChain([config, 'access', _19 => _19.server, 'optionalAccess', _20 => _20.proxy]))
|
|
76
|
+
return;
|
|
77
|
+
const onProxyError = (err, _req, res) => {
|
|
78
|
+
console.error(_picocolors2.default.red(_optionalChain([err, 'optionalAccess', _21 => _21.stack]) || err.message));
|
|
79
|
+
res.statusCode = 500;
|
|
80
|
+
res.end();
|
|
81
|
+
};
|
|
82
|
+
if (_utils.isArray.call(void 0, config.server.proxy)) {
|
|
83
|
+
config.server.proxy = config.server.proxy.map((item) => {
|
|
84
|
+
if (typeof item !== "function" && !item.ws) {
|
|
85
|
+
const onProxyReq = item.onProxyReq;
|
|
86
|
+
const onError = item.onError;
|
|
87
|
+
return {
|
|
88
|
+
...item,
|
|
89
|
+
onError: onError || onProxyError,
|
|
90
|
+
onProxyReq: (proxyReq, req, ...args) => {
|
|
91
|
+
_optionalChain([onProxyReq, 'optionalCall', _22 => _22(proxyReq, req, ...args)]);
|
|
92
|
+
_chunk2S4KCTKWcjs.rewriteRequest.call(void 0, proxyReq, req);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
return item;
|
|
97
|
+
});
|
|
98
|
+
} else if ("target" in config.server.proxy) {
|
|
99
|
+
const onProxyReq = config.server.proxy.onProxyReq;
|
|
100
|
+
config.server.proxy.onProxyReq = (proxyReq, req, ...args) => {
|
|
101
|
+
_optionalChain([onProxyReq, 'optionalCall', _23 => _23(proxyReq, req, ...args)]);
|
|
102
|
+
_chunk2S4KCTKWcjs.rewriteRequest.call(void 0, proxyReq, req);
|
|
103
|
+
};
|
|
104
|
+
config.server.proxy.onError ??= onProxyError;
|
|
105
|
+
} else if (config.server.proxy) {
|
|
106
|
+
const proxy = config.server.proxy;
|
|
107
|
+
Object.keys(proxy).forEach((key) => {
|
|
108
|
+
const target = proxy[key];
|
|
109
|
+
const options = typeof target === "string" ? { target } : target;
|
|
110
|
+
if (options.ws)
|
|
111
|
+
return;
|
|
112
|
+
const { onProxyReq, onError, ...rest } = options;
|
|
113
|
+
proxy[key] = {
|
|
114
|
+
...rest,
|
|
115
|
+
onProxyReq: (proxyReq, req, ...args) => {
|
|
116
|
+
_optionalChain([onProxyReq, 'optionalCall', _24 => _24(proxyReq, req, ...args)]);
|
|
117
|
+
_chunk2S4KCTKWcjs.rewriteRequest.call(void 0, proxyReq, req);
|
|
118
|
+
},
|
|
119
|
+
onError: onError || onProxyError
|
|
120
|
+
};
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
function resolveConfigProxies(config, wsPrefix, port) {
|
|
125
|
+
const proxy = config.server.proxy ??= {};
|
|
126
|
+
const proxies = [];
|
|
127
|
+
const wsTarget = `ws://localhost:${port}`;
|
|
128
|
+
const prefix = _utils.toArray.call(void 0, wsPrefix);
|
|
129
|
+
const has = (context) => typeof context === "string" && prefix.includes(context);
|
|
130
|
+
const used = /* @__PURE__ */ new Set();
|
|
131
|
+
function updateProxy(item) {
|
|
132
|
+
if (_utils.isArray.call(void 0, item.context)) {
|
|
133
|
+
item.context = item.context.filter(has);
|
|
134
|
+
} else if (has(item.context)) {
|
|
135
|
+
used.add(item.context);
|
|
136
|
+
item.target = wsTarget;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
if (_utils.isArray.call(void 0, proxy)) {
|
|
140
|
+
for (const item of proxy) {
|
|
141
|
+
if (typeof item !== "function" && item.context) {
|
|
142
|
+
if (!item.ws) {
|
|
143
|
+
proxies.push(..._utils.toArray.call(void 0, item.context));
|
|
144
|
+
} else {
|
|
145
|
+
updateProxy(item);
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
prefix.filter((context) => !used.has(context)).forEach((context) => {
|
|
150
|
+
proxy.push({ context, target: wsTarget });
|
|
151
|
+
});
|
|
152
|
+
} else if ("target" in proxy) {
|
|
153
|
+
if (!proxy.ws) {
|
|
154
|
+
proxies.push(..._utils.toArray.call(void 0, proxy.context));
|
|
155
|
+
} else {
|
|
156
|
+
updateProxy(proxy);
|
|
157
|
+
const list = config.server.proxy = [proxy];
|
|
158
|
+
prefix.filter((context) => !used.has(context)).forEach((context) => {
|
|
159
|
+
list.push({ context, target: wsTarget });
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
} else {
|
|
163
|
+
Object.entries(proxy).forEach(([context, opt]) => {
|
|
164
|
+
if (typeof opt === "string" || !opt.ws) {
|
|
165
|
+
proxies.push(context);
|
|
166
|
+
}
|
|
167
|
+
if (typeof opt !== "string" && opt.ws) {
|
|
168
|
+
updateProxy(opt);
|
|
169
|
+
}
|
|
170
|
+
});
|
|
171
|
+
prefix.filter((context) => !used.has(context)).forEach((context) => {
|
|
172
|
+
proxy[context] = { target: wsTarget, ws: true };
|
|
173
|
+
});
|
|
174
|
+
}
|
|
175
|
+
return proxies;
|
|
176
|
+
}
|
|
119
177
|
|
|
120
178
|
|
|
121
179
|
exports.pluginMockServer = pluginMockServer;
|
package/dist/rsbuild.d.cts
CHANGED
package/dist/rsbuild.d.ts
CHANGED