vue-pure-admin2/vite.config.ts

132 lines
4.1 KiB
TypeScript
Raw Normal View History

2021-08-31 11:50:39 +08:00
import { resolve } from "path";
import { UserConfigExport, ConfigEnv, loadEnv } from "vite";
2021-08-31 11:50:39 +08:00
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import { warpperEnv } from "./build/utils";
2021-08-31 11:50:39 +08:00
import { createProxy } from "./build/proxy";
import { viteMockServe } from "vite-plugin-mock";
import svgLoader from "vite-svg-loader";
import styleImport from "vite-plugin-style-import";
2021-09-29 09:55:50 +08:00
import ElementPlus from "unplugin-element-plus/vite";
2021-10-27 09:03:12 +08:00
import themePreprocessorPlugin from "@zougt/vite-plugin-theme-preprocessor";
2021-03-01 15:26:05 +08:00
2021-10-19 18:58:47 +08:00
const pathResolve = (dir: string): string => {
2021-08-31 11:50:39 +08:00
return resolve(__dirname, ".", dir);
};
2021-03-01 15:26:05 +08:00
const alias: Record<string, string> = {
2021-04-21 23:20:14 +08:00
"/@": pathResolve("src"),
"@build": pathResolve("build"),
2021-04-29 03:12:44 +08:00
//解决开发环境下的警告 You are running the esm-bundler build of vue-i18n. It is recommended to configure your bundler to explicitly replace feature flag globals with boolean literals to get proper tree-shaking in the final bundle.
2021-07-07 13:33:47 +08:00
"vue-i18n": "vue-i18n/dist/vue-i18n.cjs.js"
2021-08-31 11:50:39 +08:00
};
2021-03-01 15:26:05 +08:00
2021-08-31 11:50:39 +08:00
const root: string = process.cwd();
2021-03-01 15:26:05 +08:00
export default ({ command, mode }: ConfigEnv): UserConfigExport => {
const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY } = warpperEnv(
loadEnv(mode, root)
);
2021-08-31 11:50:39 +08:00
const prodMock = true;
return {
2021-03-01 15:26:05 +08:00
/**
*
2021-04-21 23:20:14 +08:00
* /manages/ /manages/
* @default '/'
2021-03-01 15:26:05 +08:00
*/
2021-04-21 23:20:14 +08:00
base:
process.env.NODE_ENV === "production" ? "/manages/" : VITE_PUBLIC_PATH,
root,
resolve: {
2021-07-07 13:33:47 +08:00
alias
},
// 服务端渲染
server: {
// 是否开启 https
https: false,
/**
*
* @default 3000
*/
port: VITE_PORT,
2021-07-22 11:33:45 +08:00
host: "0.0.0.0",
// 本地跨域代理
2021-07-07 13:33:47 +08:00
proxy: createProxy(VITE_PROXY)
},
plugins: [
vue(),
vueJsx(),
2021-10-27 09:03:12 +08:00
themePreprocessorPlugin({
scss: {
multipleScopeVars: [
{
scopeName: "layout-default",
path: pathResolve("src/layout/theme/default-vars.scss")
},
{
scopeName: "layout-light",
path: pathResolve("src/layout/theme/light-vars.scss")
}
],
// 默认取 multipleScopeVars[0].scopeName
defaultScopeName: "",
// 在生产模式是否抽取独立的主题css文件extract为true以下属性有效
extract: true,
// 独立主题css文件的输出路径默认取 viteConfig.build.assetsDir 相对于 (viteConfig.build.outDir)
outputDir: "",
// 会选取defaultScopeName对应的主题css文件在html添加link
themeLinkTagId: "body",
// "head"||"head-prepend" || "body" ||"body-prepend"
themeLinkTagInjectTo: "body",
// 是否对抽取的css文件内对应scopeName的权重类名移除
removeCssScopeName: false,
// 可以自定义css文件名称的函数
customThemeCssFileName: scopeName => scopeName
}
}),
svgLoader(),
2021-04-21 23:20:14 +08:00
styleImport({
libs: [
// 按需加载vxe-table
{
libraryName: "vxe-table",
esModule: true,
ensureStyleFile: true,
2021-07-07 13:33:47 +08:00
resolveComponent: name => `vxe-table/es/${name}`,
resolveStyle: name => `vxe-table/es/${name}/style.css`
}
]
2021-04-21 23:20:14 +08:00
}),
2021-09-16 14:31:02 +08:00
ElementPlus({}),
viteMockServe({
2021-04-21 23:20:14 +08:00
mockPath: "mock",
localEnabled: command === "serve",
prodEnabled: command !== "serve" && prodMock,
injectCode: `
import { setupProdMockServer } from './mockProdServer';
setupProdMockServer();
`,
2021-07-07 13:33:47 +08:00
logger: true
})
],
2021-04-21 23:20:14 +08:00
optimizeDeps: {
include: [
"element-plus/lib/locale/lang/zh-cn",
"element-plus/lib/locale/lang/en",
2021-05-09 22:37:17 +08:00
"vxe-table/lib/locale/lang/zh-CN",
2021-07-07 13:33:47 +08:00
"vxe-table/lib/locale/lang/en-US"
]
2021-04-21 23:20:14 +08:00
},
build: {
2021-08-29 10:20:59 +08:00
// @ts-ignore
2021-08-27 01:33:57 +08:00
sourcemap: false,
brotliSize: false,
// 消除打包大小超过500kb警告
2021-07-07 13:33:47 +08:00
chunkSizeWarningLimit: 2000
2021-04-09 12:54:03 +08:00
},
define: {
2021-07-07 13:33:47 +08:00
__INTLIFY_PROD_DEVTOOLS__: false
}
2021-08-31 11:50:39 +08:00
};
};