rspack-plugin-mock 0.2.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.
- package/README.md +110 -15
- package/README.zh-CN.md +888 -0
- package/dist/chunk-4GMRSIIN.cjs +543 -0
- package/dist/chunk-B5W3PWCX.js +543 -0
- package/dist/{chunk-UJAKORAH.js → chunk-EY46RSAC.js} +90 -433
- package/dist/{chunk-2S4KCTKW.cjs → chunk-ZEC4FWWY.cjs} +90 -433
- package/dist/helper.cjs +16 -5
- package/dist/helper.d.cts +2 -2
- package/dist/helper.d.ts +2 -2
- package/dist/helper.js +15 -4
- package/dist/index.cjs +88 -5
- package/dist/index.d.cts +13 -49
- package/dist/index.d.ts +13 -49
- package/dist/index.js +95 -12
- package/dist/mockWebsocket-CPuTAvL0.d.ts +76 -0
- package/dist/mockWebsocket-Dc9CZBfv.d.cts +76 -0
- package/dist/rsbuild.cjs +87 -64
- package/dist/rsbuild.d.cts +1 -1
- package/dist/rsbuild.d.ts +1 -1
- package/dist/rsbuild.js +79 -56
- package/dist/server.cjs +18 -0
- package/dist/server.d.cts +31 -0
- package/dist/server.d.ts +31 -0
- package/dist/server.js +18 -0
- package/dist/{types-C770q3L0.d.cts → types-BgpcN3jm.d.cts} +1 -1
- package/dist/{types-C770q3L0.d.ts → types-BgpcN3jm.d.ts} +1 -1
- package/package.json +15 -14
- package/dist/chunk-HTOWPFQ7.js +0 -20
- package/dist/chunk-KE2LVHUP.cjs +0 -20
- package/dist/chunk-P6DRAHHI.js +0 -90
- package/dist/chunk-T4AI3L6R.cjs +0 -90
- package/dist/rspack-BcdszmoI.d.cts +0 -29
- package/dist/rspack-E_yBzR5v.d.ts +0 -29
- package/dist/rspack.cjs +0 -9
- package/dist/rspack.d.cts +0 -11
- package/dist/rspack.d.ts +0 -11
- package/dist/rspack.js +0 -9
package/dist/chunk-HTOWPFQ7.js
DELETED
|
@@ -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
|
-
};
|
package/dist/chunk-KE2LVHUP.cjs
DELETED
|
@@ -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;
|
package/dist/chunk-P6DRAHHI.js
DELETED
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
createMockCompiler,
|
|
3
|
-
createMockMiddleware,
|
|
4
|
-
mockWebSocket,
|
|
5
|
-
resolvePluginOptions,
|
|
6
|
-
rewriteRequest,
|
|
7
|
-
waitingFor
|
|
8
|
-
} from "./chunk-UJAKORAH.js";
|
|
9
|
-
|
|
10
|
-
// src/rspack.ts
|
|
11
|
-
import process from "process";
|
|
12
|
-
import rspack from "@rspack/core";
|
|
13
|
-
import { isString, toArray } from "@pengzhanbo/utils";
|
|
14
|
-
var PLUGIN_NAME = "rspack-plugin-mock";
|
|
15
|
-
var MockServerPlugin = class {
|
|
16
|
-
constructor(options = {}) {
|
|
17
|
-
this.options = options;
|
|
18
|
-
}
|
|
19
|
-
apply(compiler) {
|
|
20
|
-
const compilerOptions = compiler.options;
|
|
21
|
-
if (process.env.NODE_ENV !== "production") {
|
|
22
|
-
const options = resolvePluginOptions2(compiler, this.options);
|
|
23
|
-
const mockCompiler = createMockCompiler(options);
|
|
24
|
-
const mockMiddleware = createMockMiddleware(mockCompiler, options);
|
|
25
|
-
const setupMiddlewares = compilerOptions.devServer?.setupMiddlewares;
|
|
26
|
-
const waitServer = waitingFor((server) => {
|
|
27
|
-
mockWebSocket(mockCompiler, server, options);
|
|
28
|
-
});
|
|
29
|
-
compilerOptions.devServer = {
|
|
30
|
-
...compilerOptions.devServer,
|
|
31
|
-
setupMiddlewares: (middlewares, devServer) => {
|
|
32
|
-
middlewares = setupMiddlewares?.(middlewares, devServer) || middlewares;
|
|
33
|
-
const reload = () => {
|
|
34
|
-
if (devServer.webSocketServer?.clients)
|
|
35
|
-
devServer.sendMessage(devServer.webSocketServer.clients, "static-changed");
|
|
36
|
-
};
|
|
37
|
-
middlewares = mockMiddleware(middlewares, reload) || middlewares;
|
|
38
|
-
waitServer(() => devServer.server);
|
|
39
|
-
return middlewares;
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
const proxy = compilerOptions.devServer?.proxy || [];
|
|
43
|
-
const wsPrefix = toArray(options.wsPrefix);
|
|
44
|
-
if (proxy.length) {
|
|
45
|
-
compilerOptions.devServer.proxy = proxy.map((item) => {
|
|
46
|
-
if (typeof item !== "function" && !item.ws) {
|
|
47
|
-
const onProxyReq = item.onProxyReq;
|
|
48
|
-
item.onProxyReq = (proxyReq, req, ...args) => {
|
|
49
|
-
onProxyReq?.(proxyReq, req, ...args);
|
|
50
|
-
rewriteRequest(proxyReq, req);
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
return item;
|
|
54
|
-
}).filter((item) => {
|
|
55
|
-
if (typeof item !== "function" && item.ws === true && wsPrefix.length) {
|
|
56
|
-
return !toArray(item.context).filter(isString).some((context) => wsPrefix.includes(context));
|
|
57
|
-
}
|
|
58
|
-
return true;
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
compiler.hooks.watchRun.tap(PLUGIN_NAME, () => mockCompiler.run());
|
|
62
|
-
compiler.hooks.watchClose.tap(PLUGIN_NAME, () => mockCompiler.close());
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
function resolvePluginOptions2(compiler, options) {
|
|
67
|
-
const compilerOptions = compiler.options;
|
|
68
|
-
const alias = compilerOptions.resolve?.alias || {};
|
|
69
|
-
const context = compilerOptions.context;
|
|
70
|
-
const definePluginInstance = compilerOptions.plugins?.find(
|
|
71
|
-
(plugin) => plugin instanceof rspack.DefinePlugin
|
|
72
|
-
);
|
|
73
|
-
const proxies = (compilerOptions.devServer?.proxy || []).flatMap((item) => {
|
|
74
|
-
if (typeof item !== "function" && item.context && !item.ws) {
|
|
75
|
-
return item.context;
|
|
76
|
-
}
|
|
77
|
-
return [];
|
|
78
|
-
});
|
|
79
|
-
return resolvePluginOptions(options, {
|
|
80
|
-
alias,
|
|
81
|
-
context,
|
|
82
|
-
plugins: toArray(definePluginInstance),
|
|
83
|
-
proxies
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
export {
|
|
88
|
-
MockServerPlugin,
|
|
89
|
-
resolvePluginOptions2 as resolvePluginOptions
|
|
90
|
-
};
|
package/dist/chunk-T4AI3L6R.cjs
DELETED
|
@@ -1,90 +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
|
-
var _chunk2S4KCTKWcjs = require('./chunk-2S4KCTKW.cjs');
|
|
9
|
-
|
|
10
|
-
// src/rspack.ts
|
|
11
|
-
var _process = require('process'); var _process2 = _interopRequireDefault(_process);
|
|
12
|
-
var _core = require('@rspack/core'); var _core2 = _interopRequireDefault(_core);
|
|
13
|
-
var _utils = require('@pengzhanbo/utils');
|
|
14
|
-
var PLUGIN_NAME = "rspack-plugin-mock";
|
|
15
|
-
var MockServerPlugin = class {
|
|
16
|
-
constructor(options = {}) {
|
|
17
|
-
this.options = options;
|
|
18
|
-
}
|
|
19
|
-
apply(compiler) {
|
|
20
|
-
const compilerOptions = compiler.options;
|
|
21
|
-
if (_process2.default.env.NODE_ENV !== "production") {
|
|
22
|
-
const options = resolvePluginOptions2(compiler, this.options);
|
|
23
|
-
const mockCompiler = _chunk2S4KCTKWcjs.createMockCompiler.call(void 0, options);
|
|
24
|
-
const mockMiddleware = _chunk2S4KCTKWcjs.createMockMiddleware.call(void 0, mockCompiler, options);
|
|
25
|
-
const setupMiddlewares = _optionalChain([compilerOptions, 'access', _ => _.devServer, 'optionalAccess', _2 => _2.setupMiddlewares]);
|
|
26
|
-
const waitServer = _chunk2S4KCTKWcjs.waitingFor.call(void 0, (server) => {
|
|
27
|
-
_chunk2S4KCTKWcjs.mockWebSocket.call(void 0, mockCompiler, server, options);
|
|
28
|
-
});
|
|
29
|
-
compilerOptions.devServer = {
|
|
30
|
-
...compilerOptions.devServer,
|
|
31
|
-
setupMiddlewares: (middlewares, devServer) => {
|
|
32
|
-
middlewares = _optionalChain([setupMiddlewares, 'optionalCall', _3 => _3(middlewares, devServer)]) || middlewares;
|
|
33
|
-
const reload = () => {
|
|
34
|
-
if (_optionalChain([devServer, 'access', _4 => _4.webSocketServer, 'optionalAccess', _5 => _5.clients]))
|
|
35
|
-
devServer.sendMessage(devServer.webSocketServer.clients, "static-changed");
|
|
36
|
-
};
|
|
37
|
-
middlewares = mockMiddleware(middlewares, reload) || middlewares;
|
|
38
|
-
waitServer(() => devServer.server);
|
|
39
|
-
return middlewares;
|
|
40
|
-
}
|
|
41
|
-
};
|
|
42
|
-
const proxy = _optionalChain([compilerOptions, 'access', _6 => _6.devServer, 'optionalAccess', _7 => _7.proxy]) || [];
|
|
43
|
-
const wsPrefix = _utils.toArray.call(void 0, options.wsPrefix);
|
|
44
|
-
if (proxy.length) {
|
|
45
|
-
compilerOptions.devServer.proxy = proxy.map((item) => {
|
|
46
|
-
if (typeof item !== "function" && !item.ws) {
|
|
47
|
-
const onProxyReq = item.onProxyReq;
|
|
48
|
-
item.onProxyReq = (proxyReq, req, ...args) => {
|
|
49
|
-
_optionalChain([onProxyReq, 'optionalCall', _8 => _8(proxyReq, req, ...args)]);
|
|
50
|
-
_chunk2S4KCTKWcjs.rewriteRequest.call(void 0, proxyReq, req);
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
return item;
|
|
54
|
-
}).filter((item) => {
|
|
55
|
-
if (typeof item !== "function" && item.ws === true && wsPrefix.length) {
|
|
56
|
-
return !_utils.toArray.call(void 0, item.context).filter(_utils.isString).some((context) => wsPrefix.includes(context));
|
|
57
|
-
}
|
|
58
|
-
return true;
|
|
59
|
-
});
|
|
60
|
-
}
|
|
61
|
-
compiler.hooks.watchRun.tap(PLUGIN_NAME, () => mockCompiler.run());
|
|
62
|
-
compiler.hooks.watchClose.tap(PLUGIN_NAME, () => mockCompiler.close());
|
|
63
|
-
}
|
|
64
|
-
}
|
|
65
|
-
};
|
|
66
|
-
function resolvePluginOptions2(compiler, options) {
|
|
67
|
-
const compilerOptions = compiler.options;
|
|
68
|
-
const alias = _optionalChain([compilerOptions, 'access', _9 => _9.resolve, 'optionalAccess', _10 => _10.alias]) || {};
|
|
69
|
-
const context = compilerOptions.context;
|
|
70
|
-
const definePluginInstance = _optionalChain([compilerOptions, 'access', _11 => _11.plugins, 'optionalAccess', _12 => _12.find, 'call', _13 => _13(
|
|
71
|
-
(plugin) => plugin instanceof _core2.default.DefinePlugin
|
|
72
|
-
)]);
|
|
73
|
-
const proxies = (_optionalChain([compilerOptions, 'access', _14 => _14.devServer, 'optionalAccess', _15 => _15.proxy]) || []).flatMap((item) => {
|
|
74
|
-
if (typeof item !== "function" && item.context && !item.ws) {
|
|
75
|
-
return item.context;
|
|
76
|
-
}
|
|
77
|
-
return [];
|
|
78
|
-
});
|
|
79
|
-
return _chunk2S4KCTKWcjs.resolvePluginOptions.call(void 0, options, {
|
|
80
|
-
alias,
|
|
81
|
-
context,
|
|
82
|
-
plugins: _utils.toArray.call(void 0, definePluginInstance),
|
|
83
|
-
proxies
|
|
84
|
-
});
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
exports.MockServerPlugin = MockServerPlugin; exports.resolvePluginOptions = resolvePluginOptions2;
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { RspackPluginInstance, Compiler } from '@rspack/core';
|
|
2
|
-
import { L as LogLevel, M as MockServerPluginOptions } 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
|
-
|
|
11
|
-
interface ResolvedCompilerOptions {
|
|
12
|
-
alias: Record<string, false | string | (string | false)[]>;
|
|
13
|
-
proxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
14
|
-
wsProxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
15
|
-
plugins: RspackPluginInstance[];
|
|
16
|
-
context?: string;
|
|
17
|
-
}
|
|
18
|
-
type ResolvePluginOptions = Required<MockServerPluginOptions> & ResolvedCompilerOptions & {
|
|
19
|
-
logger: Logger;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
declare class MockServerPlugin implements RspackPluginInstance {
|
|
23
|
-
options: MockServerPluginOptions;
|
|
24
|
-
constructor(options?: MockServerPluginOptions);
|
|
25
|
-
apply(compiler: Compiler): void;
|
|
26
|
-
}
|
|
27
|
-
declare function resolvePluginOptions(compiler: Compiler, options: MockServerPluginOptions): ResolvePluginOptions;
|
|
28
|
-
|
|
29
|
-
export { MockServerPlugin as M, type ResolvePluginOptions as R, resolvePluginOptions as r };
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { RspackPluginInstance, Compiler } from '@rspack/core';
|
|
2
|
-
import { L as LogLevel, M as MockServerPluginOptions } 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
|
-
|
|
11
|
-
interface ResolvedCompilerOptions {
|
|
12
|
-
alias: Record<string, false | string | (string | false)[]>;
|
|
13
|
-
proxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
14
|
-
wsProxies: (string | ((pathname: string, req: any) => boolean))[];
|
|
15
|
-
plugins: RspackPluginInstance[];
|
|
16
|
-
context?: string;
|
|
17
|
-
}
|
|
18
|
-
type ResolvePluginOptions = Required<MockServerPluginOptions> & ResolvedCompilerOptions & {
|
|
19
|
-
logger: Logger;
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
declare class MockServerPlugin implements RspackPluginInstance {
|
|
23
|
-
options: MockServerPluginOptions;
|
|
24
|
-
constructor(options?: MockServerPluginOptions);
|
|
25
|
-
apply(compiler: Compiler): void;
|
|
26
|
-
}
|
|
27
|
-
declare function resolvePluginOptions(compiler: Compiler, options: MockServerPluginOptions): ResolvePluginOptions;
|
|
28
|
-
|
|
29
|
-
export { MockServerPlugin as M, type ResolvePluginOptions as R, 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 _chunkT4AI3L6Rcjs = require('./chunk-T4AI3L6R.cjs');
|
|
5
|
-
require('./chunk-2S4KCTKW.cjs');
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
exports.MockServerPlugin = _chunkT4AI3L6Rcjs.MockServerPlugin; exports.resolvePluginOptions = _chunkT4AI3L6Rcjs.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-BcdszmoI.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';
|
package/dist/rspack.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import '@rspack/core';
|
|
2
|
-
import './types-C770q3L0.js';
|
|
3
|
-
export { M as MockServerPlugin, r as resolvePluginOptions } from './rspack-E_yBzR5v.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';
|