vite-plugin-mock-dev-server 1.0.6 → 1.0.7
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 -0
- package/README.zh-CN.md +19 -0
- package/dist/index.cjs +60 -11
- package/dist/index.d.ts +11 -2
- package/dist/index.js +59 -11
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -38,6 +38,7 @@
|
|
|
38
38
|
- 🔥 HMR
|
|
39
39
|
- ⚖️ Use `server.proxy`
|
|
40
40
|
- 🍕 Support `viteConfig.define` in mock file
|
|
41
|
+
- ⚓️ Support `resolve.alias`
|
|
41
42
|
- 📤 Support `multipart` content-type,mock upload file.
|
|
42
43
|
- 🌈 Support `vite preview` mode
|
|
43
44
|
- 🗂 Support building small independent deployable mock services.
|
|
@@ -132,6 +133,7 @@ export default defineConfig({
|
|
|
132
133
|
})
|
|
133
134
|
```
|
|
134
135
|
|
|
136
|
+
|
|
135
137
|
#### options
|
|
136
138
|
|
|
137
139
|
- `options.prefix`
|
|
@@ -227,6 +229,24 @@ export default defineMock({
|
|
|
227
229
|
})
|
|
228
230
|
```
|
|
229
231
|
|
|
232
|
+
### createDefineMock(transformer)
|
|
233
|
+
|
|
234
|
+
Return a custom defineMock function to support preprocessing of mock config.
|
|
235
|
+
|
|
236
|
+
```ts
|
|
237
|
+
import path from 'path'
|
|
238
|
+
import { createDefineMock } from 'vite-plugin-mock-dev-server'
|
|
239
|
+
|
|
240
|
+
// Preprocessed mock url
|
|
241
|
+
const defineAPIMock = createDefineMock((mock) => {
|
|
242
|
+
mock.url = path.join('/api', mock.url)
|
|
243
|
+
})
|
|
244
|
+
|
|
245
|
+
export default defineApiMock({
|
|
246
|
+
url: '/test' // Complete as '/api/test'
|
|
247
|
+
})
|
|
248
|
+
```
|
|
249
|
+
|
|
230
250
|
## Mock Configuration
|
|
231
251
|
|
|
232
252
|
```ts
|
package/README.zh-CN.md
CHANGED
|
@@ -39,6 +39,7 @@
|
|
|
39
39
|
- 🔥 热更新
|
|
40
40
|
- ⚖️ 使用 `server.proxy` 配置
|
|
41
41
|
- 🍕 支持在 mock文件中使用 `viteConfig.define`配置字段
|
|
42
|
+
- ⚓️ 支持 `resolve.alias`
|
|
42
43
|
- 📤 支持 multipart 类型,模拟文件上传
|
|
43
44
|
- 🌈 支持 `vite preview` 模式
|
|
44
45
|
- 🗂 支持构建可独立部署的小型mock服务
|
|
@@ -225,6 +226,24 @@ export default defineMock({
|
|
|
225
226
|
})
|
|
226
227
|
```
|
|
227
228
|
|
|
229
|
+
### createDefineMock(transformer)
|
|
230
|
+
|
|
231
|
+
返回一个自定义的 defineMock 函数,用于支持对 mock config 的预处理。
|
|
232
|
+
|
|
233
|
+
```ts
|
|
234
|
+
import path from 'path'
|
|
235
|
+
import { createDefineMock } from 'vite-plugin-mock-dev-server'
|
|
236
|
+
|
|
237
|
+
// 预处理 mock url
|
|
238
|
+
const defineAPIMock = createDefineMock((mock) => {
|
|
239
|
+
mock.url = path.join('/api', mock.url)
|
|
240
|
+
})
|
|
241
|
+
|
|
242
|
+
export default defineApiMock({
|
|
243
|
+
url: '/test' // 补全为 '/api/test'
|
|
244
|
+
})
|
|
245
|
+
```
|
|
246
|
+
|
|
228
247
|
## Mock 配置
|
|
229
248
|
|
|
230
249
|
```ts
|
package/dist/index.cjs
CHANGED
|
@@ -31,6 +31,7 @@ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: tru
|
|
|
31
31
|
var src_exports = {};
|
|
32
32
|
__export(src_exports, {
|
|
33
33
|
baseMiddleware: () => baseMiddleware,
|
|
34
|
+
createDefineMock: () => createDefineMock,
|
|
34
35
|
default: () => src_default,
|
|
35
36
|
defineMock: () => defineMock,
|
|
36
37
|
mockDevServerPlugin: () => mockDevServerPlugin,
|
|
@@ -53,7 +54,7 @@ var import_vite = require("vite");
|
|
|
53
54
|
|
|
54
55
|
// package.json
|
|
55
56
|
var name = "vite-plugin-mock-dev-server";
|
|
56
|
-
var version = "1.0.
|
|
57
|
+
var version = "1.0.7";
|
|
57
58
|
|
|
58
59
|
// src/esbuildPlugin.ts
|
|
59
60
|
var import_promises = __toESM(require("fs/promises"), 1);
|
|
@@ -95,6 +96,41 @@ var jsonLoader = {
|
|
|
95
96
|
});
|
|
96
97
|
}
|
|
97
98
|
};
|
|
99
|
+
var aliasPlugin = (alias) => {
|
|
100
|
+
return {
|
|
101
|
+
name: "alias-plugin",
|
|
102
|
+
setup(build3) {
|
|
103
|
+
build3.onResolve({ filter: /.*/ }, async ({ path: id }) => {
|
|
104
|
+
const matchedEntry = alias.find(({ find: find2 }) => matches(find2, id));
|
|
105
|
+
if (!matchedEntry) {
|
|
106
|
+
return null;
|
|
107
|
+
}
|
|
108
|
+
const { find, replacement } = matchedEntry;
|
|
109
|
+
const result = await build3.resolve(id.replace(find, replacement), {
|
|
110
|
+
kind: "import-statement",
|
|
111
|
+
resolveDir: replacement,
|
|
112
|
+
namespace: "file"
|
|
113
|
+
});
|
|
114
|
+
return {
|
|
115
|
+
path: result.path,
|
|
116
|
+
external: false
|
|
117
|
+
};
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
};
|
|
121
|
+
};
|
|
122
|
+
function matches(pattern, importee) {
|
|
123
|
+
if (pattern instanceof RegExp) {
|
|
124
|
+
return pattern.test(importee);
|
|
125
|
+
}
|
|
126
|
+
if (importee.length < pattern.length) {
|
|
127
|
+
return false;
|
|
128
|
+
}
|
|
129
|
+
if (importee === pattern) {
|
|
130
|
+
return true;
|
|
131
|
+
}
|
|
132
|
+
return importee.startsWith(`${pattern}/`);
|
|
133
|
+
}
|
|
98
134
|
|
|
99
135
|
// src/utils.ts
|
|
100
136
|
var import_node_fs = __toESM(require("fs"), 1);
|
|
@@ -174,7 +210,11 @@ async function generateMockServer(ctx, config, options) {
|
|
|
174
210
|
const content = await generateMockEntryCode(process.cwd(), include, exclude);
|
|
175
211
|
const mockEntry = import_node_path3.default.join(config.root, `mock-data-${Date.now()}.js`);
|
|
176
212
|
await import_promises2.default.writeFile(mockEntry, content, "utf-8");
|
|
177
|
-
const { code, deps } = await buildMockEntry(
|
|
213
|
+
const { code, deps } = await buildMockEntry(
|
|
214
|
+
mockEntry,
|
|
215
|
+
define,
|
|
216
|
+
config.resolve.alias
|
|
217
|
+
);
|
|
178
218
|
const mockDeps = getMockDependencies(deps);
|
|
179
219
|
await import_promises2.default.unlink(mockEntry);
|
|
180
220
|
const outputList = [
|
|
@@ -288,7 +328,7 @@ const mockList = exporters.map((raw) => raw && raw.default
|
|
|
288
328
|
export default transformMockData(mockList);
|
|
289
329
|
`;
|
|
290
330
|
}
|
|
291
|
-
async function buildMockEntry(inputFile, define) {
|
|
331
|
+
async function buildMockEntry(inputFile, define, alias) {
|
|
292
332
|
var _a;
|
|
293
333
|
try {
|
|
294
334
|
const result = await (0, import_esbuild.build)({
|
|
@@ -301,7 +341,7 @@ async function buildMockEntry(inputFile, define) {
|
|
|
301
341
|
metafile: true,
|
|
302
342
|
format: "esm",
|
|
303
343
|
define,
|
|
304
|
-
plugins: [externalizeDeps, json5Loader, jsonLoader]
|
|
344
|
+
plugins: [aliasPlugin(alias), externalizeDeps, json5Loader, jsonLoader]
|
|
305
345
|
});
|
|
306
346
|
return {
|
|
307
347
|
code: result.outputFiles[0].text,
|
|
@@ -740,7 +780,7 @@ var _MockLoader = class extends import_node_events.default {
|
|
|
740
780
|
metafile: true,
|
|
741
781
|
format: isESM ? "esm" : "cjs",
|
|
742
782
|
define: this.options.define,
|
|
743
|
-
plugins: [externalizeDeps]
|
|
783
|
+
plugins: [aliasPlugin(this.options.alias), externalizeDeps]
|
|
744
784
|
});
|
|
745
785
|
return {
|
|
746
786
|
code: result.outputFiles[0].text,
|
|
@@ -771,7 +811,8 @@ async function mockServerMiddleware(config, options, httpServer, ws) {
|
|
|
771
811
|
const loader = new MockLoader({
|
|
772
812
|
include,
|
|
773
813
|
exclude,
|
|
774
|
-
define
|
|
814
|
+
define,
|
|
815
|
+
alias: config.resolve.alias
|
|
775
816
|
});
|
|
776
817
|
await loader.load();
|
|
777
818
|
httpServer == null ? void 0 : httpServer.on("close", () => loader.close());
|
|
@@ -792,11 +833,7 @@ async function mockServerMiddleware(config, options, httpServer, ws) {
|
|
|
792
833
|
function mockDevServerPlugin({
|
|
793
834
|
prefix = [],
|
|
794
835
|
include = ["mock/**/*.mock.{js,ts,cjs,mjs,json,json5}"],
|
|
795
|
-
exclude = [
|
|
796
|
-
"**/node_modules/**",
|
|
797
|
-
"**/.vscode/**",
|
|
798
|
-
"**/.git/**"
|
|
799
|
-
],
|
|
836
|
+
exclude = ["**/node_modules/**", "**/.vscode/**", "**/.git/**"],
|
|
800
837
|
reload = false,
|
|
801
838
|
formidableOptions = {},
|
|
802
839
|
build: build3 = false
|
|
@@ -877,12 +914,24 @@ function serverPlugin(pluginOptions) {
|
|
|
877
914
|
function defineMock(config) {
|
|
878
915
|
return config;
|
|
879
916
|
}
|
|
917
|
+
function createDefineMock(transformer) {
|
|
918
|
+
const define = (config) => {
|
|
919
|
+
if (isArray(config)) {
|
|
920
|
+
config = config.map((item) => transformer(item) || item);
|
|
921
|
+
} else {
|
|
922
|
+
config = transformer(config) || config;
|
|
923
|
+
}
|
|
924
|
+
return config;
|
|
925
|
+
};
|
|
926
|
+
return define;
|
|
927
|
+
}
|
|
880
928
|
|
|
881
929
|
// src/index.ts
|
|
882
930
|
var src_default = mockDevServerPlugin;
|
|
883
931
|
// Annotate the CommonJS export names for ESM import in node:
|
|
884
932
|
0 && (module.exports = {
|
|
885
933
|
baseMiddleware,
|
|
934
|
+
createDefineMock,
|
|
886
935
|
defineMock,
|
|
887
936
|
mockDevServerPlugin,
|
|
888
937
|
transformMockData
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Connect, Plugin } from 'vite';
|
|
1
|
+
import { Connect, Plugin, ResolvedConfig } from 'vite';
|
|
2
2
|
import http from 'node:http';
|
|
3
3
|
import formidable from 'formidable';
|
|
4
4
|
import EventEmitter from 'node:events';
|
|
@@ -172,12 +172,21 @@ declare function mockDevServerPlugin({ prefix, include, exclude, reload, formida
|
|
|
172
172
|
*/
|
|
173
173
|
declare function defineMock(config: MockOptionsItem): MockOptionsItem;
|
|
174
174
|
declare function defineMock(config: MockOptions): MockOptions;
|
|
175
|
+
/**
|
|
176
|
+
* 返回一个自定义的 defineMock 函数,用于支持对 mock config 的预处理。
|
|
177
|
+
*
|
|
178
|
+
* Return a custom defineMock function to support preprocessing of mock config.
|
|
179
|
+
*
|
|
180
|
+
* @param transformer preprocessing function
|
|
181
|
+
*/
|
|
182
|
+
declare function createDefineMock(transformer: (mock: MockOptionsItem) => MockOptionsItem | void): typeof defineMock;
|
|
175
183
|
|
|
176
184
|
interface MockLoaderOptions {
|
|
177
185
|
cwd?: string;
|
|
178
186
|
include: string[];
|
|
179
187
|
exclude: string[];
|
|
180
188
|
define: Record<string, any>;
|
|
189
|
+
alias: ResolvedConfig['resolve']['alias'];
|
|
181
190
|
}
|
|
182
191
|
/**
|
|
183
192
|
* mock配置加载器
|
|
@@ -219,4 +228,4 @@ declare function baseMiddleware(mockLoader: MockLoader, { formidableOptions, pro
|
|
|
219
228
|
|
|
220
229
|
declare function transformMockData(mockList: Map<string, MockOptionsItem | MockOptions> | (MockOptionsItem | MockOptions)[]): Record<string, MockOptions>;
|
|
221
230
|
|
|
222
|
-
export { BaseMiddlewareOptions, FormidableFile, MockOptions, MockOptionsItem, MockServerPluginOptions, baseMiddleware, mockDevServerPlugin as default, defineMock, mockDevServerPlugin, transformMockData };
|
|
231
|
+
export { BaseMiddlewareOptions, FormidableFile, MockOptions, MockOptionsItem, MockServerPluginOptions, baseMiddleware, createDefineMock, mockDevServerPlugin as default, defineMock, mockDevServerPlugin, transformMockData };
|
package/dist/index.js
CHANGED
|
@@ -9,7 +9,7 @@ import { createFilter, normalizePath } from "vite";
|
|
|
9
9
|
|
|
10
10
|
// package.json
|
|
11
11
|
var name = "vite-plugin-mock-dev-server";
|
|
12
|
-
var version = "1.0.
|
|
12
|
+
var version = "1.0.7";
|
|
13
13
|
|
|
14
14
|
// src/esbuildPlugin.ts
|
|
15
15
|
import fsp from "fs/promises";
|
|
@@ -51,6 +51,41 @@ var jsonLoader = {
|
|
|
51
51
|
});
|
|
52
52
|
}
|
|
53
53
|
};
|
|
54
|
+
var aliasPlugin = (alias) => {
|
|
55
|
+
return {
|
|
56
|
+
name: "alias-plugin",
|
|
57
|
+
setup(build3) {
|
|
58
|
+
build3.onResolve({ filter: /.*/ }, async ({ path: id }) => {
|
|
59
|
+
const matchedEntry = alias.find(({ find: find2 }) => matches(find2, id));
|
|
60
|
+
if (!matchedEntry) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
const { find, replacement } = matchedEntry;
|
|
64
|
+
const result = await build3.resolve(id.replace(find, replacement), {
|
|
65
|
+
kind: "import-statement",
|
|
66
|
+
resolveDir: replacement,
|
|
67
|
+
namespace: "file"
|
|
68
|
+
});
|
|
69
|
+
return {
|
|
70
|
+
path: result.path,
|
|
71
|
+
external: false
|
|
72
|
+
};
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
function matches(pattern, importee) {
|
|
78
|
+
if (pattern instanceof RegExp) {
|
|
79
|
+
return pattern.test(importee);
|
|
80
|
+
}
|
|
81
|
+
if (importee.length < pattern.length) {
|
|
82
|
+
return false;
|
|
83
|
+
}
|
|
84
|
+
if (importee === pattern) {
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
return importee.startsWith(`${pattern}/`);
|
|
88
|
+
}
|
|
54
89
|
|
|
55
90
|
// src/utils.ts
|
|
56
91
|
import fs from "fs";
|
|
@@ -130,7 +165,11 @@ async function generateMockServer(ctx, config, options) {
|
|
|
130
165
|
const content = await generateMockEntryCode(process.cwd(), include, exclude);
|
|
131
166
|
const mockEntry = path3.join(config.root, `mock-data-${Date.now()}.js`);
|
|
132
167
|
await fsp2.writeFile(mockEntry, content, "utf-8");
|
|
133
|
-
const { code, deps } = await buildMockEntry(
|
|
168
|
+
const { code, deps } = await buildMockEntry(
|
|
169
|
+
mockEntry,
|
|
170
|
+
define,
|
|
171
|
+
config.resolve.alias
|
|
172
|
+
);
|
|
134
173
|
const mockDeps = getMockDependencies(deps);
|
|
135
174
|
await fsp2.unlink(mockEntry);
|
|
136
175
|
const outputList = [
|
|
@@ -244,7 +283,7 @@ const mockList = exporters.map((raw) => raw && raw.default
|
|
|
244
283
|
export default transformMockData(mockList);
|
|
245
284
|
`;
|
|
246
285
|
}
|
|
247
|
-
async function buildMockEntry(inputFile, define) {
|
|
286
|
+
async function buildMockEntry(inputFile, define, alias) {
|
|
248
287
|
var _a;
|
|
249
288
|
try {
|
|
250
289
|
const result = await build({
|
|
@@ -257,7 +296,7 @@ async function buildMockEntry(inputFile, define) {
|
|
|
257
296
|
metafile: true,
|
|
258
297
|
format: "esm",
|
|
259
298
|
define,
|
|
260
|
-
plugins: [externalizeDeps, json5Loader, jsonLoader]
|
|
299
|
+
plugins: [aliasPlugin(alias), externalizeDeps, json5Loader, jsonLoader]
|
|
261
300
|
});
|
|
262
301
|
return {
|
|
263
302
|
code: result.outputFiles[0].text,
|
|
@@ -696,7 +735,7 @@ var _MockLoader = class extends EventEmitter {
|
|
|
696
735
|
metafile: true,
|
|
697
736
|
format: isESM ? "esm" : "cjs",
|
|
698
737
|
define: this.options.define,
|
|
699
|
-
plugins: [externalizeDeps]
|
|
738
|
+
plugins: [aliasPlugin(this.options.alias), externalizeDeps]
|
|
700
739
|
});
|
|
701
740
|
return {
|
|
702
741
|
code: result.outputFiles[0].text,
|
|
@@ -727,7 +766,8 @@ async function mockServerMiddleware(config, options, httpServer, ws) {
|
|
|
727
766
|
const loader = new MockLoader({
|
|
728
767
|
include,
|
|
729
768
|
exclude,
|
|
730
|
-
define
|
|
769
|
+
define,
|
|
770
|
+
alias: config.resolve.alias
|
|
731
771
|
});
|
|
732
772
|
await loader.load();
|
|
733
773
|
httpServer == null ? void 0 : httpServer.on("close", () => loader.close());
|
|
@@ -748,11 +788,7 @@ async function mockServerMiddleware(config, options, httpServer, ws) {
|
|
|
748
788
|
function mockDevServerPlugin({
|
|
749
789
|
prefix = [],
|
|
750
790
|
include = ["mock/**/*.mock.{js,ts,cjs,mjs,json,json5}"],
|
|
751
|
-
exclude = [
|
|
752
|
-
"**/node_modules/**",
|
|
753
|
-
"**/.vscode/**",
|
|
754
|
-
"**/.git/**"
|
|
755
|
-
],
|
|
791
|
+
exclude = ["**/node_modules/**", "**/.vscode/**", "**/.git/**"],
|
|
756
792
|
reload = false,
|
|
757
793
|
formidableOptions = {},
|
|
758
794
|
build: build3 = false
|
|
@@ -833,11 +869,23 @@ function serverPlugin(pluginOptions) {
|
|
|
833
869
|
function defineMock(config) {
|
|
834
870
|
return config;
|
|
835
871
|
}
|
|
872
|
+
function createDefineMock(transformer) {
|
|
873
|
+
const define = (config) => {
|
|
874
|
+
if (isArray(config)) {
|
|
875
|
+
config = config.map((item) => transformer(item) || item);
|
|
876
|
+
} else {
|
|
877
|
+
config = transformer(config) || config;
|
|
878
|
+
}
|
|
879
|
+
return config;
|
|
880
|
+
};
|
|
881
|
+
return define;
|
|
882
|
+
}
|
|
836
883
|
|
|
837
884
|
// src/index.ts
|
|
838
885
|
var src_default = mockDevServerPlugin;
|
|
839
886
|
export {
|
|
840
887
|
baseMiddleware,
|
|
888
|
+
createDefineMock,
|
|
841
889
|
src_default as default,
|
|
842
890
|
defineMock,
|
|
843
891
|
mockDevServerPlugin,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-mock-dev-server",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.7",
|
|
4
4
|
"keywords": [
|
|
5
5
|
"vite",
|
|
6
6
|
"plugin",
|
|
@@ -43,8 +43,8 @@
|
|
|
43
43
|
"picocolors": "^1.0.0"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
|
-
"@pengzhanbo/eslint-config-ts": "^0.3.
|
|
47
|
-
"@pengzhanbo/prettier-config": "^0.3.
|
|
46
|
+
"@pengzhanbo/eslint-config-ts": "^0.3.4",
|
|
47
|
+
"@pengzhanbo/prettier-config": "^0.3.4",
|
|
48
48
|
"@types/co-body": "^6.1.0",
|
|
49
49
|
"@types/debug": "^4.1.7",
|
|
50
50
|
"@types/formidable": "^2.0.5",
|