vite-plugin-mock-dev-server 0.4.3 → 1.0.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 CHANGED
@@ -11,7 +11,7 @@
11
11
  <a href="https://www.npmjs.com/package/vite-plugin-mock-dev-server"><img alt="npm" src="https://img.shields.io/npm/v/vite-plugin-mock-dev-server?style=flat-square"></a>
12
12
  <img alt="node-current" src="https://img.shields.io/node/v/vite-plugin-mock-dev-server?style=flat-square">
13
13
  <img alt="npm peer dependency version" src="https://img.shields.io/npm/dependency-version/vite-plugin-mock-dev-server/peer/vite?style=flat-square">
14
- <img alt="GitHub" src="https://img.shields.io/github/license/pengzhanbo/vite-plugin-mock-dev-server?style=flat-square">
14
+ <img alt="GitHub Workflow Status" src="https://img.shields.io/github/actions/workflow/status/pengzhanbo/vite-plugin-mock-dev-server/lint.yml?style=flat-square">
15
15
  <img alt="npm" src="https://img.shields.io/npm/dm/vite-plugin-mock-dev-server?style=flat-square">
16
16
  </p>
17
17
  <br>
@@ -131,25 +131,29 @@ export default defineConfig({
131
131
 
132
132
  - `options.prefix`
133
133
 
134
- Type: `string | string[]`
134
+ **Type:** `string | string[]`
135
135
 
136
136
  Configure custom matches rules for the mock server. Any requests that request path starts with that `prefix` will be proxied to that specified target. If the `prefix` starts with ^, it will be interpreted as a RegExp.
137
137
 
138
138
  > In general, `server.proxy` is sufficient to meet the requirements, and this options is added to be compatible with some scenarios.
139
139
 
140
- Default: `[]`
140
+ **Default:** `[]`
141
141
 
142
142
  - `option.include`
143
+
144
+ **Type:** `string | string[]`
143
145
 
144
146
  Configure to read mock files, which can be a directory, glob, or array
145
147
 
146
- Default `['mock/**/*.mock.{js,ts,cjs,mjs,json,json5}']` (relative for `process.cwd()`)
148
+ **Default:** `['mock/**/*.mock.{js,ts,cjs,mjs,json,json5}']` (relative for `process.cwd()`)
147
149
 
148
150
  - `options.exclude`
151
+
152
+ **Type:** `string | string[]`
149
153
 
150
154
  When you configure the mock files to be read, the files you want to exclude can be a directory, glob, or array
151
155
 
152
- Default
156
+ **Default:**
153
157
  ```ts
154
158
  [
155
159
  '**/node_modules/**',
@@ -161,11 +165,21 @@ export default defineConfig({
161
165
  ]
162
166
  ```
163
167
 
168
+ - `options.reload`
169
+
170
+ **Type:** `boolean`
171
+
172
+ When mock resources are hot updated, only the data content is updated, but the page is not refreshed by default.
173
+ Turn this on when you want to refresh the page every time you modify the mock file.
174
+
175
+
176
+ **Default:** `false`
177
+
164
178
  - `options.formidableOptions`
165
179
 
166
180
  Configure to `formidable`,see [formidable options](https://github.com/node-formidable/formidable#options)
167
181
 
168
- Default: `{}`
182
+ **Default:** `{}`
169
183
 
170
184
  example: Configure to file upload dir
171
185
  ```ts
@@ -180,9 +194,9 @@ export default defineConfig({
180
194
 
181
195
  When building a small, independently deployable mock service.
182
196
 
183
- Type `boolean | ServerBuildOptions`
197
+ **Type:** `boolean | ServerBuildOptions`
184
198
 
185
- Default:`false`
199
+ **Default:** `false`
186
200
 
187
201
  ```ts
188
202
  interface ServerBuildOptions {
@@ -217,7 +231,7 @@ export default defineMock({
217
231
  ```ts
218
232
  export default defineMock({
219
233
  /**
220
- * Address of request,support `/api/user/:id`
234
+ * Address of request,and support like `/api/user/:id`
221
235
  */
222
236
  url: '/api/test',
223
237
  /**
@@ -396,7 +410,7 @@ export default defineMock([
396
410
  }
397
411
  },
398
412
  body: {
399
- message: 'query.a === 1'
413
+ message: 'query.a == 1'
400
414
  }
401
415
  },
402
416
  // Match /api/test?a=2
@@ -408,7 +422,16 @@ export default defineMock([
408
422
  }
409
423
  },
410
424
  body: {
411
- message: 'query.a === 2'
425
+ message: 'query.a == 2'
426
+ }
427
+ },
428
+ {
429
+ /**
430
+ * `?a=3` will resolve to `validator.query`
431
+ */
432
+ url: '/api/test?a=3',
433
+ body: {
434
+ message: 'query.a == 3'
412
435
  }
413
436
  }
414
437
  ])
package/README.zh-CN.md CHANGED
@@ -13,7 +13,7 @@
13
13
  <a href="https://www.npmjs.com/package/vite-plugin-mock-dev-server"><img alt="npm" src="https://img.shields.io/npm/v/vite-plugin-mock-dev-server?style=flat-square"></a>
14
14
  <img alt="node-current" src="https://img.shields.io/node/v/vite-plugin-mock-dev-server?style=flat-square">
15
15
  <img alt="npm peer dependency version" src="https://img.shields.io/npm/dependency-version/vite-plugin-mock-dev-server/peer/vite?style=flat-square">
16
- <img alt="GitHub" src="https://img.shields.io/github/license/pengzhanbo/vite-plugin-mock-dev-server?style=flat-square">
16
+ <img alt="GitHub Workflow Status" src="https://img.shields.io/github/actions/workflow/status/pengzhanbo/vite-plugin-mock-dev-server/lint.yml?style=flat-square">
17
17
  <img alt="npm" src="https://img.shields.io/npm/dm/vite-plugin-mock-dev-server?style=flat-square">
18
18
  </p>
19
19
  <br>
@@ -133,25 +133,29 @@ export default defineConfig({
133
133
 
134
134
  - `options.prefix`
135
135
 
136
- Type: `string | string[]`
136
+ **类型:** `string | string[]`
137
137
 
138
138
  为mock服务器配置自定义匹配规则。任何请求路径以 `prefix` 值开头的请求将被代理到对应的目标。如果 `prefix` 值以 ^ 开头,将被识别为 RegExp。
139
139
 
140
140
  > 一般情况下, `server.proxy` 已经足够满足需求,添加此项是为了与某些场景兼容。
141
141
 
142
- Default: `[]`
142
+ **默认值:** `[]`
143
143
 
144
144
  - `option.include`
145
+
146
+ **类型:** `string | string[]`
145
147
 
146
148
  配置读取 mock文件,可以是一个 目录,glob,或者一个数组
147
149
 
148
- 默认值: `['mock/**/*.mock.{js,ts,cjs,mjs,json,json5}']` (相对于根目录)
150
+ **默认值:** `['mock/**/*.mock.{js,ts,cjs,mjs,json,json5}']` (相对于根目录)
149
151
 
150
152
  - `options.exclude`
153
+
154
+ **类型:** `string | string[]`
151
155
 
152
156
  配置读取 mock文件时,需要排除的文件, 可以是一个 目录、glob、或者一个数组
153
157
 
154
- 默认值:
158
+ **默认值:**
155
159
  ```ts
156
160
  [
157
161
  '**/node_modules/**',
@@ -163,11 +167,19 @@ export default defineConfig({
163
167
  ]
164
168
  ```
165
169
 
170
+ - `options.reload`
171
+
172
+ **Type:** `boolean`
173
+
174
+ mock资源热更新时,仅更新了数据内容,但是默认不重新刷新页面。当你希望每次修改mock文件都刷新页面时,可以打开此选项。
175
+
176
+ **Default:** `false`
177
+
166
178
  - `options.formidableOptions`
167
179
 
168
180
  配置 `formidable`,查看 [formidable options](https://github.com/node-formidable/formidable#options)
169
181
 
170
- 默认值: `{}`
182
+ **默认值:** `{}`
171
183
 
172
184
  示例: 配置文件上传的存放目录
173
185
  ```ts
@@ -182,9 +194,9 @@ export default defineConfig({
182
194
 
183
195
  构建可独立部署的小型mock服务时配置。
184
196
 
185
- 类型: `boolean | ServerBuildOptions`
197
+ **类型:** `boolean | ServerBuildOptions`
186
198
 
187
- 默认值:`false`
199
+ **默认值:**`false`
188
200
 
189
201
  ```ts
190
202
  interface ServerBuildOptions {
@@ -413,6 +425,15 @@ export default defineMock([
413
425
  body: {
414
426
  message: 'query.a === 2'
415
427
  }
428
+ },
429
+ {
430
+ /**
431
+ * `?a=3` 将会解析到 `validator.query`
432
+ */
433
+ url: '/api/test?a=3',
434
+ body: {
435
+ message: 'query.a == 3'
436
+ }
416
437
  }
417
438
  ])
418
439
  ```
package/dist/index.cjs CHANGED
@@ -52,7 +52,7 @@ var import_vite = require("vite");
52
52
 
53
53
  // package.json
54
54
  var name = "vite-plugin-mock-dev-server";
55
- var version = "0.4.3";
55
+ var version = "1.0.0";
56
56
 
57
57
  // src/esbuildPlugin.ts
58
58
  var import_promises = __toESM(require("fs/promises"), 1);
@@ -550,12 +550,14 @@ var _MockLoader = class extends import_node_events.default {
550
550
  return;
551
551
  await this.loadMock(filepath);
552
552
  this.updateMockList();
553
+ this.emit("mock:update-end");
553
554
  });
554
555
  this.on("mock:unlink", async (filepath) => {
555
556
  if (!includeFilter(filepath))
556
557
  return;
557
558
  this.moduleCache.delete(filepath);
558
559
  this.updateMockList();
560
+ this.emit("mock:update-end");
559
561
  });
560
562
  }
561
563
  watchMockEntry() {
@@ -625,10 +627,19 @@ var _MockLoader = class extends import_node_events.default {
625
627
  }
626
628
  const mocks = {};
627
629
  mockList.filter((mock) => mock.enabled || typeof mock.enabled === "undefined").forEach((mock) => {
628
- if (!mocks[mock.url]) {
629
- mocks[mock.url] = [];
630
+ const { pathname, query } = (0, import_node_url3.parse)(mock.url, true);
631
+ if (!mocks[pathname]) {
632
+ mocks[pathname] = [];
633
+ }
634
+ mock.url = pathname;
635
+ const list = mocks[pathname];
636
+ if (query && !isFunction(mock.validator)) {
637
+ mock.validator ?? (mock.validator = {});
638
+ mock.validator.query = Object.assign(
639
+ query,
640
+ mock.validator.query || {}
641
+ );
630
642
  }
631
- const list = mocks[mock.url];
632
643
  mock.validator ? list.unshift(mock) : list.push(mock);
633
644
  });
634
645
  this._mockData = mocks;
@@ -749,7 +760,7 @@ var MockLoader = _MockLoader;
749
760
  MockLoader.EXT_JSON = /\.json5?$/;
750
761
 
751
762
  // src/mockMiddleware.ts
752
- async function mockServerMiddleware(httpServer, config, options) {
763
+ async function mockServerMiddleware(config, options, httpServer, ws) {
753
764
  const include = ensureArray(options.include);
754
765
  const exclude = ensureArray(options.exclude);
755
766
  const define = {};
@@ -766,6 +777,11 @@ async function mockServerMiddleware(httpServer, config, options) {
766
777
  });
767
778
  await loader.load();
768
779
  httpServer == null ? void 0 : httpServer.on("close", () => loader.close());
780
+ loader.on("mock:update-end", () => {
781
+ if (options.reload) {
782
+ ws == null ? void 0 : ws.send({ type: "full-reload" });
783
+ }
784
+ });
769
785
  const proxies = ensureProxies(config.server.proxy || {});
770
786
  const prefix = ensureArray(options.prefix);
771
787
  return baseMiddleware(loader, {
@@ -786,6 +802,7 @@ function mockDevServerPlugin({
786
802
  "**/.git/**",
787
803
  "**/dist/**"
788
804
  ],
805
+ reload = false,
789
806
  formidableOptions = {},
790
807
  build: build3 = false
791
808
  } = {}) {
@@ -793,6 +810,7 @@ function mockDevServerPlugin({
793
810
  prefix,
794
811
  include,
795
812
  exclude,
813
+ reload,
796
814
  formidableOptions: {
797
815
  multiples: true,
798
816
  ...formidableOptions
@@ -840,19 +858,20 @@ function serverPlugin(pluginOptions) {
840
858
  viteConfig = config;
841
859
  config.logger.warn("");
842
860
  },
843
- async configureServer({ middlewares, config, httpServer }) {
861
+ async configureServer({ middlewares, config, httpServer, ws }) {
844
862
  const middleware = await mockServerMiddleware(
845
- httpServer,
846
863
  config,
847
- pluginOptions
864
+ pluginOptions,
865
+ httpServer,
866
+ ws
848
867
  );
849
868
  middlewares.use(middleware);
850
869
  },
851
870
  async configurePreviewServer({ middlewares, httpServer }) {
852
871
  const middleware = await mockServerMiddleware(
853
- httpServer,
854
872
  viteConfig,
855
- pluginOptions
873
+ pluginOptions,
874
+ httpServer
856
875
  );
857
876
  middlewares.use(middleware);
858
877
  }
package/dist/index.d.ts CHANGED
@@ -23,6 +23,13 @@ interface MockServerPluginOptions {
23
23
  * @see https://github.com/micromatch/picomatch#globbing-features
24
24
  */
25
25
  exclude?: string | string[];
26
+ /**
27
+ * mock资源热更新时,仅更新了数据内容,但是默认不重新刷新页面。
28
+ * 当你希望每次修改mock文件都刷新页面时,可以打开此选项。
29
+ *
30
+ * @default false
31
+ */
32
+ reload?: boolean;
26
33
  /**
27
34
  * formidable options
28
35
  * @see https://github.com/node-formidable/formidable#options
@@ -146,7 +153,7 @@ interface MockOptionsItem {
146
153
  type MockOptions = MockOptionsItem[];
147
154
  type FormidableFile = formidable.File | formidable.File[];
148
155
 
149
- declare function mockDevServerPlugin({ prefix, include, exclude, formidableOptions, build, }?: MockServerPluginOptions): Plugin[];
156
+ declare function mockDevServerPlugin({ prefix, include, exclude, reload, formidableOptions, build, }?: MockServerPluginOptions): Plugin[];
150
157
 
151
158
  /**
152
159
  * mock config helper
package/dist/index.js CHANGED
@@ -9,7 +9,7 @@ import { createFilter } from "vite";
9
9
 
10
10
  // package.json
11
11
  var name = "vite-plugin-mock-dev-server";
12
- var version = "0.4.3";
12
+ var version = "1.0.0";
13
13
 
14
14
  // src/esbuildPlugin.ts
15
15
  import fsp from "fs/promises";
@@ -462,7 +462,7 @@ import EventEmitter from "events";
462
462
  import fs3 from "fs";
463
463
  import { createRequire } from "module";
464
464
  import path4 from "path";
465
- import { pathToFileURL } from "url";
465
+ import { pathToFileURL, parse as urlParse2 } from "url";
466
466
  import chokidar from "chokidar";
467
467
  import { build as build2 } from "esbuild";
468
468
  import fastGlob from "fast-glob";
@@ -507,12 +507,14 @@ var _MockLoader = class extends EventEmitter {
507
507
  return;
508
508
  await this.loadMock(filepath);
509
509
  this.updateMockList();
510
+ this.emit("mock:update-end");
510
511
  });
511
512
  this.on("mock:unlink", async (filepath) => {
512
513
  if (!includeFilter(filepath))
513
514
  return;
514
515
  this.moduleCache.delete(filepath);
515
516
  this.updateMockList();
517
+ this.emit("mock:update-end");
516
518
  });
517
519
  }
518
520
  watchMockEntry() {
@@ -582,10 +584,19 @@ var _MockLoader = class extends EventEmitter {
582
584
  }
583
585
  const mocks = {};
584
586
  mockList.filter((mock) => mock.enabled || typeof mock.enabled === "undefined").forEach((mock) => {
585
- if (!mocks[mock.url]) {
586
- mocks[mock.url] = [];
587
+ const { pathname, query } = urlParse2(mock.url, true);
588
+ if (!mocks[pathname]) {
589
+ mocks[pathname] = [];
590
+ }
591
+ mock.url = pathname;
592
+ const list = mocks[pathname];
593
+ if (query && !isFunction(mock.validator)) {
594
+ mock.validator ?? (mock.validator = {});
595
+ mock.validator.query = Object.assign(
596
+ query,
597
+ mock.validator.query || {}
598
+ );
587
599
  }
588
- const list = mocks[mock.url];
589
600
  mock.validator ? list.unshift(mock) : list.push(mock);
590
601
  });
591
602
  this._mockData = mocks;
@@ -706,7 +717,7 @@ var MockLoader = _MockLoader;
706
717
  MockLoader.EXT_JSON = /\.json5?$/;
707
718
 
708
719
  // src/mockMiddleware.ts
709
- async function mockServerMiddleware(httpServer, config, options) {
720
+ async function mockServerMiddleware(config, options, httpServer, ws) {
710
721
  const include = ensureArray(options.include);
711
722
  const exclude = ensureArray(options.exclude);
712
723
  const define = {};
@@ -723,6 +734,11 @@ async function mockServerMiddleware(httpServer, config, options) {
723
734
  });
724
735
  await loader.load();
725
736
  httpServer == null ? void 0 : httpServer.on("close", () => loader.close());
737
+ loader.on("mock:update-end", () => {
738
+ if (options.reload) {
739
+ ws == null ? void 0 : ws.send({ type: "full-reload" });
740
+ }
741
+ });
726
742
  const proxies = ensureProxies(config.server.proxy || {});
727
743
  const prefix = ensureArray(options.prefix);
728
744
  return baseMiddleware(loader, {
@@ -743,6 +759,7 @@ function mockDevServerPlugin({
743
759
  "**/.git/**",
744
760
  "**/dist/**"
745
761
  ],
762
+ reload = false,
746
763
  formidableOptions = {},
747
764
  build: build3 = false
748
765
  } = {}) {
@@ -750,6 +767,7 @@ function mockDevServerPlugin({
750
767
  prefix,
751
768
  include,
752
769
  exclude,
770
+ reload,
753
771
  formidableOptions: {
754
772
  multiples: true,
755
773
  ...formidableOptions
@@ -797,19 +815,20 @@ function serverPlugin(pluginOptions) {
797
815
  viteConfig = config;
798
816
  config.logger.warn("");
799
817
  },
800
- async configureServer({ middlewares, config, httpServer }) {
818
+ async configureServer({ middlewares, config, httpServer, ws }) {
801
819
  const middleware = await mockServerMiddleware(
802
- httpServer,
803
820
  config,
804
- pluginOptions
821
+ pluginOptions,
822
+ httpServer,
823
+ ws
805
824
  );
806
825
  middlewares.use(middleware);
807
826
  },
808
827
  async configurePreviewServer({ middlewares, httpServer }) {
809
828
  const middleware = await mockServerMiddleware(
810
- httpServer,
811
829
  viteConfig,
812
- pluginOptions
830
+ pluginOptions,
831
+ httpServer
813
832
  );
814
833
  middlewares.use(middleware);
815
834
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-mock-dev-server",
3
- "version": "0.4.3",
3
+ "version": "1.0.0",
4
4
  "keywords": [
5
5
  "vite",
6
6
  "plugin",
@@ -33,7 +33,7 @@
33
33
  "chokidar": "^3.5.3",
34
34
  "co-body": "^6.1.0",
35
35
  "debug": "^4.3.4",
36
- "esbuild": "^0.17.6",
36
+ "esbuild": "^0.17.11",
37
37
  "fast-glob": "^3.2.12",
38
38
  "formidable": "^2.1.1",
39
39
  "http-status": "^1.6.2",
@@ -49,16 +49,16 @@
49
49
  "@types/debug": "^4.1.7",
50
50
  "@types/formidable": "^2.0.5",
51
51
  "@types/is-core-module": "^2.2.0",
52
- "@types/node": "^18.11.19",
53
- "bumpp": "^8.2.1",
52
+ "@types/node": "^18.14.6",
53
+ "bumpp": "^9.0.0",
54
54
  "conventional-changelog-cli": "^2.2.2",
55
- "eslint": "^8.33.0",
55
+ "eslint": "^8.35.0",
56
56
  "mockjs": "^1.1.0",
57
- "prettier": "^2.8.3",
57
+ "prettier": "^2.8.4",
58
58
  "tsup": "^6.6.3",
59
59
  "typescript": "^4.9.5",
60
- "vite": "^4.1.1",
61
- "vitepress": "1.0.0-alpha.45",
60
+ "vite": "^4.1.4",
61
+ "vitepress": "1.0.0-alpha.49",
62
62
  "vue": "^3.2.47"
63
63
  },
64
64
  "peerDependencies": {