vben-admin-thin-next/vite.config.ts

140 lines
3.5 KiB
TypeScript
Raw Normal View History

import type { UserConfig } from 'vite';
2020-09-28 20:19:10 +08:00
import { resolve } from 'path';
import { modifyVars } from './build/config/lessModifyVars';
import { createProxy } from './build/vite/proxy';
import globbyTransform from './build/vite/plugin/context/transform';
2020-11-08 23:13:47 +08:00
import dynamicImportTransform from './build/vite/plugin/dynamicImport/index';
import { isDevFn, loadEnv } from './build/utils';
2020-09-28 20:19:10 +08:00
import { createRollupPlugin, createVitePlugins } from './build/vite/plugin';
const pkg = require('./package.json');
2020-09-28 20:19:10 +08:00
const viteEnv = loadEnv();
const {
VITE_PORT,
VITE_PUBLIC_PATH,
VITE_PROXY,
2020-10-16 22:03:44 +08:00
VITE_DROP_CONSOLE,
// VITE_USE_CDN,
} = viteEnv;
2020-09-28 20:19:10 +08:00
function pathResolve(dir: string) {
return resolve(__dirname, '.', dir);
}
2020-10-10 21:28:43 +08:00
2020-09-28 20:19:10 +08:00
const viteConfig: UserConfig = {
2020-10-27 00:17:11 +08:00
/**
* 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'
*/
2020-10-27 01:57:39 +08:00
// TODO build error
2020-11-10 22:45:39 +08:00
// entry: 'public/index.html',
2020-10-10 21:28:43 +08:00
/**
2020-10-29 23:01:11 +08:00
* port
2020-10-10 21:28:43 +08:00
* @default '3000'
*/
port: VITE_PORT,
2020-09-28 20:19:10 +08:00
/**
* @default 'localhost'
*/
hostname: 'localhost',
/**
2020-10-29 23:01:11 +08:00
* Run to open the browser automatically
2020-09-28 20:19:10 +08:00
* @default 'false'
*/
open: false,
/**
2020-10-29 23:01:11 +08:00
* Set to `false` to disable minification, or specify the minifier to use.
* Available options are 'terser' or 'esbuild'.
2020-09-28 20:19:10 +08:00
* @default 'terser'
*/
2020-10-21 21:44:57 +08:00
minify: isDevFn() ? 'esbuild' : 'terser',
2020-09-28 20:19:10 +08:00
/**
2020-10-29 23:01:11 +08:00
* Base public path when served in production.
2020-09-28 20:19:10 +08:00
* @default '/'
*/
2020-10-10 21:28:43 +08:00
base: VITE_PUBLIC_PATH,
2020-09-28 20:19:10 +08:00
/**
2020-10-29 23:01:11 +08:00
* Directory relative from `root` where build output will be placed. If the
* directory exists, it will be removed before the build.
2020-09-28 20:19:10 +08:00
* @default 'dist'
*/
outDir: 'dist',
/**
2020-10-29 23:01:11 +08:00
* Whether to generate sourcemap
* @default false
2020-09-28 20:19:10 +08:00
*/
sourcemap: false,
/**
2020-10-29 23:01:11 +08:00
* Directory relative from `outDir` where the built js/css/image assets will
* be placed.
2020-09-28 20:19:10 +08:00
* @default '_assets'
*/
assetsDir: '_assets',
/**
2020-10-29 23:01:11 +08:00
* Static asset files smaller than this number (in bytes) will be inlined as
* base64 strings. Default limit is `4096` (4kb). Set to `0` to disable.
* @default 4096
2020-09-28 20:19:10 +08:00
*/
assetsInlineLimit: 4096,
/**
2020-10-29 23:01:11 +08:00
* Transpile target for esbuild.
2020-10-16 22:03:44 +08:00
* @default 'es2020'
2020-09-28 20:19:10 +08:00
*/
2020-10-16 22:03:44 +08:00
esbuildTarget: 'es2020',
2020-10-29 23:01:11 +08:00
/**
* Whether to log asset info to console
* @default false
*/
2020-10-10 21:28:43 +08:00
silent: false,
2020-10-29 23:01:11 +08:00
/**
* Import alias. The entries can either be exact request -> request mappings
* (exact, no wildcard syntax), or request path -> fs directory mappings.
* When using directory mappings, the key **must start and end with a slash**.
* ```
*/
2020-09-28 20:19:10 +08:00
alias: {
'/@/': pathResolve('src'),
},
2020-10-29 23:01:11 +08:00
// terser options
2020-10-24 01:46:29 +08:00
terserOptions: {
2020-10-16 22:03:44 +08:00
compress: {
drop_console: VITE_DROP_CONSOLE,
},
},
define: {
__VERSION__: pkg.version,
},
2020-09-28 20:19:10 +08:00
cssPreprocessOptions: {
less: {
modifyVars: modifyVars,
javascriptEnabled: true,
},
},
2020-10-29 23:01:11 +08:00
// The package will be recompiled using rollup, and the new package compiled into the esm module specification will be put into node_modules/.vite_opt_cache
2020-09-28 20:19:10 +08:00
optimizeDeps: {
2020-11-06 22:41:00 +08:00
include: ['echarts/map/js/china', 'ant-design-vue/es/locale/zh_CN', '@ant-design/icons-vue'],
2020-09-28 20:19:10 +08:00
},
2020-10-16 22:03:44 +08:00
2020-10-29 23:01:11 +08:00
// Local cross-domain proxy
2020-10-10 21:28:43 +08:00
proxy: createProxy(VITE_PROXY),
plugins: createVitePlugins(viteEnv),
2020-09-28 20:19:10 +08:00
rollupInputOptions: {
// TODO
// external: VITE_USE_CDN ? externals : [],
plugins: createRollupPlugin(),
2020-09-28 20:19:10 +08:00
},
};
2020-10-16 22:03:44 +08:00
export default {
...viteConfig,
2020-11-08 23:13:47 +08:00
transforms: [globbyTransform(viteConfig), dynamicImportTransform(viteEnv)],
2020-10-16 22:03:44 +08:00
} as UserConfig;