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