rspack-plugin-mock 0.1.0 → 0.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 +221 -19
- package/README.zh-CN.md +888 -0
- package/dist/{chunk-G53QRHGV.cjs → chunk-I54ZNZWL.cjs} +942 -607
- package/dist/chunk-OYBMX3GQ.cjs +97 -0
- package/dist/chunk-P5FOCSCE.js +97 -0
- package/dist/{chunk-5MGZAMDI.js → chunk-YSJVV4SH.js} +1104 -769
- package/dist/helper.d.cts +2 -2
- package/dist/helper.d.ts +2 -2
- package/dist/index.cjs +17 -3
- package/dist/index.d.cts +71 -4
- package/dist/index.d.ts +71 -4
- package/dist/index.js +21 -7
- package/dist/rsbuild.cjs +172 -93
- package/dist/rsbuild.d.cts +1 -1
- package/dist/rsbuild.d.ts +1 -1
- package/dist/rsbuild.js +173 -94
- package/dist/rspack-BB-Jtq4f.d.cts +32 -0
- package/dist/rspack-h3uerEgg.d.ts +32 -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 +11 -9
- package/dist/chunk-4BGDHRTO.cjs +0 -68
- package/dist/chunk-B56QNVSS.js +0 -68
- package/dist/rspack-BI-Ifj4a.d.cts +0 -28
- package/dist/rspack-Db7drzDm.d.ts +0 -28
|
@@ -0,0 +1,97 @@
|
|
|
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
|
+
|
|
9
|
+
var _chunkI54ZNZWLcjs = require('./chunk-I54ZNZWL.cjs');
|
|
10
|
+
|
|
11
|
+
// src/rspack.ts
|
|
12
|
+
var _process = require('process'); var _process2 = _interopRequireDefault(_process);
|
|
13
|
+
var _path = require('path'); var _path2 = _interopRequireDefault(_path);
|
|
14
|
+
var _core = require('@rspack/core'); var _core2 = _interopRequireDefault(_core);
|
|
15
|
+
var _utils = require('@pengzhanbo/utils');
|
|
16
|
+
var PLUGIN_NAME = "rspack-plugin-mock";
|
|
17
|
+
var MockServerPlugin = class {
|
|
18
|
+
constructor(options = {}) {
|
|
19
|
+
this.options = options;
|
|
20
|
+
}
|
|
21
|
+
apply(compiler) {
|
|
22
|
+
const compilerOptions = compiler.options;
|
|
23
|
+
const options = resolvePluginOptions2(compiler, this.options);
|
|
24
|
+
if (_process2.default.env.NODE_ENV !== "production") {
|
|
25
|
+
const mockCompiler = _chunkI54ZNZWLcjs.createMockCompiler.call(void 0, options);
|
|
26
|
+
const mockMiddleware = _chunkI54ZNZWLcjs.createMockMiddleware.call(void 0, mockCompiler, options);
|
|
27
|
+
const setupMiddlewares = _optionalChain([compilerOptions, 'access', _ => _.devServer, 'optionalAccess', _2 => _2.setupMiddlewares]);
|
|
28
|
+
const waitServer = _chunkI54ZNZWLcjs.waitingFor.call(void 0, (server) => {
|
|
29
|
+
_chunkI54ZNZWLcjs.mockWebSocket.call(void 0, mockCompiler, server, options);
|
|
30
|
+
});
|
|
31
|
+
compilerOptions.devServer = {
|
|
32
|
+
...compilerOptions.devServer,
|
|
33
|
+
setupMiddlewares: (middlewares, devServer) => {
|
|
34
|
+
middlewares = _optionalChain([setupMiddlewares, 'optionalCall', _3 => _3(middlewares, devServer)]) || middlewares;
|
|
35
|
+
const reload = () => {
|
|
36
|
+
if (_optionalChain([devServer, 'access', _4 => _4.webSocketServer, 'optionalAccess', _5 => _5.clients]))
|
|
37
|
+
devServer.sendMessage(devServer.webSocketServer.clients, "static-changed");
|
|
38
|
+
};
|
|
39
|
+
middlewares = mockMiddleware(middlewares, reload) || middlewares;
|
|
40
|
+
waitServer(() => devServer.server);
|
|
41
|
+
return middlewares;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const proxy = _optionalChain([compilerOptions, 'access', _6 => _6.devServer, 'optionalAccess', _7 => _7.proxy]) || [];
|
|
45
|
+
const wsPrefix = _utils.toArray.call(void 0, options.wsPrefix);
|
|
46
|
+
if (proxy.length) {
|
|
47
|
+
compilerOptions.devServer.proxy = proxy.map((item) => {
|
|
48
|
+
if (typeof item !== "function" && !item.ws) {
|
|
49
|
+
const onProxyReq = item.onProxyReq;
|
|
50
|
+
item.onProxyReq = (proxyReq, req, ...args) => {
|
|
51
|
+
_optionalChain([onProxyReq, 'optionalCall', _8 => _8(proxyReq, req, ...args)]);
|
|
52
|
+
_chunkI54ZNZWLcjs.rewriteRequest.call(void 0, proxyReq, req);
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return item;
|
|
56
|
+
}).filter((item) => {
|
|
57
|
+
if (typeof item !== "function" && item.ws === true && wsPrefix.length) {
|
|
58
|
+
return !_utils.toArray.call(void 0, item.context).filter(_utils.isString).some((context) => wsPrefix.includes(context));
|
|
59
|
+
}
|
|
60
|
+
return true;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
compiler.hooks.watchRun.tap(PLUGIN_NAME, () => mockCompiler.run());
|
|
64
|
+
compiler.hooks.watchClose.tap(PLUGIN_NAME, () => mockCompiler.close());
|
|
65
|
+
} else if (options.build !== false) {
|
|
66
|
+
compiler.hooks.afterEmit.tap(PLUGIN_NAME, () => _chunkI54ZNZWLcjs.buildMockServer.call(void 0,
|
|
67
|
+
options,
|
|
68
|
+
compilerOptions.output.path || _path2.default.resolve(_process2.default.cwd(), "dist")
|
|
69
|
+
));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
function resolvePluginOptions2(compiler, options) {
|
|
74
|
+
const compilerOptions = compiler.options;
|
|
75
|
+
const alias = _optionalChain([compilerOptions, 'access', _9 => _9.resolve, 'optionalAccess', _10 => _10.alias]) || {};
|
|
76
|
+
const context = compilerOptions.context;
|
|
77
|
+
const definePluginInstance = _optionalChain([compilerOptions, 'access', _11 => _11.plugins, 'optionalAccess', _12 => _12.find, 'call', _13 => _13(
|
|
78
|
+
(plugin) => plugin instanceof _core2.default.DefinePlugin
|
|
79
|
+
)]);
|
|
80
|
+
const proxies = (_optionalChain([compilerOptions, 'access', _14 => _14.devServer, 'optionalAccess', _15 => _15.proxy]) || []).flatMap((item) => {
|
|
81
|
+
if (typeof item !== "function" && item.context && !item.ws) {
|
|
82
|
+
return item.context;
|
|
83
|
+
}
|
|
84
|
+
return [];
|
|
85
|
+
});
|
|
86
|
+
return _chunkI54ZNZWLcjs.resolvePluginOptions.call(void 0, options, {
|
|
87
|
+
alias,
|
|
88
|
+
context,
|
|
89
|
+
plugins: _utils.toArray.call(void 0, definePluginInstance),
|
|
90
|
+
proxies
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
exports.MockServerPlugin = MockServerPlugin; exports.resolvePluginOptions = resolvePluginOptions2;
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import {
|
|
2
|
+
buildMockServer,
|
|
3
|
+
createMockCompiler,
|
|
4
|
+
createMockMiddleware,
|
|
5
|
+
mockWebSocket,
|
|
6
|
+
resolvePluginOptions,
|
|
7
|
+
rewriteRequest,
|
|
8
|
+
waitingFor
|
|
9
|
+
} from "./chunk-YSJVV4SH.js";
|
|
10
|
+
|
|
11
|
+
// src/rspack.ts
|
|
12
|
+
import process from "process";
|
|
13
|
+
import path from "path";
|
|
14
|
+
import rspack from "@rspack/core";
|
|
15
|
+
import { isString, toArray } from "@pengzhanbo/utils";
|
|
16
|
+
var PLUGIN_NAME = "rspack-plugin-mock";
|
|
17
|
+
var MockServerPlugin = class {
|
|
18
|
+
constructor(options = {}) {
|
|
19
|
+
this.options = options;
|
|
20
|
+
}
|
|
21
|
+
apply(compiler) {
|
|
22
|
+
const compilerOptions = compiler.options;
|
|
23
|
+
const options = resolvePluginOptions2(compiler, this.options);
|
|
24
|
+
if (process.env.NODE_ENV !== "production") {
|
|
25
|
+
const mockCompiler = createMockCompiler(options);
|
|
26
|
+
const mockMiddleware = createMockMiddleware(mockCompiler, options);
|
|
27
|
+
const setupMiddlewares = compilerOptions.devServer?.setupMiddlewares;
|
|
28
|
+
const waitServer = waitingFor((server) => {
|
|
29
|
+
mockWebSocket(mockCompiler, server, options);
|
|
30
|
+
});
|
|
31
|
+
compilerOptions.devServer = {
|
|
32
|
+
...compilerOptions.devServer,
|
|
33
|
+
setupMiddlewares: (middlewares, devServer) => {
|
|
34
|
+
middlewares = setupMiddlewares?.(middlewares, devServer) || middlewares;
|
|
35
|
+
const reload = () => {
|
|
36
|
+
if (devServer.webSocketServer?.clients)
|
|
37
|
+
devServer.sendMessage(devServer.webSocketServer.clients, "static-changed");
|
|
38
|
+
};
|
|
39
|
+
middlewares = mockMiddleware(middlewares, reload) || middlewares;
|
|
40
|
+
waitServer(() => devServer.server);
|
|
41
|
+
return middlewares;
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
const proxy = compilerOptions.devServer?.proxy || [];
|
|
45
|
+
const wsPrefix = toArray(options.wsPrefix);
|
|
46
|
+
if (proxy.length) {
|
|
47
|
+
compilerOptions.devServer.proxy = proxy.map((item) => {
|
|
48
|
+
if (typeof item !== "function" && !item.ws) {
|
|
49
|
+
const onProxyReq = item.onProxyReq;
|
|
50
|
+
item.onProxyReq = (proxyReq, req, ...args) => {
|
|
51
|
+
onProxyReq?.(proxyReq, req, ...args);
|
|
52
|
+
rewriteRequest(proxyReq, req);
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
return item;
|
|
56
|
+
}).filter((item) => {
|
|
57
|
+
if (typeof item !== "function" && item.ws === true && wsPrefix.length) {
|
|
58
|
+
return !toArray(item.context).filter(isString).some((context) => wsPrefix.includes(context));
|
|
59
|
+
}
|
|
60
|
+
return true;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
compiler.hooks.watchRun.tap(PLUGIN_NAME, () => mockCompiler.run());
|
|
64
|
+
compiler.hooks.watchClose.tap(PLUGIN_NAME, () => mockCompiler.close());
|
|
65
|
+
} else if (options.build !== false) {
|
|
66
|
+
compiler.hooks.afterEmit.tap(PLUGIN_NAME, () => buildMockServer(
|
|
67
|
+
options,
|
|
68
|
+
compilerOptions.output.path || path.resolve(process.cwd(), "dist")
|
|
69
|
+
));
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
function resolvePluginOptions2(compiler, options) {
|
|
74
|
+
const compilerOptions = compiler.options;
|
|
75
|
+
const alias = compilerOptions.resolve?.alias || {};
|
|
76
|
+
const context = compilerOptions.context;
|
|
77
|
+
const definePluginInstance = compilerOptions.plugins?.find(
|
|
78
|
+
(plugin) => plugin instanceof rspack.DefinePlugin
|
|
79
|
+
);
|
|
80
|
+
const proxies = (compilerOptions.devServer?.proxy || []).flatMap((item) => {
|
|
81
|
+
if (typeof item !== "function" && item.context && !item.ws) {
|
|
82
|
+
return item.context;
|
|
83
|
+
}
|
|
84
|
+
return [];
|
|
85
|
+
});
|
|
86
|
+
return resolvePluginOptions(options, {
|
|
87
|
+
alias,
|
|
88
|
+
context,
|
|
89
|
+
plugins: toArray(definePluginInstance),
|
|
90
|
+
proxies
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export {
|
|
95
|
+
MockServerPlugin,
|
|
96
|
+
resolvePluginOptions2 as resolvePluginOptions
|
|
97
|
+
};
|