vite-plugin-mock-dev-server 0.3.19 → 0.3.21
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 +20 -4
- package/README.zh-CN.md +21 -4
- package/dist/index.cjs +41 -14
- package/dist/index.d.ts +12 -1
- package/dist/index.js +31 -11
- package/package.json +13 -12
package/README.md
CHANGED
|
@@ -85,11 +85,11 @@ export default defineConfig({
|
|
|
85
85
|
}
|
|
86
86
|
})
|
|
87
87
|
```
|
|
88
|
-
The plugin reads the configuration for `server.proxy` and enables mock matching
|
|
88
|
+
The plugin reads the configuration for `server.proxy` or `options.prefix` and enables mock matching.
|
|
89
89
|
|
|
90
90
|
The plugin also reads the `define` configuration and supports direct use in mock files.
|
|
91
91
|
|
|
92
|
-
> In a general case, we only need to mock the url with the proxy so that we can proxy and mock through the http service provided by vite
|
|
92
|
+
> In a general case, we only need to mock the url with the proxy so that we can proxy and mock through the http service provided by vite, but you can also configure the mock using 'options.prefix'
|
|
93
93
|
|
|
94
94
|
### Edit Mock File
|
|
95
95
|
|
|
@@ -129,6 +129,16 @@ export default defineConfig({
|
|
|
129
129
|
|
|
130
130
|
#### options
|
|
131
131
|
|
|
132
|
+
- `options.prefix`
|
|
133
|
+
|
|
134
|
+
Type: `string | string[]`
|
|
135
|
+
|
|
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
|
+
|
|
138
|
+
> In general, `server.proxy` is sufficient to meet the requirements, and this options is added to be compatible with some scenarios.
|
|
139
|
+
|
|
140
|
+
Default: `[]`
|
|
141
|
+
|
|
132
142
|
- `option.include`
|
|
133
143
|
|
|
134
144
|
Configure to read mock files, which can be a directory, glob, or array
|
|
@@ -253,14 +263,14 @@ export default defineMock({
|
|
|
253
263
|
* configurations and determines which mock configuration
|
|
254
264
|
* is valid according to the validator.
|
|
255
265
|
*
|
|
256
|
-
* @type { headers?: object; body?: object; query?: object; params?: object }
|
|
266
|
+
* @type { headers?: object; body?: object; query?: object; params?: object; refererQuery?: object }
|
|
257
267
|
*
|
|
258
268
|
* If the validator incoming is an object,
|
|
259
269
|
* then the validation method is the comparison of the
|
|
260
270
|
* strict request of interface, headers/body/query/params
|
|
261
271
|
* each `key-value` congruent, congruent check through
|
|
262
272
|
*
|
|
263
|
-
* @type ({ headers: object; body: object; query: object; params: object }) => boolean
|
|
273
|
+
* @type ({ headers: object; body: object; query: object; params: object; refererQuery: object }) => boolean
|
|
264
274
|
* If the validator is passed a function,
|
|
265
275
|
* it takes the requested interface-related data as an input,
|
|
266
276
|
* gives it to the consumer for custom validation,
|
|
@@ -272,6 +282,12 @@ export default defineMock({
|
|
|
272
282
|
body: {},
|
|
273
283
|
query: {},
|
|
274
284
|
params: {},
|
|
285
|
+
/**
|
|
286
|
+
* refererQuery validates the query in the url of the page from which the request originated,
|
|
287
|
+
* which makes it possible to modify parameters directly in the browser address bar to get
|
|
288
|
+
* different mock data
|
|
289
|
+
*/
|
|
290
|
+
refererQuery: {}
|
|
275
291
|
},
|
|
276
292
|
/**
|
|
277
293
|
*
|
package/README.zh-CN.md
CHANGED
|
@@ -86,11 +86,12 @@ export default defineConfig({
|
|
|
86
86
|
}
|
|
87
87
|
})
|
|
88
88
|
```
|
|
89
|
-
插件会读取 `server.proxy`
|
|
89
|
+
插件会读取 `server.proxy` 或 `options.prefix` 的配置,对匹配的 url 启用mock 匹配。
|
|
90
90
|
|
|
91
91
|
插件也会读取 `define` 配置, 支持在 mock 文件中直接使用。
|
|
92
92
|
|
|
93
|
-
> 因为一般场景下,我们只需要对有代理的url进行mock,这样才能通过 vite 提供的 http 服务进行 代理和 mock
|
|
93
|
+
> 因为一般场景下,我们只需要对有代理的url进行mock,这样才能通过 vite 提供的 http 服务进行 代理和 mock,
|
|
94
|
+
> 但你也可以使用 `options.prefix`配置 mock
|
|
94
95
|
|
|
95
96
|
### 编写mock文件
|
|
96
97
|
|
|
@@ -130,6 +131,16 @@ export default defineConfig({
|
|
|
130
131
|
|
|
131
132
|
#### options
|
|
132
133
|
|
|
134
|
+
- `options.prefix`
|
|
135
|
+
|
|
136
|
+
Type: `string | string[]`
|
|
137
|
+
|
|
138
|
+
为mock服务器配置自定义匹配规则。任何请求路径以 `prefix` 值开头的请求将被代理到对应的目标。如果 `prefix` 值以 ^ 开头,将被识别为 RegExp。
|
|
139
|
+
|
|
140
|
+
> 一般情况下, `server.proxy` 已经足够满足需求,添加此项是为了与某些场景兼容。
|
|
141
|
+
|
|
142
|
+
Default: `[]`
|
|
143
|
+
|
|
133
144
|
- `option.include`
|
|
134
145
|
|
|
135
146
|
配置读取 mock文件,可以是一个 目录,glob,或者一个数组
|
|
@@ -251,13 +262,13 @@ export default defineMock({
|
|
|
251
262
|
* 验证器可以很好的解决这一类问题,将同个 url 分为多个 mock配置,
|
|
252
263
|
* 根据 验证器来判断哪个mock配置生效。
|
|
253
264
|
*
|
|
254
|
-
* @type { headers?: object; body?: object; query?: object; params?: object }
|
|
265
|
+
* @type { headers?: object; body?: object; query?: object; params?: object; refererQuery?: object }
|
|
255
266
|
*
|
|
256
267
|
* 如果 validator 传入的是一个对象,那么验证方式是严格比较 请求的接口
|
|
257
268
|
* 中,headers/body/query/params 的各个`key`的`value`是否全等,
|
|
258
269
|
* 全等则校验通过
|
|
259
270
|
*
|
|
260
|
-
* @type ({ headers: object; body: object; query: object; params: object }) => boolean
|
|
271
|
+
* @type ({ headers: object; body: object; query: object; params: object; refererQuery: object }) => boolean
|
|
261
272
|
* 如果 validator 传入的是一个函数,那么会讲 请求的接口相关数据作为入参,提供给使用者进行自定义校验,并返回一个 boolean
|
|
262
273
|
*
|
|
263
274
|
*/
|
|
@@ -266,6 +277,12 @@ export default defineMock({
|
|
|
266
277
|
body: {},
|
|
267
278
|
query: {},
|
|
268
279
|
params: {},
|
|
280
|
+
/**
|
|
281
|
+
* refererQuery validates the query in the url of the page from which the request originated,
|
|
282
|
+
* which makes it possible to modify parameters directly in the browser address bar to get
|
|
283
|
+
* different mock data
|
|
284
|
+
*/
|
|
285
|
+
refererQuery: {}
|
|
269
286
|
},
|
|
270
287
|
/**
|
|
271
288
|
*
|
package/dist/index.cjs
CHANGED
|
@@ -18,6 +18,10 @@ var __copyProps = (to, from, except, desc) => {
|
|
|
18
18
|
return to;
|
|
19
19
|
};
|
|
20
20
|
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
21
25
|
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
22
26
|
mod
|
|
23
27
|
));
|
|
@@ -33,6 +37,10 @@ __export(src_exports, {
|
|
|
33
37
|
});
|
|
34
38
|
module.exports = __toCommonJS(src_exports);
|
|
35
39
|
|
|
40
|
+
// node_modules/.pnpm/tsup@6.6.3_typescript@4.9.5/node_modules/tsup/assets/cjs_shims.js
|
|
41
|
+
var getImportMetaUrl = () => typeof document === "undefined" ? new URL("file:" + __filename).href : document.currentScript && document.currentScript.src || new URL("main.js", document.baseURI).href;
|
|
42
|
+
var importMetaUrl = /* @__PURE__ */ getImportMetaUrl();
|
|
43
|
+
|
|
36
44
|
// src/build.ts
|
|
37
45
|
var import_node_fs2 = __toESM(require("fs"), 1);
|
|
38
46
|
var import_promises2 = __toESM(require("fs/promises"), 1);
|
|
@@ -44,7 +52,7 @@ var import_vite = require("vite");
|
|
|
44
52
|
|
|
45
53
|
// package.json
|
|
46
54
|
var name = "vite-plugin-mock-dev-server";
|
|
47
|
-
var version = "0.3.
|
|
55
|
+
var version = "0.3.21";
|
|
48
56
|
|
|
49
57
|
// src/esbuildPlugin.ts
|
|
50
58
|
var import_promises = __toESM(require("fs/promises"), 1);
|
|
@@ -97,10 +105,17 @@ var isFunction = (val) => typeof val === "function";
|
|
|
97
105
|
function sleep(timeout) {
|
|
98
106
|
return new Promise((resolve) => setTimeout(resolve, timeout));
|
|
99
107
|
}
|
|
100
|
-
function getDirname(
|
|
101
|
-
return import_node_path2.default.dirname((0, import_node_url.fileURLToPath)(
|
|
108
|
+
function getDirname(importMetaUrl2) {
|
|
109
|
+
return import_node_path2.default.dirname((0, import_node_url.fileURLToPath)(importMetaUrl2));
|
|
102
110
|
}
|
|
103
111
|
var debug = (0, import_debug.default)("vite:plugin-mock-dev-server");
|
|
112
|
+
var ensureArray = (thing) => {
|
|
113
|
+
if (isArray(thing))
|
|
114
|
+
return thing;
|
|
115
|
+
if (thing === null || thing === void 0)
|
|
116
|
+
return [];
|
|
117
|
+
return [thing];
|
|
118
|
+
};
|
|
104
119
|
function lookupFile(dir, formats, options) {
|
|
105
120
|
for (const format of formats) {
|
|
106
121
|
const fullPath = import_node_path2.default.join(dir, format);
|
|
@@ -119,8 +134,8 @@ function lookupFile(dir, formats, options) {
|
|
|
119
134
|
|
|
120
135
|
// src/build.ts
|
|
121
136
|
async function generateMockServer(ctx, config, options) {
|
|
122
|
-
const include =
|
|
123
|
-
const exclude =
|
|
137
|
+
const include = ensureArray(options.include);
|
|
138
|
+
const exclude = ensureArray(options.exclude);
|
|
124
139
|
const define = {};
|
|
125
140
|
if (config.define) {
|
|
126
141
|
for (const key in config.define) {
|
|
@@ -128,6 +143,8 @@ async function generateMockServer(ctx, config, options) {
|
|
|
128
143
|
define[key] = typeof val === "string" ? val : JSON.stringify(val);
|
|
129
144
|
}
|
|
130
145
|
}
|
|
146
|
+
const proxies = Object.keys(config.server.proxy || {});
|
|
147
|
+
const prefix = ensureArray(options.prefix);
|
|
131
148
|
let pkg = {};
|
|
132
149
|
try {
|
|
133
150
|
const pkgStr = lookupFile(config.root, ["package.json"]);
|
|
@@ -151,7 +168,7 @@ async function generateMockServer(ctx, config, options) {
|
|
|
151
168
|
{
|
|
152
169
|
filename: import_node_path3.default.join(outputDir, "index.js"),
|
|
153
170
|
source: generatorServerEntryCode(
|
|
154
|
-
|
|
171
|
+
[...prefix, ...proxies],
|
|
155
172
|
options.build.serverPort
|
|
156
173
|
)
|
|
157
174
|
},
|
|
@@ -211,8 +228,7 @@ function generatePackageJson(pkg, mockDeps) {
|
|
|
211
228
|
});
|
|
212
229
|
return JSON.stringify(mockPkg, null, 2);
|
|
213
230
|
}
|
|
214
|
-
function generatorServerEntryCode(
|
|
215
|
-
const proxies = Object.keys(proxy || {});
|
|
231
|
+
function generatorServerEntryCode(proxies, port = 8080) {
|
|
216
232
|
return `import connect from 'connect'
|
|
217
233
|
import { baseMiddleware } from 'vite-plugin-mock-dev-server'
|
|
218
234
|
import mockData from './mock-data.js'
|
|
@@ -337,7 +353,7 @@ async function parseMultipart(req, options) {
|
|
|
337
353
|
|
|
338
354
|
// src/validator.ts
|
|
339
355
|
function validate(request, validator) {
|
|
340
|
-
return equalObj(request.headers, validator.headers) && equalObj(request.body, validator.body) && equalObj(request.params, validator.params) && equalObj(request.query, validator.query);
|
|
356
|
+
return equalObj(request.headers, validator.headers) && equalObj(request.body, validator.body) && equalObj(request.params, validator.params) && equalObj(request.query, validator.query) && equalObj(request.refererQuery, validator.refererQuery);
|
|
341
357
|
}
|
|
342
358
|
function equalObj(left, right) {
|
|
343
359
|
if (!right)
|
|
@@ -354,6 +370,7 @@ function baseMiddleware(mockLoader, { formidableOptions = {}, proxies }) {
|
|
|
354
370
|
return async function(req, res, next) {
|
|
355
371
|
const method = req.method.toUpperCase();
|
|
356
372
|
const { query, pathname } = (0, import_node_url2.parse)(req.url, true);
|
|
373
|
+
const { query: refererQuery } = (0, import_node_url2.parse)(req.headers.referer || "", true);
|
|
357
374
|
if (!pathname || proxies.length === 0 || !proxies.some((context) => doesProxyContextMatchUrl(context, req.url))) {
|
|
358
375
|
return next();
|
|
359
376
|
}
|
|
@@ -380,6 +397,7 @@ function baseMiddleware(mockLoader, { formidableOptions = {}, proxies }) {
|
|
|
380
397
|
const params2 = urlMatch2.params || {};
|
|
381
398
|
const request = {
|
|
382
399
|
query,
|
|
400
|
+
refererQuery,
|
|
383
401
|
params: params2,
|
|
384
402
|
body: reqBody,
|
|
385
403
|
headers: req.headers
|
|
@@ -406,12 +424,14 @@ function baseMiddleware(mockLoader, { formidableOptions = {}, proxies }) {
|
|
|
406
424
|
const params = urlMatch.params || {};
|
|
407
425
|
req.body = reqBody;
|
|
408
426
|
req.query = query;
|
|
427
|
+
req.refererQuery = refererQuery;
|
|
409
428
|
req.params = params;
|
|
410
429
|
res.setHeader("Content-Type", "application/json");
|
|
411
430
|
res.setHeader("X-Mock", "generate by vite:mock-dev-server");
|
|
412
431
|
if (currentMock.headers) {
|
|
413
432
|
const headers = isFunction(currentMock.headers) ? await currentMock.headers({
|
|
414
433
|
query,
|
|
434
|
+
refererQuery,
|
|
415
435
|
body: reqBody,
|
|
416
436
|
params,
|
|
417
437
|
headers: req.headers
|
|
@@ -425,6 +445,7 @@ function baseMiddleware(mockLoader, { formidableOptions = {}, proxies }) {
|
|
|
425
445
|
if (isFunction(currentMock.body)) {
|
|
426
446
|
body = await currentMock.body({
|
|
427
447
|
query,
|
|
448
|
+
refererQuery,
|
|
428
449
|
body: reqBody,
|
|
429
450
|
params,
|
|
430
451
|
headers: req.headers
|
|
@@ -461,8 +482,7 @@ var import_esbuild2 = require("esbuild");
|
|
|
461
482
|
var import_fast_glob2 = __toESM(require("fast-glob"), 1);
|
|
462
483
|
var import_json52 = __toESM(require("json5"), 1);
|
|
463
484
|
var import_vite2 = require("vite");
|
|
464
|
-
var
|
|
465
|
-
var _dirname = typeof __dirname !== "undefined" ? __dirname : getDirname(import_meta.url);
|
|
485
|
+
var _dirname = getDirname(importMetaUrl);
|
|
466
486
|
var _require = (0, import_node_module.createRequire)(_dirname);
|
|
467
487
|
var _MockLoader = class extends import_node_events.default {
|
|
468
488
|
constructor(options) {
|
|
@@ -531,6 +551,10 @@ var _MockLoader = class extends import_node_events.default {
|
|
|
531
551
|
});
|
|
532
552
|
this.mockWatcher = watcher;
|
|
533
553
|
}
|
|
554
|
+
/**
|
|
555
|
+
* 监听 mock文件依赖的本地文件变动,
|
|
556
|
+
* mock依赖文件更新,mock文件也一并更新
|
|
557
|
+
*/
|
|
534
558
|
watchDeps() {
|
|
535
559
|
const oldDeps = [];
|
|
536
560
|
this.depsWatcher = import_chokidar.default.watch([], {
|
|
@@ -692,8 +716,8 @@ MockLoader.EXT_JSON = /\.json5?$/;
|
|
|
692
716
|
|
|
693
717
|
// src/mockMiddleware.ts
|
|
694
718
|
async function mockServerMiddleware(httpServer, config, options) {
|
|
695
|
-
const include =
|
|
696
|
-
const exclude =
|
|
719
|
+
const include = ensureArray(options.include);
|
|
720
|
+
const exclude = ensureArray(options.exclude);
|
|
697
721
|
const define = {};
|
|
698
722
|
if (config.define) {
|
|
699
723
|
for (const key in config.define) {
|
|
@@ -709,14 +733,16 @@ async function mockServerMiddleware(httpServer, config, options) {
|
|
|
709
733
|
await loader.load();
|
|
710
734
|
httpServer == null ? void 0 : httpServer.on("close", () => loader.close());
|
|
711
735
|
const proxies = Object.keys(config.server.proxy || {});
|
|
736
|
+
const prefix = ensureArray(options.prefix);
|
|
712
737
|
return baseMiddleware(loader, {
|
|
713
738
|
formidableOptions: options.formidableOptions,
|
|
714
|
-
proxies
|
|
739
|
+
proxies: [...prefix, ...proxies]
|
|
715
740
|
});
|
|
716
741
|
}
|
|
717
742
|
|
|
718
743
|
// src/plugin.ts
|
|
719
744
|
function mockDevServerPlugin({
|
|
745
|
+
prefix = [],
|
|
720
746
|
include = ["mock/**/*.mock.{js,ts,cjs,mjs,json,json5}"],
|
|
721
747
|
exclude = [
|
|
722
748
|
"**/node_modules/**",
|
|
@@ -730,6 +756,7 @@ function mockDevServerPlugin({
|
|
|
730
756
|
build: build3 = false
|
|
731
757
|
} = {}) {
|
|
732
758
|
const pluginOptions = {
|
|
759
|
+
prefix,
|
|
733
760
|
include,
|
|
734
761
|
exclude,
|
|
735
762
|
formidableOptions: {
|
package/dist/index.d.ts
CHANGED
|
@@ -5,6 +5,13 @@ import EventEmitter from 'node:events';
|
|
|
5
5
|
import chokidar from 'chokidar';
|
|
6
6
|
|
|
7
7
|
interface MockServerPluginOptions {
|
|
8
|
+
/**
|
|
9
|
+
* 为 mock 服务配置 路径匹配规则,任何请求路径以 prefix 开头的都将被拦截代理。
|
|
10
|
+
* 如果 prefix 以 `^` 开头,将被识别为 `RegExp`。
|
|
11
|
+
*
|
|
12
|
+
* @default []
|
|
13
|
+
*/
|
|
14
|
+
prefix?: string | string[];
|
|
8
15
|
/**
|
|
9
16
|
* glob 字符串匹配 mock 包含的文件
|
|
10
17
|
* @see https://github.com/micromatch/picomatch#globbing-features
|
|
@@ -49,6 +56,10 @@ interface ResponseReq {
|
|
|
49
56
|
* 请求地址中位于 `?` 后面的 queryString,已解析为 json
|
|
50
57
|
*/
|
|
51
58
|
query: Record<string, any>;
|
|
59
|
+
/**
|
|
60
|
+
* 请求 referer 中位于 `?` 后面的 queryString,已解析为 json
|
|
61
|
+
*/
|
|
62
|
+
refererQuery: Record<string, any>;
|
|
52
63
|
/**
|
|
53
64
|
* 请求体中 body 数据
|
|
54
65
|
*/
|
|
@@ -138,7 +149,7 @@ interface MockOptionsItem {
|
|
|
138
149
|
type MockOptions = MockOptionsItem[];
|
|
139
150
|
type FormidableFile = formidable.File | formidable.File[];
|
|
140
151
|
|
|
141
|
-
declare function mockDevServerPlugin({ include, exclude, formidableOptions, build, }?: MockServerPluginOptions): Plugin[];
|
|
152
|
+
declare function mockDevServerPlugin({ prefix, include, exclude, formidableOptions, build, }?: MockServerPluginOptions): Plugin[];
|
|
142
153
|
|
|
143
154
|
/**
|
|
144
155
|
* 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.3.
|
|
12
|
+
var version = "0.3.21";
|
|
13
13
|
|
|
14
14
|
// src/esbuildPlugin.ts
|
|
15
15
|
import fsp from "fs/promises";
|
|
@@ -66,6 +66,13 @@ function getDirname(importMetaUrl) {
|
|
|
66
66
|
return path2.dirname(fileURLToPath(importMetaUrl));
|
|
67
67
|
}
|
|
68
68
|
var debug = Debug("vite:plugin-mock-dev-server");
|
|
69
|
+
var ensureArray = (thing) => {
|
|
70
|
+
if (isArray(thing))
|
|
71
|
+
return thing;
|
|
72
|
+
if (thing === null || thing === void 0)
|
|
73
|
+
return [];
|
|
74
|
+
return [thing];
|
|
75
|
+
};
|
|
69
76
|
function lookupFile(dir, formats, options) {
|
|
70
77
|
for (const format of formats) {
|
|
71
78
|
const fullPath = path2.join(dir, format);
|
|
@@ -84,8 +91,8 @@ function lookupFile(dir, formats, options) {
|
|
|
84
91
|
|
|
85
92
|
// src/build.ts
|
|
86
93
|
async function generateMockServer(ctx, config, options) {
|
|
87
|
-
const include =
|
|
88
|
-
const exclude =
|
|
94
|
+
const include = ensureArray(options.include);
|
|
95
|
+
const exclude = ensureArray(options.exclude);
|
|
89
96
|
const define = {};
|
|
90
97
|
if (config.define) {
|
|
91
98
|
for (const key in config.define) {
|
|
@@ -93,6 +100,8 @@ async function generateMockServer(ctx, config, options) {
|
|
|
93
100
|
define[key] = typeof val === "string" ? val : JSON.stringify(val);
|
|
94
101
|
}
|
|
95
102
|
}
|
|
103
|
+
const proxies = Object.keys(config.server.proxy || {});
|
|
104
|
+
const prefix = ensureArray(options.prefix);
|
|
96
105
|
let pkg = {};
|
|
97
106
|
try {
|
|
98
107
|
const pkgStr = lookupFile(config.root, ["package.json"]);
|
|
@@ -116,7 +125,7 @@ async function generateMockServer(ctx, config, options) {
|
|
|
116
125
|
{
|
|
117
126
|
filename: path3.join(outputDir, "index.js"),
|
|
118
127
|
source: generatorServerEntryCode(
|
|
119
|
-
|
|
128
|
+
[...prefix, ...proxies],
|
|
120
129
|
options.build.serverPort
|
|
121
130
|
)
|
|
122
131
|
},
|
|
@@ -176,8 +185,7 @@ function generatePackageJson(pkg, mockDeps) {
|
|
|
176
185
|
});
|
|
177
186
|
return JSON.stringify(mockPkg, null, 2);
|
|
178
187
|
}
|
|
179
|
-
function generatorServerEntryCode(
|
|
180
|
-
const proxies = Object.keys(proxy || {});
|
|
188
|
+
function generatorServerEntryCode(proxies, port = 8080) {
|
|
181
189
|
return `import connect from 'connect'
|
|
182
190
|
import { baseMiddleware } from 'vite-plugin-mock-dev-server'
|
|
183
191
|
import mockData from './mock-data.js'
|
|
@@ -302,7 +310,7 @@ async function parseMultipart(req, options) {
|
|
|
302
310
|
|
|
303
311
|
// src/validator.ts
|
|
304
312
|
function validate(request, validator) {
|
|
305
|
-
return equalObj(request.headers, validator.headers) && equalObj(request.body, validator.body) && equalObj(request.params, validator.params) && equalObj(request.query, validator.query);
|
|
313
|
+
return equalObj(request.headers, validator.headers) && equalObj(request.body, validator.body) && equalObj(request.params, validator.params) && equalObj(request.query, validator.query) && equalObj(request.refererQuery, validator.refererQuery);
|
|
306
314
|
}
|
|
307
315
|
function equalObj(left, right) {
|
|
308
316
|
if (!right)
|
|
@@ -319,6 +327,7 @@ function baseMiddleware(mockLoader, { formidableOptions = {}, proxies }) {
|
|
|
319
327
|
return async function(req, res, next) {
|
|
320
328
|
const method = req.method.toUpperCase();
|
|
321
329
|
const { query, pathname } = urlParse(req.url, true);
|
|
330
|
+
const { query: refererQuery } = urlParse(req.headers.referer || "", true);
|
|
322
331
|
if (!pathname || proxies.length === 0 || !proxies.some((context) => doesProxyContextMatchUrl(context, req.url))) {
|
|
323
332
|
return next();
|
|
324
333
|
}
|
|
@@ -345,6 +354,7 @@ function baseMiddleware(mockLoader, { formidableOptions = {}, proxies }) {
|
|
|
345
354
|
const params2 = urlMatch2.params || {};
|
|
346
355
|
const request = {
|
|
347
356
|
query,
|
|
357
|
+
refererQuery,
|
|
348
358
|
params: params2,
|
|
349
359
|
body: reqBody,
|
|
350
360
|
headers: req.headers
|
|
@@ -371,12 +381,14 @@ function baseMiddleware(mockLoader, { formidableOptions = {}, proxies }) {
|
|
|
371
381
|
const params = urlMatch.params || {};
|
|
372
382
|
req.body = reqBody;
|
|
373
383
|
req.query = query;
|
|
384
|
+
req.refererQuery = refererQuery;
|
|
374
385
|
req.params = params;
|
|
375
386
|
res.setHeader("Content-Type", "application/json");
|
|
376
387
|
res.setHeader("X-Mock", "generate by vite:mock-dev-server");
|
|
377
388
|
if (currentMock.headers) {
|
|
378
389
|
const headers = isFunction(currentMock.headers) ? await currentMock.headers({
|
|
379
390
|
query,
|
|
391
|
+
refererQuery,
|
|
380
392
|
body: reqBody,
|
|
381
393
|
params,
|
|
382
394
|
headers: req.headers
|
|
@@ -390,6 +402,7 @@ function baseMiddleware(mockLoader, { formidableOptions = {}, proxies }) {
|
|
|
390
402
|
if (isFunction(currentMock.body)) {
|
|
391
403
|
body = await currentMock.body({
|
|
392
404
|
query,
|
|
405
|
+
refererQuery,
|
|
393
406
|
body: reqBody,
|
|
394
407
|
params,
|
|
395
408
|
headers: req.headers
|
|
@@ -426,7 +439,7 @@ import { build as build2 } from "esbuild";
|
|
|
426
439
|
import fastGlob from "fast-glob";
|
|
427
440
|
import JSON52 from "json5";
|
|
428
441
|
import { createFilter as createFilter2 } from "vite";
|
|
429
|
-
var _dirname =
|
|
442
|
+
var _dirname = getDirname(import.meta.url);
|
|
430
443
|
var _require = createRequire(_dirname);
|
|
431
444
|
var _MockLoader = class extends EventEmitter {
|
|
432
445
|
constructor(options) {
|
|
@@ -495,6 +508,10 @@ var _MockLoader = class extends EventEmitter {
|
|
|
495
508
|
});
|
|
496
509
|
this.mockWatcher = watcher;
|
|
497
510
|
}
|
|
511
|
+
/**
|
|
512
|
+
* 监听 mock文件依赖的本地文件变动,
|
|
513
|
+
* mock依赖文件更新,mock文件也一并更新
|
|
514
|
+
*/
|
|
498
515
|
watchDeps() {
|
|
499
516
|
const oldDeps = [];
|
|
500
517
|
this.depsWatcher = chokidar.watch([], {
|
|
@@ -656,8 +673,8 @@ MockLoader.EXT_JSON = /\.json5?$/;
|
|
|
656
673
|
|
|
657
674
|
// src/mockMiddleware.ts
|
|
658
675
|
async function mockServerMiddleware(httpServer, config, options) {
|
|
659
|
-
const include =
|
|
660
|
-
const exclude =
|
|
676
|
+
const include = ensureArray(options.include);
|
|
677
|
+
const exclude = ensureArray(options.exclude);
|
|
661
678
|
const define = {};
|
|
662
679
|
if (config.define) {
|
|
663
680
|
for (const key in config.define) {
|
|
@@ -673,14 +690,16 @@ async function mockServerMiddleware(httpServer, config, options) {
|
|
|
673
690
|
await loader.load();
|
|
674
691
|
httpServer == null ? void 0 : httpServer.on("close", () => loader.close());
|
|
675
692
|
const proxies = Object.keys(config.server.proxy || {});
|
|
693
|
+
const prefix = ensureArray(options.prefix);
|
|
676
694
|
return baseMiddleware(loader, {
|
|
677
695
|
formidableOptions: options.formidableOptions,
|
|
678
|
-
proxies
|
|
696
|
+
proxies: [...prefix, ...proxies]
|
|
679
697
|
});
|
|
680
698
|
}
|
|
681
699
|
|
|
682
700
|
// src/plugin.ts
|
|
683
701
|
function mockDevServerPlugin({
|
|
702
|
+
prefix = [],
|
|
684
703
|
include = ["mock/**/*.mock.{js,ts,cjs,mjs,json,json5}"],
|
|
685
704
|
exclude = [
|
|
686
705
|
"**/node_modules/**",
|
|
@@ -694,6 +713,7 @@ function mockDevServerPlugin({
|
|
|
694
713
|
build: build3 = false
|
|
695
714
|
} = {}) {
|
|
696
715
|
const pluginOptions = {
|
|
716
|
+
prefix,
|
|
697
717
|
include,
|
|
698
718
|
exclude,
|
|
699
719
|
formidableOptions: {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-mock-dev-server",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.21",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"vite",
|
|
6
6
|
"plugin",
|
|
@@ -33,11 +33,11 @@
|
|
|
33
33
|
"chokidar": "^3.5.3",
|
|
34
34
|
"co-body": "^6.1.0",
|
|
35
35
|
"debug": "^4.3.4",
|
|
36
|
-
"esbuild": "^0.
|
|
36
|
+
"esbuild": "^0.17.6",
|
|
37
37
|
"fast-glob": "^3.2.12",
|
|
38
38
|
"formidable": "^2.1.1",
|
|
39
39
|
"is-core-module": "^2.11.0",
|
|
40
|
-
"json5": "^2.2.
|
|
40
|
+
"json5": "^2.2.3",
|
|
41
41
|
"path-to-regexp": "^6.2.1"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
@@ -47,22 +47,22 @@
|
|
|
47
47
|
"@types/debug": "^4.1.7",
|
|
48
48
|
"@types/formidable": "^2.0.5",
|
|
49
49
|
"@types/is-core-module": "^2.2.0",
|
|
50
|
-
"@types/node": "^18.11.
|
|
50
|
+
"@types/node": "^18.11.19",
|
|
51
51
|
"bumpp": "^8.2.1",
|
|
52
52
|
"conventional-changelog-cli": "^2.2.2",
|
|
53
|
-
"eslint": "^8.
|
|
53
|
+
"eslint": "^8.33.0",
|
|
54
54
|
"mockjs": "^1.1.0",
|
|
55
|
-
"prettier": "^2.8.
|
|
56
|
-
"tsup": "^6.
|
|
57
|
-
"typescript": "^4.9.
|
|
58
|
-
"vite": "^4.
|
|
59
|
-
"vitepress": "1.0.0-alpha.
|
|
60
|
-
"vue": "^3.2.
|
|
55
|
+
"prettier": "^2.8.3",
|
|
56
|
+
"tsup": "^6.6.3",
|
|
57
|
+
"typescript": "^4.9.5",
|
|
58
|
+
"vite": "^4.1.1",
|
|
59
|
+
"vitepress": "1.0.0-alpha.45",
|
|
60
|
+
"vue": "^3.2.47"
|
|
61
61
|
},
|
|
62
62
|
"peerDependencies": {
|
|
63
63
|
"vite": ">=3.0.0"
|
|
64
64
|
},
|
|
65
|
-
"packageManager": "pnpm@7.
|
|
65
|
+
"packageManager": "pnpm@7.26.3",
|
|
66
66
|
"engines": {
|
|
67
67
|
"node": "^14.18.0 || >=16"
|
|
68
68
|
},
|
|
@@ -70,6 +70,7 @@
|
|
|
70
70
|
"entry": [
|
|
71
71
|
"src/index.ts"
|
|
72
72
|
],
|
|
73
|
+
"shims": true,
|
|
73
74
|
"sourcemap": false,
|
|
74
75
|
"dts": true,
|
|
75
76
|
"splitting": false,
|