vite-plugin-mock-data 4.0.6 → 6.0.1
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 +57 -10
- package/dist/index.d.ts +3 -31
- package/dist/index.js +44 -36
- package/dist/index.mjs +45 -37
- package/dist/typings.d.ts +35 -0
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -15,12 +15,58 @@ npm install vite-plugin-mock-data --save-dev
|
|
|
15
15
|
|
|
16
16
|
## Options
|
|
17
17
|
|
|
18
|
+
```ts
|
|
19
|
+
import { Config as SirvConfig, HTTPVersion, RouteOptions, Handler } from 'find-my-way';
|
|
20
|
+
|
|
21
|
+
export interface HandleRoute {
|
|
22
|
+
file?: string;
|
|
23
|
+
handler?: any | Handler<HTTPVersion.V1>;
|
|
24
|
+
options?: RouteOptions;
|
|
25
|
+
store?: any;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export interface RouteConfig {
|
|
29
|
+
[route: string]: string | Handler<HTTPVersion.V1> | HandleRoute;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export interface Options {
|
|
33
|
+
/**
|
|
34
|
+
* The directory to serve files from.
|
|
35
|
+
* @default `process.cwd()`
|
|
36
|
+
*/
|
|
37
|
+
cwd?: string;
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* If `true`, these mock routes is matched after internal middlewares are installed.
|
|
41
|
+
* @default `false`
|
|
42
|
+
*/
|
|
43
|
+
isAfter?: boolean;
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Specify the directory to define mock assets.
|
|
47
|
+
*/
|
|
48
|
+
assets?: string;
|
|
49
|
+
|
|
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
|
+
|
|
18
63
|
* `cwd` - Default: `process.cwd()`.
|
|
19
64
|
* `isAfter` - If `true`, these mock routes is matched after internal middlewares are installed.
|
|
20
|
-
* `
|
|
21
|
-
* `
|
|
22
|
-
* `
|
|
23
|
-
* `
|
|
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.
|
|
24
70
|
|
|
25
71
|
## Usage
|
|
26
72
|
|
|
@@ -33,7 +79,7 @@ import mockData from 'vite-plugin-mock-data';
|
|
|
33
79
|
export default defineConfig({
|
|
34
80
|
plugins: [
|
|
35
81
|
mockData({
|
|
36
|
-
|
|
82
|
+
assets: './mockAssets'
|
|
37
83
|
})
|
|
38
84
|
]
|
|
39
85
|
});
|
|
@@ -67,7 +113,7 @@ import mockData from 'vite-plugin-mock-data';
|
|
|
67
113
|
export default defineConfig({
|
|
68
114
|
plugins: [
|
|
69
115
|
mockData({
|
|
70
|
-
|
|
116
|
+
routes: {
|
|
71
117
|
'/hello': 'hello',
|
|
72
118
|
'/hello2'(req, res) {
|
|
73
119
|
res.statusCode = 200;
|
|
@@ -110,7 +156,7 @@ import mockData from 'vite-plugin-mock-data';
|
|
|
110
156
|
export default defineConfig({
|
|
111
157
|
plugins: [
|
|
112
158
|
mockData({
|
|
113
|
-
|
|
159
|
+
routes: './mock'
|
|
114
160
|
})
|
|
115
161
|
]
|
|
116
162
|
});
|
|
@@ -156,6 +202,7 @@ fetch('/package.json')
|
|
|
156
202
|
|
|
157
203
|
## Examples
|
|
158
204
|
|
|
159
|
-
* [See vite3 demo](../../examples/vite3-
|
|
160
|
-
* [See vite4 demo](../../examples/vite4-
|
|
161
|
-
* [See vite5 demo](../../examples/vite5-
|
|
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)
|
package/dist/index.d.ts
CHANGED
|
@@ -1,34 +1,6 @@
|
|
|
1
|
-
import { Config as SirvConfig, HTTPVersion, RouteOptions, Handler } from 'find-my-way';
|
|
2
1
|
import { Plugin } from 'vite';
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
handler?: any | Handler<HTTPVersion.V1>;
|
|
6
|
-
options?: RouteOptions;
|
|
7
|
-
store?: any;
|
|
8
|
-
}
|
|
9
|
-
export interface RouteConfig {
|
|
10
|
-
[route: string]: string | Handler<HTTPVersion.V1> | HandleRoute;
|
|
11
|
-
}
|
|
12
|
-
export interface Options {
|
|
13
|
-
/**
|
|
14
|
-
* The directory to serve files from.
|
|
15
|
-
* @default `process.cwd()`
|
|
16
|
-
*/
|
|
17
|
-
cwd?: string;
|
|
18
|
-
/**
|
|
19
|
-
* If `true`, these mock routes is matched after internal middlewares are installed.
|
|
20
|
-
* @default `false`
|
|
21
|
-
*/
|
|
22
|
-
isAfter?: boolean;
|
|
23
|
-
/** Specify the directory to define mock assets. */
|
|
24
|
-
mockAssetsDir?: string;
|
|
25
|
-
/** Initial options of `find-my-way`. see more at https://github.com/delvedor/find-my-way#findmywayoptions */
|
|
26
|
-
mockRouterOptions?: SirvConfig<HTTPVersion.V1> | SirvConfig<HTTPVersion.V2>;
|
|
27
|
-
/** Initial list of mock routes that should be added to the dev server. */
|
|
28
|
-
mockRoutes?: RouteConfig | RouteConfig[];
|
|
29
|
-
/** Specify the directory to define mock routes that should be added to the dev server. */
|
|
30
|
-
mockRoutesDir?: string;
|
|
31
|
-
}
|
|
2
|
+
import { Options } from './typings';
|
|
3
|
+
export * from './typings';
|
|
32
4
|
/**
|
|
33
5
|
* Provides a simple way to mock data.
|
|
34
6
|
*
|
|
@@ -40,7 +12,7 @@ export interface Options {
|
|
|
40
12
|
* export default defineConfig({
|
|
41
13
|
* plugins: [
|
|
42
14
|
* mockData({
|
|
43
|
-
*
|
|
15
|
+
* routes: './mock'
|
|
44
16
|
* })
|
|
45
17
|
* ]
|
|
46
18
|
* });
|
package/dist/index.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
const node_path = require("node:path");
|
|
3
3
|
const node_module = require("node:module");
|
|
4
4
|
const node_fs = require("node:fs");
|
|
5
|
-
const
|
|
5
|
+
const tinyglobby = require("tinyglobby");
|
|
6
6
|
const getRouter = require("find-my-way");
|
|
7
7
|
const sirv = require("sirv");
|
|
8
8
|
const vite = require("vite");
|
|
@@ -13,6 +13,31 @@ function isObject(val) {
|
|
|
13
13
|
function toAbsolute(pth, cwd) {
|
|
14
14
|
return node_path.isAbsolute(pth) ? pth : node_path.posix.join(cwd || process.cwd(), pth);
|
|
15
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
|
+
}
|
|
16
41
|
function sirvOptions(headers) {
|
|
17
42
|
return {
|
|
18
43
|
dev: true,
|
|
@@ -81,47 +106,30 @@ function configureServer(server, routerOpts, routes, serve, cwd) {
|
|
|
81
106
|
});
|
|
82
107
|
}
|
|
83
108
|
function createPlugin(opts) {
|
|
84
|
-
const { isAfter,
|
|
85
|
-
|
|
86
|
-
let mockRoutes = opts.mockRoutes || [];
|
|
87
|
-
if (!cwd) {
|
|
88
|
-
cwd = process.cwd();
|
|
89
|
-
}
|
|
90
|
-
if (isObject(mockRoutes) && !Array.isArray(mockRoutes)) {
|
|
91
|
-
mockRoutes = [mockRoutes];
|
|
92
|
-
}
|
|
109
|
+
const { isAfter, routerOptions, routes, assets, cwd = process.cwd() } = opts;
|
|
110
|
+
const allRoutes = [];
|
|
93
111
|
return {
|
|
94
112
|
name: "vite-plugin-mock-data",
|
|
95
113
|
async configureServer(server) {
|
|
96
|
-
if (
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
break;
|
|
109
|
-
case ".json":
|
|
110
|
-
config = JSON.parse(node_fs.readFileSync(file, "utf-8"));
|
|
111
|
-
break;
|
|
112
|
-
}
|
|
113
|
-
if (config) {
|
|
114
|
-
mockRoutes.push(config);
|
|
115
|
-
}
|
|
116
|
-
})();
|
|
117
|
-
}));
|
|
114
|
+
if (typeof routes === "string") {
|
|
115
|
+
await loadRoutes(toAbsolute(routes, cwd), allRoutes);
|
|
116
|
+
} else if (Array.isArray(routes)) {
|
|
117
|
+
for (const route of routes) {
|
|
118
|
+
if (typeof route === "string") {
|
|
119
|
+
await loadRoutes(toAbsolute(route, cwd), allRoutes);
|
|
120
|
+
} else {
|
|
121
|
+
allRoutes.push(route);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
} else if (isObject(routes)) {
|
|
125
|
+
allRoutes.push(routes);
|
|
118
126
|
}
|
|
119
127
|
let serve = null;
|
|
120
|
-
if (
|
|
121
|
-
serve = sirv(toAbsolute(
|
|
128
|
+
if (assets) {
|
|
129
|
+
serve = sirv(toAbsolute(assets, cwd), sirvOptions(server.config.server.headers));
|
|
122
130
|
}
|
|
123
|
-
if (
|
|
124
|
-
return isAfter ? () => configureServer(server,
|
|
131
|
+
if (allRoutes && allRoutes.length > 0) {
|
|
132
|
+
return isAfter ? () => configureServer(server, routerOptions, allRoutes, serve, cwd) : configureServer(server, routerOptions, allRoutes, serve, cwd);
|
|
125
133
|
}
|
|
126
134
|
}
|
|
127
135
|
};
|
package/dist/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { posix, isAbsolute, extname, parse } from "node:path";
|
|
2
2
|
import { createRequire } from "node:module";
|
|
3
3
|
import { readFileSync } from "node:fs";
|
|
4
|
-
import {
|
|
4
|
+
import { glob } from "tinyglobby";
|
|
5
5
|
import getRouter from "find-my-way";
|
|
6
6
|
import sirv from "sirv";
|
|
7
7
|
import { send } from "vite";
|
|
@@ -11,6 +11,31 @@ function isObject(val) {
|
|
|
11
11
|
function toAbsolute(pth, cwd) {
|
|
12
12
|
return isAbsolute(pth) ? pth : posix.join(cwd || process.cwd(), pth);
|
|
13
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
|
+
}
|
|
14
39
|
function sirvOptions(headers) {
|
|
15
40
|
return {
|
|
16
41
|
dev: true,
|
|
@@ -79,47 +104,30 @@ function configureServer(server, routerOpts, routes, serve, cwd) {
|
|
|
79
104
|
});
|
|
80
105
|
}
|
|
81
106
|
function createPlugin(opts) {
|
|
82
|
-
const { isAfter,
|
|
83
|
-
|
|
84
|
-
let mockRoutes = opts.mockRoutes || [];
|
|
85
|
-
if (!cwd) {
|
|
86
|
-
cwd = process.cwd();
|
|
87
|
-
}
|
|
88
|
-
if (isObject(mockRoutes) && !Array.isArray(mockRoutes)) {
|
|
89
|
-
mockRoutes = [mockRoutes];
|
|
90
|
-
}
|
|
107
|
+
const { isAfter, routerOptions, routes, assets, cwd = process.cwd() } = opts;
|
|
108
|
+
const allRoutes = [];
|
|
91
109
|
return {
|
|
92
110
|
name: "vite-plugin-mock-data",
|
|
93
111
|
async configureServer(server) {
|
|
94
|
-
if (
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
break;
|
|
107
|
-
case ".json":
|
|
108
|
-
config = JSON.parse(readFileSync(file, "utf-8"));
|
|
109
|
-
break;
|
|
110
|
-
}
|
|
111
|
-
if (config) {
|
|
112
|
-
mockRoutes.push(config);
|
|
113
|
-
}
|
|
114
|
-
})();
|
|
115
|
-
}));
|
|
112
|
+
if (typeof routes === "string") {
|
|
113
|
+
await loadRoutes(toAbsolute(routes, cwd), allRoutes);
|
|
114
|
+
} else if (Array.isArray(routes)) {
|
|
115
|
+
for (const route of routes) {
|
|
116
|
+
if (typeof route === "string") {
|
|
117
|
+
await loadRoutes(toAbsolute(route, cwd), allRoutes);
|
|
118
|
+
} else {
|
|
119
|
+
allRoutes.push(route);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
} else if (isObject(routes)) {
|
|
123
|
+
allRoutes.push(routes);
|
|
116
124
|
}
|
|
117
125
|
let serve = null;
|
|
118
|
-
if (
|
|
119
|
-
serve = sirv(toAbsolute(
|
|
126
|
+
if (assets) {
|
|
127
|
+
serve = sirv(toAbsolute(assets, cwd), sirvOptions(server.config.server.headers));
|
|
120
128
|
}
|
|
121
|
-
if (
|
|
122
|
-
return isAfter ? () => configureServer(server,
|
|
129
|
+
if (allRoutes && allRoutes.length > 0) {
|
|
130
|
+
return isAfter ? () => configureServer(server, routerOptions, allRoutes, serve, cwd) : configureServer(server, routerOptions, allRoutes, serve, cwd);
|
|
123
131
|
}
|
|
124
132
|
}
|
|
125
133
|
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Config as SirvConfig, HTTPVersion, RouteOptions, Handler } from 'find-my-way';
|
|
2
|
+
export interface HandleRoute {
|
|
3
|
+
file?: string;
|
|
4
|
+
handler?: any | Handler<HTTPVersion.V1>;
|
|
5
|
+
options?: RouteOptions;
|
|
6
|
+
store?: any;
|
|
7
|
+
}
|
|
8
|
+
export interface RouteConfig {
|
|
9
|
+
[route: string]: string | Handler<HTTPVersion.V1> | HandleRoute;
|
|
10
|
+
}
|
|
11
|
+
export interface Options {
|
|
12
|
+
/**
|
|
13
|
+
* The directory to serve files from.
|
|
14
|
+
* @default `process.cwd()`
|
|
15
|
+
*/
|
|
16
|
+
cwd?: string;
|
|
17
|
+
/**
|
|
18
|
+
* If `true`, these mock routes is matched after internal middlewares are installed.
|
|
19
|
+
* @default `false`
|
|
20
|
+
*/
|
|
21
|
+
isAfter?: boolean;
|
|
22
|
+
/**
|
|
23
|
+
* Specify the directory to define mock assets.
|
|
24
|
+
*/
|
|
25
|
+
assets?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Initial options of `find-my-way`. see more at https://github.com/delvedor/find-my-way#findmywayoptions
|
|
28
|
+
*/
|
|
29
|
+
routerOptions?: SirvConfig<HTTPVersion.V1> | SirvConfig<HTTPVersion.V2>;
|
|
30
|
+
/**
|
|
31
|
+
* Initial list of mock routes that should be added to the dev server
|
|
32
|
+
* or specify the directory to define mock routes that should be added to the dev server.
|
|
33
|
+
*/
|
|
34
|
+
routes?: RouteConfig | Array<RouteConfig | string> | string;
|
|
35
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-mock-data",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "6.0.1",
|
|
4
4
|
"description": "Provides a simple way to mock data.",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
@@ -18,7 +18,7 @@
|
|
|
18
18
|
},
|
|
19
19
|
"scripts": {
|
|
20
20
|
"release": "npm publish",
|
|
21
|
-
"build": "vite build",
|
|
21
|
+
"build:lib": "vite build",
|
|
22
22
|
"watch": "vite build --watch"
|
|
23
23
|
},
|
|
24
24
|
"repository": {
|
|
@@ -37,9 +37,9 @@
|
|
|
37
37
|
},
|
|
38
38
|
"homepage": "https://github.com/fengxinming/vite-plugins#readme",
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"find-my-way": "^
|
|
41
|
-
"
|
|
42
|
-
"
|
|
40
|
+
"find-my-way": "^9.2.0",
|
|
41
|
+
"sirv": "^3.0.1",
|
|
42
|
+
"tinyglobby": "^0.2.12"
|
|
43
43
|
},
|
|
44
44
|
"files": [
|
|
45
45
|
"dist"
|