vite-plugin-es6-mock
Support vite mock data, use es6-mock to mock data.
Install
Install with npm:
$ npm install --save-dev vite-plugin-es6-mock
Config vite.config.ts
import vitePluginMock from 'vite-plugin-es6-mock';
export default defineConfig({
server: {
port: 8088,
},
plugins: [vitePluginMock()]
});
import vitePluginMock from 'vite-plugin-es6-mock';
export default defineConfig({
plugins: [vitePluginMock({ path: '/mockjs', dir: './mockjs'})]
});
Mock data
Step one
Make a new file test.js in the config dir.
import path from 'path';
import fs from 'fs';
import { delay, validate, request } from 'es6-mock';
import test1 from './test1';
import test2 from './test2';
delay(500);
validate({
header: {
'Cache-Control': 'required|equals:no-cache',
cookie: {
_gid: 'required'
},
},
param: {
name: 'required|string',
id: 'required|integer'
},
method: 'get|post'
});
export default {
'code|1-10': '0',
data: {
"switch|1-2": true,
name: 'test03.js',
test1,
test2,
param: request.query,
param2: request.body,
existTest1: fs.existsSync(path.join(__dirname, 'test1.js')),
existTest0: fs.existsSync(path.join(__dirname, 'no-exist.js'))
}
};
Step two
Start your vite, then you can get the mock content through the URL: http://localhost:8088/test