From b77b7c9d6296ab92cf74ce678a49b7092f7c36b2 Mon Sep 17 00:00:00 2001 From: Carfield <mikasaleslie@163.com> Date: 星期四, 28 一月 2021 21:17:30 +0800 Subject: [PATCH] Update CHANGELOG.zh_CN.md (#225) --- vite.config.ts | 260 ++++++++++++++------------------------------------- 1 files changed, 74 insertions(+), 186 deletions(-) diff --git a/vite.config.ts b/vite.config.ts index 9579cd0..c57da14 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -1,205 +1,93 @@ +import type { UserConfig, ConfigEnv } from 'vite'; import { resolve } from 'path'; +import vue from '@vitejs/plugin-vue'; +import vueJsx from '@vitejs/plugin-vue-jsx'; +import legacy from '@vitejs/plugin-legacy'; -import type { UserConfig, Plugin as VitePlugin } from 'vite'; +import { loadEnv } from 'vite'; -import visualizer from 'rollup-plugin-visualizer'; -import { modifyVars } from './build/config/glob/lessModifyVars'; -import { - // externals, - cdnConf, -} from './build/config/vite/cdn'; -import { createProxy } from './build/config/vite/proxy'; -import { createMockServer } from 'vite-plugin-mock'; -import PurgeIcons from 'vite-plugin-purge-icons'; -import gzipPlugin from './build/plugin/gzip/index'; -import globbyTransform from './build/plugin/vite-plugin-context/transform'; +import { modifyVars } from './build/config/lessModifyVars'; +import { createProxy } from './build/vite/proxy'; -import { isDevFn, isReportMode, isProdFn, loadEnv, isBuildGzip, isSiteMode } from './build/utils'; +import { wrapperEnv } from './build/utils'; + +import { createVitePlugins } from './build/vite/plugin'; + const pkg = require('./package.json'); - -const { - VITE_USE_MOCK, - VITE_PORT, - VITE_PUBLIC_PATH, - VITE_PROXY, - VITE_GLOB_APP_TITLE, - VITE_DROP_CONSOLE, - // VITE_USE_CDN, -} = loadEnv(); function pathResolve(dir: string) { return resolve(__dirname, '.', dir); } -const rollupPlugins: any[] = []; -const vitePlugins: VitePlugin[] = []; +const root: string = process.cwd(); -(() => { - if (isProdFn()) { - if (isReportMode()) { - // report - rollupPlugins.push( - visualizer({ filename: './build/.cache/stats.html', open: true }) as Plugin - ); - } - if (isBuildGzip() || isSiteMode()) { - rollupPlugins.push(gzipPlugin()); - } - } +export default ({ command, mode }: ConfigEnv): UserConfig => { + const env = loadEnv(mode, root); + const viteEnv = wrapperEnv(env); + const { VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY, VITE_DROP_CONSOLE, VITE_LEGACY } = viteEnv; - if (isDevFn() && VITE_USE_MOCK) { - // open mock - vitePlugins.push( - createMockServer({ - ignore: /^\_/, - mockPath: 'mock', - }) - ); - } -})(); + const isBuild = command === 'build'; -const viteConfig: UserConfig = { - /** - * Entry. Use this to specify a js entry file in use cases where an - * `index.html` does not exist (e.g. serving vite assets from a different host) - * @default 'index.html' - */ - entry: 'public/index.html', - /** - * 绔彛鍙� - * @default '3000' - */ - port: VITE_PORT, - /** - * 鏈嶅姟鍦板潃 - * @default 'localhost' - */ - hostname: 'localhost', - /** - * 杩愯鑷姩鎵撳紑娴忚鍣� - * @default 'false' - */ - open: false, - /** - * 鍘嬬缉浠g爜 - * boolean | 'terser' | 'esbuild' - * @default 'terser' - */ - minify: isDevFn() ? 'esbuild' : 'terser', - /** - * 鍩烘湰鍏叡璺緞 - * @default '/' - */ - base: VITE_PUBLIC_PATH, - - /** - * 鎵撳寘杈撳叆璺緞 - * @default 'dist' - */ - outDir: 'dist', - /** - * @default 'false' - */ - sourcemap: false, - /** - * 璧勬簮杈撳嚭璺緞 - * @default '_assets' - */ - assetsDir: '_assets', - /** - * 闈欐�佽祫婧愬皬浜庤澶у皬灏嗕細鍐呰仈锛岄粯璁�4096kb - * @default '4096' - */ - assetsInlineLimit: 4096, - /** - * esbuild杞崲鐩爣銆� - * @default 'es2020' - */ - esbuildTarget: 'es2020', - silent: false, - // 鍒悕 - alias: { - '/@/': pathResolve('src'), - }, - // terser閰嶇疆 - terserOptions: { - compress: { - // 鏄惁鍒犻櫎console - drop_console: VITE_DROP_CONSOLE, + return { + base: VITE_PUBLIC_PATH, + root, + alias: { + '/@/': `${pathResolve('src')}/`, }, - }, - define: { - __VERSION__: pkg.version, - }, - // css棰勫鐞� - cssPreprocessOptions: { - less: { - modifyVars: modifyVars, - javascriptEnabled: true, + server: { + port: VITE_PORT, + proxy: createProxy(VITE_PROXY), + hmr: { + overlay: true, + }, }, - }, - // 浼氫娇鐢� rollup 瀵� 鍖呴噸鏂扮紪璇戯紝灏嗙紪璇戞垚绗﹀悎 esm 妯″潡瑙勮寖鐨勬柊鐨勫寘鏀惧叆 node_modules/.vite_opt_cache - optimizeDeps: { - include: [ - 'echarts', - 'echarts/map/js/china', - 'ant-design-vue/es/locale/zh_CN', - '@ant-design/icons-vue', - 'moment/locale/zh-cn', + build: { + polyfillDynamicImport: VITE_LEGACY, + terserOptions: { + compress: { + keep_infinity: true, + drop_console: VITE_DROP_CONSOLE, + }, + }, + }, + define: { + __VERSION__: pkg.version, + // setting vue-i18-next + // Suppress warning + __VUE_I18N_LEGACY_API__: false, + __VUE_I18N_FULL_INSTALL__: false, + __INTLIFY_PROD_DEVTOOLS__: false, + }, + css: { + preprocessorOptions: { + less: { + modifyVars: { + // reference: Avoid repeated references + hack: `true; @import (reference) "${resolve('src/design/config.less')}";`, + ...modifyVars, + }, + javascriptEnabled: true, + }, + }, + }, + + plugins: [ + vue(), + vueJsx(), + ...(VITE_LEGACY && isBuild ? [legacy()] : []), + ...createVitePlugins(viteEnv, isBuild, mode), ], - }, - // 鏈湴璺ㄥ煙浠g悊 - proxy: createProxy(VITE_PROXY), - - plugins: [PurgeIcons(), ...vitePlugins], - rollupOutputOptions: {}, - rollupInputOptions: { - // TODO - // external: VITE_USE_CDN ? externals : [], - plugins: rollupPlugins, - }, -}; - -// 鎵╁睍閰嶇疆, 寰�鎵撳寘鍚庣殑html娉ㄥ叆鍐呭 -// 鍙拡瀵圭敓浜х幆澧� -// TODO 鐩墠鍙槸绠�鍗曟墜鍔ㄦ敞鍏ュ疄鐜帮紝鍚庣画vite搴旇浼氭彁渚涢厤缃」 -export const htmlConfig: { - title: string; - addHm?: boolean; - cdnConf?: { - css?: string[]; - js?: string[]; + optimizeDeps: { + include: [ + 'moment', + '@ant-design/icons-vue', + 'echarts/map/js/china', + 'ant-design-vue/es/locale/zh_CN', + 'moment/dist/locale/zh-cn', + 'ant-design-vue/es/locale/en_US', + 'resize-observer-polyfill', + ], + }, }; - useCdn: boolean; - minify: { - enable: boolean; - removeComments: boolean; - collapseWhitespace: boolean; - minifyJS: boolean; - minifyCSS: boolean; - }; -} = { - // html title - title: VITE_GLOB_APP_TITLE, - // 鐧惧害缁熻锛屼笉闇�瑕佸彲浠ュ垹闄� - // 鐢ㄤ簬鎵撳寘閮ㄧ讲绔欑偣浣跨敤銆傚疄闄呴」鐩彲浠ュ垹闄� - addHm: isSiteMode(), - // 浣跨敤cdn鎵撳寘 - // TODO Cdn esm浣跨敤鏂瑰紡闇�瑕佸彧鑳芥敮鎸乬oogle锛屾殏鏃跺叧闂紝鍚庣画鏌ヨ鏇村ソ鐨勬柟寮� - useCdn: false, - // useCdn: VITE_USE_CDN, - // cdn鍒楄〃 - cdnConf, - minify: { - enable: true, - removeComments: true, - collapseWhitespace: true, - minifyJS: true, - minifyCSS: true, - }, }; -export default { - ...viteConfig, - transforms: [globbyTransform(viteConfig)], -} as UserConfig; -- Gitblit v1.8.0