You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

75 lines
2.1 KiB

  1. import dayjs from "dayjs";
  2. import { resolve } from "path";
  3. import pkg from "./package.json";
  4. import { warpperEnv } from "./build";
  5. import { getPluginsList } from "./build/plugins";
  6. import { include, exclude } from "./build/optimize";
  7. import { UserConfigExport, ConfigEnv, loadEnv } from "vite";
  8. /** 当前执行node命令时文件夹的地址(工作目录) */
  9. const root: string = process.cwd();
  10. /** 路径查找 */
  11. const pathResolve = (dir: string): string => {
  12. return resolve(__dirname, ".", dir);
  13. };
  14. /** 设置别名 */
  15. const alias: Record<string, string> = {
  16. "@": pathResolve("src"),
  17. "@build": pathResolve("build")
  18. };
  19. const { dependencies, devDependencies, name, version } = pkg;
  20. const __APP_INFO__ = {
  21. pkg: { dependencies, devDependencies, name, version },
  22. lastBuildTime: dayjs(new Date()).format("YYYY-MM-DD HH:mm:ss")
  23. };
  24. export default ({ command, mode }: ConfigEnv): UserConfigExport => {
  25. const { VITE_CDN, VITE_PORT, VITE_COMPRESSION, VITE_PUBLIC_PATH } =
  26. warpperEnv(loadEnv(mode, root));
  27. return {
  28. base: VITE_PUBLIC_PATH,
  29. root,
  30. resolve: {
  31. alias
  32. },
  33. // 服务端渲染
  34. server: {
  35. // 是否开启 https
  36. https: false,
  37. // 端口号
  38. port: VITE_PORT,
  39. host: "0.0.0.0",
  40. // 本地跨域代理 https://cn.vitejs.dev/config/server-options.html#server-proxy
  41. proxy: {}
  42. },
  43. plugins: getPluginsList(command, VITE_CDN, VITE_COMPRESSION),
  44. // https://cn.vitejs.dev/config/dep-optimization-options.html#dep-optimization-options
  45. optimizeDeps: {
  46. include,
  47. exclude
  48. },
  49. build: {
  50. sourcemap: false,
  51. // 消除打包大小超过500kb警告
  52. chunkSizeWarningLimit: 4000,
  53. rollupOptions: {
  54. input: {
  55. index: pathResolve("index.html")
  56. },
  57. // 静态资源分类打包
  58. output: {
  59. chunkFileNames: "static/js/[name]-[hash].js",
  60. entryFileNames: "static/js/[name]-[hash].js",
  61. assetFileNames: "static/[ext]/[name]-[hash].[ext]"
  62. }
  63. }
  64. },
  65. define: {
  66. __INTLIFY_PROD_DEVTOOLS__: false,
  67. __APP_INFO__: JSON.stringify(__APP_INFO__)
  68. }
  69. };
  70. };