vite-plugin-mock-dev-server 0.3.19 → 0.3.20
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 +12 -2
- package/README.zh-CN.md +13 -2
- package/dist/index.cjs +20 -9
- package/dist/index.d.ts +8 -1
- package/dist/index.js +20 -9
- package/package.json +11 -11
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
|
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,或者一个数组
|
package/dist/index.cjs
CHANGED
|
@@ -44,7 +44,7 @@ var import_vite = require("vite");
|
|
|
44
44
|
|
|
45
45
|
// package.json
|
|
46
46
|
var name = "vite-plugin-mock-dev-server";
|
|
47
|
-
var version = "0.3.
|
|
47
|
+
var version = "0.3.20";
|
|
48
48
|
|
|
49
49
|
// src/esbuildPlugin.ts
|
|
50
50
|
var import_promises = __toESM(require("fs/promises"), 1);
|
|
@@ -101,6 +101,13 @@ function getDirname(importMetaUrl) {
|
|
|
101
101
|
return import_node_path2.default.dirname((0, import_node_url.fileURLToPath)(importMetaUrl));
|
|
102
102
|
}
|
|
103
103
|
var debug = (0, import_debug.default)("vite:plugin-mock-dev-server");
|
|
104
|
+
var ensureArray = (thing) => {
|
|
105
|
+
if (isArray(thing))
|
|
106
|
+
return thing;
|
|
107
|
+
if (thing === null || thing === void 0)
|
|
108
|
+
return [];
|
|
109
|
+
return [thing];
|
|
110
|
+
};
|
|
104
111
|
function lookupFile(dir, formats, options) {
|
|
105
112
|
for (const format of formats) {
|
|
106
113
|
const fullPath = import_node_path2.default.join(dir, format);
|
|
@@ -119,8 +126,8 @@ function lookupFile(dir, formats, options) {
|
|
|
119
126
|
|
|
120
127
|
// src/build.ts
|
|
121
128
|
async function generateMockServer(ctx, config, options) {
|
|
122
|
-
const include =
|
|
123
|
-
const exclude =
|
|
129
|
+
const include = ensureArray(options.include);
|
|
130
|
+
const exclude = ensureArray(options.exclude);
|
|
124
131
|
const define = {};
|
|
125
132
|
if (config.define) {
|
|
126
133
|
for (const key in config.define) {
|
|
@@ -128,6 +135,8 @@ async function generateMockServer(ctx, config, options) {
|
|
|
128
135
|
define[key] = typeof val === "string" ? val : JSON.stringify(val);
|
|
129
136
|
}
|
|
130
137
|
}
|
|
138
|
+
const proxies = Object.keys(config.server.proxy || {});
|
|
139
|
+
const prefix = ensureArray(options.prefix);
|
|
131
140
|
let pkg = {};
|
|
132
141
|
try {
|
|
133
142
|
const pkgStr = lookupFile(config.root, ["package.json"]);
|
|
@@ -151,7 +160,7 @@ async function generateMockServer(ctx, config, options) {
|
|
|
151
160
|
{
|
|
152
161
|
filename: import_node_path3.default.join(outputDir, "index.js"),
|
|
153
162
|
source: generatorServerEntryCode(
|
|
154
|
-
|
|
163
|
+
[...prefix, ...proxies],
|
|
155
164
|
options.build.serverPort
|
|
156
165
|
)
|
|
157
166
|
},
|
|
@@ -211,8 +220,7 @@ function generatePackageJson(pkg, mockDeps) {
|
|
|
211
220
|
});
|
|
212
221
|
return JSON.stringify(mockPkg, null, 2);
|
|
213
222
|
}
|
|
214
|
-
function generatorServerEntryCode(
|
|
215
|
-
const proxies = Object.keys(proxy || {});
|
|
223
|
+
function generatorServerEntryCode(proxies, port = 8080) {
|
|
216
224
|
return `import connect from 'connect'
|
|
217
225
|
import { baseMiddleware } from 'vite-plugin-mock-dev-server'
|
|
218
226
|
import mockData from './mock-data.js'
|
|
@@ -692,8 +700,8 @@ MockLoader.EXT_JSON = /\.json5?$/;
|
|
|
692
700
|
|
|
693
701
|
// src/mockMiddleware.ts
|
|
694
702
|
async function mockServerMiddleware(httpServer, config, options) {
|
|
695
|
-
const include =
|
|
696
|
-
const exclude =
|
|
703
|
+
const include = ensureArray(options.include);
|
|
704
|
+
const exclude = ensureArray(options.exclude);
|
|
697
705
|
const define = {};
|
|
698
706
|
if (config.define) {
|
|
699
707
|
for (const key in config.define) {
|
|
@@ -709,14 +717,16 @@ async function mockServerMiddleware(httpServer, config, options) {
|
|
|
709
717
|
await loader.load();
|
|
710
718
|
httpServer == null ? void 0 : httpServer.on("close", () => loader.close());
|
|
711
719
|
const proxies = Object.keys(config.server.proxy || {});
|
|
720
|
+
const prefix = ensureArray(options.prefix);
|
|
712
721
|
return baseMiddleware(loader, {
|
|
713
722
|
formidableOptions: options.formidableOptions,
|
|
714
|
-
proxies
|
|
723
|
+
proxies: [...prefix, ...proxies]
|
|
715
724
|
});
|
|
716
725
|
}
|
|
717
726
|
|
|
718
727
|
// src/plugin.ts
|
|
719
728
|
function mockDevServerPlugin({
|
|
729
|
+
prefix = [],
|
|
720
730
|
include = ["mock/**/*.mock.{js,ts,cjs,mjs,json,json5}"],
|
|
721
731
|
exclude = [
|
|
722
732
|
"**/node_modules/**",
|
|
@@ -730,6 +740,7 @@ function mockDevServerPlugin({
|
|
|
730
740
|
build: build3 = false
|
|
731
741
|
} = {}) {
|
|
732
742
|
const pluginOptions = {
|
|
743
|
+
prefix,
|
|
733
744
|
include,
|
|
734
745
|
exclude,
|
|
735
746
|
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
|
|
@@ -138,7 +145,7 @@ interface MockOptionsItem {
|
|
|
138
145
|
type MockOptions = MockOptionsItem[];
|
|
139
146
|
type FormidableFile = formidable.File | formidable.File[];
|
|
140
147
|
|
|
141
|
-
declare function mockDevServerPlugin({ include, exclude, formidableOptions, build, }?: MockServerPluginOptions): Plugin[];
|
|
148
|
+
declare function mockDevServerPlugin({ prefix, include, exclude, formidableOptions, build, }?: MockServerPluginOptions): Plugin[];
|
|
142
149
|
|
|
143
150
|
/**
|
|
144
151
|
* 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.20";
|
|
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'
|
|
@@ -656,8 +664,8 @@ MockLoader.EXT_JSON = /\.json5?$/;
|
|
|
656
664
|
|
|
657
665
|
// src/mockMiddleware.ts
|
|
658
666
|
async function mockServerMiddleware(httpServer, config, options) {
|
|
659
|
-
const include =
|
|
660
|
-
const exclude =
|
|
667
|
+
const include = ensureArray(options.include);
|
|
668
|
+
const exclude = ensureArray(options.exclude);
|
|
661
669
|
const define = {};
|
|
662
670
|
if (config.define) {
|
|
663
671
|
for (const key in config.define) {
|
|
@@ -673,14 +681,16 @@ async function mockServerMiddleware(httpServer, config, options) {
|
|
|
673
681
|
await loader.load();
|
|
674
682
|
httpServer == null ? void 0 : httpServer.on("close", () => loader.close());
|
|
675
683
|
const proxies = Object.keys(config.server.proxy || {});
|
|
684
|
+
const prefix = ensureArray(options.prefix);
|
|
676
685
|
return baseMiddleware(loader, {
|
|
677
686
|
formidableOptions: options.formidableOptions,
|
|
678
|
-
proxies
|
|
687
|
+
proxies: [...prefix, ...proxies]
|
|
679
688
|
});
|
|
680
689
|
}
|
|
681
690
|
|
|
682
691
|
// src/plugin.ts
|
|
683
692
|
function mockDevServerPlugin({
|
|
693
|
+
prefix = [],
|
|
684
694
|
include = ["mock/**/*.mock.{js,ts,cjs,mjs,json,json5}"],
|
|
685
695
|
exclude = [
|
|
686
696
|
"**/node_modules/**",
|
|
@@ -694,6 +704,7 @@ function mockDevServerPlugin({
|
|
|
694
704
|
build: build3 = false
|
|
695
705
|
} = {}) {
|
|
696
706
|
const pluginOptions = {
|
|
707
|
+
prefix,
|
|
697
708
|
include,
|
|
698
709
|
exclude,
|
|
699
710
|
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.20",
|
|
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.
|
|
55
|
+
"prettier": "^2.8.3",
|
|
56
56
|
"tsup": "^6.5.0",
|
|
57
|
-
"typescript": "^4.9.
|
|
58
|
-
"vite": "^4.
|
|
59
|
-
"vitepress": "1.0.0-alpha.
|
|
60
|
-
"vue": "^3.2.
|
|
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
|
},
|