vite-plugin-mock-dev-server 1.4.0 → 1.4.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/dist/index.cjs CHANGED
@@ -32,9 +32,11 @@ var src_exports = {};
32
32
  __export(src_exports, {
33
33
  baseMiddleware: () => baseMiddleware,
34
34
  createDefineMock: () => createDefineMock,
35
+ createLogger: () => createLogger,
35
36
  default: () => src_default,
36
37
  defineMock: () => defineMock,
37
38
  defineMockData: () => defineMockData,
39
+ logLevels: () => logLevels,
38
40
  mockDevServerPlugin: () => mockDevServerPlugin,
39
41
  mockWebSocket: () => mockWebSocket,
40
42
  sortByValidator: () => sortByValidator,
@@ -42,7 +44,7 @@ __export(src_exports, {
42
44
  });
43
45
  module.exports = __toCommonJS(src_exports);
44
46
 
45
- // node_modules/.pnpm/tsup@8.0.1_typescript@5.3.2/node_modules/tsup/assets/cjs_shims.js
47
+ // node_modules/.pnpm/tsup@8.0.1_typescript@5.3.3/node_modules/tsup/assets/cjs_shims.js
46
48
  var getImportMetaUrl = () => typeof document === "undefined" ? new URL("file:" + __filename).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
47
49
  var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
48
50
 
@@ -61,7 +63,7 @@ var import_vite = require("vite");
61
63
 
62
64
  // package.json
63
65
  var name = "vite-plugin-mock-dev-server";
64
- var version = "1.4.0";
66
+ var version = "1.4.1";
65
67
 
66
68
  // src/compiler.ts
67
69
  var import_node_fs2 = __toESM(require("fs"), 1);
@@ -169,7 +171,7 @@ function aliasPlugin(alias) {
169
171
  name: "alias-plugin",
170
172
  setup(build2) {
171
173
  build2.onResolve({ filter: /.*/ }, async ({ path: id }) => {
172
- const matchedEntry = alias.find(({ find: find2 }) => matches(find2, id));
174
+ const matchedEntry = alias.find(({ find: find2 }) => aliasMatches(find2, id));
173
175
  if (!matchedEntry)
174
176
  return null;
175
177
  const { find, replacement } = matchedEntry;
@@ -186,7 +188,7 @@ function aliasPlugin(alias) {
186
188
  }
187
189
  };
188
190
  }
189
- function matches(pattern, importee) {
191
+ function aliasMatches(pattern, importee) {
190
192
  if (pattern instanceof RegExp)
191
193
  return pattern.test(importee);
192
194
  if (importee.length < pattern.length)
@@ -385,7 +387,7 @@ async function generateMockServer(ctx, config, options) {
385
387
  define,
386
388
  alias: config.resolve.alias
387
389
  });
388
- const mockDeps = getMockDependencies(deps);
390
+ const mockDeps = getMockDependencies(deps, config.resolve.alias);
389
391
  await import_promises.default.unlink(mockEntry);
390
392
  const outputList = [
391
393
  {
@@ -425,11 +427,12 @@ async function generateMockServer(ctx, config, options) {
425
427
  } catch {
426
428
  }
427
429
  }
428
- function getMockDependencies(deps) {
430
+ function getMockDependencies(deps, alias) {
429
431
  const list = /* @__PURE__ */ new Set();
430
432
  const excludeDeps = [name, "connect", "cors"];
433
+ const isAlias = (p) => alias.find(({ find }) => aliasMatches(find, p));
431
434
  Object.keys(deps).forEach((mPath) => {
432
- const imports = deps[mPath].imports.filter((_) => _.external && !_.path.startsWith("<define:")).map((_) => _.path);
435
+ const imports = deps[mPath].imports.filter((_) => _.external && !_.path.startsWith("<define:") && !isAlias(_.path)).map((_) => _.path);
433
436
  imports.forEach((dep) => {
434
437
  if (!excludeDeps.includes(dep) && !(0, import_is_core_module.default)(dep))
435
438
  list.add(dep);
@@ -462,17 +465,24 @@ function generatorServerEntryCode(httpProxies, wsProxies, cookiesOptions = {}, p
462
465
  return `import { createServer } from 'node:http';
463
466
  import connect from 'connect';
464
467
  import corsMiddleware from 'cors';
465
- import { baseMiddleware, mockWebSocket } from 'vite-plugin-mock-dev-server';
468
+ import { baseMiddleware, mockWebSocket, createLogger } from 'vite-plugin-mock-dev-server';
466
469
  import mockData from './mock-data.js';
467
470
 
468
471
  const app = connect();
469
472
  const server = createServer(app);
473
+ const logger = createLogger('mock-server', 'error');
470
474
  const httpProxies = ${JSON.stringify(httpProxies)};
471
475
  const wsProxies = ${JSON.stringify(wsProxies)};
472
476
  const cookiesOptions = ${JSON.stringify(cookiesOptions)};
473
477
  const priority = ${JSON.stringify(priority)};
474
478
 
475
- mockWebSocket({ mockData }, server, wsProxies, cookiesOptions);
479
+ mockWebSocket({
480
+ loader: { mockData },
481
+ httpServer: server,
482
+ proxies: wsProxies,
483
+ cookiesOptions,
484
+ logger,
485
+ });
476
486
 
477
487
  app.use(corsMiddleware());
478
488
  app.use(baseMiddleware({ mockData }, {
@@ -480,6 +490,7 @@ app.use(baseMiddleware({ mockData }, {
480
490
  proxies: httpProxies,
481
491
  priority,
482
492
  cookiesOptions,
493
+ logger,
483
494
  }));
484
495
 
485
496
  server.listen(${port});
@@ -511,7 +522,7 @@ const mockList = exporters.map((raw) => {
511
522
  } else {
512
523
  mockConfig = []
513
524
  Object.keys(raw || {}).forEach((key) => {
514
- isArray(raw[key])
525
+ Array.isArray(raw[key])
515
526
  ? mockConfig.push(...raw[key])
516
527
  : mockConfig.push(raw[key])
517
528
  })
@@ -1697,10 +1708,24 @@ var src_default = mockDevServerPlugin;
1697
1708
  0 && (module.exports = {
1698
1709
  baseMiddleware,
1699
1710
  createDefineMock,
1711
+ createLogger,
1700
1712
  defineMock,
1701
1713
  defineMockData,
1714
+ logLevels,
1702
1715
  mockDevServerPlugin,
1703
1716
  mockWebSocket,
1704
1717
  sortByValidator,
1705
1718
  transformMockData
1706
1719
  });
1720
+
1721
+ warnCjsUsage()
1722
+ function warnCjsUsage() {
1723
+ if (process.env.VITE_CJS_IGNORE_WARNING) return
1724
+ const yellow = (str) => `\u001b[33m${str}\u001b[39m`
1725
+ const log = process.env.VITE_CJS_TRACE ? console.trace : console.warn
1726
+ log(
1727
+ yellow(
1728
+ `The CJS build of Vite's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details.`,
1729
+ ),
1730
+ )
1731
+ }
package/dist/index.d.cts CHANGED
@@ -542,6 +542,7 @@ type FormidableFile = formidable.File | formidable.File[];
542
542
  type LogType = 'info' | 'warn' | 'error' | 'debug';
543
543
  type LogLevel = LogType | 'silent';
544
544
 
545
+ /** @deprecated The CJS build of Vite's Node API is deprecated. See https://vitejs.dev/guide/troubleshooting.html#vite-cjs-node-api-deprecated for more details. */
545
546
  declare function mockDevServerPlugin({ prefix, wsPrefix, include, exclude, reload, log, cors, formidableOptions, build, cookiesOptions, priority, }?: MockServerPluginOptions): Plugin[];
546
547
 
547
548
  /**
@@ -624,6 +625,8 @@ interface Logger {
624
625
  warn(msg: string, level?: boolean | LogLevel): void;
625
626
  error(msg: string, level?: boolean | LogLevel): void;
626
627
  }
628
+ declare const logLevels: Record<LogLevel, number>;
629
+ declare function createLogger(prefix: string, defaultLevel?: LogLevel): Logger;
627
630
 
628
631
  interface MockLoaderOptions {
629
632
  cwd?: string;
@@ -692,4 +695,4 @@ declare function mockWebSocket({ loader, httpServer, proxies, cookiesOptions, lo
692
695
  declare function transformMockData(mockList: Map<string, MockHttpItem | MockWebsocketItem | MockOptions> | (MockHttpItem | MockWebsocketItem | MockOptions)[]): Record<string, MockOptions>;
693
696
  declare function sortByValidator(mocks: MockOptions): (MockHttpItem | MockWebsocketItem)[];
694
697
 
695
- export { type BaseMiddlewareOptions, type FormidableFile, type MockData, type MockHttpItem, type MockOptions, type MockRequest, type MockServerPluginOptions, type MockSocketOptions, type MockWebsocketItem, baseMiddleware, createDefineMock, mockDevServerPlugin as default, defineMock, defineMockData, mockDevServerPlugin, mockWebSocket, sortByValidator, transformMockData };
698
+ export { type BaseMiddlewareOptions, type FormidableFile, type Logger, type MockData, type MockHttpItem, type MockOptions, type MockRequest, type MockServerPluginOptions, type MockSocketOptions, type MockWebsocketItem, baseMiddleware, createDefineMock, createLogger, mockDevServerPlugin as default, defineMock, defineMockData, logLevels, mockDevServerPlugin, mockWebSocket, sortByValidator, transformMockData };
package/dist/index.d.ts CHANGED
@@ -624,6 +624,8 @@ interface Logger {
624
624
  warn(msg: string, level?: boolean | LogLevel): void;
625
625
  error(msg: string, level?: boolean | LogLevel): void;
626
626
  }
627
+ declare const logLevels: Record<LogLevel, number>;
628
+ declare function createLogger(prefix: string, defaultLevel?: LogLevel): Logger;
627
629
 
628
630
  interface MockLoaderOptions {
629
631
  cwd?: string;
@@ -692,4 +694,4 @@ declare function mockWebSocket({ loader, httpServer, proxies, cookiesOptions, lo
692
694
  declare function transformMockData(mockList: Map<string, MockHttpItem | MockWebsocketItem | MockOptions> | (MockHttpItem | MockWebsocketItem | MockOptions)[]): Record<string, MockOptions>;
693
695
  declare function sortByValidator(mocks: MockOptions): (MockHttpItem | MockWebsocketItem)[];
694
696
 
695
- export { type BaseMiddlewareOptions, type FormidableFile, type MockData, type MockHttpItem, type MockOptions, type MockRequest, type MockServerPluginOptions, type MockSocketOptions, type MockWebsocketItem, baseMiddleware, createDefineMock, mockDevServerPlugin as default, defineMock, defineMockData, mockDevServerPlugin, mockWebSocket, sortByValidator, transformMockData };
697
+ export { type BaseMiddlewareOptions, type FormidableFile, type Logger, type MockData, type MockHttpItem, type MockOptions, type MockRequest, type MockServerPluginOptions, type MockSocketOptions, type MockWebsocketItem, baseMiddleware, createDefineMock, createLogger, mockDevServerPlugin as default, defineMock, defineMockData, logLevels, mockDevServerPlugin, mockWebSocket, sortByValidator, transformMockData };
package/dist/index.js CHANGED
@@ -13,7 +13,7 @@ import { createFilter, normalizePath } from "vite";
13
13
 
14
14
  // package.json
15
15
  var name = "vite-plugin-mock-dev-server";
16
- var version = "1.4.0";
16
+ var version = "1.4.1";
17
17
 
18
18
  // src/compiler.ts
19
19
  import fs2, { promises as fsp } from "node:fs";
@@ -121,7 +121,7 @@ function aliasPlugin(alias) {
121
121
  name: "alias-plugin",
122
122
  setup(build2) {
123
123
  build2.onResolve({ filter: /.*/ }, async ({ path: id }) => {
124
- const matchedEntry = alias.find(({ find: find2 }) => matches(find2, id));
124
+ const matchedEntry = alias.find(({ find: find2 }) => aliasMatches(find2, id));
125
125
  if (!matchedEntry)
126
126
  return null;
127
127
  const { find, replacement } = matchedEntry;
@@ -138,7 +138,7 @@ function aliasPlugin(alias) {
138
138
  }
139
139
  };
140
140
  }
141
- function matches(pattern, importee) {
141
+ function aliasMatches(pattern, importee) {
142
142
  if (pattern instanceof RegExp)
143
143
  return pattern.test(importee);
144
144
  if (importee.length < pattern.length)
@@ -337,7 +337,7 @@ async function generateMockServer(ctx, config, options) {
337
337
  define,
338
338
  alias: config.resolve.alias
339
339
  });
340
- const mockDeps = getMockDependencies(deps);
340
+ const mockDeps = getMockDependencies(deps, config.resolve.alias);
341
341
  await fsp2.unlink(mockEntry);
342
342
  const outputList = [
343
343
  {
@@ -377,11 +377,12 @@ async function generateMockServer(ctx, config, options) {
377
377
  } catch {
378
378
  }
379
379
  }
380
- function getMockDependencies(deps) {
380
+ function getMockDependencies(deps, alias) {
381
381
  const list = /* @__PURE__ */ new Set();
382
382
  const excludeDeps = [name, "connect", "cors"];
383
+ const isAlias = (p) => alias.find(({ find }) => aliasMatches(find, p));
383
384
  Object.keys(deps).forEach((mPath) => {
384
- const imports = deps[mPath].imports.filter((_) => _.external && !_.path.startsWith("<define:")).map((_) => _.path);
385
+ const imports = deps[mPath].imports.filter((_) => _.external && !_.path.startsWith("<define:") && !isAlias(_.path)).map((_) => _.path);
385
386
  imports.forEach((dep) => {
386
387
  if (!excludeDeps.includes(dep) && !isCore(dep))
387
388
  list.add(dep);
@@ -414,17 +415,24 @@ function generatorServerEntryCode(httpProxies, wsProxies, cookiesOptions = {}, p
414
415
  return `import { createServer } from 'node:http';
415
416
  import connect from 'connect';
416
417
  import corsMiddleware from 'cors';
417
- import { baseMiddleware, mockWebSocket } from 'vite-plugin-mock-dev-server';
418
+ import { baseMiddleware, mockWebSocket, createLogger } from 'vite-plugin-mock-dev-server';
418
419
  import mockData from './mock-data.js';
419
420
 
420
421
  const app = connect();
421
422
  const server = createServer(app);
423
+ const logger = createLogger('mock-server', 'error');
422
424
  const httpProxies = ${JSON.stringify(httpProxies)};
423
425
  const wsProxies = ${JSON.stringify(wsProxies)};
424
426
  const cookiesOptions = ${JSON.stringify(cookiesOptions)};
425
427
  const priority = ${JSON.stringify(priority)};
426
428
 
427
- mockWebSocket({ mockData }, server, wsProxies, cookiesOptions);
429
+ mockWebSocket({
430
+ loader: { mockData },
431
+ httpServer: server,
432
+ proxies: wsProxies,
433
+ cookiesOptions,
434
+ logger,
435
+ });
428
436
 
429
437
  app.use(corsMiddleware());
430
438
  app.use(baseMiddleware({ mockData }, {
@@ -432,6 +440,7 @@ app.use(baseMiddleware({ mockData }, {
432
440
  proxies: httpProxies,
433
441
  priority,
434
442
  cookiesOptions,
443
+ logger,
435
444
  }));
436
445
 
437
446
  server.listen(${port});
@@ -463,7 +472,7 @@ const mockList = exporters.map((raw) => {
463
472
  } else {
464
473
  mockConfig = []
465
474
  Object.keys(raw || {}).forEach((key) => {
466
- isArray(raw[key])
475
+ Array.isArray(raw[key])
467
476
  ? mockConfig.push(...raw[key])
468
477
  : mockConfig.push(raw[key])
469
478
  })
@@ -1667,9 +1676,11 @@ var src_default = mockDevServerPlugin;
1667
1676
  export {
1668
1677
  baseMiddleware,
1669
1678
  createDefineMock,
1679
+ createLogger,
1670
1680
  src_default as default,
1671
1681
  defineMock,
1672
1682
  defineMockData,
1683
+ logLevels,
1673
1684
  mockDevServerPlugin,
1674
1685
  mockWebSocket,
1675
1686
  sortByValidator,
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.0",
4
+ "version": "1.4.1",
5
5
  "packageManager": "pnpm@8.11.0",
6
6
  "author": "pengzhanbo <q942450674@outlook.com> (https://github.com/pengzhanbo)",
7
7
  "license": "MIT",
@@ -49,7 +49,7 @@
49
49
  "cookies": "^0.8.0",
50
50
  "cors": "^2.8.5",
51
51
  "debug": "^4.3.4",
52
- "esbuild": "^0.19.8",
52
+ "esbuild": "^0.19.9",
53
53
  "fast-glob": "^3.3.2",
54
54
  "formidable": "2.1.1",
55
55
  "http-status": "^1.7.3",
@@ -58,10 +58,10 @@
58
58
  "mime-types": "^2.1.35",
59
59
  "path-to-regexp": "^6.2.1",
60
60
  "picocolors": "^1.0.0",
61
- "ws": "^8.14.2"
61
+ "ws": "^8.15.1"
62
62
  },
63
63
  "devDependencies": {
64
- "@pengzhanbo/eslint-config": "^1.2.5",
64
+ "@pengzhanbo/eslint-config": "^1.3.1",
65
65
  "@types/co-body": "^6.1.3",
66
66
  "@types/cookies": "^0.7.10",
67
67
  "@types/cors": "^2.8.17",
@@ -69,20 +69,20 @@
69
69
  "@types/formidable": "2.0.6",
70
70
  "@types/is-core-module": "^2.2.2",
71
71
  "@types/mime-types": "^2.1.4",
72
- "@types/node": "^20.10.2",
72
+ "@types/node": "^20.10.3",
73
73
  "@types/ws": "^8.5.10",
74
- "bumpp": "^9.2.0",
74
+ "bumpp": "^9.2.1",
75
75
  "conventional-changelog-cli": "^4.1.0",
76
76
  "eslint": "^8.55.0",
77
77
  "mockjs": "^1.1.0",
78
78
  "tsup": "^8.0.1",
79
- "typescript": "^5.3.2",
80
- "vite": "^5.0.4",
79
+ "typescript": "^5.3.3",
80
+ "vite": "^5.0.8",
81
81
  "vitepress": "^1.0.0-rc.31",
82
- "vitest": "^0.34.6"
82
+ "vitest": "^1.0.4"
83
83
  },
84
84
  "scripts": {
85
- "build": "tsup",
85
+ "build": "tsup && node ./deprecate.mjs",
86
86
  "dev": "DEBUG=vite:mock-dev-server vite example --config ./example/vite.config.ts",
87
87
  "example:build": "DEBUG=vite:mock-dev-server vite build example --config ./example/vite.config.ts",
88
88
  "docs:build": "vitepress build docs",