vite-plugin-mock-data 4.0.0 → 4.0.2
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 +10 -5
- package/dist/index.d.ts +32 -0
- package/dist/index.js +129 -0
- package/dist/index.mjs +115 -121
- package/package.json +9 -16
- package/dist/index.cjs +0 -137
package/README.md
CHANGED
|
@@ -16,7 +16,7 @@ npm install vite-plugin-mock-data --save-dev
|
|
|
16
16
|
## Options
|
|
17
17
|
|
|
18
18
|
* `cwd` - Default: `process.cwd()`.
|
|
19
|
-
* `isAfter` - If `true`, these mock routes is matched
|
|
19
|
+
* `isAfter` - If `true`, these mock routes is matched after internal middlewares are installed.
|
|
20
20
|
* `mockAssetsDir` - Specify the directory to define mock assets.
|
|
21
21
|
* `mockRouterOptions` - [Initial options of `find-my-way`](https://github.com/delvedor/find-my-way#findmywayoptions)
|
|
22
22
|
* `mockRoutes` - Initial list of mock routes that should be added to the dev server.
|
|
@@ -27,6 +27,7 @@ npm install vite-plugin-mock-data --save-dev
|
|
|
27
27
|
### Specify the directory to define mock assets
|
|
28
28
|
|
|
29
29
|
```js
|
|
30
|
+
import { defineConfig } from 'vite';
|
|
30
31
|
import mockData from 'vite-plugin-mock-data';
|
|
31
32
|
|
|
32
33
|
export default defineConfig({
|
|
@@ -60,6 +61,7 @@ fetch('/test.json')
|
|
|
60
61
|
### add mock routes to the dev server
|
|
61
62
|
|
|
62
63
|
```js
|
|
64
|
+
import { defineConfig } from 'vite';
|
|
63
65
|
import mockData from 'vite-plugin-mock-data';
|
|
64
66
|
|
|
65
67
|
export default defineConfig({
|
|
@@ -81,10 +83,10 @@ export default defineConfig({
|
|
|
81
83
|
},
|
|
82
84
|
'/json': {
|
|
83
85
|
handler: { hello: 1 }
|
|
84
|
-
}
|
|
86
|
+
},
|
|
85
87
|
'/package.json': {
|
|
86
88
|
file: './package.json'
|
|
87
|
-
}
|
|
89
|
+
}
|
|
88
90
|
}
|
|
89
91
|
})
|
|
90
92
|
]
|
|
@@ -102,6 +104,7 @@ fetch('/package.json')
|
|
|
102
104
|
### Specify the directory to add mock routes to the dev server
|
|
103
105
|
|
|
104
106
|
```js
|
|
107
|
+
import { defineConfig } from 'vite';
|
|
105
108
|
import mockData from 'vite-plugin-mock-data';
|
|
106
109
|
|
|
107
110
|
export default defineConfig({
|
|
@@ -139,7 +142,7 @@ module.exports = {
|
|
|
139
142
|
},
|
|
140
143
|
'/package.json': {
|
|
141
144
|
file: './package.json'
|
|
142
|
-
}
|
|
145
|
+
}
|
|
143
146
|
};
|
|
144
147
|
```
|
|
145
148
|
|
|
@@ -153,4 +156,6 @@ fetch('/package.json')
|
|
|
153
156
|
|
|
154
157
|
## Examples
|
|
155
158
|
|
|
156
|
-
|
|
159
|
+
* [See vite3 demo](../../examples/vite3-mock-data)
|
|
160
|
+
* [See vite4 demo](../../examples/vite4-mock-data)
|
|
161
|
+
* [See vite5 demo](../../examples/vite5-mock-data)
|
package/dist/index.d.ts
CHANGED
|
@@ -10,11 +10,43 @@ export interface RouteConfig {
|
|
|
10
10
|
[route: string]: string | Handler<HTTPVersion.V1> | HandleRoute;
|
|
11
11
|
}
|
|
12
12
|
export interface Options {
|
|
13
|
+
/**
|
|
14
|
+
* The directory to serve files from.
|
|
15
|
+
* @default `process.cwd()`
|
|
16
|
+
*/
|
|
13
17
|
cwd?: string;
|
|
18
|
+
/**
|
|
19
|
+
* If `true`, these mock routes is matched after internal middlewares are installed.
|
|
20
|
+
* @default `false`
|
|
21
|
+
*/
|
|
14
22
|
isAfter?: boolean;
|
|
23
|
+
/** Specify the directory to define mock assets. */
|
|
15
24
|
mockAssetsDir?: string;
|
|
25
|
+
/** Initial options of `find-my-way`. see more at https://github.com/delvedor/find-my-way#findmywayoptions */
|
|
16
26
|
mockRouterOptions?: SirvConfig<HTTPVersion.V1> | SirvConfig<HTTPVersion.V2>;
|
|
27
|
+
/** Initial list of mock routes that should be added to the dev server. */
|
|
17
28
|
mockRoutes?: RouteConfig | RouteConfig[];
|
|
29
|
+
/** Specify the directory to define mock routes that should be added to the dev server. */
|
|
18
30
|
mockRoutesDir?: string;
|
|
19
31
|
}
|
|
32
|
+
/**
|
|
33
|
+
* Provides a simple way to mock data.
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```js
|
|
37
|
+
* import { defineConfig } from 'vite';
|
|
38
|
+
* import mockData from 'vite-plugin-mock-data';
|
|
39
|
+
*
|
|
40
|
+
* export default defineConfig({
|
|
41
|
+
* plugins: [
|
|
42
|
+
* mockData({
|
|
43
|
+
* mockRoutesDir: './mock'
|
|
44
|
+
* })
|
|
45
|
+
* ]
|
|
46
|
+
* });
|
|
47
|
+
* ```
|
|
48
|
+
*
|
|
49
|
+
* @param opts Options
|
|
50
|
+
* @returns a vite plugin
|
|
51
|
+
*/
|
|
20
52
|
export default function createPlugin(opts: Options): Plugin;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
const node_path = require("node:path");
|
|
3
|
+
const node_module = require("node:module");
|
|
4
|
+
const node_fs = require("node:fs");
|
|
5
|
+
const globby = require("globby");
|
|
6
|
+
const getRouter = require("find-my-way");
|
|
7
|
+
const sirv = require("sirv");
|
|
8
|
+
const vite = require("vite");
|
|
9
|
+
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
|
+
function sirvOptions(headers) {
|
|
17
|
+
return {
|
|
18
|
+
dev: true,
|
|
19
|
+
etag: true,
|
|
20
|
+
extensions: [],
|
|
21
|
+
setHeaders(res, pathname) {
|
|
22
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
23
|
+
if (/\.[tj]sx?$/.test(pathname)) {
|
|
24
|
+
res.setHeader("Content-Type", "application/javascript");
|
|
25
|
+
}
|
|
26
|
+
if (headers) {
|
|
27
|
+
Object.entries(headers).forEach(([key, val]) => {
|
|
28
|
+
if (val) {
|
|
29
|
+
res.setHeader(key, val);
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function configureServer(server, routerOpts, routes, serve, cwd) {
|
|
37
|
+
const router = getRouter(routerOpts);
|
|
38
|
+
if (Array.isArray(routes)) {
|
|
39
|
+
routes.forEach((route) => {
|
|
40
|
+
Object.keys(route).forEach((xpath) => {
|
|
41
|
+
let [methods, pathname] = xpath.split(" ");
|
|
42
|
+
if (!pathname) {
|
|
43
|
+
pathname = methods;
|
|
44
|
+
methods = "GET";
|
|
45
|
+
}
|
|
46
|
+
let routeConfig = route[xpath];
|
|
47
|
+
if (!isObject(routeConfig)) {
|
|
48
|
+
routeConfig = { handler: routeConfig };
|
|
49
|
+
}
|
|
50
|
+
let handler;
|
|
51
|
+
if (typeof routeConfig.file === "string") {
|
|
52
|
+
handler = (req, res) => {
|
|
53
|
+
const parsedPath = node_path.parse(toAbsolute(routeConfig.file, cwd));
|
|
54
|
+
const serve2 = sirv(parsedPath.dir, sirvOptions(server.config.server.headers));
|
|
55
|
+
req.url = `/${parsedPath.base}`;
|
|
56
|
+
serve2(req, res);
|
|
57
|
+
};
|
|
58
|
+
} else if (typeof routeConfig.handler !== "function") {
|
|
59
|
+
const ret = routeConfig.handler;
|
|
60
|
+
const retType = typeof ret;
|
|
61
|
+
handler = (req, res) => {
|
|
62
|
+
vite.send(req, res, retType !== "string" ? JSON.stringify(ret) : ret, isObject(ret) ? "json" : "html", {
|
|
63
|
+
headers: server.config.server.headers
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
} else {
|
|
67
|
+
handler = routeConfig.handler;
|
|
68
|
+
}
|
|
69
|
+
if (handler) {
|
|
70
|
+
router.on(methods.split("/"), pathname, {}, handler, routeConfig.store);
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
if (serve) {
|
|
76
|
+
server.middlewares.use(serve);
|
|
77
|
+
}
|
|
78
|
+
server.middlewares.use((req, res, next) => {
|
|
79
|
+
router.defaultRoute = () => next();
|
|
80
|
+
router.lookup(req, res);
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
function createPlugin(opts) {
|
|
84
|
+
const { isAfter, mockRouterOptions, mockAssetsDir } = opts;
|
|
85
|
+
let { cwd, mockRoutesDir } = opts;
|
|
86
|
+
let mockRoutes = opts.mockRoutes || [];
|
|
87
|
+
if (!cwd) {
|
|
88
|
+
cwd = process.cwd();
|
|
89
|
+
}
|
|
90
|
+
if (isObject(mockRoutes) && !Array.isArray(mockRoutes)) {
|
|
91
|
+
mockRoutes = [mockRoutes];
|
|
92
|
+
}
|
|
93
|
+
return {
|
|
94
|
+
name: "vite:mock-data",
|
|
95
|
+
async configureServer(server) {
|
|
96
|
+
if (mockRoutesDir) {
|
|
97
|
+
mockRoutesDir = toAbsolute(mockRoutesDir, cwd);
|
|
98
|
+
const paths = await globby.globby(`${mockRoutesDir}/**/*.{js,mjs,json}`);
|
|
99
|
+
await Promise.all(paths.map((file) => {
|
|
100
|
+
return (async () => {
|
|
101
|
+
let config;
|
|
102
|
+
switch (node_path.extname(file)) {
|
|
103
|
+
case ".js":
|
|
104
|
+
config = node_module.createRequire(typeof document === "undefined" ? require("url").pathToFileURL(__filename).href : _documentCurrentScript && _documentCurrentScript.src || new URL("index.js", document.baseURI).href)(file);
|
|
105
|
+
break;
|
|
106
|
+
case ".mjs":
|
|
107
|
+
config = (await import(file)).default;
|
|
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
|
+
}));
|
|
118
|
+
}
|
|
119
|
+
let serve = null;
|
|
120
|
+
if (mockAssetsDir) {
|
|
121
|
+
serve = sirv(toAbsolute(mockAssetsDir, cwd), sirvOptions(server.config.server.headers));
|
|
122
|
+
}
|
|
123
|
+
if (mockRoutes && mockRoutes.length > 0) {
|
|
124
|
+
return isAfter ? () => configureServer(server, mockRouterOptions, mockRoutes, serve, cwd) : configureServer(server, mockRouterOptions, mockRoutes, serve, cwd);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
module.exports = createPlugin;
|
package/dist/index.mjs
CHANGED
|
@@ -1,135 +1,129 @@
|
|
|
1
|
-
import { extname, isAbsolute, posix, parse } from
|
|
2
|
-
import { createRequire } from
|
|
3
|
-
import { readFileSync } from
|
|
4
|
-
import globby from
|
|
5
|
-
import getRouter from
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
|
|
1
|
+
import { extname, isAbsolute, posix, parse } from "node:path";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { readFileSync } from "node:fs";
|
|
4
|
+
import { globby } from "globby";
|
|
5
|
+
import getRouter from "find-my-way";
|
|
6
|
+
import sirv from "sirv";
|
|
7
|
+
import { send } from "vite";
|
|
9
8
|
function isObject(val) {
|
|
10
|
-
|
|
9
|
+
return val && typeof val === "object";
|
|
11
10
|
}
|
|
12
11
|
function toAbsolute(pth, cwd) {
|
|
13
|
-
|
|
14
|
-
? pth
|
|
15
|
-
: posix.join(cwd || process.cwd(), pth);
|
|
12
|
+
return isAbsolute(pth) ? pth : posix.join(cwd || process.cwd(), pth);
|
|
16
13
|
}
|
|
17
14
|
function sirvOptions(headers) {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
15
|
+
return {
|
|
16
|
+
dev: true,
|
|
17
|
+
etag: true,
|
|
18
|
+
extensions: [],
|
|
19
|
+
setHeaders(res, pathname) {
|
|
20
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
21
|
+
if (/\.[tj]sx?$/.test(pathname)) {
|
|
22
|
+
res.setHeader("Content-Type", "application/javascript");
|
|
23
|
+
}
|
|
24
|
+
if (headers) {
|
|
25
|
+
Object.entries(headers).forEach(([key, val]) => {
|
|
26
|
+
if (val) {
|
|
27
|
+
res.setHeader(key, val);
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
};
|
|
36
33
|
}
|
|
37
34
|
function configureServer(server, routerOpts, routes, serve, cwd) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
send(req, res, retType !== 'string' ? JSON.stringify(ret) : ret, isObject(ret) ? 'json' : 'html', {
|
|
66
|
-
headers: server.config.server.headers
|
|
67
|
-
});
|
|
68
|
-
};
|
|
69
|
-
}
|
|
70
|
-
else {
|
|
71
|
-
handler = routeConfig.handler;
|
|
72
|
-
}
|
|
73
|
-
if (handler) {
|
|
74
|
-
router.on(methods.split('/'), pathname, {}, handler, store);
|
|
75
|
-
}
|
|
35
|
+
const router = getRouter(routerOpts);
|
|
36
|
+
if (Array.isArray(routes)) {
|
|
37
|
+
routes.forEach((route) => {
|
|
38
|
+
Object.keys(route).forEach((xpath) => {
|
|
39
|
+
let [methods, pathname] = xpath.split(" ");
|
|
40
|
+
if (!pathname) {
|
|
41
|
+
pathname = methods;
|
|
42
|
+
methods = "GET";
|
|
43
|
+
}
|
|
44
|
+
let routeConfig = route[xpath];
|
|
45
|
+
if (!isObject(routeConfig)) {
|
|
46
|
+
routeConfig = { handler: routeConfig };
|
|
47
|
+
}
|
|
48
|
+
let handler;
|
|
49
|
+
if (typeof routeConfig.file === "string") {
|
|
50
|
+
handler = (req, res) => {
|
|
51
|
+
const parsedPath = parse(toAbsolute(routeConfig.file, cwd));
|
|
52
|
+
const serve2 = sirv(parsedPath.dir, sirvOptions(server.config.server.headers));
|
|
53
|
+
req.url = `/${parsedPath.base}`;
|
|
54
|
+
serve2(req, res);
|
|
55
|
+
};
|
|
56
|
+
} else if (typeof routeConfig.handler !== "function") {
|
|
57
|
+
const ret = routeConfig.handler;
|
|
58
|
+
const retType = typeof ret;
|
|
59
|
+
handler = (req, res) => {
|
|
60
|
+
send(req, res, retType !== "string" ? JSON.stringify(ret) : ret, isObject(ret) ? "json" : "html", {
|
|
61
|
+
headers: server.config.server.headers
|
|
76
62
|
});
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
63
|
+
};
|
|
64
|
+
} else {
|
|
65
|
+
handler = routeConfig.handler;
|
|
66
|
+
}
|
|
67
|
+
if (handler) {
|
|
68
|
+
router.on(methods.split("/"), pathname, {}, handler, routeConfig.store);
|
|
69
|
+
}
|
|
70
|
+
});
|
|
85
71
|
});
|
|
72
|
+
}
|
|
73
|
+
if (serve) {
|
|
74
|
+
server.middlewares.use(serve);
|
|
75
|
+
}
|
|
76
|
+
server.middlewares.use((req, res, next) => {
|
|
77
|
+
router.defaultRoute = () => next();
|
|
78
|
+
router.lookup(req, res);
|
|
79
|
+
});
|
|
86
80
|
}
|
|
87
81
|
function createPlugin(opts) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
break;
|
|
117
|
-
}
|
|
118
|
-
if (config) {
|
|
119
|
-
mockRoutes.push(config);
|
|
120
|
-
}
|
|
121
|
-
})();
|
|
122
|
-
}));
|
|
82
|
+
const { isAfter, mockRouterOptions, mockAssetsDir } = opts;
|
|
83
|
+
let { cwd, mockRoutesDir } = opts;
|
|
84
|
+
let mockRoutes = opts.mockRoutes || [];
|
|
85
|
+
if (!cwd) {
|
|
86
|
+
cwd = process.cwd();
|
|
87
|
+
}
|
|
88
|
+
if (isObject(mockRoutes) && !Array.isArray(mockRoutes)) {
|
|
89
|
+
mockRoutes = [mockRoutes];
|
|
90
|
+
}
|
|
91
|
+
return {
|
|
92
|
+
name: "vite:mock-data",
|
|
93
|
+
async configureServer(server) {
|
|
94
|
+
if (mockRoutesDir) {
|
|
95
|
+
mockRoutesDir = toAbsolute(mockRoutesDir, cwd);
|
|
96
|
+
const paths = await globby(`${mockRoutesDir}/**/*.{js,mjs,json}`);
|
|
97
|
+
await Promise.all(paths.map((file) => {
|
|
98
|
+
return (async () => {
|
|
99
|
+
let config;
|
|
100
|
+
switch (extname(file)) {
|
|
101
|
+
case ".js":
|
|
102
|
+
config = createRequire(import.meta.url)(file);
|
|
103
|
+
break;
|
|
104
|
+
case ".mjs":
|
|
105
|
+
config = (await import(file)).default;
|
|
106
|
+
break;
|
|
107
|
+
case ".json":
|
|
108
|
+
config = JSON.parse(readFileSync(file, "utf-8"));
|
|
109
|
+
break;
|
|
123
110
|
}
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
serve = sirv(toAbsolute(mockAssetsDir, cwd), sirvOptions(server.config.server.headers));
|
|
111
|
+
if (config) {
|
|
112
|
+
mockRoutes.push(config);
|
|
127
113
|
}
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
114
|
+
})();
|
|
115
|
+
}));
|
|
116
|
+
}
|
|
117
|
+
let serve = null;
|
|
118
|
+
if (mockAssetsDir) {
|
|
119
|
+
serve = sirv(toAbsolute(mockAssetsDir, cwd), sirvOptions(server.config.server.headers));
|
|
120
|
+
}
|
|
121
|
+
if (mockRoutes && mockRoutes.length > 0) {
|
|
122
|
+
return isAfter ? () => configureServer(server, mockRouterOptions, mockRoutes, serve, cwd) : configureServer(server, mockRouterOptions, mockRoutes, serve, cwd);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
};
|
|
133
126
|
}
|
|
134
|
-
|
|
135
|
-
|
|
127
|
+
export {
|
|
128
|
+
createPlugin as default
|
|
129
|
+
};
|
package/package.json
CHANGED
|
@@ -1,19 +1,21 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "vite-plugin-mock-data",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.2",
|
|
4
4
|
"description": "Provides a simple way to mock data.",
|
|
5
|
-
"main": "./dist/index.
|
|
6
|
-
"
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
7
|
"types": "./dist/index.d.ts",
|
|
8
8
|
"exports": {
|
|
9
9
|
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
10
11
|
"import": "./dist/index.mjs",
|
|
11
|
-
"require": "./dist/index.
|
|
12
|
+
"require": "./dist/index.js"
|
|
12
13
|
}
|
|
13
14
|
},
|
|
14
15
|
"scripts": {
|
|
15
16
|
"release": "npm publish",
|
|
16
|
-
"build": "
|
|
17
|
+
"build": "vite build",
|
|
18
|
+
"watch": "vite build --watch"
|
|
17
19
|
},
|
|
18
20
|
"repository": {
|
|
19
21
|
"type": "git",
|
|
@@ -25,25 +27,16 @@
|
|
|
25
27
|
"vite-plugin-mock-data"
|
|
26
28
|
],
|
|
27
29
|
"author": "Jesse Feng <fxm0016@126.com>",
|
|
28
|
-
"license": "MIT",
|
|
29
30
|
"bugs": {
|
|
30
31
|
"url": "https://github.com/fengxinming/vite-plugins/issues"
|
|
31
32
|
},
|
|
32
33
|
"homepage": "https://github.com/fengxinming/vite-plugins#readme",
|
|
33
34
|
"dependencies": {
|
|
34
35
|
"find-my-way": "^7.6.2",
|
|
35
|
-
"globby": "^
|
|
36
|
+
"globby": "^13.2.2",
|
|
36
37
|
"sirv": "^2.0.2"
|
|
37
38
|
},
|
|
38
|
-
"devDependencies": {
|
|
39
|
-
"@rollup/plugin-typescript": "^11.1.3",
|
|
40
|
-
"@types/node": "^20.5.9",
|
|
41
|
-
"rollup": "^3.28.1",
|
|
42
|
-
"rollup-plugin-empty": "^1.0.0",
|
|
43
|
-
"rollup-plugin-filesize": "^10.0.0",
|
|
44
|
-
"vite": "^4.4.9"
|
|
45
|
-
},
|
|
46
39
|
"files": [
|
|
47
40
|
"dist"
|
|
48
41
|
]
|
|
49
|
-
}
|
|
42
|
+
}
|
package/dist/index.cjs
DELETED
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var node_path = require('node:path');
|
|
4
|
-
var node_module = require('node:module');
|
|
5
|
-
var node_fs = require('node:fs');
|
|
6
|
-
var globby = require('globby');
|
|
7
|
-
var getRouter = require('find-my-way');
|
|
8
|
-
var vite = require('vite');
|
|
9
|
-
var sirv = require('sirv');
|
|
10
|
-
|
|
11
|
-
function isObject(val) {
|
|
12
|
-
return val && typeof val === 'object';
|
|
13
|
-
}
|
|
14
|
-
function toAbsolute(pth, cwd) {
|
|
15
|
-
return node_path.isAbsolute(pth)
|
|
16
|
-
? pth
|
|
17
|
-
: node_path.posix.join(cwd || process.cwd(), pth);
|
|
18
|
-
}
|
|
19
|
-
function sirvOptions(headers) {
|
|
20
|
-
return {
|
|
21
|
-
dev: true,
|
|
22
|
-
etag: true,
|
|
23
|
-
extensions: [],
|
|
24
|
-
setHeaders(res, pathname) {
|
|
25
|
-
res.setHeader('Access-Control-Allow-Origin', '*');
|
|
26
|
-
if (/\.[tj]sx?$/.test(pathname)) {
|
|
27
|
-
res.setHeader('Content-Type', 'application/javascript');
|
|
28
|
-
}
|
|
29
|
-
if (headers) {
|
|
30
|
-
Object.entries(headers).forEach(([key, val]) => {
|
|
31
|
-
if (val) {
|
|
32
|
-
res.setHeader(key, val);
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
function configureServer(server, routerOpts, routes, serve, cwd) {
|
|
40
|
-
const router = getRouter(routerOpts);
|
|
41
|
-
if (Array.isArray(routes)) {
|
|
42
|
-
routes.forEach((route) => {
|
|
43
|
-
Object.keys(route).forEach((xpath) => {
|
|
44
|
-
let [methods, pathname] = xpath.split(' ');
|
|
45
|
-
if (!pathname) {
|
|
46
|
-
pathname = methods;
|
|
47
|
-
methods = 'GET';
|
|
48
|
-
}
|
|
49
|
-
let routeConfig = route[xpath];
|
|
50
|
-
if (!isObject(routeConfig)) {
|
|
51
|
-
routeConfig = { handler: routeConfig };
|
|
52
|
-
}
|
|
53
|
-
let handler;
|
|
54
|
-
let store;
|
|
55
|
-
if (typeof routeConfig.file === 'string') {
|
|
56
|
-
handler = (req, res) => {
|
|
57
|
-
const parsedPath = node_path.parse(toAbsolute(routeConfig.file, cwd));
|
|
58
|
-
const serve = sirv(parsedPath.dir, sirvOptions(server.config.server.headers));
|
|
59
|
-
req.url = `/${parsedPath.base}`;
|
|
60
|
-
serve(req, res);
|
|
61
|
-
};
|
|
62
|
-
}
|
|
63
|
-
else if (typeof routeConfig.handler !== 'function') {
|
|
64
|
-
const ret = routeConfig.handler;
|
|
65
|
-
const retType = typeof ret;
|
|
66
|
-
handler = (req, res) => {
|
|
67
|
-
vite.send(req, res, retType !== 'string' ? JSON.stringify(ret) : ret, isObject(ret) ? 'json' : 'html', {
|
|
68
|
-
headers: server.config.server.headers
|
|
69
|
-
});
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
handler = routeConfig.handler;
|
|
74
|
-
}
|
|
75
|
-
if (handler) {
|
|
76
|
-
router.on(methods.split('/'), pathname, {}, handler, store);
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
if (serve) {
|
|
82
|
-
server.middlewares.use(serve);
|
|
83
|
-
}
|
|
84
|
-
server.middlewares.use((req, res, next) => {
|
|
85
|
-
router.defaultRoute = () => next();
|
|
86
|
-
router.lookup(req, res);
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
function createPlugin(opts) {
|
|
90
|
-
const { isAfter, mockRouterOptions, mockAssetsDir } = opts;
|
|
91
|
-
let { cwd, mockRoutesDir } = opts;
|
|
92
|
-
let mockRoutes = (opts.mockRoutes || []);
|
|
93
|
-
if (!cwd) {
|
|
94
|
-
cwd = process.cwd();
|
|
95
|
-
}
|
|
96
|
-
if (isObject(mockRoutes) && !Array.isArray(mockRoutes)) {
|
|
97
|
-
mockRoutes = [mockRoutes];
|
|
98
|
-
}
|
|
99
|
-
return {
|
|
100
|
-
name: 'vite-plugin-mock-data',
|
|
101
|
-
async configureServer(server) {
|
|
102
|
-
if (mockRoutesDir) {
|
|
103
|
-
mockRoutesDir = toAbsolute(mockRoutesDir, cwd);
|
|
104
|
-
const paths = await globby(`${mockRoutesDir}/**/*.{js,mjs,json}`);
|
|
105
|
-
console.log(paths);
|
|
106
|
-
await Promise.all(paths.map((file) => {
|
|
107
|
-
return (async () => {
|
|
108
|
-
let config;
|
|
109
|
-
switch (node_path.extname(file)) {
|
|
110
|
-
case '.js':
|
|
111
|
-
config = node_module.createRequire((typeof document === 'undefined' ? require('u' + 'rl').pathToFileURL(__filename).href : (document.currentScript && document.currentScript.src || new URL('index.cjs', document.baseURI).href)))(file);
|
|
112
|
-
break;
|
|
113
|
-
case '.mjs':
|
|
114
|
-
config = (await import(file)).default;
|
|
115
|
-
break;
|
|
116
|
-
case '.json':
|
|
117
|
-
config = JSON.parse(node_fs.readFileSync(file, 'utf-8'));
|
|
118
|
-
break;
|
|
119
|
-
}
|
|
120
|
-
if (config) {
|
|
121
|
-
mockRoutes.push(config);
|
|
122
|
-
}
|
|
123
|
-
})();
|
|
124
|
-
}));
|
|
125
|
-
}
|
|
126
|
-
let serve = null;
|
|
127
|
-
if (mockAssetsDir) {
|
|
128
|
-
serve = sirv(toAbsolute(mockAssetsDir, cwd), sirvOptions(server.config.server.headers));
|
|
129
|
-
}
|
|
130
|
-
return isAfter
|
|
131
|
-
? () => configureServer(server, mockRouterOptions, mockRoutes, serve, cwd)
|
|
132
|
-
: configureServer(server, mockRouterOptions, mockRoutes, serve, cwd);
|
|
133
|
-
}
|
|
134
|
-
};
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
module.exports = createPlugin;
|