vite-plugin-mock-data 6.0.1 โ†’ 6.0.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
@@ -2,10 +2,11 @@
2
2
 
3
3
  [![npm package](https://nodei.co/npm/vite-plugin-mock-data.png?downloads=true&downloadRank=true&stars=true)](https://www.npmjs.com/package/vite-plugin-mock-data)
4
4
 
5
- > Provides a simple way to mock data. Vite >= 3.1
5
+ > Provides a simple way to mock data.
6
6
 
7
7
  [![NPM version](https://img.shields.io/npm/v/vite-plugin-mock-data.svg?style=flat)](https://npmjs.org/package/vite-plugin-mock-data)
8
8
  [![NPM Downloads](https://img.shields.io/npm/dm/vite-plugin-mock-data.svg?style=flat)](https://npmjs.org/package/vite-plugin-mock-data)
9
+ [![Node version](https://img.shields.io/node/v/vite-plugin-mock-data.svg?style=flat)](https://npmjs.org/package/vite-plugin-mock-data)
9
10
 
10
11
  ## Installation
11
12
 
@@ -13,196 +14,21 @@
13
14
  npm install vite-plugin-mock-data --save-dev
14
15
  ```
15
16
 
16
- ## Options
17
+ ## Documentation
17
18
 
18
- ```ts
19
- import { Config as SirvConfig, HTTPVersion, RouteOptions, Handler } from 'find-my-way';
19
+ For detailed usage instructions and API references, please visit the official documentation:
20
20
 
21
- export interface HandleRoute {
22
- file?: string;
23
- handler?: any | Handler<HTTPVersion.V1>;
24
- options?: RouteOptions;
25
- store?: any;
26
- }
21
+ ๐Ÿ‘‰ [View Full Documentation](https://fengxinming.github.io/vite-plugins/plugins/vite-plugin-mock-data/quick-start)
27
22
 
28
- export interface RouteConfig {
29
- [route: string]: string | Handler<HTTPVersion.V1> | HandleRoute;
30
- }
23
+ ## Contributing
31
24
 
32
- export interface Options {
33
- /**
34
- * The directory to serve files from.
35
- * @default `process.cwd()`
36
- */
37
- cwd?: string;
25
+ We welcome contributions from the community! If you find a bug or want to suggest an improvement, feel free to open an issue or submit a pull request.
38
26
 
39
- /**
40
- * If `true`, these mock routes is matched after internal middlewares are installed.
41
- * @default `false`
42
- */
43
- isAfter?: boolean;
27
+ ### How to Contribute
28
+ 1. Fork the repository.
29
+ 2. Create a new branch for your changes.
30
+ 3. Submit a pull request with a clear description of your changes.
44
31
 
45
- /**
46
- * Specify the directory to define mock assets.
47
- */
48
- assets?: string;
32
+ ## License
49
33
 
50
- /**
51
- * Initial options of `find-my-way`. see more at https://github.com/delvedor/find-my-way#findmywayoptions
52
- */
53
- routerOptions?: SirvConfig<HTTPVersion.V1> | SirvConfig<HTTPVersion.V2>;
54
-
55
- /**
56
- * Initial list of mock routes that should be added to the dev server
57
- * or specify the directory to define mock routes that should be added to the dev server.
58
- */
59
- routes?: RouteConfig | Array<RouteConfig | string> | string;
60
- }
61
- ```
62
-
63
- * `cwd` - Default: `process.cwd()`.
64
- * `isAfter` - If `true`, these mock routes is matched after internal middlewares are installed.
65
- * `assets` - Specify the directory to define mock assets.
66
- * `routerOptions` - [Initial options of `find-my-way`](https://github.com/delvedor/find-my-way#findmywayoptions)
67
- * `routes`
68
- * `RouteConfig | Array<RouteConfig | string>` - Initial list of mock routes that should be added to the dev server.
69
- * `string` - Specify the directory to define mock routes that should be added to the dev server.
70
-
71
- ## Usage
72
-
73
- ### Specify the directory to define mock assets
74
-
75
- ```js
76
- import { defineConfig } from 'vite';
77
- import mockData from 'vite-plugin-mock-data';
78
-
79
- export default defineConfig({
80
- plugins: [
81
- mockData({
82
- assets: './mockAssets'
83
- })
84
- ]
85
- });
86
- ```
87
-
88
- ```txt
89
- .
90
- โ”œโ”€โ”€ mockAssets
91
- โ”‚ โ”œโ”€โ”€ test.zip
92
- โ”‚ โ””โ”€โ”€ test.json
93
- ```
94
-
95
- ```js
96
- fetch('/test.json')
97
- .then(res => res.json())
98
- .then((json) => {
99
- console.log(json);
100
- });
101
- ```
102
-
103
- ```html
104
- <a class="download" href="./test.zip">Download</a>
105
- ```
106
-
107
- ### add mock routes to the dev server
108
-
109
- ```js
110
- import { defineConfig } from 'vite';
111
- import mockData from 'vite-plugin-mock-data';
112
-
113
- export default defineConfig({
114
- plugins: [
115
- mockData({
116
- routes: {
117
- '/hello': 'hello',
118
- '/hello2'(req, res) {
119
- res.statusCode = 200;
120
- res.setHeader('Content-Type', 'text/html');
121
- res.end('hello2');
122
- },
123
- '/hello3': {
124
- handler(req, res) {
125
- res.statusCode = 200;
126
- res.setHeader('Content-Type', 'text/html');
127
- res.end('hello3');
128
- }
129
- },
130
- '/json': {
131
- handler: { hello: 1 }
132
- },
133
- '/package.json': {
134
- file: './package.json'
135
- }
136
- }
137
- })
138
- ]
139
- });
140
- ```
141
-
142
- ```js
143
- fetch('/package.json')
144
- .then(res => res.json())
145
- .then((json) => {
146
- console.log(json);
147
- });
148
- ```
149
-
150
- ### Specify the directory to add mock routes to the dev server
151
-
152
- ```js
153
- import { defineConfig } from 'vite';
154
- import mockData from 'vite-plugin-mock-data';
155
-
156
- export default defineConfig({
157
- plugins: [
158
- mockData({
159
- routes: './mock'
160
- })
161
- ]
162
- });
163
- ```
164
-
165
- ```txt
166
- .
167
- โ”œโ”€โ”€ mock
168
- โ”‚ โ””โ”€โ”€ test.js
169
- ```
170
-
171
- ```js
172
- module.exports = {
173
- '/hello': 'hello',
174
- '/hello2'(req, res) {
175
- res.statusCode = 200;
176
- res.setHeader('Content-Type', 'text/html');
177
- res.end('hello2');
178
- },
179
- '/hello3': {
180
- handler(req, res) {
181
- res.statusCode = 200;
182
- res.setHeader('Content-Type', 'text/html');
183
- res.end('hello3');
184
- }
185
- },
186
- '/json': {
187
- handler: { hello: 1 }
188
- },
189
- '/package.json': {
190
- file: './package.json'
191
- }
192
- };
193
- ```
194
-
195
- ```js
196
- fetch('/package.json')
197
- .then(res => res.json())
198
- .then((json) => {
199
- console.log(json);
200
- });
201
- ```
202
-
203
- ## Examples
204
-
205
- * [See vite3 demo](../../examples/vite3-demo)
206
- * [See vite4 demo](../../examples/vite4-demo)
207
- * [See vite5 demo](../../examples/vite5-demo)
208
- * [See vite6 demo](../../examples/vite6-demo)
34
+ This project is licensed under the [MIT License](LICENSE).
@@ -0,0 +1,7 @@
1
+ import { OutgoingHttpHeaders } from 'node:http';
2
+ import { Config as SirvConfig, HTTPVersion } from 'find-my-way';
3
+ import { type Options as SirvOptions } from 'sirv';
4
+ import { ViteDevServer } from 'vite';
5
+ import { RouteConfig } from './types';
6
+ export declare function sirvOptions(headers?: OutgoingHttpHeaders): SirvOptions;
7
+ export declare function configureServer(server: ViteDevServer, routerOpts: SirvConfig<HTTPVersion.V1> | SirvConfig<HTTPVersion.V2> | undefined, routes: RouteConfig[], cwd: string): void;
package/dist/index.d.ts CHANGED
@@ -1,17 +1,17 @@
1
1
  import { Plugin } from 'vite';
2
- import { Options } from './typings';
3
- export * from './typings';
2
+ import { Options } from './types';
3
+ export * from './types';
4
4
  /**
5
5
  * Provides a simple way to mock data.
6
6
  *
7
7
  * @example
8
8
  * ```js
9
9
  * import { defineConfig } from 'vite';
10
- * import mockData from 'vite-plugin-mock-data';
10
+ * import pluginMockDate from 'vite-plugin-mock-data';
11
11
  *
12
12
  * export default defineConfig({
13
13
  * plugins: [
14
- * mockData({
14
+ * pluginMockDate({
15
15
  * routes: './mock'
16
16
  * })
17
17
  * ]
@@ -21,4 +21,4 @@ export * from './typings';
21
21
  * @param opts Options
22
22
  * @returns a vite plugin
23
23
  */
24
- export default function createPlugin(opts: Options): Plugin;
24
+ export default function pluginMockDate(opts: Options): Plugin;
package/dist/index.js CHANGED
@@ -1,43 +1,19 @@
1
1
  "use strict";
2
+ const isWhatType = require("is-what-type");
3
+ const vpRuntimeHelper = require("vp-runtime-helper");
2
4
  const node_path = require("node:path");
3
- const node_module = require("node:module");
4
- const node_fs = require("node:fs");
5
- const tinyglobby = require("tinyglobby");
6
5
  const getRouter = require("find-my-way");
7
6
  const sirv = require("sirv");
8
7
  const vite = require("vite");
8
+ const node_fs = require("node:fs");
9
+ const promises = require("node:fs/promises");
10
+ const node_module = require("node:module");
11
+ const tinyglobby = require("tinyglobby");
9
12
  var _documentCurrentScript = typeof document !== "undefined" ? document.currentScript : null;
10
- function isObject(val) {
11
- return val && typeof val === "object";
12
- }
13
- function toAbsolute(pth, cwd) {
14
- return node_path.isAbsolute(pth) ? pth : node_path.posix.join(cwd || process.cwd(), pth);
15
- }
16
- async function getRoute(filename) {
17
- let config;
18
- switch (node_path.extname(filename)) {
19
- case ".js":
20
- config = node_module.createRequire(typeof document === "undefined" ? require("url").pathToFileURL(__filename).href : _documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === "SCRIPT" && _documentCurrentScript.src || new URL("index.js", document.baseURI).href)(filename);
21
- break;
22
- case ".mjs":
23
- config = (await import(filename)).default;
24
- break;
25
- case ".json":
26
- config = JSON.parse(node_fs.readFileSync(filename, "utf-8"));
27
- break;
28
- }
29
- return config;
30
- }
31
- async function loadRoutes(dir, routes) {
32
- const paths = await tinyglobby.glob(`${dir}/**/*.{js,mjs,json}`, { absolute: true });
33
- const configs = await Promise.all(paths.map(getRoute));
34
- configs.reduce((prev, cur) => {
35
- if (cur) {
36
- prev.push(cur);
37
- }
38
- return prev;
39
- }, routes);
40
- }
13
+ const name = "vite-plugin-mock-data";
14
+ const pkg = {
15
+ name
16
+ };
41
17
  function sirvOptions(headers) {
42
18
  return {
43
19
  dev: true,
@@ -58,7 +34,7 @@ function sirvOptions(headers) {
58
34
  }
59
35
  };
60
36
  }
61
- function configureServer(server, routerOpts, routes, serve, cwd) {
37
+ function configureServer(server, routerOpts, routes, cwd) {
62
38
  const router = getRouter(routerOpts);
63
39
  if (Array.isArray(routes)) {
64
40
  routes.forEach((route) => {
@@ -68,23 +44,24 @@ function configureServer(server, routerOpts, routes, serve, cwd) {
68
44
  pathname = methods;
69
45
  methods = "GET";
70
46
  }
47
+ methods = methods.toUpperCase();
71
48
  let routeConfig = route[xpath];
72
- if (!isObject(routeConfig)) {
49
+ if (!isWhatType.isObject(routeConfig)) {
73
50
  routeConfig = { handler: routeConfig };
74
51
  }
75
52
  let handler;
76
53
  if (typeof routeConfig.file === "string") {
77
54
  handler = (req, res) => {
78
- const parsedPath = node_path.parse(toAbsolute(routeConfig.file, cwd));
79
- const serve2 = sirv(parsedPath.dir, sirvOptions(server.config.server.headers));
55
+ const parsedPath = node_path.parse(vpRuntimeHelper.toAbsolutePath(routeConfig.file, cwd));
56
+ const serve = sirv(parsedPath.dir, sirvOptions(server.config.server.headers));
80
57
  req.url = `/${parsedPath.base}`;
81
- serve2(req, res);
58
+ serve(req, res);
82
59
  };
83
60
  } else if (typeof routeConfig.handler !== "function") {
84
61
  const ret = routeConfig.handler;
85
62
  const retType = typeof ret;
86
63
  handler = (req, res) => {
87
- vite.send(req, res, retType !== "string" ? JSON.stringify(ret) : ret, isObject(ret) ? "json" : "html", {
64
+ vite.send(req, res, retType !== "string" ? JSON.stringify(ret) : ret, isWhatType.isObject(ret) ? "json" : "html", {
88
65
  headers: server.config.server.headers
89
66
  });
90
67
  };
@@ -97,41 +74,82 @@ function configureServer(server, routerOpts, routes, serve, cwd) {
97
74
  });
98
75
  });
99
76
  }
100
- if (serve) {
101
- server.middlewares.use(serve);
102
- }
103
77
  server.middlewares.use((req, res, next) => {
104
78
  router.defaultRoute = () => next();
105
79
  router.lookup(req, res);
106
80
  });
107
81
  }
108
- function createPlugin(opts) {
109
- const { isAfter, routerOptions, routes, assets, cwd = process.cwd() } = opts;
82
+ const PLUGIN_NAME = name;
83
+ const logger = vpRuntimeHelper.logFactory.getLogger(PLUGIN_NAME);
84
+ async function getRoute(filename) {
85
+ logger.debug("Load mock file:", filename);
86
+ let { ext, dir, name: name2 } = node_path.parse(filename);
87
+ const isTs = ext === ".ts" || ext === ".mts";
88
+ if (isTs) {
89
+ const { code } = await vite.transformWithEsbuild(node_fs.readFileSync(filename, "utf-8"), filename, {
90
+ loader: "ts",
91
+ target: "esnext"
92
+ });
93
+ filename = node_path.join(dir, `${name2}-${PLUGIN_NAME}.mjs`);
94
+ ext = ".mjs";
95
+ await promises.writeFile(filename, code);
96
+ }
97
+ let config;
98
+ switch (ext) {
99
+ case ".js":
100
+ config = node_module.createRequire(typeof document === "undefined" ? require("url").pathToFileURL(__filename).href : _documentCurrentScript && _documentCurrentScript.tagName.toUpperCase() === "SCRIPT" && _documentCurrentScript.src || new URL("index.js", document.baseURI).href)(filename);
101
+ break;
102
+ case ".mjs":
103
+ config = (await import(filename)).default;
104
+ if (isTs) {
105
+ await promises.unlink(filename);
106
+ }
107
+ break;
108
+ case ".json":
109
+ config = JSON.parse(node_fs.readFileSync(filename, "utf-8"));
110
+ break;
111
+ }
112
+ return config;
113
+ }
114
+ async function loadRoutes(dir, routes) {
115
+ const paths = await tinyglobby.glob(`${dir}/**/*.{js,mjs,json,ts,mts}`, { absolute: true });
116
+ const configs = await Promise.all(paths.map(getRoute));
117
+ for (const config of configs) {
118
+ if (config) {
119
+ routes.push(config);
120
+ }
121
+ }
122
+ }
123
+ function pluginMockDate(opts) {
124
+ if (opts.enableBanner) {
125
+ vpRuntimeHelper.banner(pkg.name);
126
+ }
127
+ const { isAfter, routerOptions, routes, logLevel, cwd = process.cwd() } = opts;
128
+ if (logLevel) {
129
+ logger.level = logLevel;
130
+ }
110
131
  const allRoutes = [];
111
132
  return {
112
- name: "vite-plugin-mock-data",
133
+ name: PLUGIN_NAME,
113
134
  async configureServer(server) {
114
135
  if (typeof routes === "string") {
115
- await loadRoutes(toAbsolute(routes, cwd), allRoutes);
136
+ logger.debug("Load routes from", routes);
137
+ await loadRoutes(vpRuntimeHelper.toAbsolutePath(routes, cwd), allRoutes);
116
138
  } else if (Array.isArray(routes)) {
117
139
  for (const route of routes) {
140
+ logger.debug("Load routes from", route);
118
141
  if (typeof route === "string") {
119
- await loadRoutes(toAbsolute(route, cwd), allRoutes);
142
+ await loadRoutes(vpRuntimeHelper.toAbsolutePath(route, cwd), allRoutes);
120
143
  } else {
121
144
  allRoutes.push(route);
122
145
  }
123
146
  }
124
- } else if (isObject(routes)) {
147
+ } else if (isWhatType.isObject(routes)) {
148
+ logger.debug("Load routes from", routes);
125
149
  allRoutes.push(routes);
126
150
  }
127
- let serve = null;
128
- if (assets) {
129
- serve = sirv(toAbsolute(assets, cwd), sirvOptions(server.config.server.headers));
130
- }
131
- if (allRoutes && allRoutes.length > 0) {
132
- return isAfter ? () => configureServer(server, routerOptions, allRoutes, serve, cwd) : configureServer(server, routerOptions, allRoutes, serve, cwd);
133
- }
151
+ return isAfter ? () => configureServer(server, routerOptions, allRoutes, cwd) : configureServer(server, routerOptions, allRoutes, cwd);
134
152
  }
135
153
  };
136
154
  }
137
- module.exports = createPlugin;
155
+ module.exports = pluginMockDate;
package/dist/index.mjs CHANGED
@@ -1,41 +1,17 @@
1
- import { posix, isAbsolute, extname, parse } from "node:path";
2
- import { createRequire } from "node:module";
3
- import { readFileSync } from "node:fs";
4
- import { glob } from "tinyglobby";
1
+ import { isObject } from "is-what-type";
2
+ import { toAbsolutePath, logFactory, banner } from "vp-runtime-helper";
3
+ import { parse, join } from "node:path";
5
4
  import getRouter from "find-my-way";
6
5
  import sirv from "sirv";
7
- import { send } from "vite";
8
- function isObject(val) {
9
- return val && typeof val === "object";
10
- }
11
- function toAbsolute(pth, cwd) {
12
- return isAbsolute(pth) ? pth : posix.join(cwd || process.cwd(), pth);
13
- }
14
- async function getRoute(filename) {
15
- let config;
16
- switch (extname(filename)) {
17
- case ".js":
18
- config = createRequire(import.meta.url)(filename);
19
- break;
20
- case ".mjs":
21
- config = (await import(filename)).default;
22
- break;
23
- case ".json":
24
- config = JSON.parse(readFileSync(filename, "utf-8"));
25
- break;
26
- }
27
- return config;
28
- }
29
- async function loadRoutes(dir, routes) {
30
- const paths = await glob(`${dir}/**/*.{js,mjs,json}`, { absolute: true });
31
- const configs = await Promise.all(paths.map(getRoute));
32
- configs.reduce((prev, cur) => {
33
- if (cur) {
34
- prev.push(cur);
35
- }
36
- return prev;
37
- }, routes);
38
- }
6
+ import { send, transformWithEsbuild } from "vite";
7
+ import { readFileSync } from "node:fs";
8
+ import { writeFile, unlink } from "node:fs/promises";
9
+ import { createRequire } from "node:module";
10
+ import { glob } from "tinyglobby";
11
+ const name = "vite-plugin-mock-data";
12
+ const pkg = {
13
+ name
14
+ };
39
15
  function sirvOptions(headers) {
40
16
  return {
41
17
  dev: true,
@@ -56,7 +32,7 @@ function sirvOptions(headers) {
56
32
  }
57
33
  };
58
34
  }
59
- function configureServer(server, routerOpts, routes, serve, cwd) {
35
+ function configureServer(server, routerOpts, routes, cwd) {
60
36
  const router = getRouter(routerOpts);
61
37
  if (Array.isArray(routes)) {
62
38
  routes.forEach((route) => {
@@ -66,6 +42,7 @@ function configureServer(server, routerOpts, routes, serve, cwd) {
66
42
  pathname = methods;
67
43
  methods = "GET";
68
44
  }
45
+ methods = methods.toUpperCase();
69
46
  let routeConfig = route[xpath];
70
47
  if (!isObject(routeConfig)) {
71
48
  routeConfig = { handler: routeConfig };
@@ -73,10 +50,10 @@ function configureServer(server, routerOpts, routes, serve, cwd) {
73
50
  let handler;
74
51
  if (typeof routeConfig.file === "string") {
75
52
  handler = (req, res) => {
76
- const parsedPath = parse(toAbsolute(routeConfig.file, cwd));
77
- const serve2 = sirv(parsedPath.dir, sirvOptions(server.config.server.headers));
53
+ const parsedPath = parse(toAbsolutePath(routeConfig.file, cwd));
54
+ const serve = sirv(parsedPath.dir, sirvOptions(server.config.server.headers));
78
55
  req.url = `/${parsedPath.base}`;
79
- serve2(req, res);
56
+ serve(req, res);
80
57
  };
81
58
  } else if (typeof routeConfig.handler !== "function") {
82
59
  const ret = routeConfig.handler;
@@ -95,43 +72,84 @@ function configureServer(server, routerOpts, routes, serve, cwd) {
95
72
  });
96
73
  });
97
74
  }
98
- if (serve) {
99
- server.middlewares.use(serve);
100
- }
101
75
  server.middlewares.use((req, res, next) => {
102
76
  router.defaultRoute = () => next();
103
77
  router.lookup(req, res);
104
78
  });
105
79
  }
106
- function createPlugin(opts) {
107
- const { isAfter, routerOptions, routes, assets, cwd = process.cwd() } = opts;
80
+ const PLUGIN_NAME = name;
81
+ const logger = logFactory.getLogger(PLUGIN_NAME);
82
+ async function getRoute(filename) {
83
+ logger.debug("Load mock file:", filename);
84
+ let { ext, dir, name: name2 } = parse(filename);
85
+ const isTs = ext === ".ts" || ext === ".mts";
86
+ if (isTs) {
87
+ const { code } = await transformWithEsbuild(readFileSync(filename, "utf-8"), filename, {
88
+ loader: "ts",
89
+ target: "esnext"
90
+ });
91
+ filename = join(dir, `${name2}-${PLUGIN_NAME}.mjs`);
92
+ ext = ".mjs";
93
+ await writeFile(filename, code);
94
+ }
95
+ let config;
96
+ switch (ext) {
97
+ case ".js":
98
+ config = createRequire(import.meta.url)(filename);
99
+ break;
100
+ case ".mjs":
101
+ config = (await import(filename)).default;
102
+ if (isTs) {
103
+ await unlink(filename);
104
+ }
105
+ break;
106
+ case ".json":
107
+ config = JSON.parse(readFileSync(filename, "utf-8"));
108
+ break;
109
+ }
110
+ return config;
111
+ }
112
+ async function loadRoutes(dir, routes) {
113
+ const paths = await glob(`${dir}/**/*.{js,mjs,json,ts,mts}`, { absolute: true });
114
+ const configs = await Promise.all(paths.map(getRoute));
115
+ for (const config of configs) {
116
+ if (config) {
117
+ routes.push(config);
118
+ }
119
+ }
120
+ }
121
+ function pluginMockDate(opts) {
122
+ if (opts.enableBanner) {
123
+ banner(pkg.name);
124
+ }
125
+ const { isAfter, routerOptions, routes, logLevel, cwd = process.cwd() } = opts;
126
+ if (logLevel) {
127
+ logger.level = logLevel;
128
+ }
108
129
  const allRoutes = [];
109
130
  return {
110
- name: "vite-plugin-mock-data",
131
+ name: PLUGIN_NAME,
111
132
  async configureServer(server) {
112
133
  if (typeof routes === "string") {
113
- await loadRoutes(toAbsolute(routes, cwd), allRoutes);
134
+ logger.debug("Load routes from", routes);
135
+ await loadRoutes(toAbsolutePath(routes, cwd), allRoutes);
114
136
  } else if (Array.isArray(routes)) {
115
137
  for (const route of routes) {
138
+ logger.debug("Load routes from", route);
116
139
  if (typeof route === "string") {
117
- await loadRoutes(toAbsolute(route, cwd), allRoutes);
140
+ await loadRoutes(toAbsolutePath(route, cwd), allRoutes);
118
141
  } else {
119
142
  allRoutes.push(route);
120
143
  }
121
144
  }
122
145
  } else if (isObject(routes)) {
146
+ logger.debug("Load routes from", routes);
123
147
  allRoutes.push(routes);
124
148
  }
125
- let serve = null;
126
- if (assets) {
127
- serve = sirv(toAbsolute(assets, cwd), sirvOptions(server.config.server.headers));
128
- }
129
- if (allRoutes && allRoutes.length > 0) {
130
- return isAfter ? () => configureServer(server, routerOptions, allRoutes, serve, cwd) : configureServer(server, routerOptions, allRoutes, serve, cwd);
131
- }
149
+ return isAfter ? () => configureServer(server, routerOptions, allRoutes, cwd) : configureServer(server, routerOptions, allRoutes, cwd);
132
150
  }
133
151
  };
134
152
  }
135
153
  export {
136
- createPlugin as default
154
+ pluginMockDate as default
137
155
  };
@@ -0,0 +1,2 @@
1
+ import { RouteConfig } from './types';
2
+ export default function loadRoutes(dir: string, routes: RouteConfig[]): Promise<void>;
@@ -1,4 +1,5 @@
1
- import { Config as SirvConfig, HTTPVersion, RouteOptions, Handler } from 'find-my-way';
1
+ import { Config as SirvConfig, Handler, HTTPVersion, RouteOptions } from 'find-my-way';
2
+ import { LogLevel } from 'vp-runtime-helper';
2
3
  export interface HandleRoute {
3
4
  file?: string;
4
5
  handler?: any | Handler<HTTPVersion.V1>;
@@ -14,15 +15,25 @@ export interface Options {
14
15
  * @default `process.cwd()`
15
16
  */
16
17
  cwd?: string;
18
+ /**
19
+ * Cache directory for compiled files.
20
+ *
21
+ * ็”จไบŽๅญ˜ๆ”พ ts ่ขซ็ผ–่ฏ‘ๅŽๅญ˜ๆ”พ็š„ๆ–‡ไปถ็›ฎๅฝ•ใ€‚
22
+ *
23
+ * @default `${cwd}/node_modules/.vite_mock_data`
24
+ */
25
+ cacheDir?: string;
26
+ /**
27
+ * Log level
28
+ *
29
+ * ่พ“ๅ‡บๆ—ฅๅฟ—็ญ‰็บง
30
+ */
31
+ logLevel?: LogLevel;
17
32
  /**
18
33
  * If `true`, these mock routes is matched after internal middlewares are installed.
19
34
  * @default `false`
20
35
  */
21
36
  isAfter?: boolean;
22
- /**
23
- * Specify the directory to define mock assets.
24
- */
25
- assets?: string;
26
37
  /**
27
38
  * Initial options of `find-my-way`. see more at https://github.com/delvedor/find-my-way#findmywayoptions
28
39
  */
@@ -32,4 +43,10 @@ export interface Options {
32
43
  * or specify the directory to define mock routes that should be added to the dev server.
33
44
  */
34
45
  routes?: RouteConfig | Array<RouteConfig | string> | string;
46
+ /**
47
+ * Whether to output the banner
48
+ *
49
+ * ๆ˜ฏๅฆ่พ“ๅ‡บ banner
50
+ */
51
+ enableBanner?: boolean;
35
52
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-mock-data",
3
- "version": "6.0.1",
3
+ "version": "6.0.3",
4
4
  "description": "Provides a simple way to mock data.",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.mjs",
@@ -16,11 +16,6 @@
16
16
  "node": ">=14.18.0",
17
17
  "vite": ">=3.1.0"
18
18
  },
19
- "scripts": {
20
- "release": "npm publish",
21
- "build:lib": "vite build",
22
- "watch": "vite build --watch"
23
- },
24
19
  "repository": {
25
20
  "type": "git",
26
21
  "url": "git+https://github.com/fengxinming/vite-plugins.git",
@@ -35,13 +30,25 @@
35
30
  "bugs": {
36
31
  "url": "https://github.com/fengxinming/vite-plugins/issues"
37
32
  },
38
- "homepage": "https://github.com/fengxinming/vite-plugins#readme",
33
+ "homepage": "https://fengxinming.github.io/vite-plugins/plugins/vite-plugin-mock-data/quick-start",
39
34
  "dependencies": {
40
35
  "find-my-way": "^9.2.0",
36
+ "is-what-type": "^1.1.4",
41
37
  "sirv": "^3.0.1",
42
- "tinyglobby": "^0.2.12"
38
+ "tinyglobby": "^0.2.12",
39
+ "vp-runtime-helper": "^1.0.10"
40
+ },
41
+ "devDependencies": {
42
+ "@rollup/plugin-typescript": "^12.1.2",
43
+ "@types/fs-extra": "^11.0.4",
44
+ "vite": "^6.1.0"
43
45
  },
44
46
  "files": [
45
47
  "dist"
46
- ]
48
+ ],
49
+ "scripts": {
50
+ "build": "vite build",
51
+ "watch": "vite build --watch",
52
+ "release": "pnpm publish --no-git-checks"
53
+ }
47
54
  }