vite-plugin-mock-dev-server 1.4.1 → 1.4.2

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 CHANGED
@@ -232,6 +232,11 @@ export default defineConfig({
232
232
  * @default 'mockServer'
233
233
  */
234
234
  dist?: string
235
+ /**
236
+ * log level
237
+ * @default 'error'
238
+ */
239
+ log?: LogLevel
235
240
  }
236
241
  ```
237
242
 
package/README.zh-CN.md CHANGED
@@ -227,6 +227,12 @@ export default defineConfig({
227
227
  * @default 'mockServer'
228
228
  */
229
229
  dist?: string
230
+
231
+ /**
232
+ * 日志级别
233
+ * @default 'error'
234
+ */
235
+ log?: LogLevel
230
236
  }
231
237
  ```
232
238
 
package/dist/index.cjs CHANGED
@@ -59,11 +59,11 @@ 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 import_vite = require("vite");
62
+ var import_pluginutils = require("@rollup/pluginutils");
63
63
 
64
64
  // package.json
65
65
  var name = "vite-plugin-mock-dev-server";
66
- var version = "1.4.1";
66
+ var version = "1.4.2";
67
67
 
68
68
  // src/compiler.ts
69
69
  var import_node_fs2 = __toESM(require("fs"), 1);
@@ -78,6 +78,7 @@ var import_node_fs = __toESM(require("fs"), 1);
78
78
  var import_node_path = __toESM(require("path"), 1);
79
79
  var import_node_querystring = require("querystring");
80
80
  var import_node_url = require("url");
81
+ var import_node_os = __toESM(require("os"), 1);
81
82
  var import_debug = __toESM(require("debug"), 1);
82
83
  var import_path_to_regexp = require("path-to-regexp");
83
84
  function isStream(stream) {
@@ -131,6 +132,14 @@ function urlParse(input) {
131
132
  const query = (0, import_node_querystring.parse)(url.search.replace(/^\?/, ""));
132
133
  return { pathname, query };
133
134
  }
135
+ var windowsSlashRE = /\\/g;
136
+ var isWindows = import_node_os.default.platform() === "win32";
137
+ function slash(p) {
138
+ return p.replace(windowsSlashRE, "/");
139
+ }
140
+ function normalizePath(id) {
141
+ return import_node_path.default.posix.normalize(isWindows ? slash(id) : id);
142
+ }
134
143
 
135
144
  // src/compiler.ts
136
145
  var externalizeDeps = {
@@ -401,7 +410,7 @@ async function generateMockServer(ctx, config, options) {
401
410
  wsProxies,
402
411
  options.cookiesOptions,
403
412
  options.priority,
404
- options.build.serverPort
413
+ options.build
405
414
  )
406
415
  },
407
416
  {
@@ -411,7 +420,8 @@ async function generateMockServer(ctx, config, options) {
411
420
  ];
412
421
  try {
413
422
  if (import_node_path3.default.isAbsolute(outputDir)) {
414
- await import_promises.default.rm(outputDir, { recursive: true });
423
+ for (const { filename } of outputList)
424
+ await import_promises.default.rm(filename);
415
425
  import_node_fs3.default.mkdirSync(outputDir, { recursive: true });
416
426
  for (const { filename, source } of outputList)
417
427
  await import_promises.default.writeFile(filename, source, "utf-8");
@@ -461,7 +471,8 @@ function generatePackageJson(pkg, mockDeps) {
461
471
  });
462
472
  return JSON.stringify(mockPkg, null, 2);
463
473
  }
464
- function generatorServerEntryCode(httpProxies, wsProxies, cookiesOptions = {}, priority = {}, port = 8080) {
474
+ function generatorServerEntryCode(httpProxies, wsProxies, cookiesOptions = {}, priority = {}, build2) {
475
+ const { serverPort, log } = build2;
465
476
  return `import { createServer } from 'node:http';
466
477
  import connect from 'connect';
467
478
  import corsMiddleware from 'cors';
@@ -470,7 +481,7 @@ import mockData from './mock-data.js';
470
481
 
471
482
  const app = connect();
472
483
  const server = createServer(app);
473
- const logger = createLogger('mock-server', 'error');
484
+ const logger = createLogger('mock-server', '${log}');
474
485
  const httpProxies = ${JSON.stringify(httpProxies)};
475
486
  const wsProxies = ${JSON.stringify(wsProxies)};
476
487
  const cookiesOptions = ${JSON.stringify(cookiesOptions)};
@@ -493,21 +504,21 @@ app.use(baseMiddleware({ mockData }, {
493
504
  logger,
494
505
  }));
495
506
 
496
- server.listen(${port});
507
+ server.listen(${serverPort});
497
508
 
498
- console.log('listen: http://localhost:${port}');
509
+ console.log('listen: http://localhost:${serverPort}');
499
510
  `;
500
511
  }
501
512
  async function generateMockEntryCode(cwd, include, exclude) {
502
513
  const includePaths = await (0, import_fast_glob.default)(include, { cwd });
503
- const includeFilter = (0, import_vite.createFilter)(include, exclude, {
514
+ const includeFilter = (0, import_pluginutils.createFilter)(include, exclude, {
504
515
  resolve: false
505
516
  });
506
517
  const mockFiles = includePaths.filter(includeFilter);
507
518
  let importers = "";
508
519
  let exporters = "";
509
520
  mockFiles.forEach((filepath, index) => {
510
- const file = (0, import_vite.normalizePath)(import_node_path3.default.join(cwd, filepath));
521
+ const file = normalizePath(import_node_path3.default.join(cwd, filepath));
511
522
  importers += `import * as m${index} from '${file}';
512
523
  `;
513
524
  exporters += `m${index}, `;
@@ -968,7 +979,7 @@ async function provideHeaders(req, res, mock, logger) {
968
979
  contentType2 && res.setHeader("Content-Type", contentType2);
969
980
  res.setHeader("Cache-Control", "no-cache,max-age=0");
970
981
  res.setHeader("X-Mock-Power-By", "vite-plugin-mock-dev-server");
971
- res.setHeader("X-File-Path", filepath);
982
+ filepath && res.setHeader("X-File-Path", filepath);
972
983
  if (!headers)
973
984
  return;
974
985
  try {
@@ -1061,7 +1072,7 @@ var import_node_process3 = __toESM(require("process"), 1);
1061
1072
  var import_utils11 = require("@pengzhanbo/utils");
1062
1073
  var import_chokidar = __toESM(require("chokidar"), 1);
1063
1074
  var import_fast_glob2 = __toESM(require("fast-glob"), 1);
1064
- var import_vite2 = require("vite");
1075
+ var import_pluginutils2 = require("@rollup/pluginutils");
1065
1076
 
1066
1077
  // src/transform.ts
1067
1078
  var import_utils9 = require("@pengzhanbo/utils");
@@ -1144,7 +1155,7 @@ var MockLoader = class extends import_node_events.default {
1144
1155
  }
1145
1156
  load() {
1146
1157
  const { include, exclude } = this.options;
1147
- const includeFilter = (0, import_vite2.createFilter)(include, exclude, {
1158
+ const includeFilter = (0, import_pluginutils2.createFilter)(include, exclude, {
1148
1159
  resolve: false
1149
1160
  });
1150
1161
  (0, import_fast_glob2.default)(include, { cwd: this.cwd }).then(
@@ -1181,17 +1192,17 @@ var MockLoader = class extends import_node_events.default {
1181
1192
  });
1182
1193
  otherGlob.length > 0 && otherGlob.forEach((glob) => watcher.add(glob));
1183
1194
  watcher.on("add", async (filepath) => {
1184
- filepath = (0, import_vite2.normalizePath)(filepath);
1195
+ filepath = normalizePath(filepath);
1185
1196
  this.emit("mock:update", filepath);
1186
1197
  debug("watcher:add", filepath);
1187
1198
  });
1188
1199
  watcher.on("change", async (filepath) => {
1189
- filepath = (0, import_vite2.normalizePath)(filepath);
1200
+ filepath = normalizePath(filepath);
1190
1201
  this.emit("mock:update", filepath);
1191
1202
  debug("watcher:change", filepath);
1192
1203
  });
1193
1204
  watcher.on("unlink", async (filepath) => {
1194
- filepath = (0, import_vite2.normalizePath)(filepath);
1205
+ filepath = normalizePath(filepath);
1195
1206
  this.emit("mock:unlink", filepath);
1196
1207
  debug("watcher:unlink", filepath);
1197
1208
  });
@@ -1207,14 +1218,14 @@ var MockLoader = class extends import_node_events.default {
1207
1218
  cwd: this.cwd
1208
1219
  });
1209
1220
  this.depsWatcher.on("change", (filepath) => {
1210
- filepath = (0, import_vite2.normalizePath)(filepath);
1221
+ filepath = normalizePath(filepath);
1211
1222
  const mockFiles = this.moduleDeps.get(filepath);
1212
1223
  mockFiles && mockFiles.forEach((file) => {
1213
1224
  this.emit("mock:update", file);
1214
1225
  });
1215
1226
  });
1216
1227
  this.depsWatcher.on("unlink", (filepath) => {
1217
- filepath = (0, import_vite2.normalizePath)(filepath);
1228
+ filepath = normalizePath(filepath);
1218
1229
  this.moduleDeps.delete(filepath);
1219
1230
  });
1220
1231
  this.on("update:deps", () => {
@@ -1562,7 +1573,8 @@ function mockDevServerPlugin({
1562
1573
  build: build2 ? Object.assign(
1563
1574
  {
1564
1575
  serverPort: 8080,
1565
- dist: "mockServer"
1576
+ dist: "mockServer",
1577
+ log: "error"
1566
1578
  },
1567
1579
  typeof build2 === "object" ? build2 : {}
1568
1580
  ) : 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,11 @@ 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, normalizePath } from "vite";
12
+ import { createFilter } from "@rollup/pluginutils";
13
13
 
14
14
  // package.json
15
15
  var name = "vite-plugin-mock-dev-server";
16
- var version = "1.4.1";
16
+ var version = "1.4.2";
17
17
 
18
18
  // src/compiler.ts
19
19
  import fs2, { promises as fsp } from "node:fs";
@@ -28,6 +28,7 @@ import fs from "node:fs";
28
28
  import path from "node:path";
29
29
  import { parse as queryParse } from "node:querystring";
30
30
  import { URL as URL2, fileURLToPath } from "node:url";
31
+ import os from "node:os";
31
32
  import Debug from "debug";
32
33
  import { match } from "path-to-regexp";
33
34
  function isStream(stream) {
@@ -81,6 +82,14 @@ function urlParse(input) {
81
82
  const query = queryParse(url.search.replace(/^\?/, ""));
82
83
  return { pathname, query };
83
84
  }
85
+ var windowsSlashRE = /\\/g;
86
+ var isWindows = os.platform() === "win32";
87
+ function slash(p) {
88
+ return p.replace(windowsSlashRE, "/");
89
+ }
90
+ function normalizePath(id) {
91
+ return path.posix.normalize(isWindows ? slash(id) : id);
92
+ }
84
93
 
85
94
  // src/compiler.ts
86
95
  var externalizeDeps = {
@@ -351,7 +360,7 @@ async function generateMockServer(ctx, config, options) {
351
360
  wsProxies,
352
361
  options.cookiesOptions,
353
362
  options.priority,
354
- options.build.serverPort
363
+ options.build
355
364
  )
356
365
  },
357
366
  {
@@ -361,7 +370,8 @@ async function generateMockServer(ctx, config, options) {
361
370
  ];
362
371
  try {
363
372
  if (path3.isAbsolute(outputDir)) {
364
- await fsp2.rm(outputDir, { recursive: true });
373
+ for (const { filename } of outputList)
374
+ await fsp2.rm(filename);
365
375
  fs3.mkdirSync(outputDir, { recursive: true });
366
376
  for (const { filename, source } of outputList)
367
377
  await fsp2.writeFile(filename, source, "utf-8");
@@ -411,7 +421,8 @@ function generatePackageJson(pkg, mockDeps) {
411
421
  });
412
422
  return JSON.stringify(mockPkg, null, 2);
413
423
  }
414
- function generatorServerEntryCode(httpProxies, wsProxies, cookiesOptions = {}, priority = {}, port = 8080) {
424
+ function generatorServerEntryCode(httpProxies, wsProxies, cookiesOptions = {}, priority = {}, build2) {
425
+ const { serverPort, log } = build2;
415
426
  return `import { createServer } from 'node:http';
416
427
  import connect from 'connect';
417
428
  import corsMiddleware from 'cors';
@@ -420,7 +431,7 @@ import mockData from './mock-data.js';
420
431
 
421
432
  const app = connect();
422
433
  const server = createServer(app);
423
- const logger = createLogger('mock-server', 'error');
434
+ const logger = createLogger('mock-server', '${log}');
424
435
  const httpProxies = ${JSON.stringify(httpProxies)};
425
436
  const wsProxies = ${JSON.stringify(wsProxies)};
426
437
  const cookiesOptions = ${JSON.stringify(cookiesOptions)};
@@ -443,9 +454,9 @@ app.use(baseMiddleware({ mockData }, {
443
454
  logger,
444
455
  }));
445
456
 
446
- server.listen(${port});
457
+ server.listen(${serverPort});
447
458
 
448
- console.log('listen: http://localhost:${port}');
459
+ console.log('listen: http://localhost:${serverPort}');
449
460
  `;
450
461
  }
451
462
  async function generateMockEntryCode(cwd, include, exclude) {
@@ -931,7 +942,7 @@ async function provideHeaders(req, res, mock, logger) {
931
942
  contentType2 && res.setHeader("Content-Type", contentType2);
932
943
  res.setHeader("Cache-Control", "no-cache,max-age=0");
933
944
  res.setHeader("X-Mock-Power-By", "vite-plugin-mock-dev-server");
934
- res.setHeader("X-File-Path", filepath);
945
+ filepath && res.setHeader("X-File-Path", filepath);
935
946
  if (!headers)
936
947
  return;
937
948
  try {
@@ -1024,7 +1035,7 @@ import process3 from "node:process";
1024
1035
  import { hasOwn, isArray as isArray5, promiseParallel, toArray as toArray2 } from "@pengzhanbo/utils";
1025
1036
  import chokidar from "chokidar";
1026
1037
  import fastGlob from "fast-glob";
1027
- import { createFilter as createFilter2, normalizePath as normalizePath2 } from "vite";
1038
+ import { createFilter as createFilter2 } from "@rollup/pluginutils";
1028
1039
 
1029
1040
  // src/transform.ts
1030
1041
  import {
@@ -1150,17 +1161,17 @@ var MockLoader = class extends EventEmitter {
1150
1161
  });
1151
1162
  otherGlob.length > 0 && otherGlob.forEach((glob) => watcher.add(glob));
1152
1163
  watcher.on("add", async (filepath) => {
1153
- filepath = normalizePath2(filepath);
1164
+ filepath = normalizePath(filepath);
1154
1165
  this.emit("mock:update", filepath);
1155
1166
  debug("watcher:add", filepath);
1156
1167
  });
1157
1168
  watcher.on("change", async (filepath) => {
1158
- filepath = normalizePath2(filepath);
1169
+ filepath = normalizePath(filepath);
1159
1170
  this.emit("mock:update", filepath);
1160
1171
  debug("watcher:change", filepath);
1161
1172
  });
1162
1173
  watcher.on("unlink", async (filepath) => {
1163
- filepath = normalizePath2(filepath);
1174
+ filepath = normalizePath(filepath);
1164
1175
  this.emit("mock:unlink", filepath);
1165
1176
  debug("watcher:unlink", filepath);
1166
1177
  });
@@ -1176,14 +1187,14 @@ var MockLoader = class extends EventEmitter {
1176
1187
  cwd: this.cwd
1177
1188
  });
1178
1189
  this.depsWatcher.on("change", (filepath) => {
1179
- filepath = normalizePath2(filepath);
1190
+ filepath = normalizePath(filepath);
1180
1191
  const mockFiles = this.moduleDeps.get(filepath);
1181
1192
  mockFiles && mockFiles.forEach((file) => {
1182
1193
  this.emit("mock:update", file);
1183
1194
  });
1184
1195
  });
1185
1196
  this.depsWatcher.on("unlink", (filepath) => {
1186
- filepath = normalizePath2(filepath);
1197
+ filepath = normalizePath(filepath);
1187
1198
  this.moduleDeps.delete(filepath);
1188
1199
  });
1189
1200
  this.on("update:deps", () => {
@@ -1531,7 +1542,8 @@ function mockDevServerPlugin({
1531
1542
  build: build2 ? Object.assign(
1532
1543
  {
1533
1544
  serverPort: 8080,
1534
- dist: "mockServer"
1545
+ dist: "mockServer",
1546
+ log: "error"
1535
1547
  },
1536
1548
  typeof build2 === "object" ? build2 : {}
1537
1549
  ) : 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.1",
4
+ "version": "1.4.2",
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",