Skip to content

Commit

Permalink
feature: 添加mock
Browse files Browse the repository at this point in the history
  • Loading branch information
jsxiaosi committed Nov 15, 2021
1 parent 835c7ec commit 728769a
Show file tree
Hide file tree
Showing 9 changed files with 787 additions and 49 deletions.
16 changes: 15 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ module.exports = {
node: true,
},
extends: [
'standard',
'plugin:vue/vue3-recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'plugin:prettier/recommended',
],
parser: 'vue-eslint-parser',
Expand Down Expand Up @@ -46,5 +46,19 @@ module.exports = {
order: ['template', 'script', 'style'],
},
],
'@typescript-eslint/no-unused-vars': [
'error',
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
},
],
'no-unused-vars': [
'error',
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
},
],
},
}
6 changes: 5 additions & 1 deletion build/vite/plugin/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ import { configThemePlugin } from './theme'
import { configSvgPlugin } from './svg'
// 压缩
import { configCompressPlugin } from './compress'
// mock
import { configMockPlugin } from './mock'

// import viteESLint from '@ehutch79/vite-eslint'

export function createVitePlugins() {
export function createVitePlugins(isBuild = false) {
const vitePlugins: (Plugin | Plugin[])[] = [vue()]
vitePlugins.push(configStylePlugin())

Expand All @@ -23,6 +25,8 @@ export function createVitePlugins() {

vitePlugins.push(configCompressPlugin('gzip', true))

vitePlugins.push(configMockPlugin(isBuild))

// vitePlugins.push(viteESLint())
return vitePlugins
}
19 changes: 19 additions & 0 deletions build/vite/plugin/mock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Mock plugin for development and production.
* https://github.com/anncwb/vite-plugin-mock
*/
import { viteMockServe } from 'vite-plugin-mock'

export function configMockPlugin(isBuild: boolean) {
return viteMockServe({
ignore: /^\_/,
mockPath: 'mock',
localEnabled: !isBuild,
prodEnabled: isBuild,
injectCode: `
import { setupProdMockServer } from '../mock/_createProductionServer';
setupProdMockServer();
`,
})
}
15 changes: 15 additions & 0 deletions mock/_createProductionServer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { createProdMockServer } from 'vite-plugin-mock/es/createProdMockServer'

const modules = import.meta.globEager('./**/*.ts')

const mockModules: any[] = []
Object.keys(modules).forEach((key) => {
if (key.includes('/_')) {
return
}
mockModules.push(...modules[key].default)
})

export function setupProdMockServer() {
createProdMockServer(mockModules)
}
53 changes: 53 additions & 0 deletions mock/demo/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { MockMethod } from 'vite-plugin-mock'

const userInfo = {
name: 'Vben',
userid: '00000001',
email: 'test@gmail.com',
signature: '海纳百川,有容乃大',
introduction: '微笑着,努力着,欣赏着',
title: '交互专家',
group: '某某某事业群-某某平台部-某某技术部-UED',
tags: [
{
key: '0',
label: '很有想法的',
},
{
key: '1',
label: '专注设计',
},
{
key: '2',
label: '辣~',
},
{
key: '3',
label: '大长腿',
},
{
key: '4',
label: '川妹子',
},
{
key: '5',
label: '海纳百川',
},
],
notifyCount: 12,
unreadCount: 11,
country: 'China',
address: 'Xiamen City 77',
phone: '0592-268888888',
}

export default [
{
url: '/mock_api/getUserInfo',
timeout: 1000,
method: 'get',
response: () => {
return userInfo
},
},
] as MockMethod[]
Loading

0 comments on commit 728769a

Please sign in to comment.