环境变量里增加 mock 开关,解决生产环境上传组件无法上传的问题

This commit is contained in:
Hooray 2022-05-12 11:25:27 +08:00
parent 255534d23d
commit 64884b2c35
4 changed files with 9 additions and 4 deletions

View File

@ -7,6 +7,8 @@ VITE_APP_API_BASEURL = /
# 调试工具,可设置 eruda 或 vconsole如果不需要开启则留空
VITE_APP_DEBUG_TOOL =
# 是否在打包时启用 Mock
VITE_BUILD_MOCK = false
# 是否在打包时生成 sourcemap
VITE_BUILD_SOURCEMAP = false
# 是否在打包时删除 console 代码

View File

@ -7,11 +7,11 @@ VITE_APP_API_BASEURL = /
# 调试工具,可设置 eruda 或 vconsole如果不需要开启则留空
VITE_APP_DEBUG_TOOL =
# 是否在打包时启用 Mock
VITE_BUILD_MOCK = true
# 是否在打包时生成 sourcemap
VITE_BUILD_SOURCEMAP = true
# 是否在打包时删除 console 代码
VITE_BUILD_DROP_CONSOLE = true
# 是否在打包时开启压缩,支持 gzip 和 brotli
VITE_BUILD_COMPRESS = gzip,brotli
# 是否在打包时候生成PWA
VITE_BUILD_PWA = false

View File

@ -23,7 +23,7 @@ export default function createVitePlugins(viteEnv, isBuild = false) {
vitePlugins.push(createComponents())
vitePlugins.push(createSetupExtend())
vitePlugins.push(createSvgIcon(isBuild))
vitePlugins.push(createMock())
vitePlugins.push(createMock(viteEnv, isBuild))
vitePlugins.push(createLayouts())
vitePlugins.push(createPages())
isBuild && vitePlugins.push(...createCompression(viteEnv))

View File

@ -1,8 +1,11 @@
import { viteMockServe } from 'vite-plugin-mock'
export default function createMock() {
export default function createMock(env, isBuild) {
const { VITE_BUILD_MOCK } = env
return viteMockServe({
mockPath: 'src/mock',
localEnabled: !isBuild,
prodEnabled: isBuild && VITE_BUILD_MOCK === 'true',
injectCode: `
import { setupProdMockServer } from './mockProdServer';
setupProdMockServer();