vite-plugin-mock-dev-server 1.0.3 → 1.0.5
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 +50 -70
- package/dist/index.d.ts +4 -2
- package/dist/index.js +54 -75
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -33,7 +33,8 @@ __export(src_exports, {
|
|
|
33
33
|
baseMiddleware: () => baseMiddleware,
|
|
34
34
|
default: () => src_default,
|
|
35
35
|
defineMock: () => defineMock,
|
|
36
|
-
mockDevServerPlugin: () => mockDevServerPlugin
|
|
36
|
+
mockDevServerPlugin: () => mockDevServerPlugin,
|
|
37
|
+
transformMockData: () => transformMockData
|
|
37
38
|
});
|
|
38
39
|
module.exports = __toCommonJS(src_exports);
|
|
39
40
|
|
|
@@ -52,7 +53,7 @@ var import_vite = require("vite");
|
|
|
52
53
|
|
|
53
54
|
// package.json
|
|
54
55
|
var name = "vite-plugin-mock-dev-server";
|
|
55
|
-
var version = "1.0.
|
|
56
|
+
var version = "1.0.5";
|
|
56
57
|
|
|
57
58
|
// src/esbuildPlugin.ts
|
|
58
59
|
var import_promises = __toESM(require("fs/promises"), 1);
|
|
@@ -214,7 +215,7 @@ async function generateMockServer(ctx, config, options) {
|
|
|
214
215
|
}
|
|
215
216
|
function getMockDependencies(deps) {
|
|
216
217
|
const list = /* @__PURE__ */ new Set();
|
|
217
|
-
const excludeDeps = [name, "connect"];
|
|
218
|
+
const excludeDeps = [name, "connect", "cors"];
|
|
218
219
|
Object.keys(deps).forEach((mPath) => {
|
|
219
220
|
const imports = deps[mPath].imports.filter((_) => _.external).map((_) => _.path);
|
|
220
221
|
imports.forEach((dep) => {
|
|
@@ -269,50 +270,23 @@ async function generateMockEntryCode(cwd, include, exclude) {
|
|
|
269
270
|
resolve: false
|
|
270
271
|
});
|
|
271
272
|
const mockFiles = includePaths.filter(includeFilter);
|
|
272
|
-
let importers = "
|
|
273
|
+
let importers = "";
|
|
273
274
|
let exporters = "";
|
|
274
275
|
mockFiles.forEach((filepath, index) => {
|
|
275
|
-
const file = import_node_path3.default.join(cwd, filepath);
|
|
276
|
+
const file = (0, import_vite.normalizePath)(import_node_path3.default.join(cwd, filepath));
|
|
276
277
|
importers += `import * as m${index} from '${file}';
|
|
277
278
|
`;
|
|
278
279
|
exporters += `m${index}, `;
|
|
279
280
|
});
|
|
280
|
-
return
|
|
281
|
-
|
|
282
|
-
const
|
|
283
|
-
const
|
|
284
|
-
|
|
285
|
-
Object.keys(
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
} else {
|
|
290
|
-
if (typeof mockData === 'object' && Object.prototype.hasOwnProperty.call(mockData, 'url')) {
|
|
291
|
-
data.push(mockData);
|
|
292
|
-
}
|
|
293
|
-
}
|
|
294
|
-
})
|
|
295
|
-
})
|
|
296
|
-
data
|
|
297
|
-
.filter((mock) => mock.enabled || typeof mock.enabled === "undefined")
|
|
298
|
-
.forEach((mock) => {
|
|
299
|
-
const { pathname, query } = urlParse(mock.url, true);
|
|
300
|
-
if (!mocks[pathname]) {
|
|
301
|
-
mocks[pathname] = [];
|
|
302
|
-
}
|
|
303
|
-
mock.url = pathname;
|
|
304
|
-
const list = mocks[pathname];
|
|
305
|
-
if (query && typeof mock.validator === 'function') {
|
|
306
|
-
mock.validator ??= {};
|
|
307
|
-
mock.validator.query = Object.assign(
|
|
308
|
-
query,
|
|
309
|
-
mock.validator.query || {},
|
|
310
|
-
);
|
|
311
|
-
}
|
|
312
|
-
mock.validator ? list.unshift(mock) : list.push(mock);
|
|
313
|
-
});
|
|
314
|
-
export default mocks;
|
|
315
|
-
`;
|
|
281
|
+
return `import { transformMockData } from 'vite-plugin-mock-dev-server';
|
|
282
|
+
${importers}
|
|
283
|
+
const exporters = [${exporters}];
|
|
284
|
+
const mockList = exporters.map((raw) => raw && raw.default
|
|
285
|
+
? raw.default
|
|
286
|
+
: Object.keys(raw || {}).map((key) => raw[key])
|
|
287
|
+
)
|
|
288
|
+
export default transformMockData(mockList);
|
|
289
|
+
`;
|
|
316
290
|
}
|
|
317
291
|
async function buildMockEntry(inputFile, define) {
|
|
318
292
|
var _a;
|
|
@@ -520,12 +494,42 @@ var import_node_events = __toESM(require("events"), 1);
|
|
|
520
494
|
var import_node_fs3 = __toESM(require("fs"), 1);
|
|
521
495
|
var import_node_module = require("module");
|
|
522
496
|
var import_node_path4 = __toESM(require("path"), 1);
|
|
523
|
-
var
|
|
497
|
+
var import_node_url4 = require("url");
|
|
524
498
|
var import_chokidar = __toESM(require("chokidar"), 1);
|
|
525
499
|
var import_esbuild2 = require("esbuild");
|
|
526
500
|
var import_fast_glob2 = __toESM(require("fast-glob"), 1);
|
|
527
501
|
var import_json52 = __toESM(require("json5"), 1);
|
|
528
502
|
var import_vite2 = require("vite");
|
|
503
|
+
|
|
504
|
+
// src/transform.ts
|
|
505
|
+
var import_node_url3 = require("url");
|
|
506
|
+
function transformMockData(mockList) {
|
|
507
|
+
const list = [];
|
|
508
|
+
for (const [, handle] of mockList.entries()) {
|
|
509
|
+
if (handle) {
|
|
510
|
+
isArray(handle) ? list.push(...handle) : list.push(handle);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
const mocks = {};
|
|
514
|
+
list.filter(
|
|
515
|
+
(mock) => (mock.enabled || typeof mock.enabled === "undefined") && mock.url
|
|
516
|
+
).forEach((mock) => {
|
|
517
|
+
const { pathname, query } = (0, import_node_url3.parse)(mock.url, true);
|
|
518
|
+
const list2 = mocks[pathname] ?? (mocks[pathname] = []);
|
|
519
|
+
const current = Object.assign({}, mock, { url: pathname });
|
|
520
|
+
if (query && !isFunction(current.validator)) {
|
|
521
|
+
current.validator = Object.assign({}, current.validator || {});
|
|
522
|
+
current.validator.query = Object.assign(
|
|
523
|
+
query,
|
|
524
|
+
current.validator.query || {}
|
|
525
|
+
);
|
|
526
|
+
}
|
|
527
|
+
current.validator ? list2.unshift(current) : list2.push(current);
|
|
528
|
+
});
|
|
529
|
+
return mocks;
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
// src/MockLoader.ts
|
|
529
533
|
var _dirname = getDirname(importMetaUrl);
|
|
530
534
|
var _require = (0, import_node_module.createRequire)(_dirname);
|
|
531
535
|
var _MockLoader = class extends import_node_events.default {
|
|
@@ -636,32 +640,7 @@ var _MockLoader = class extends import_node_events.default {
|
|
|
636
640
|
(_b = this.depsWatcher) == null ? void 0 : _b.close();
|
|
637
641
|
}
|
|
638
642
|
updateMockList() {
|
|
639
|
-
|
|
640
|
-
for (const [, handle] of this.moduleCache.entries()) {
|
|
641
|
-
if (handle) {
|
|
642
|
-
isArray(handle) ? mockList.push(...handle) : mockList.push(handle);
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
const mocks = {};
|
|
646
|
-
mockList.filter(
|
|
647
|
-
(mock) => (mock.enabled || typeof mock.enabled === "undefined") && mock.url
|
|
648
|
-
).forEach((mock) => {
|
|
649
|
-
const { pathname, query } = (0, import_node_url3.parse)(mock.url, true);
|
|
650
|
-
if (!mocks[pathname]) {
|
|
651
|
-
mocks[pathname] = [];
|
|
652
|
-
}
|
|
653
|
-
mock.url = pathname;
|
|
654
|
-
const list = mocks[pathname];
|
|
655
|
-
if (query && !isFunction(mock.validator)) {
|
|
656
|
-
mock.validator ?? (mock.validator = {});
|
|
657
|
-
mock.validator.query = Object.assign(
|
|
658
|
-
query,
|
|
659
|
-
mock.validator.query || {}
|
|
660
|
-
);
|
|
661
|
-
}
|
|
662
|
-
mock.validator ? list.unshift(mock) : list.push(mock);
|
|
663
|
-
});
|
|
664
|
-
this._mockData = mocks;
|
|
643
|
+
this._mockData = transformMockData(this.moduleCache);
|
|
665
644
|
}
|
|
666
645
|
updateModuleDeps(filepath, deps) {
|
|
667
646
|
Object.keys(deps).forEach((mPath) => {
|
|
@@ -718,7 +697,7 @@ var _MockLoader = class extends import_node_events.default {
|
|
|
718
697
|
if (isESM) {
|
|
719
698
|
const fileBase = `${filepath}.timestamp-${Date.now()}`;
|
|
720
699
|
const fileNameTmp = `${fileBase}.mjs`;
|
|
721
|
-
const fileUrl = `${(0,
|
|
700
|
+
const fileUrl = `${(0, import_node_url4.pathToFileURL)(fileBase)}.mjs`;
|
|
722
701
|
await import_node_fs3.default.promises.writeFile(fileNameTmp, code, "utf8");
|
|
723
702
|
try {
|
|
724
703
|
return await import(fileUrl);
|
|
@@ -908,5 +887,6 @@ var src_default = mockDevServerPlugin;
|
|
|
908
887
|
0 && (module.exports = {
|
|
909
888
|
baseMiddleware,
|
|
910
889
|
defineMock,
|
|
911
|
-
mockDevServerPlugin
|
|
890
|
+
mockDevServerPlugin,
|
|
891
|
+
transformMockData
|
|
912
892
|
});
|
package/dist/index.d.ts
CHANGED
|
@@ -158,7 +158,7 @@ declare function mockDevServerPlugin({ prefix, include, exclude, reload, formida
|
|
|
158
158
|
/**
|
|
159
159
|
* mock config helper
|
|
160
160
|
*
|
|
161
|
-
* mock
|
|
161
|
+
* mock配置辅助函数
|
|
162
162
|
*
|
|
163
163
|
* @param config
|
|
164
164
|
* @example
|
|
@@ -217,4 +217,6 @@ interface BaseMiddlewareOptions {
|
|
|
217
217
|
}
|
|
218
218
|
declare function baseMiddleware(mockLoader: MockLoader, { formidableOptions, proxies }: BaseMiddlewareOptions): Connect.NextHandleFunction;
|
|
219
219
|
|
|
220
|
-
|
|
220
|
+
declare function transformMockData(mockList: Map<string, MockOptionsItem | MockOptions> | (MockOptionsItem | MockOptions)[]): Record<string, MockOptions>;
|
|
221
|
+
|
|
222
|
+
export { BaseMiddlewareOptions, FormidableFile, MockOptions, MockOptionsItem, MockServerPluginOptions, baseMiddleware, mockDevServerPlugin as default, defineMock, mockDevServerPlugin, transformMockData };
|
package/dist/index.js
CHANGED
|
@@ -5,11 +5,11 @@ import path3 from "path";
|
|
|
5
5
|
import { build } from "esbuild";
|
|
6
6
|
import fg from "fast-glob";
|
|
7
7
|
import isCore from "is-core-module";
|
|
8
|
-
import { createFilter } from "vite";
|
|
8
|
+
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.5";
|
|
13
13
|
|
|
14
14
|
// src/esbuildPlugin.ts
|
|
15
15
|
import fsp from "fs/promises";
|
|
@@ -171,7 +171,7 @@ async function generateMockServer(ctx, config, options) {
|
|
|
171
171
|
}
|
|
172
172
|
function getMockDependencies(deps) {
|
|
173
173
|
const list = /* @__PURE__ */ new Set();
|
|
174
|
-
const excludeDeps = [name, "connect"];
|
|
174
|
+
const excludeDeps = [name, "connect", "cors"];
|
|
175
175
|
Object.keys(deps).forEach((mPath) => {
|
|
176
176
|
const imports = deps[mPath].imports.filter((_) => _.external).map((_) => _.path);
|
|
177
177
|
imports.forEach((dep) => {
|
|
@@ -226,50 +226,23 @@ async function generateMockEntryCode(cwd, include, exclude) {
|
|
|
226
226
|
resolve: false
|
|
227
227
|
});
|
|
228
228
|
const mockFiles = includePaths.filter(includeFilter);
|
|
229
|
-
let importers = "
|
|
229
|
+
let importers = "";
|
|
230
230
|
let exporters = "";
|
|
231
231
|
mockFiles.forEach((filepath, index) => {
|
|
232
|
-
const file = path3.join(cwd, filepath);
|
|
232
|
+
const file = normalizePath(path3.join(cwd, filepath));
|
|
233
233
|
importers += `import * as m${index} from '${file}';
|
|
234
234
|
`;
|
|
235
235
|
exporters += `m${index}, `;
|
|
236
236
|
});
|
|
237
|
-
return
|
|
238
|
-
|
|
239
|
-
const
|
|
240
|
-
const
|
|
241
|
-
|
|
242
|
-
Object.keys(
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
} else {
|
|
247
|
-
if (typeof mockData === 'object' && Object.prototype.hasOwnProperty.call(mockData, 'url')) {
|
|
248
|
-
data.push(mockData);
|
|
249
|
-
}
|
|
250
|
-
}
|
|
251
|
-
})
|
|
252
|
-
})
|
|
253
|
-
data
|
|
254
|
-
.filter((mock) => mock.enabled || typeof mock.enabled === "undefined")
|
|
255
|
-
.forEach((mock) => {
|
|
256
|
-
const { pathname, query } = urlParse(mock.url, true);
|
|
257
|
-
if (!mocks[pathname]) {
|
|
258
|
-
mocks[pathname] = [];
|
|
259
|
-
}
|
|
260
|
-
mock.url = pathname;
|
|
261
|
-
const list = mocks[pathname];
|
|
262
|
-
if (query && typeof mock.validator === 'function') {
|
|
263
|
-
mock.validator ??= {};
|
|
264
|
-
mock.validator.query = Object.assign(
|
|
265
|
-
query,
|
|
266
|
-
mock.validator.query || {},
|
|
267
|
-
);
|
|
268
|
-
}
|
|
269
|
-
mock.validator ? list.unshift(mock) : list.push(mock);
|
|
270
|
-
});
|
|
271
|
-
export default mocks;
|
|
272
|
-
`;
|
|
237
|
+
return `import { transformMockData } from 'vite-plugin-mock-dev-server';
|
|
238
|
+
${importers}
|
|
239
|
+
const exporters = [${exporters}];
|
|
240
|
+
const mockList = exporters.map((raw) => raw && raw.default
|
|
241
|
+
? raw.default
|
|
242
|
+
: Object.keys(raw || {}).map((key) => raw[key])
|
|
243
|
+
)
|
|
244
|
+
export default transformMockData(mockList);
|
|
245
|
+
`;
|
|
273
246
|
}
|
|
274
247
|
async function buildMockEntry(inputFile, define) {
|
|
275
248
|
var _a;
|
|
@@ -477,12 +450,42 @@ import EventEmitter from "events";
|
|
|
477
450
|
import fs3 from "fs";
|
|
478
451
|
import { createRequire } from "module";
|
|
479
452
|
import path4 from "path";
|
|
480
|
-
import { pathToFileURL
|
|
453
|
+
import { pathToFileURL } from "url";
|
|
481
454
|
import chokidar from "chokidar";
|
|
482
455
|
import { build as build2 } from "esbuild";
|
|
483
456
|
import fastGlob from "fast-glob";
|
|
484
457
|
import JSON52 from "json5";
|
|
485
|
-
import { createFilter as createFilter2, normalizePath } from "vite";
|
|
458
|
+
import { createFilter as createFilter2, normalizePath as normalizePath2 } from "vite";
|
|
459
|
+
|
|
460
|
+
// src/transform.ts
|
|
461
|
+
import { parse as urlParse2 } from "url";
|
|
462
|
+
function transformMockData(mockList) {
|
|
463
|
+
const list = [];
|
|
464
|
+
for (const [, handle] of mockList.entries()) {
|
|
465
|
+
if (handle) {
|
|
466
|
+
isArray(handle) ? list.push(...handle) : list.push(handle);
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
const mocks = {};
|
|
470
|
+
list.filter(
|
|
471
|
+
(mock) => (mock.enabled || typeof mock.enabled === "undefined") && mock.url
|
|
472
|
+
).forEach((mock) => {
|
|
473
|
+
const { pathname, query } = urlParse2(mock.url, true);
|
|
474
|
+
const list2 = mocks[pathname] ?? (mocks[pathname] = []);
|
|
475
|
+
const current = Object.assign({}, mock, { url: pathname });
|
|
476
|
+
if (query && !isFunction(current.validator)) {
|
|
477
|
+
current.validator = Object.assign({}, current.validator || {});
|
|
478
|
+
current.validator.query = Object.assign(
|
|
479
|
+
query,
|
|
480
|
+
current.validator.query || {}
|
|
481
|
+
);
|
|
482
|
+
}
|
|
483
|
+
current.validator ? list2.unshift(current) : list2.push(current);
|
|
484
|
+
});
|
|
485
|
+
return mocks;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
// src/MockLoader.ts
|
|
486
489
|
var _dirname = getDirname(import.meta.url);
|
|
487
490
|
var _require = createRequire(_dirname);
|
|
488
491
|
var _MockLoader = class extends EventEmitter {
|
|
@@ -541,17 +544,17 @@ var _MockLoader = class extends EventEmitter {
|
|
|
541
544
|
});
|
|
542
545
|
otherGlob.length > 0 && otherGlob.forEach((glob) => watcher.add(glob));
|
|
543
546
|
watcher.on("add", async (filepath) => {
|
|
544
|
-
filepath =
|
|
547
|
+
filepath = normalizePath2(filepath);
|
|
545
548
|
this.emit("mock:update", filepath);
|
|
546
549
|
debug("watcher:add", filepath);
|
|
547
550
|
});
|
|
548
551
|
watcher.on("change", async (filepath) => {
|
|
549
|
-
filepath =
|
|
552
|
+
filepath = normalizePath2(filepath);
|
|
550
553
|
this.emit("mock:update", filepath);
|
|
551
554
|
debug("watcher:change", filepath);
|
|
552
555
|
});
|
|
553
556
|
watcher.on("unlink", async (filepath) => {
|
|
554
|
-
filepath =
|
|
557
|
+
filepath = normalizePath2(filepath);
|
|
555
558
|
this.emit("mock:unlink", filepath);
|
|
556
559
|
debug("watcher:unlink", filepath);
|
|
557
560
|
});
|
|
@@ -568,14 +571,14 @@ var _MockLoader = class extends EventEmitter {
|
|
|
568
571
|
cwd: this.cwd
|
|
569
572
|
});
|
|
570
573
|
this.depsWatcher.on("change", (filepath) => {
|
|
571
|
-
filepath =
|
|
574
|
+
filepath = normalizePath2(filepath);
|
|
572
575
|
const mockFiles = this.moduleDeps.get(filepath);
|
|
573
576
|
mockFiles && mockFiles.forEach((file) => {
|
|
574
577
|
this.emit("mock:update", file);
|
|
575
578
|
});
|
|
576
579
|
});
|
|
577
580
|
this.depsWatcher.on("unlink", (filepath) => {
|
|
578
|
-
filepath =
|
|
581
|
+
filepath = normalizePath2(filepath);
|
|
579
582
|
this.moduleDeps.delete(filepath);
|
|
580
583
|
});
|
|
581
584
|
this.on("update:deps", () => {
|
|
@@ -593,32 +596,7 @@ var _MockLoader = class extends EventEmitter {
|
|
|
593
596
|
(_b = this.depsWatcher) == null ? void 0 : _b.close();
|
|
594
597
|
}
|
|
595
598
|
updateMockList() {
|
|
596
|
-
|
|
597
|
-
for (const [, handle] of this.moduleCache.entries()) {
|
|
598
|
-
if (handle) {
|
|
599
|
-
isArray(handle) ? mockList.push(...handle) : mockList.push(handle);
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
const mocks = {};
|
|
603
|
-
mockList.filter(
|
|
604
|
-
(mock) => (mock.enabled || typeof mock.enabled === "undefined") && mock.url
|
|
605
|
-
).forEach((mock) => {
|
|
606
|
-
const { pathname, query } = urlParse2(mock.url, true);
|
|
607
|
-
if (!mocks[pathname]) {
|
|
608
|
-
mocks[pathname] = [];
|
|
609
|
-
}
|
|
610
|
-
mock.url = pathname;
|
|
611
|
-
const list = mocks[pathname];
|
|
612
|
-
if (query && !isFunction(mock.validator)) {
|
|
613
|
-
mock.validator ?? (mock.validator = {});
|
|
614
|
-
mock.validator.query = Object.assign(
|
|
615
|
-
query,
|
|
616
|
-
mock.validator.query || {}
|
|
617
|
-
);
|
|
618
|
-
}
|
|
619
|
-
mock.validator ? list.unshift(mock) : list.push(mock);
|
|
620
|
-
});
|
|
621
|
-
this._mockData = mocks;
|
|
599
|
+
this._mockData = transformMockData(this.moduleCache);
|
|
622
600
|
}
|
|
623
601
|
updateModuleDeps(filepath, deps) {
|
|
624
602
|
Object.keys(deps).forEach((mPath) => {
|
|
@@ -865,5 +843,6 @@ export {
|
|
|
865
843
|
baseMiddleware,
|
|
866
844
|
src_default as default,
|
|
867
845
|
defineMock,
|
|
868
|
-
mockDevServerPlugin
|
|
846
|
+
mockDevServerPlugin,
|
|
847
|
+
transformMockData
|
|
869
848
|
};
|