vite-plugin-mock-dev-server 0.2.2 → 0.2.3

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
@@ -8,7 +8,9 @@ vite mock开发服务(mock-dev-server)插件。
8
8
 
9
9
  - ⚡️ 轻量,灵活,快速
10
10
  - 🧲 非注入式,对客户端代码无侵入
11
+ - 💡 ESModule
11
12
  - 🦾 Typescript
13
+ - 🏷 支持 json / json5 编写 mock 数据
12
14
  - 📦 自动加载 mock 文件
13
15
  - 🎨 可选择你喜欢的任意用于生成mock数据库,如 `mockjs`,或者不是用其他库
14
16
  - 📥 路径规则匹配,请求参数匹配
@@ -101,7 +103,7 @@ export default defineConfig({
101
103
 
102
104
  配置读取 mock文件,可以是一个 目录,glob,或者一个数组
103
105
 
104
- 默认值: `['mock/**/*.mock.*']` (相对于根目录)
106
+ 默认值: `['mock/**/*.mock.{js,ts,cjs,mjs,json,json5}']` (相对于根目录)
105
107
 
106
108
  - `options.exclude`
107
109
 
@@ -232,7 +234,11 @@ export default defineMock({
232
234
 
233
235
  ```
234
236
 
235
- `mock/**/*.mock.ts`
237
+ > 注意:
238
+ >
239
+ > 如果使用 json/json5 编写 mock文件,则不支持使用 `response` 方法,以及不支持使用其他字段的函数形式。
240
+
241
+ `mock/**/*.mock.{ts,js,mjs,cjs,json,json5}`
236
242
 
237
243
  查看更多示例: [example](/example/)
238
244
 
@@ -369,3 +375,15 @@ export default defineMock({
369
375
  }
370
376
  })
371
377
  ```
378
+
379
+ ### 示例10:
380
+ 使用 json / json5
381
+ ```json
382
+ {
383
+ // 支持 comment
384
+ "url": "/api/test",
385
+ "body": {
386
+ "a": 1
387
+ }
388
+ }
389
+ ```
package/dist/index.cjs CHANGED
@@ -44,6 +44,7 @@ var import_pluginutils = require("@rollup/pluginutils");
44
44
  var import_chokidar = __toESM(require("chokidar"), 1);
45
45
  var import_esbuild = require("esbuild");
46
46
  var import_fast_glob = __toESM(require("fast-glob"), 1);
47
+ var import_json5 = __toESM(require("json5"), 1);
47
48
 
48
49
  // src/utils.ts
49
50
  var import_promises = __toESM(require("fs/promises"), 1);
@@ -65,7 +66,7 @@ async function getPackageDeps(cwd) {
65
66
  }
66
67
 
67
68
  // src/MockLoader.ts
68
- var MockLoader = class extends import_node_events.default {
69
+ var _MockLoader = class extends import_node_events.default {
69
70
  constructor(options) {
70
71
  super();
71
72
  this.moduleCache = /* @__PURE__ */ new Map();
@@ -183,21 +184,42 @@ var MockLoader = class extends import_node_events.default {
183
184
  async loadModule(filepath) {
184
185
  if (!filepath)
185
186
  return;
186
- const { code, deps } = await this.transform(filepath);
187
+ if (_MockLoader.EXT_JSON.test(filepath)) {
188
+ await this.loadJson(filepath);
189
+ } else {
190
+ await this.loadESModule(filepath);
191
+ }
192
+ }
193
+ async loadJson(filepath) {
194
+ const content = await import_promises2.default.readFile(filepath, "utf-8");
195
+ try {
196
+ const mockConfig = import_json5.default.parse(content);
197
+ this.moduleCache.set(filepath, mockConfig);
198
+ } catch (e) {
199
+ }
200
+ }
201
+ async loadESModule(filepath) {
202
+ if (!filepath)
203
+ return;
204
+ const { code, deps } = await this.transformWithEsbuild(filepath);
187
205
  const tempFile = import_node_path2.default.join(
188
206
  this.cwd,
189
207
  this.tempDir,
190
- filepath.replace(/\.ts$/, ".mjs")
208
+ filepath.replace(/\.(ts|js|cjs)$/, ".mjs")
191
209
  );
192
- const tempBasename = import_node_path2.default.dirname(tempFile);
193
- await import_promises2.default.mkdir(tempBasename, { recursive: true });
210
+ const tempDirname = import_node_path2.default.dirname(tempFile);
211
+ await import_promises2.default.mkdir(tempDirname, { recursive: true });
194
212
  await import_promises2.default.writeFile(tempFile, code, "utf8");
195
- const handle = await import(`${tempFile}?${Date.now()}`);
196
- const mockConfig = handle && handle.default ? handle.default : Object.keys(handle || {}).map((key) => handle[key]);
197
- this.moduleCache.set(filepath, mockConfig);
198
- this.updateModuleDeps(filepath, deps);
213
+ try {
214
+ const handle = await import(`${tempFile}?${Date.now()}`);
215
+ const mockConfig = handle && handle.default ? handle.default : Object.keys(handle || {}).map((key) => handle[key]);
216
+ this.moduleCache.set(filepath, mockConfig);
217
+ this.updateModuleDeps(filepath, deps);
218
+ } catch (e) {
219
+ console.error(e);
220
+ }
199
221
  }
200
- async transform(filepath) {
222
+ async transformWithEsbuild(filepath) {
201
223
  var _a;
202
224
  try {
203
225
  const result = await (0, import_esbuild.build)({
@@ -217,7 +239,6 @@ var MockLoader = class extends import_node_events.default {
217
239
  deps: ((_a = result.metafile) == null ? void 0 : _a.inputs) || {}
218
240
  };
219
241
  } catch (e) {
220
- console.error(e);
221
242
  }
222
243
  return {
223
244
  code: "",
@@ -225,6 +246,8 @@ var MockLoader = class extends import_node_events.default {
225
246
  };
226
247
  }
227
248
  };
249
+ var MockLoader = _MockLoader;
250
+ MockLoader.EXT_JSON = /\.json5?$/;
228
251
 
229
252
  // src/parseReqBody.ts
230
253
  var import_co_body = __toESM(require("co-body"), 1);
@@ -366,7 +389,7 @@ function doesProxyContextMatchUrl(context, url) {
366
389
 
367
390
  // src/plugin.ts
368
391
  function mockDevServerPlugin({
369
- include = ["mock/**/*.mock.*"],
392
+ include = ["mock/**/*.mock.{js,ts,cjs,mjs,json,json5}"],
370
393
  exclude = [
371
394
  "**/node_modules/**",
372
395
  "**/test/**",
package/dist/index.js CHANGED
@@ -10,6 +10,7 @@ import { createFilter } from "@rollup/pluginutils";
10
10
  import chokidar from "chokidar";
11
11
  import { build } from "esbuild";
12
12
  import fastGlob from "fast-glob";
13
+ import JSON5 from "json5";
13
14
 
14
15
  // src/utils.ts
15
16
  import fs from "fs/promises";
@@ -31,7 +32,7 @@ async function getPackageDeps(cwd) {
31
32
  }
32
33
 
33
34
  // src/MockLoader.ts
34
- var MockLoader = class extends EventEmitter {
35
+ var _MockLoader = class extends EventEmitter {
35
36
  constructor(options) {
36
37
  super();
37
38
  this.moduleCache = /* @__PURE__ */ new Map();
@@ -149,21 +150,42 @@ var MockLoader = class extends EventEmitter {
149
150
  async loadModule(filepath) {
150
151
  if (!filepath)
151
152
  return;
152
- const { code, deps } = await this.transform(filepath);
153
+ if (_MockLoader.EXT_JSON.test(filepath)) {
154
+ await this.loadJson(filepath);
155
+ } else {
156
+ await this.loadESModule(filepath);
157
+ }
158
+ }
159
+ async loadJson(filepath) {
160
+ const content = await fs2.readFile(filepath, "utf-8");
161
+ try {
162
+ const mockConfig = JSON5.parse(content);
163
+ this.moduleCache.set(filepath, mockConfig);
164
+ } catch (e) {
165
+ }
166
+ }
167
+ async loadESModule(filepath) {
168
+ if (!filepath)
169
+ return;
170
+ const { code, deps } = await this.transformWithEsbuild(filepath);
153
171
  const tempFile = path2.join(
154
172
  this.cwd,
155
173
  this.tempDir,
156
- filepath.replace(/\.ts$/, ".mjs")
174
+ filepath.replace(/\.(ts|js|cjs)$/, ".mjs")
157
175
  );
158
- const tempBasename = path2.dirname(tempFile);
159
- await fs2.mkdir(tempBasename, { recursive: true });
176
+ const tempDirname = path2.dirname(tempFile);
177
+ await fs2.mkdir(tempDirname, { recursive: true });
160
178
  await fs2.writeFile(tempFile, code, "utf8");
161
- const handle = await import(`${tempFile}?${Date.now()}`);
162
- const mockConfig = handle && handle.default ? handle.default : Object.keys(handle || {}).map((key) => handle[key]);
163
- this.moduleCache.set(filepath, mockConfig);
164
- this.updateModuleDeps(filepath, deps);
179
+ try {
180
+ const handle = await import(`${tempFile}?${Date.now()}`);
181
+ const mockConfig = handle && handle.default ? handle.default : Object.keys(handle || {}).map((key) => handle[key]);
182
+ this.moduleCache.set(filepath, mockConfig);
183
+ this.updateModuleDeps(filepath, deps);
184
+ } catch (e) {
185
+ console.error(e);
186
+ }
165
187
  }
166
- async transform(filepath) {
188
+ async transformWithEsbuild(filepath) {
167
189
  var _a;
168
190
  try {
169
191
  const result = await build({
@@ -183,7 +205,6 @@ var MockLoader = class extends EventEmitter {
183
205
  deps: ((_a = result.metafile) == null ? void 0 : _a.inputs) || {}
184
206
  };
185
207
  } catch (e) {
186
- console.error(e);
187
208
  }
188
209
  return {
189
210
  code: "",
@@ -191,6 +212,8 @@ var MockLoader = class extends EventEmitter {
191
212
  };
192
213
  }
193
214
  };
215
+ var MockLoader = _MockLoader;
216
+ MockLoader.EXT_JSON = /\.json5?$/;
194
217
 
195
218
  // src/parseReqBody.ts
196
219
  import bodyParser from "co-body";
@@ -332,7 +355,7 @@ function doesProxyContextMatchUrl(context, url) {
332
355
 
333
356
  // src/plugin.ts
334
357
  function mockDevServerPlugin({
335
- include = ["mock/**/*.mock.*"],
358
+ include = ["mock/**/*.mock.{js,ts,cjs,mjs,json,json5}"],
336
359
  exclude = [
337
360
  "**/node_modules/**",
338
361
  "**/test/**",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-mock-dev-server",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "keywords": [
5
5
  "vite",
6
6
  "plugin",
@@ -48,6 +48,7 @@
48
48
  "debug": "^4.3.4",
49
49
  "esbuild": "^0.15.12",
50
50
  "fast-glob": "^3.2.12",
51
+ "json5": "^2.2.1",
51
52
  "path-to-regexp": "^6.2.1",
52
53
  "vite": "^3.2.0"
53
54
  },