加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
vite.config.ts 2.79 KB
一键复制 编辑 原始数据 按行查看 历史
tangbo 提交于 2021-12-24 15:42 . 更新 vite-plugin-style-import
import vue from '@vitejs/plugin-vue'
import path from 'path'
import { ConfigEnv, defineConfig, loadEnv, UserConfigExport } from 'vite'
import { configCompressPlugin } from './src/plugins/configCompressPlugin'
import { configHtmlPlugin } from './src/plugins/configHtmlPlugin'
import { configMockPlugin } from './src/plugins/configMockPlugin'
import { configStyleImportPlugin } from './src/plugins/configStyleImportPlugin'
import { configSvgIconsPlugin } from './src/plugins/configSvgIconsPlugin'
import { wrapperEnv } from './src/utils/env'
const resolve = (dir: string) => path.join(__dirname, dir)
// 根据环境变量配置代理 https://blog.csdn.net/chendf__/article/details/115676683
// https://vitejs.dev/config/
export default ({ command, mode }: ConfigEnv): UserConfigExport => {
const root = process.cwd()
const env = loadEnv(mode, root)
const isBuild = command === 'build'
// The boolean type read by loadEnv is a string. This function can be converted to boolean type
const viteEnv = wrapperEnv(env)
const {
VITE_PORT,
VITE_USE_MOCK,
VITE_BUILD_COMPRESS,
VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE
} = viteEnv
return defineConfig({
base: './',
plugins: [
vue(),
configSvgIconsPlugin(isBuild, 'src/assets/icons'), // svg 处理
configStyleImportPlugin(isBuild), // element-plus 按需引入
configHtmlPlugin(viteEnv, isBuild), // EJS 标签处理
configMockPlugin(VITE_USE_MOCK, isBuild), // mock 模拟请求
configCompressPlugin(
VITE_BUILD_COMPRESS,
VITE_BUILD_COMPRESS_DELETE_ORIGIN_FILE
) // gzip 或者 brotli 来压缩资源
],
resolve: {
alias: {
'@': resolve('src'),
comps: resolve('src/components'),
apis: resolve('src/apis'),
views: resolve('src/views'),
store: resolve('src/store'),
routes: resolve('src/routes'),
styles: resolve('src/styles')
}
},
server: {
//服务器主机名
host: '',
//端口号
port: VITE_PORT,
//设为 true 时若端口已被占用则会直接退出,而不是尝试下一个可用端口
strictPort: false,
//服务器启动时自动在浏览器中打开应用程序,当此值为字符串时,会被用作 URL 的路径名
open: false,
//自定义代理规则
proxy: {
// 选项写法
'/api': {
target: 'http://localhost:' + VITE_PORT,
changeOrigin: true,
rewrite: (path) => path.replace(/^\/api/, '')
}
}
},
build: {
rollupOptions: {
// input: 'app.js',
// external: ['elementPlus'],
// output: {
// globals: {
// 'elementPlus': 'https://unpkg.com/element-plus@1.0.2-beta.44/lib/index.js'
// }
// }
}
}
})
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化