Skip to content

Commit

Permalink
fix: 修复开关生产环境 mock
Browse files Browse the repository at this point in the history
  • Loading branch information
markthree committed Mar 6, 2022
1 parent 75158c9 commit f4c3dff
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 14 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@vitejs/plugin-vue-jsx": "^1.3.8",
"@vueuse/components": "^7.7.0",
"@vueuse/core": "^7.7.0",
"axios": "^0.26.0",
"c8": "^7.11.0",
"esno": "^0.14.1",
"ityped": "^1.0.3",
Expand Down Expand Up @@ -82,6 +83,7 @@
"optimizeDeps": {
"include": [
"@vueuse/core",
"axios",
"ityped",
"mockjs",
"nprogress",
Expand Down
36 changes: 28 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions presets/shared/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@
import Mock from 'mockjs'

export function createFetchSever(mockList: any[]) {
if (
!window['originFetch'] &&
import.meta.env.VITE_APP_MOCK_IN_PRODUCTION
) {
if (!window['originFetch']) {
window['originFetch'] = window.fetch
window.fetch = function (
fetchUrl: string,
Expand Down
1 change: 1 addition & 0 deletions presets/types/auto-imports.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ declare global {
const useCssVars: typeof import('vue')['useCssVars']
const useCycleList: typeof import('@vueuse/core')['useCycleList']
const useDark: typeof import('@vueuse/core')['useDark']
const useDarks: typeof import('/src/composables/useDarks')['default']
const useDebounce: typeof import('@vueuse/core')['useDebounce']
const useDebouncedRefHistory: typeof import('@vueuse/core')['useDebouncedRefHistory']
const useDebounceFn: typeof import('@vueuse/core')['useDebounceFn']
Expand Down
1 change: 0 additions & 1 deletion src/mock.ts

This file was deleted.

6 changes: 5 additions & 1 deletion src/modules/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
import { createProdMockServer } from 'vite-plugin-mock/es/createProdMockServer'
import { createFetchSever } from '../../presets/shared/mock'

const shouldCreateServer =
!import.meta.env.DEV &&
import.meta.env.VITE_APP_MOCK_IN_PRODUCTION

// 生产环境时才创建服务
if (!import.meta.env.DEV) {
if (shouldCreateServer) {
const mockModules: any[] = []
const modules = import.meta.globEager('../../mock/*.ts')
Object.values(modules).forEach(v => {
Expand Down

0 comments on commit f4c3dff

Please sign in to comment.