rspack-plugin-mock 1.1.0 → 1.3.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 +10 -2
- package/README.zh-CN.md +13 -5
- package/dist/helper.d.mts +126 -0
- package/dist/helper.mjs +4 -0
- package/dist/index.d.mts +13 -0
- package/dist/index.mjs +1 -0
- package/dist/json5-loader.cjs +1 -17
- package/dist/options-BUfaThYe.mjs +43 -0
- package/dist/rsbuild.d.mts +7 -0
- package/dist/rsbuild.mjs +1 -0
- package/dist/server-4ETytB7L.d.mts +89 -0
- package/dist/server.d.mts +3 -0
- package/dist/server.mjs +1 -0
- package/dist/types-Bt_OTa7I.d.mts +701 -0
- package/dist/ws-BM06pHhL.mjs +1 -0
- package/package.json +46 -57
- package/dist/chunk-HTVJXQRM.cjs +0 -906
- package/dist/chunk-HV5L72CY.js +0 -557
- package/dist/chunk-M7F5AAOF.cjs +0 -557
- package/dist/chunk-OGWV5ZGG.js +0 -906
- package/dist/helper.cjs +0 -129
- package/dist/helper.d.cts +0 -122
- package/dist/helper.d.ts +0 -122
- package/dist/helper.js +0 -129
- package/dist/index.cjs +0 -103
- package/dist/index.d.cts +0 -25
- package/dist/index.d.ts +0 -25
- package/dist/index.js +0 -103
- package/dist/mockWebsocket-DBgZBsdo.d.ts +0 -76
- package/dist/mockWebsocket-Ki_cShTv.d.cts +0 -76
- package/dist/rsbuild.cjs +0 -202
- package/dist/rsbuild.d.cts +0 -15
- package/dist/rsbuild.d.ts +0 -15
- package/dist/rsbuild.js +0 -202
- package/dist/server.cjs +0 -18
- package/dist/server.d.cts +0 -31
- package/dist/server.d.ts +0 -31
- package/dist/server.js +0 -18
- package/dist/types-Aw0AciTG.d.cts +0 -570
- package/dist/types-Aw0AciTG.d.ts +0 -570
package/dist/index.js
DELETED
|
@@ -1,103 +0,0 @@
|
|
|
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
|
|
15
|
-
import path from "node:path";
|
|
16
|
-
import process from "node:process";
|
|
17
|
-
import { isString, toArray } from "@pengzhanbo/utils";
|
|
18
|
-
import rspack from "@rspack/core";
|
|
19
|
-
var PLUGIN_NAME = "rspack-plugin-mock";
|
|
20
|
-
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
|
-
}
|
|
75
|
-
};
|
|
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
|
-
});
|
|
95
|
-
}
|
|
96
|
-
export {
|
|
97
|
-
MockCompiler,
|
|
98
|
-
MockServerPlugin,
|
|
99
|
-
createMockCompiler,
|
|
100
|
-
createMockMiddleware,
|
|
101
|
-
mockWebSocket,
|
|
102
|
-
resolvePluginOptions2 as resolvePluginOptions
|
|
103
|
-
};
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { RspackPluginInstance, Compiler, RspackOptionsNormalized } from '@rspack/core';
|
|
2
|
-
import { i as LogLevel, b as MockOptions, c as MockServerPluginOptions, S as ServerBuildOption } from './types-Aw0AciTG.js';
|
|
3
|
-
import { Server as Server$1 } from 'node:http';
|
|
4
|
-
import { Http2SecureServer } from 'node:http2';
|
|
5
|
-
import { FSWatcher } from 'node:fs';
|
|
6
|
-
import EventEmitter from 'node:events';
|
|
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 MockCompilerOptions {
|
|
18
|
-
alias?: Record<string, false | string | (string | false)[]>;
|
|
19
|
-
plugins: RspackPluginInstance[];
|
|
20
|
-
cwd?: string;
|
|
21
|
-
include: string | string[];
|
|
22
|
-
exclude: string | string[];
|
|
23
|
-
logger: Logger;
|
|
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
|
-
private _mockData;
|
|
33
|
-
private fileFilter;
|
|
34
|
-
private watchInfo?;
|
|
35
|
-
compiler?: Compiler | null;
|
|
36
|
-
constructor(options: MockCompilerOptions);
|
|
37
|
-
get mockData(): Record<string, MockOptions>;
|
|
38
|
-
run(): Promise<void>;
|
|
39
|
-
close(): void;
|
|
40
|
-
updateAlias(alias: Record<string, false | string | (string | false)[]>): void;
|
|
41
|
-
updateMockEntry(): Promise<void>;
|
|
42
|
-
getMockFiles(): Promise<string[]>;
|
|
43
|
-
watchMockFiles(): void;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
interface ResolvedCompilerOptions {
|
|
47
|
-
alias: Record<string, false | string | (string | false)[]>;
|
|
48
|
-
proxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
49
|
-
wsProxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
50
|
-
plugins: RspackPluginInstance[];
|
|
51
|
-
context?: string;
|
|
52
|
-
}
|
|
53
|
-
type ResolvePluginOptions = Required<Omit<MockServerPluginOptions, 'build'>> & ResolvedCompilerOptions & {
|
|
54
|
-
logger: Logger;
|
|
55
|
-
build: false | ServerBuildOption;
|
|
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 MockCompilerOptions as M, type ResolvePluginOptions as R, type Server as S, MockCompiler as a, type MiddlewareOptions as b, createMockCompiler as c, createMockMiddleware as d, type Middleware as e, type MockSocketOptions as f, createLogger as g, logLevels as l, mockWebSocket as m };
|
|
@@ -1,76 +0,0 @@
|
|
|
1
|
-
import { RspackPluginInstance, Compiler, RspackOptionsNormalized } from '@rspack/core';
|
|
2
|
-
import { i as LogLevel, b as MockOptions, c as MockServerPluginOptions, S as ServerBuildOption } from './types-Aw0AciTG.cjs';
|
|
3
|
-
import { Server as Server$1 } from 'node:http';
|
|
4
|
-
import { Http2SecureServer } from 'node:http2';
|
|
5
|
-
import { FSWatcher } from 'node:fs';
|
|
6
|
-
import EventEmitter from 'node:events';
|
|
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 MockCompilerOptions {
|
|
18
|
-
alias?: Record<string, false | string | (string | false)[]>;
|
|
19
|
-
plugins: RspackPluginInstance[];
|
|
20
|
-
cwd?: string;
|
|
21
|
-
include: string | string[];
|
|
22
|
-
exclude: string | string[];
|
|
23
|
-
logger: Logger;
|
|
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
|
-
private _mockData;
|
|
33
|
-
private fileFilter;
|
|
34
|
-
private watchInfo?;
|
|
35
|
-
compiler?: Compiler | null;
|
|
36
|
-
constructor(options: MockCompilerOptions);
|
|
37
|
-
get mockData(): Record<string, MockOptions>;
|
|
38
|
-
run(): Promise<void>;
|
|
39
|
-
close(): void;
|
|
40
|
-
updateAlias(alias: Record<string, false | string | (string | false)[]>): void;
|
|
41
|
-
updateMockEntry(): Promise<void>;
|
|
42
|
-
getMockFiles(): Promise<string[]>;
|
|
43
|
-
watchMockFiles(): void;
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
interface ResolvedCompilerOptions {
|
|
47
|
-
alias: Record<string, false | string | (string | false)[]>;
|
|
48
|
-
proxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
49
|
-
wsProxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
50
|
-
plugins: RspackPluginInstance[];
|
|
51
|
-
context?: string;
|
|
52
|
-
}
|
|
53
|
-
type ResolvePluginOptions = Required<Omit<MockServerPluginOptions, 'build'>> & ResolvedCompilerOptions & {
|
|
54
|
-
logger: Logger;
|
|
55
|
-
build: false | ServerBuildOption;
|
|
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 MockCompilerOptions as M, type ResolvePluginOptions as R, type Server as S, MockCompiler as a, type MiddlewareOptions as b, createMockCompiler as c, createMockMiddleware as d, type Middleware as e, type MockSocketOptions as f, createLogger as g, logLevels as l, mockWebSocket as m };
|
package/dist/rsbuild.cjs
DELETED
|
@@ -1,202 +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
|
-
var _chunkM7F5AAOFcjs = require('./chunk-M7F5AAOF.cjs');
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
var _chunkHTVJXQRMcjs = require('./chunk-HTVJXQRM.cjs');
|
|
11
|
-
|
|
12
|
-
// src/rsbuild.ts
|
|
13
|
-
var _http = require('http');
|
|
14
|
-
var _path = require('path'); var _path2 = _interopRequireDefault(_path);
|
|
15
|
-
var _process = require('process'); var _process2 = _interopRequireDefault(_process);
|
|
16
|
-
var _utils = require('@pengzhanbo/utils');
|
|
17
|
-
var _core = require('@rspack/core'); var _core2 = _interopRequireDefault(_core);
|
|
18
|
-
var _picocolors = require('picocolors'); var _picocolors2 = _interopRequireDefault(_picocolors);
|
|
19
|
-
var _portfinder = require('portfinder');
|
|
20
|
-
function pluginMockServer(options = {}) {
|
|
21
|
-
return {
|
|
22
|
-
name: "plugin-mock-server",
|
|
23
|
-
setup(api) {
|
|
24
|
-
const rsbuildConfig = api.getRsbuildConfig();
|
|
25
|
-
const resolvedOptions = _chunkM7F5AAOFcjs.resolvePluginOptions.call(void 0, options, {
|
|
26
|
-
proxies: resolveConfigProxies(rsbuildConfig),
|
|
27
|
-
alias: {},
|
|
28
|
-
context: api.context.rootPath,
|
|
29
|
-
plugins: [new _core2.default.DefinePlugin(_optionalChain([rsbuildConfig, 'access', _ => _.source, 'optionalAccess', _2 => _2.define]) || {})]
|
|
30
|
-
});
|
|
31
|
-
if (_process2.default.env.NODE_ENV === "production") {
|
|
32
|
-
if (resolvedOptions.build) {
|
|
33
|
-
api.onAfterBuild(async () => {
|
|
34
|
-
const config = api.getNormalizedConfig();
|
|
35
|
-
await _chunkM7F5AAOFcjs.buildMockServer.call(void 0,
|
|
36
|
-
resolvedOptions,
|
|
37
|
-
_path2.default.resolve(_process2.default.cwd(), config.output.distPath.root || "dist")
|
|
38
|
-
);
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
return;
|
|
42
|
-
}
|
|
43
|
-
const mockCompiler = _chunkM7F5AAOFcjs.createMockCompiler.call(void 0, resolvedOptions);
|
|
44
|
-
api.modifyRsbuildConfig((config) => {
|
|
45
|
-
updateServerProxyConfigByHttpMock(config);
|
|
46
|
-
const mockMiddleware = _chunkM7F5AAOFcjs.createMockMiddleware.call(void 0, mockCompiler, resolvedOptions);
|
|
47
|
-
config.dev ??= {};
|
|
48
|
-
config.dev.setupMiddlewares ??= [];
|
|
49
|
-
config.dev.setupMiddlewares.push((middlewares, server2) => {
|
|
50
|
-
mockMiddleware(middlewares, () => server2.sockWrite("static-changed"));
|
|
51
|
-
});
|
|
52
|
-
});
|
|
53
|
-
let port = 3079;
|
|
54
|
-
const shouldMockWs = _utils.toArray.call(void 0, resolvedOptions.wsPrefix).length > 0;
|
|
55
|
-
if (shouldMockWs) {
|
|
56
|
-
api.modifyRsbuildConfig(async (config) => {
|
|
57
|
-
const defaultPort = (_optionalChain([config, 'access', _3 => _3.server, 'optionalAccess', _4 => _4.port]) || port) + 1;
|
|
58
|
-
port = await _portfinder.getPortPromise.call(void 0, { port: defaultPort });
|
|
59
|
-
updateServerProxyConfigByWSMock(config, options.wsPrefix || [], port);
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
let server;
|
|
63
|
-
function startMockServer() {
|
|
64
|
-
mockCompiler.run();
|
|
65
|
-
if (shouldMockWs) {
|
|
66
|
-
server = _http.createServer.call(void 0, );
|
|
67
|
-
_chunkHTVJXQRMcjs.mockWebSocket.call(void 0, mockCompiler, server, resolvedOptions);
|
|
68
|
-
server.listen(port);
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
function close() {
|
|
72
|
-
mockCompiler.close();
|
|
73
|
-
_optionalChain([server, 'optionalAccess', _5 => _5.close, 'call', _6 => _6()]);
|
|
74
|
-
}
|
|
75
|
-
api.onAfterCreateCompiler(({ compiler }) => {
|
|
76
|
-
if ("compilers" in compiler) {
|
|
77
|
-
compiler.compilers.forEach((compiler2) => {
|
|
78
|
-
mockCompiler.updateAlias(_optionalChain([compiler2, 'access', _7 => _7.options, 'access', _8 => _8.resolve, 'optionalAccess', _9 => _9.alias]) || {});
|
|
79
|
-
});
|
|
80
|
-
} else {
|
|
81
|
-
mockCompiler.updateAlias(_optionalChain([compiler, 'access', _10 => _10.options, 'access', _11 => _11.resolve, 'optionalAccess', _12 => _12.alias]) || {});
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
api.onAfterStartDevServer(startMockServer);
|
|
85
|
-
api.onAfterStartProdServer(startMockServer);
|
|
86
|
-
api.onExit(close);
|
|
87
|
-
}
|
|
88
|
-
};
|
|
89
|
-
}
|
|
90
|
-
function onProxyError(err, _req, res) {
|
|
91
|
-
console.error(_picocolors2.default.red(_optionalChain([err, 'optionalAccess', _13 => _13.stack]) || err.message));
|
|
92
|
-
res.statusCode = 500;
|
|
93
|
-
res.end();
|
|
94
|
-
}
|
|
95
|
-
function updateServerProxyConfigByHttpMock(config) {
|
|
96
|
-
if (!_optionalChain([config, 'access', _14 => _14.server, 'optionalAccess', _15 => _15.proxy]))
|
|
97
|
-
return;
|
|
98
|
-
if (_utils.isArray.call(void 0, config.server.proxy)) {
|
|
99
|
-
config.server.proxy = config.server.proxy.map((item) => {
|
|
100
|
-
if (typeof item !== "function" && !item.ws) {
|
|
101
|
-
const onProxyReq = item.onProxyReq;
|
|
102
|
-
const onError = item.onError;
|
|
103
|
-
return {
|
|
104
|
-
...item,
|
|
105
|
-
onError: onError || onProxyError,
|
|
106
|
-
onProxyReq: (proxyReq, req, ...args) => {
|
|
107
|
-
_optionalChain([onProxyReq, 'optionalCall', _16 => _16(proxyReq, req, ...args)]);
|
|
108
|
-
_chunkHTVJXQRMcjs.rewriteRequest.call(void 0, proxyReq, req);
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
return item;
|
|
113
|
-
});
|
|
114
|
-
} else if ("target" in config.server.proxy) {
|
|
115
|
-
const onProxyReq = config.server.proxy.onProxyReq;
|
|
116
|
-
config.server.proxy.onProxyReq = (proxyReq, req, ...args) => {
|
|
117
|
-
_optionalChain([onProxyReq, 'optionalCall', _17 => _17(proxyReq, req, ...args)]);
|
|
118
|
-
_chunkHTVJXQRMcjs.rewriteRequest.call(void 0, proxyReq, req);
|
|
119
|
-
};
|
|
120
|
-
config.server.proxy.onError ??= onProxyError;
|
|
121
|
-
} else if (config.server.proxy) {
|
|
122
|
-
const proxy = config.server.proxy;
|
|
123
|
-
Object.keys(proxy).forEach((key) => {
|
|
124
|
-
const target = proxy[key];
|
|
125
|
-
const options = typeof target === "string" ? { target } : target;
|
|
126
|
-
if (options.ws)
|
|
127
|
-
return;
|
|
128
|
-
const { onProxyReq, onError, ...rest } = options;
|
|
129
|
-
proxy[key] = {
|
|
130
|
-
...rest,
|
|
131
|
-
onProxyReq: (proxyReq, req, ...args) => {
|
|
132
|
-
_optionalChain([onProxyReq, 'optionalCall', _18 => _18(proxyReq, req, ...args)]);
|
|
133
|
-
_chunkHTVJXQRMcjs.rewriteRequest.call(void 0, proxyReq, req);
|
|
134
|
-
},
|
|
135
|
-
onError: onError || onProxyError
|
|
136
|
-
};
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
function updateServerProxyConfigByWSMock(config, wsPrefix, port) {
|
|
141
|
-
config.server ??= {};
|
|
142
|
-
const proxy = config.server.proxy ??= {};
|
|
143
|
-
const wsTarget = `ws://localhost:${port}`;
|
|
144
|
-
const prefix = _utils.toArray.call(void 0, wsPrefix);
|
|
145
|
-
const has = (context) => typeof context === "string" && prefix.includes(context);
|
|
146
|
-
const used = /* @__PURE__ */ new Set();
|
|
147
|
-
function updateProxy(item) {
|
|
148
|
-
if (_utils.isArray.call(void 0, item.context)) {
|
|
149
|
-
item.context = item.context.filter(has);
|
|
150
|
-
} else if (has(item.context)) {
|
|
151
|
-
used.add(item.context);
|
|
152
|
-
item.target = wsTarget;
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
if (_utils.isArray.call(void 0, proxy)) {
|
|
156
|
-
for (const item of proxy) {
|
|
157
|
-
if (typeof item !== "function" && item.context && item.ws) {
|
|
158
|
-
updateProxy(item);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
prefix.filter((context) => !used.has(context)).forEach((context) => proxy.push({ context, target: wsTarget }));
|
|
162
|
-
} else if ("target" in proxy) {
|
|
163
|
-
if (proxy.ws) {
|
|
164
|
-
updateProxy(proxy);
|
|
165
|
-
const list = config.server.proxy = [proxy];
|
|
166
|
-
prefix.filter((context) => !used.has(context)).forEach((context) => list.push({ context, target: wsTarget }));
|
|
167
|
-
}
|
|
168
|
-
} else {
|
|
169
|
-
Object.entries(proxy).forEach(([, opt]) => {
|
|
170
|
-
if (typeof opt !== "string" && opt.ws) {
|
|
171
|
-
updateProxy(opt);
|
|
172
|
-
}
|
|
173
|
-
});
|
|
174
|
-
prefix.filter((context) => !used.has(context)).forEach((context) => {
|
|
175
|
-
proxy[context] = { target: wsTarget, ws: true };
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
}
|
|
179
|
-
function resolveConfigProxies(config) {
|
|
180
|
-
config.server ??= {};
|
|
181
|
-
const proxy = config.server.proxy ??= {};
|
|
182
|
-
const proxies = [];
|
|
183
|
-
if (_utils.isArray.call(void 0, proxy)) {
|
|
184
|
-
for (const item of proxy) {
|
|
185
|
-
if (typeof item !== "function" && item.context && !item.ws) {
|
|
186
|
-
proxies.push(..._utils.toArray.call(void 0, item.context));
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
} else if ("target" in proxy) {
|
|
190
|
-
if (!proxy.ws)
|
|
191
|
-
proxies.push(..._utils.toArray.call(void 0, proxy.context));
|
|
192
|
-
} else {
|
|
193
|
-
Object.entries(proxy).forEach(([context, opt]) => {
|
|
194
|
-
if (typeof opt === "string" || !opt.ws)
|
|
195
|
-
proxies.push(context);
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
return proxies;
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
exports.pluginMockServer = pluginMockServer;
|
package/dist/rsbuild.d.cts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { RsbuildPlugin } from '@rsbuild/core';
|
|
2
|
-
import { c as MockServerPluginOptions } from './types-Aw0AciTG.cjs';
|
|
3
|
-
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';
|
|
4
|
-
import 'co-body';
|
|
5
|
-
import 'cookies';
|
|
6
|
-
import 'cors';
|
|
7
|
-
import 'formidable';
|
|
8
|
-
import 'node:buffer';
|
|
9
|
-
import 'node:http';
|
|
10
|
-
import 'node:stream';
|
|
11
|
-
import 'ws';
|
|
12
|
-
|
|
13
|
-
declare function pluginMockServer(options?: MockServerPluginOptions): RsbuildPlugin;
|
|
14
|
-
|
|
15
|
-
export { MockServerPluginOptions, pluginMockServer };
|
package/dist/rsbuild.d.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { RsbuildPlugin } from '@rsbuild/core';
|
|
2
|
-
import { c as MockServerPluginOptions } from './types-Aw0AciTG.js';
|
|
3
|
-
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';
|
|
4
|
-
import 'co-body';
|
|
5
|
-
import 'cookies';
|
|
6
|
-
import 'cors';
|
|
7
|
-
import 'formidable';
|
|
8
|
-
import 'node:buffer';
|
|
9
|
-
import 'node:http';
|
|
10
|
-
import 'node:stream';
|
|
11
|
-
import 'ws';
|
|
12
|
-
|
|
13
|
-
declare function pluginMockServer(options?: MockServerPluginOptions): RsbuildPlugin;
|
|
14
|
-
|
|
15
|
-
export { MockServerPluginOptions, pluginMockServer };
|