vite-plugin-mock-dev-server 1.4.1 → 1.4.3
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 +5 -0
- package/README.zh-CN.md +6 -0
- package/dist/index.cjs +74 -56
- package/dist/index.d.cts +7 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.js +45 -27
- package/package.json +2 -1
package/README.md
CHANGED
package/README.zh-CN.md
CHANGED
package/dist/index.cjs
CHANGED
|
@@ -6,8 +6,8 @@ var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
|
6
6
|
var __getProtoOf = Object.getPrototypeOf;
|
|
7
7
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
8
|
var __export = (target, all) => {
|
|
9
|
-
for (var
|
|
10
|
-
__defProp(target,
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
11
|
};
|
|
12
12
|
var __copyProps = (to, from, except, desc) => {
|
|
13
13
|
if (from && typeof from === "object" || typeof from === "function") {
|
|
@@ -59,11 +59,8 @@ var import_node_process2 = __toESM(require("process"), 1);
|
|
|
59
59
|
var import_utils3 = require("@pengzhanbo/utils");
|
|
60
60
|
var import_fast_glob = __toESM(require("fast-glob"), 1);
|
|
61
61
|
var import_is_core_module = __toESM(require("is-core-module"), 1);
|
|
62
|
-
var
|
|
63
|
-
|
|
64
|
-
// package.json
|
|
65
|
-
var name = "vite-plugin-mock-dev-server";
|
|
66
|
-
var version = "1.4.1";
|
|
62
|
+
var import_pluginutils = require("@rollup/pluginutils");
|
|
63
|
+
var import_picocolors3 = __toESM(require("picocolors"), 1);
|
|
67
64
|
|
|
68
65
|
// src/compiler.ts
|
|
69
66
|
var import_node_fs2 = __toESM(require("fs"), 1);
|
|
@@ -78,6 +75,7 @@ var import_node_fs = __toESM(require("fs"), 1);
|
|
|
78
75
|
var import_node_path = __toESM(require("path"), 1);
|
|
79
76
|
var import_node_querystring = require("querystring");
|
|
80
77
|
var import_node_url = require("url");
|
|
78
|
+
var import_node_os = __toESM(require("os"), 1);
|
|
81
79
|
var import_debug = __toESM(require("debug"), 1);
|
|
82
80
|
var import_path_to_regexp = require("path-to-regexp");
|
|
83
81
|
function isStream(stream) {
|
|
@@ -131,6 +129,14 @@ function urlParse(input) {
|
|
|
131
129
|
const query = (0, import_node_querystring.parse)(url.search.replace(/^\?/, ""));
|
|
132
130
|
return { pathname, query };
|
|
133
131
|
}
|
|
132
|
+
var windowsSlashRE = /\\/g;
|
|
133
|
+
var isWindows = import_node_os.default.platform() === "win32";
|
|
134
|
+
function slash(p) {
|
|
135
|
+
return p.replace(windowsSlashRE, "/");
|
|
136
|
+
}
|
|
137
|
+
function normalizePath(id) {
|
|
138
|
+
return import_node_path.default.posix.normalize(isWindows ? slash(id) : id);
|
|
139
|
+
}
|
|
134
140
|
|
|
135
141
|
// src/compiler.ts
|
|
136
142
|
var externalizeDeps = {
|
|
@@ -401,7 +407,7 @@ async function generateMockServer(ctx, config, options) {
|
|
|
401
407
|
wsProxies,
|
|
402
408
|
options.cookiesOptions,
|
|
403
409
|
options.priority,
|
|
404
|
-
options.build
|
|
410
|
+
options.build
|
|
405
411
|
)
|
|
406
412
|
},
|
|
407
413
|
{
|
|
@@ -411,10 +417,19 @@ async function generateMockServer(ctx, config, options) {
|
|
|
411
417
|
];
|
|
412
418
|
try {
|
|
413
419
|
if (import_node_path3.default.isAbsolute(outputDir)) {
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
420
|
+
for (const { filename } of outputList) {
|
|
421
|
+
if (import_node_fs3.default.existsSync(filename))
|
|
422
|
+
await import_promises.default.rm(filename);
|
|
423
|
+
}
|
|
424
|
+
config.logger.info(`${import_picocolors3.default.green("\u2713")} generate mock server in ${import_picocolors3.default.cyan(outputDir)}`);
|
|
425
|
+
for (const { filename, source } of outputList) {
|
|
426
|
+
import_node_fs3.default.mkdirSync(import_node_path3.default.dirname(filename), { recursive: true });
|
|
417
427
|
await import_promises.default.writeFile(filename, source, "utf-8");
|
|
428
|
+
const sourceSize = (source.length / 1024).toFixed(2);
|
|
429
|
+
const name = import_node_path3.default.relative(outputDir, filename);
|
|
430
|
+
const space = name.length < 30 ? " ".repeat(30 - name.length) : "";
|
|
431
|
+
config.logger.info(` ${import_picocolors3.default.green(name)}${space}${import_picocolors3.default.bold(import_picocolors3.default.dim(`${sourceSize} kB`))}`);
|
|
432
|
+
}
|
|
418
433
|
} else {
|
|
419
434
|
for (const { filename, source } of outputList) {
|
|
420
435
|
ctx.emitFile({
|
|
@@ -424,12 +439,13 @@ async function generateMockServer(ctx, config, options) {
|
|
|
424
439
|
});
|
|
425
440
|
}
|
|
426
441
|
}
|
|
427
|
-
} catch {
|
|
442
|
+
} catch (e) {
|
|
443
|
+
console.error(e);
|
|
428
444
|
}
|
|
429
445
|
}
|
|
430
446
|
function getMockDependencies(deps, alias) {
|
|
431
447
|
const list = /* @__PURE__ */ new Set();
|
|
432
|
-
const excludeDeps = [
|
|
448
|
+
const excludeDeps = ["vite-plugin-mock-dev-server", "connect", "cors"];
|
|
433
449
|
const isAlias = (p) => alias.find(({ find }) => aliasMatches(find, p));
|
|
434
450
|
Object.keys(deps).forEach((mPath) => {
|
|
435
451
|
const imports = deps[mPath].imports.filter((_) => _.external && !_.path.startsWith("<define:") && !isAlias(_.path)).map((_) => _.path);
|
|
@@ -451,7 +467,7 @@ function generatePackageJson(pkg, mockDeps) {
|
|
|
451
467
|
},
|
|
452
468
|
dependencies: {
|
|
453
469
|
"connect": "^3.7.0",
|
|
454
|
-
"vite-plugin-mock-dev-server": `^${
|
|
470
|
+
"vite-plugin-mock-dev-server": `^${"1.4.3"}`,
|
|
455
471
|
"cors": "^2.8.5"
|
|
456
472
|
},
|
|
457
473
|
pnpm: { peerDependencyRules: { ignoreMissing: ["vite"] } }
|
|
@@ -461,16 +477,17 @@ function generatePackageJson(pkg, mockDeps) {
|
|
|
461
477
|
});
|
|
462
478
|
return JSON.stringify(mockPkg, null, 2);
|
|
463
479
|
}
|
|
464
|
-
function generatorServerEntryCode(httpProxies, wsProxies, cookiesOptions = {}, priority = {},
|
|
480
|
+
function generatorServerEntryCode(httpProxies, wsProxies, cookiesOptions = {}, priority = {}, build2) {
|
|
481
|
+
const { serverPort, log } = build2;
|
|
465
482
|
return `import { createServer } from 'node:http';
|
|
466
483
|
import connect from 'connect';
|
|
467
484
|
import corsMiddleware from 'cors';
|
|
468
|
-
import { baseMiddleware,
|
|
485
|
+
import { baseMiddleware, createLogger, mockWebSocket } from 'vite-plugin-mock-dev-server';
|
|
469
486
|
import mockData from './mock-data.js';
|
|
470
487
|
|
|
471
488
|
const app = connect();
|
|
472
489
|
const server = createServer(app);
|
|
473
|
-
const logger = createLogger('mock-server', '
|
|
490
|
+
const logger = createLogger('mock-server', '${log}');
|
|
474
491
|
const httpProxies = ${JSON.stringify(httpProxies)};
|
|
475
492
|
const wsProxies = ${JSON.stringify(wsProxies)};
|
|
476
493
|
const cookiesOptions = ${JSON.stringify(cookiesOptions)};
|
|
@@ -493,21 +510,21 @@ app.use(baseMiddleware({ mockData }, {
|
|
|
493
510
|
logger,
|
|
494
511
|
}));
|
|
495
512
|
|
|
496
|
-
server.listen(${
|
|
513
|
+
server.listen(${serverPort});
|
|
497
514
|
|
|
498
|
-
console.log('listen: http://localhost:${
|
|
515
|
+
console.log('listen: http://localhost:${serverPort}');
|
|
499
516
|
`;
|
|
500
517
|
}
|
|
501
518
|
async function generateMockEntryCode(cwd, include, exclude) {
|
|
502
519
|
const includePaths = await (0, import_fast_glob.default)(include, { cwd });
|
|
503
|
-
const includeFilter = (0,
|
|
520
|
+
const includeFilter = (0, import_pluginutils.createFilter)(include, exclude, {
|
|
504
521
|
resolve: false
|
|
505
522
|
});
|
|
506
523
|
const mockFiles = includePaths.filter(includeFilter);
|
|
507
524
|
let importers = "";
|
|
508
525
|
let exporters = "";
|
|
509
526
|
mockFiles.forEach((filepath, index) => {
|
|
510
|
-
const file =
|
|
527
|
+
const file = normalizePath(import_node_path3.default.join(cwd, filepath));
|
|
511
528
|
importers += `import * as m${index} from '${file}';
|
|
512
529
|
`;
|
|
513
530
|
exporters += `m${index}, `;
|
|
@@ -544,7 +561,7 @@ var import_cookies = __toESM(require("cookies"), 1);
|
|
|
544
561
|
var import_http_status = __toESM(require("http-status"), 1);
|
|
545
562
|
var mime = __toESM(require("mime-types"), 1);
|
|
546
563
|
var import_path_to_regexp3 = require("path-to-regexp");
|
|
547
|
-
var
|
|
564
|
+
var import_picocolors4 = __toESM(require("picocolors"), 1);
|
|
548
565
|
|
|
549
566
|
// src/matchingWeight.ts
|
|
550
567
|
var import_utils5 = require("@pengzhanbo/utils");
|
|
@@ -616,11 +633,11 @@ function defaultPriority(rules) {
|
|
|
616
633
|
pattern = "",
|
|
617
634
|
modifier,
|
|
618
635
|
prefix,
|
|
619
|
-
name
|
|
636
|
+
name
|
|
620
637
|
} = isDynamic ? token : {};
|
|
621
638
|
const isGlob = pattern && pattern.includes(".*");
|
|
622
639
|
const isSlash = prefix === "/";
|
|
623
|
-
const isNamed = (0, import_utils5.isString)(
|
|
640
|
+
const isNamed = (0, import_utils5.isString)(name);
|
|
624
641
|
exp += isDynamic && isSlash ? 1 : 0;
|
|
625
642
|
if (i === tokens.length - 1 && isGlob) {
|
|
626
643
|
weight += 5 * 10 ** (tokens.length === 1 ? highest + 1 : highest);
|
|
@@ -842,10 +859,10 @@ function baseMiddleware(mockLoader, {
|
|
|
842
859
|
}
|
|
843
860
|
if (!mock) {
|
|
844
861
|
const matched = mockUrls.map(
|
|
845
|
-
(m) => m === _mockUrl ?
|
|
862
|
+
(m) => m === _mockUrl ? import_picocolors4.default.underline(import_picocolors4.default.bold(m)) : import_picocolors4.default.dim(m)
|
|
846
863
|
).join(", ");
|
|
847
864
|
logger.warn(
|
|
848
|
-
`${
|
|
865
|
+
`${import_picocolors4.default.green(
|
|
849
866
|
pathname
|
|
850
867
|
)} matches ${matched} , but mock data is not found.`
|
|
851
868
|
);
|
|
@@ -874,10 +891,10 @@ function baseMiddleware(mockLoader, {
|
|
|
874
891
|
await provideCookies(request, response, mock, logger);
|
|
875
892
|
logger.info(requestLog(request, filepath), logLevel);
|
|
876
893
|
logger.debug(
|
|
877
|
-
`${
|
|
894
|
+
`${import_picocolors4.default.magenta("DEBUG")} ${import_picocolors4.default.underline(
|
|
878
895
|
pathname
|
|
879
896
|
)} matches: [ ${mockUrls.map(
|
|
880
|
-
(m) => m === _mockUrl ?
|
|
897
|
+
(m) => m === _mockUrl ? import_picocolors4.default.underline(import_picocolors4.default.bold(m)) : import_picocolors4.default.dim(m)
|
|
881
898
|
).join(", ")} ]
|
|
882
899
|
`
|
|
883
900
|
);
|
|
@@ -888,11 +905,11 @@ function baseMiddleware(mockLoader, {
|
|
|
888
905
|
sendData(response, content, type);
|
|
889
906
|
} catch (e) {
|
|
890
907
|
logger.error(
|
|
891
|
-
`${
|
|
908
|
+
`${import_picocolors4.default.red(
|
|
892
909
|
`mock error at ${pathname}`
|
|
893
910
|
)}
|
|
894
911
|
${e}
|
|
895
|
-
at body (${
|
|
912
|
+
at body (${import_picocolors4.default.underline(filepath)})`,
|
|
896
913
|
logLevel
|
|
897
914
|
);
|
|
898
915
|
responseStatus(response, 500);
|
|
@@ -906,11 +923,11 @@ ${e}
|
|
|
906
923
|
await responseFn(request, response, next);
|
|
907
924
|
} catch (e) {
|
|
908
925
|
logger.error(
|
|
909
|
-
`${
|
|
926
|
+
`${import_picocolors4.default.red(
|
|
910
927
|
`mock error at ${pathname}`
|
|
911
928
|
)}
|
|
912
929
|
${e}
|
|
913
|
-
at response (${
|
|
930
|
+
at response (${import_picocolors4.default.underline(filepath)})`,
|
|
914
931
|
logLevel
|
|
915
932
|
);
|
|
916
933
|
responseStatus(response, 500);
|
|
@@ -943,11 +960,11 @@ function fineMock(mockList, logger, {
|
|
|
943
960
|
} catch (e) {
|
|
944
961
|
const file = mock.__filepath__;
|
|
945
962
|
logger.error(
|
|
946
|
-
`${
|
|
963
|
+
`${import_picocolors4.default.red(
|
|
947
964
|
`mock error at ${pathname}`
|
|
948
965
|
)}
|
|
949
966
|
${e}
|
|
950
|
-
at validator (${
|
|
967
|
+
at validator (${import_picocolors4.default.underline(file)})`,
|
|
951
968
|
mock.log
|
|
952
969
|
);
|
|
953
970
|
return false;
|
|
@@ -968,7 +985,7 @@ async function provideHeaders(req, res, mock, logger) {
|
|
|
968
985
|
contentType2 && res.setHeader("Content-Type", contentType2);
|
|
969
986
|
res.setHeader("Cache-Control", "no-cache,max-age=0");
|
|
970
987
|
res.setHeader("X-Mock-Power-By", "vite-plugin-mock-dev-server");
|
|
971
|
-
res.setHeader("X-File-Path", filepath);
|
|
988
|
+
filepath && res.setHeader("X-File-Path", filepath);
|
|
972
989
|
if (!headers)
|
|
973
990
|
return;
|
|
974
991
|
try {
|
|
@@ -978,11 +995,11 @@ async function provideHeaders(req, res, mock, logger) {
|
|
|
978
995
|
});
|
|
979
996
|
} catch (e) {
|
|
980
997
|
logger.error(
|
|
981
|
-
`${
|
|
998
|
+
`${import_picocolors4.default.red(
|
|
982
999
|
`mock error at ${req.url.split("?")[0]}`
|
|
983
1000
|
)}
|
|
984
1001
|
${e}
|
|
985
|
-
at headers (${
|
|
1002
|
+
at headers (${import_picocolors4.default.underline(filepath)})`,
|
|
986
1003
|
mock.log
|
|
987
1004
|
);
|
|
988
1005
|
}
|
|
@@ -1005,11 +1022,11 @@ async function provideCookies(req, res, mock, logger) {
|
|
|
1005
1022
|
});
|
|
1006
1023
|
} catch (e) {
|
|
1007
1024
|
logger.error(
|
|
1008
|
-
`${
|
|
1025
|
+
`${import_picocolors4.default.red(
|
|
1009
1026
|
`mock error at ${req.url.split("?")[0]}`
|
|
1010
1027
|
)}
|
|
1011
1028
|
${e}
|
|
1012
|
-
at cookies (${
|
|
1029
|
+
at cookies (${import_picocolors4.default.underline(filepath)})`,
|
|
1013
1030
|
mock.log
|
|
1014
1031
|
);
|
|
1015
1032
|
}
|
|
@@ -1043,15 +1060,15 @@ function getHTTPStatusText(status) {
|
|
|
1043
1060
|
function requestLog(request, filepath) {
|
|
1044
1061
|
const { url, method, query, params, body } = request;
|
|
1045
1062
|
let { pathname } = new URL(url, "http://example.com");
|
|
1046
|
-
pathname =
|
|
1063
|
+
pathname = import_picocolors4.default.green(decodeURIComponent(pathname));
|
|
1047
1064
|
const format = (prefix, data) => {
|
|
1048
|
-
return !data || (0, import_utils7.isEmptyObject)(data) ? "" : ` ${
|
|
1065
|
+
return !data || (0, import_utils7.isEmptyObject)(data) ? "" : ` ${import_picocolors4.default.gray(`${prefix}:`)}${JSON.stringify(data)}`;
|
|
1049
1066
|
};
|
|
1050
|
-
const ms =
|
|
1067
|
+
const ms = import_picocolors4.default.magenta(import_picocolors4.default.bold(method));
|
|
1051
1068
|
const qs = format("query", query);
|
|
1052
1069
|
const ps = format("params", params);
|
|
1053
1070
|
const bs = format("body", body);
|
|
1054
|
-
const file = ` ${
|
|
1071
|
+
const file = ` ${import_picocolors4.default.dim(import_picocolors4.default.underline(`(${filepath})`))}`;
|
|
1055
1072
|
return `${ms} ${pathname}${qs}${ps}${bs}${file}`;
|
|
1056
1073
|
}
|
|
1057
1074
|
|
|
@@ -1061,7 +1078,7 @@ var import_node_process3 = __toESM(require("process"), 1);
|
|
|
1061
1078
|
var import_utils11 = require("@pengzhanbo/utils");
|
|
1062
1079
|
var import_chokidar = __toESM(require("chokidar"), 1);
|
|
1063
1080
|
var import_fast_glob2 = __toESM(require("fast-glob"), 1);
|
|
1064
|
-
var
|
|
1081
|
+
var import_pluginutils2 = require("@rollup/pluginutils");
|
|
1065
1082
|
|
|
1066
1083
|
// src/transform.ts
|
|
1067
1084
|
var import_utils9 = require("@pengzhanbo/utils");
|
|
@@ -1144,7 +1161,7 @@ var MockLoader = class extends import_node_events.default {
|
|
|
1144
1161
|
}
|
|
1145
1162
|
load() {
|
|
1146
1163
|
const { include, exclude } = this.options;
|
|
1147
|
-
const includeFilter = (0,
|
|
1164
|
+
const includeFilter = (0, import_pluginutils2.createFilter)(include, exclude, {
|
|
1148
1165
|
resolve: false
|
|
1149
1166
|
});
|
|
1150
1167
|
(0, import_fast_glob2.default)(include, { cwd: this.cwd }).then(
|
|
@@ -1181,17 +1198,17 @@ var MockLoader = class extends import_node_events.default {
|
|
|
1181
1198
|
});
|
|
1182
1199
|
otherGlob.length > 0 && otherGlob.forEach((glob) => watcher.add(glob));
|
|
1183
1200
|
watcher.on("add", async (filepath) => {
|
|
1184
|
-
filepath =
|
|
1201
|
+
filepath = normalizePath(filepath);
|
|
1185
1202
|
this.emit("mock:update", filepath);
|
|
1186
1203
|
debug("watcher:add", filepath);
|
|
1187
1204
|
});
|
|
1188
1205
|
watcher.on("change", async (filepath) => {
|
|
1189
|
-
filepath =
|
|
1206
|
+
filepath = normalizePath(filepath);
|
|
1190
1207
|
this.emit("mock:update", filepath);
|
|
1191
1208
|
debug("watcher:change", filepath);
|
|
1192
1209
|
});
|
|
1193
1210
|
watcher.on("unlink", async (filepath) => {
|
|
1194
|
-
filepath =
|
|
1211
|
+
filepath = normalizePath(filepath);
|
|
1195
1212
|
this.emit("mock:unlink", filepath);
|
|
1196
1213
|
debug("watcher:unlink", filepath);
|
|
1197
1214
|
});
|
|
@@ -1207,14 +1224,14 @@ var MockLoader = class extends import_node_events.default {
|
|
|
1207
1224
|
cwd: this.cwd
|
|
1208
1225
|
});
|
|
1209
1226
|
this.depsWatcher.on("change", (filepath) => {
|
|
1210
|
-
filepath =
|
|
1227
|
+
filepath = normalizePath(filepath);
|
|
1211
1228
|
const mockFiles = this.moduleDeps.get(filepath);
|
|
1212
1229
|
mockFiles && mockFiles.forEach((file) => {
|
|
1213
1230
|
this.emit("mock:update", file);
|
|
1214
1231
|
});
|
|
1215
1232
|
});
|
|
1216
1233
|
this.depsWatcher.on("unlink", (filepath) => {
|
|
1217
|
-
filepath =
|
|
1234
|
+
filepath = normalizePath(filepath);
|
|
1218
1235
|
this.moduleDeps.delete(filepath);
|
|
1219
1236
|
});
|
|
1220
1237
|
this.on("update:deps", () => {
|
|
@@ -1294,7 +1311,7 @@ var MockLoader = class extends import_node_events.default {
|
|
|
1294
1311
|
// src/ws.ts
|
|
1295
1312
|
var import_cookies2 = __toESM(require("cookies"), 1);
|
|
1296
1313
|
var import_path_to_regexp4 = require("path-to-regexp");
|
|
1297
|
-
var
|
|
1314
|
+
var import_picocolors5 = __toESM(require("picocolors"), 1);
|
|
1298
1315
|
var import_ws = require("ws");
|
|
1299
1316
|
function mockWebSocket({
|
|
1300
1317
|
loader,
|
|
@@ -1329,7 +1346,7 @@ function mockWebSocket({
|
|
|
1329
1346
|
wss.on("close", () => wssMap.delete(pathname));
|
|
1330
1347
|
wss.on("error", (e) => {
|
|
1331
1348
|
logger.error(
|
|
1332
|
-
`${
|
|
1349
|
+
`${import_picocolors5.default.red(
|
|
1333
1350
|
`WebSocket mock error at ${wss.path}`
|
|
1334
1351
|
)}
|
|
1335
1352
|
${e}
|
|
@@ -1339,7 +1356,7 @@ ${e}
|
|
|
1339
1356
|
});
|
|
1340
1357
|
} catch (e) {
|
|
1341
1358
|
logger.error(
|
|
1342
|
-
`${
|
|
1359
|
+
`${import_picocolors5.default.red(
|
|
1343
1360
|
`WebSocket mock error at ${wss.path}`
|
|
1344
1361
|
)}
|
|
1345
1362
|
${e}
|
|
@@ -1420,9 +1437,9 @@ ${e}
|
|
|
1420
1437
|
request.getCookie = cookies.get.bind(cookies);
|
|
1421
1438
|
wss.handleUpgrade(request, socket, head, (ws) => {
|
|
1422
1439
|
logger.info(
|
|
1423
|
-
`${
|
|
1440
|
+
`${import_picocolors5.default.magenta(import_picocolors5.default.bold("WebSocket"))} ${import_picocolors5.default.green(
|
|
1424
1441
|
req.url
|
|
1425
|
-
)} connected ${
|
|
1442
|
+
)} connected ${import_picocolors5.default.dim(`(${filepath})`)}`,
|
|
1426
1443
|
mock.log
|
|
1427
1444
|
);
|
|
1428
1445
|
wssContext.connectionList.push({ req: request, ws });
|
|
@@ -1562,7 +1579,8 @@ function mockDevServerPlugin({
|
|
|
1562
1579
|
build: build2 ? Object.assign(
|
|
1563
1580
|
{
|
|
1564
1581
|
serverPort: 8080,
|
|
1565
|
-
dist: "mockServer"
|
|
1582
|
+
dist: "mockServer",
|
|
1583
|
+
log: "error"
|
|
1566
1584
|
},
|
|
1567
1585
|
typeof build2 === "object" ? build2 : {}
|
|
1568
1586
|
) : false
|
package/dist/index.d.cts
CHANGED
|
@@ -208,6 +208,13 @@ interface ServerBuildOption {
|
|
|
208
208
|
* @default 'dist/mockServer'
|
|
209
209
|
*/
|
|
210
210
|
dist?: string;
|
|
211
|
+
/**
|
|
212
|
+
* Service application log level
|
|
213
|
+
*
|
|
214
|
+
* 服务应用日志级别
|
|
215
|
+
* @default 'error'
|
|
216
|
+
*/
|
|
217
|
+
log?: LogLevel;
|
|
211
218
|
}
|
|
212
219
|
type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'TRACE' | 'OPTIONS';
|
|
213
220
|
type Headers = http.IncomingHttpHeaders;
|
package/dist/index.d.ts
CHANGED
|
@@ -208,6 +208,13 @@ interface ServerBuildOption {
|
|
|
208
208
|
* @default 'dist/mockServer'
|
|
209
209
|
*/
|
|
210
210
|
dist?: string;
|
|
211
|
+
/**
|
|
212
|
+
* Service application log level
|
|
213
|
+
*
|
|
214
|
+
* 服务应用日志级别
|
|
215
|
+
* @default 'error'
|
|
216
|
+
*/
|
|
217
|
+
log?: LogLevel;
|
|
211
218
|
}
|
|
212
219
|
type Method = 'GET' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'HEAD' | 'TRACE' | 'OPTIONS';
|
|
213
220
|
type Headers = http.IncomingHttpHeaders;
|
package/dist/index.js
CHANGED
|
@@ -9,11 +9,8 @@ import process2 from "node:process";
|
|
|
9
9
|
import { toArray } from "@pengzhanbo/utils";
|
|
10
10
|
import fg from "fast-glob";
|
|
11
11
|
import isCore from "is-core-module";
|
|
12
|
-
import { createFilter
|
|
13
|
-
|
|
14
|
-
// package.json
|
|
15
|
-
var name = "vite-plugin-mock-dev-server";
|
|
16
|
-
var version = "1.4.1";
|
|
12
|
+
import { createFilter } from "@rollup/pluginutils";
|
|
13
|
+
import c from "picocolors";
|
|
17
14
|
|
|
18
15
|
// src/compiler.ts
|
|
19
16
|
import fs2, { promises as fsp } from "node:fs";
|
|
@@ -28,6 +25,7 @@ import fs from "node:fs";
|
|
|
28
25
|
import path from "node:path";
|
|
29
26
|
import { parse as queryParse } from "node:querystring";
|
|
30
27
|
import { URL as URL2, fileURLToPath } from "node:url";
|
|
28
|
+
import os from "node:os";
|
|
31
29
|
import Debug from "debug";
|
|
32
30
|
import { match } from "path-to-regexp";
|
|
33
31
|
function isStream(stream) {
|
|
@@ -81,6 +79,14 @@ function urlParse(input) {
|
|
|
81
79
|
const query = queryParse(url.search.replace(/^\?/, ""));
|
|
82
80
|
return { pathname, query };
|
|
83
81
|
}
|
|
82
|
+
var windowsSlashRE = /\\/g;
|
|
83
|
+
var isWindows = os.platform() === "win32";
|
|
84
|
+
function slash(p) {
|
|
85
|
+
return p.replace(windowsSlashRE, "/");
|
|
86
|
+
}
|
|
87
|
+
function normalizePath(id) {
|
|
88
|
+
return path.posix.normalize(isWindows ? slash(id) : id);
|
|
89
|
+
}
|
|
84
90
|
|
|
85
91
|
// src/compiler.ts
|
|
86
92
|
var externalizeDeps = {
|
|
@@ -351,7 +357,7 @@ async function generateMockServer(ctx, config, options) {
|
|
|
351
357
|
wsProxies,
|
|
352
358
|
options.cookiesOptions,
|
|
353
359
|
options.priority,
|
|
354
|
-
options.build
|
|
360
|
+
options.build
|
|
355
361
|
)
|
|
356
362
|
},
|
|
357
363
|
{
|
|
@@ -361,10 +367,19 @@ async function generateMockServer(ctx, config, options) {
|
|
|
361
367
|
];
|
|
362
368
|
try {
|
|
363
369
|
if (path3.isAbsolute(outputDir)) {
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
370
|
+
for (const { filename } of outputList) {
|
|
371
|
+
if (fs3.existsSync(filename))
|
|
372
|
+
await fsp2.rm(filename);
|
|
373
|
+
}
|
|
374
|
+
config.logger.info(`${c.green("\u2713")} generate mock server in ${c.cyan(outputDir)}`);
|
|
375
|
+
for (const { filename, source } of outputList) {
|
|
376
|
+
fs3.mkdirSync(path3.dirname(filename), { recursive: true });
|
|
367
377
|
await fsp2.writeFile(filename, source, "utf-8");
|
|
378
|
+
const sourceSize = (source.length / 1024).toFixed(2);
|
|
379
|
+
const name = path3.relative(outputDir, filename);
|
|
380
|
+
const space = name.length < 30 ? " ".repeat(30 - name.length) : "";
|
|
381
|
+
config.logger.info(` ${c.green(name)}${space}${c.bold(c.dim(`${sourceSize} kB`))}`);
|
|
382
|
+
}
|
|
368
383
|
} else {
|
|
369
384
|
for (const { filename, source } of outputList) {
|
|
370
385
|
ctx.emitFile({
|
|
@@ -374,12 +389,13 @@ async function generateMockServer(ctx, config, options) {
|
|
|
374
389
|
});
|
|
375
390
|
}
|
|
376
391
|
}
|
|
377
|
-
} catch {
|
|
392
|
+
} catch (e) {
|
|
393
|
+
console.error(e);
|
|
378
394
|
}
|
|
379
395
|
}
|
|
380
396
|
function getMockDependencies(deps, alias) {
|
|
381
397
|
const list = /* @__PURE__ */ new Set();
|
|
382
|
-
const excludeDeps = [
|
|
398
|
+
const excludeDeps = ["vite-plugin-mock-dev-server", "connect", "cors"];
|
|
383
399
|
const isAlias = (p) => alias.find(({ find }) => aliasMatches(find, p));
|
|
384
400
|
Object.keys(deps).forEach((mPath) => {
|
|
385
401
|
const imports = deps[mPath].imports.filter((_) => _.external && !_.path.startsWith("<define:") && !isAlias(_.path)).map((_) => _.path);
|
|
@@ -401,7 +417,7 @@ function generatePackageJson(pkg, mockDeps) {
|
|
|
401
417
|
},
|
|
402
418
|
dependencies: {
|
|
403
419
|
"connect": "^3.7.0",
|
|
404
|
-
"vite-plugin-mock-dev-server": `^${
|
|
420
|
+
"vite-plugin-mock-dev-server": `^${"1.4.3"}`,
|
|
405
421
|
"cors": "^2.8.5"
|
|
406
422
|
},
|
|
407
423
|
pnpm: { peerDependencyRules: { ignoreMissing: ["vite"] } }
|
|
@@ -411,16 +427,17 @@ function generatePackageJson(pkg, mockDeps) {
|
|
|
411
427
|
});
|
|
412
428
|
return JSON.stringify(mockPkg, null, 2);
|
|
413
429
|
}
|
|
414
|
-
function generatorServerEntryCode(httpProxies, wsProxies, cookiesOptions = {}, priority = {},
|
|
430
|
+
function generatorServerEntryCode(httpProxies, wsProxies, cookiesOptions = {}, priority = {}, build2) {
|
|
431
|
+
const { serverPort, log } = build2;
|
|
415
432
|
return `import { createServer } from 'node:http';
|
|
416
433
|
import connect from 'connect';
|
|
417
434
|
import corsMiddleware from 'cors';
|
|
418
|
-
import { baseMiddleware,
|
|
435
|
+
import { baseMiddleware, createLogger, mockWebSocket } from 'vite-plugin-mock-dev-server';
|
|
419
436
|
import mockData from './mock-data.js';
|
|
420
437
|
|
|
421
438
|
const app = connect();
|
|
422
439
|
const server = createServer(app);
|
|
423
|
-
const logger = createLogger('mock-server', '
|
|
440
|
+
const logger = createLogger('mock-server', '${log}');
|
|
424
441
|
const httpProxies = ${JSON.stringify(httpProxies)};
|
|
425
442
|
const wsProxies = ${JSON.stringify(wsProxies)};
|
|
426
443
|
const cookiesOptions = ${JSON.stringify(cookiesOptions)};
|
|
@@ -443,9 +460,9 @@ app.use(baseMiddleware({ mockData }, {
|
|
|
443
460
|
logger,
|
|
444
461
|
}));
|
|
445
462
|
|
|
446
|
-
server.listen(${
|
|
463
|
+
server.listen(${serverPort});
|
|
447
464
|
|
|
448
|
-
console.log('listen: http://localhost:${
|
|
465
|
+
console.log('listen: http://localhost:${serverPort}');
|
|
449
466
|
`;
|
|
450
467
|
}
|
|
451
468
|
async function generateMockEntryCode(cwd, include, exclude) {
|
|
@@ -579,11 +596,11 @@ function defaultPriority(rules) {
|
|
|
579
596
|
pattern = "",
|
|
580
597
|
modifier,
|
|
581
598
|
prefix,
|
|
582
|
-
name
|
|
599
|
+
name
|
|
583
600
|
} = isDynamic ? token : {};
|
|
584
601
|
const isGlob = pattern && pattern.includes(".*");
|
|
585
602
|
const isSlash = prefix === "/";
|
|
586
|
-
const isNamed = isString(
|
|
603
|
+
const isNamed = isString(name);
|
|
587
604
|
exp += isDynamic && isSlash ? 1 : 0;
|
|
588
605
|
if (i === tokens.length - 1 && isGlob) {
|
|
589
606
|
weight += 5 * 10 ** (tokens.length === 1 ? highest + 1 : highest);
|
|
@@ -931,7 +948,7 @@ async function provideHeaders(req, res, mock, logger) {
|
|
|
931
948
|
contentType2 && res.setHeader("Content-Type", contentType2);
|
|
932
949
|
res.setHeader("Cache-Control", "no-cache,max-age=0");
|
|
933
950
|
res.setHeader("X-Mock-Power-By", "vite-plugin-mock-dev-server");
|
|
934
|
-
res.setHeader("X-File-Path", filepath);
|
|
951
|
+
filepath && res.setHeader("X-File-Path", filepath);
|
|
935
952
|
if (!headers)
|
|
936
953
|
return;
|
|
937
954
|
try {
|
|
@@ -1024,7 +1041,7 @@ import process3 from "node:process";
|
|
|
1024
1041
|
import { hasOwn, isArray as isArray5, promiseParallel, toArray as toArray2 } from "@pengzhanbo/utils";
|
|
1025
1042
|
import chokidar from "chokidar";
|
|
1026
1043
|
import fastGlob from "fast-glob";
|
|
1027
|
-
import { createFilter as createFilter2
|
|
1044
|
+
import { createFilter as createFilter2 } from "@rollup/pluginutils";
|
|
1028
1045
|
|
|
1029
1046
|
// src/transform.ts
|
|
1030
1047
|
import {
|
|
@@ -1150,17 +1167,17 @@ var MockLoader = class extends EventEmitter {
|
|
|
1150
1167
|
});
|
|
1151
1168
|
otherGlob.length > 0 && otherGlob.forEach((glob) => watcher.add(glob));
|
|
1152
1169
|
watcher.on("add", async (filepath) => {
|
|
1153
|
-
filepath =
|
|
1170
|
+
filepath = normalizePath(filepath);
|
|
1154
1171
|
this.emit("mock:update", filepath);
|
|
1155
1172
|
debug("watcher:add", filepath);
|
|
1156
1173
|
});
|
|
1157
1174
|
watcher.on("change", async (filepath) => {
|
|
1158
|
-
filepath =
|
|
1175
|
+
filepath = normalizePath(filepath);
|
|
1159
1176
|
this.emit("mock:update", filepath);
|
|
1160
1177
|
debug("watcher:change", filepath);
|
|
1161
1178
|
});
|
|
1162
1179
|
watcher.on("unlink", async (filepath) => {
|
|
1163
|
-
filepath =
|
|
1180
|
+
filepath = normalizePath(filepath);
|
|
1164
1181
|
this.emit("mock:unlink", filepath);
|
|
1165
1182
|
debug("watcher:unlink", filepath);
|
|
1166
1183
|
});
|
|
@@ -1176,14 +1193,14 @@ var MockLoader = class extends EventEmitter {
|
|
|
1176
1193
|
cwd: this.cwd
|
|
1177
1194
|
});
|
|
1178
1195
|
this.depsWatcher.on("change", (filepath) => {
|
|
1179
|
-
filepath =
|
|
1196
|
+
filepath = normalizePath(filepath);
|
|
1180
1197
|
const mockFiles = this.moduleDeps.get(filepath);
|
|
1181
1198
|
mockFiles && mockFiles.forEach((file) => {
|
|
1182
1199
|
this.emit("mock:update", file);
|
|
1183
1200
|
});
|
|
1184
1201
|
});
|
|
1185
1202
|
this.depsWatcher.on("unlink", (filepath) => {
|
|
1186
|
-
filepath =
|
|
1203
|
+
filepath = normalizePath(filepath);
|
|
1187
1204
|
this.moduleDeps.delete(filepath);
|
|
1188
1205
|
});
|
|
1189
1206
|
this.on("update:deps", () => {
|
|
@@ -1531,7 +1548,8 @@ function mockDevServerPlugin({
|
|
|
1531
1548
|
build: build2 ? Object.assign(
|
|
1532
1549
|
{
|
|
1533
1550
|
serverPort: 8080,
|
|
1534
|
-
dist: "mockServer"
|
|
1551
|
+
dist: "mockServer",
|
|
1552
|
+
log: "error"
|
|
1535
1553
|
},
|
|
1536
1554
|
typeof build2 === "object" ? build2 : {}
|
|
1537
1555
|
) : false
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-mock-dev-server",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.4.
|
|
4
|
+
"version": "1.4.3",
|
|
5
5
|
"packageManager": "pnpm@8.11.0",
|
|
6
6
|
"author": "pengzhanbo <q942450674@outlook.com> (https://github.com/pengzhanbo)",
|
|
7
7
|
"license": "MIT",
|
|
@@ -44,6 +44,7 @@
|
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@pengzhanbo/utils": "^1.1.1",
|
|
47
|
+
"@rollup/pluginutils": "^5.1.0",
|
|
47
48
|
"chokidar": "^3.5.3",
|
|
48
49
|
"co-body": "^6.1.0",
|
|
49
50
|
"cookies": "^0.8.0",
|