vben
2020-10-29 2f1fbf8e487fdae4db5c144d97f9d20e42bb1603
vite.config.ts
@@ -1,112 +1,123 @@
import type { UserConfig } from 'vite';
import { resolve } from 'path';
import type { UserConfig, Plugin as VitePlugin } from 'vite';
import { modifyVars } from './build/config/lessModifyVars';
import { createProxy } from './build/vite/proxy';
import globbyTransform from './build/vite/plugin/context/transform';
import visualizer from 'rollup-plugin-visualizer';
import { modifyVars } from './build/config/glob/lessModifyVars';
import { setupBasicEnv } from './build/config/vite/env';
import { createProxy } from './build/config/vite/proxy';
import { createMockServer } from 'vite-plugin-mock';
import PurgeIcons from 'vite-plugin-purge-icons';
import { isDevFn, isReportMode, isProdFn, loadEnv } from './build/utils';
import { isDevFn, loadEnv } from './build/utils';
setupBasicEnv();
const { VITE_USE_MOCK, VITE_PORT, VITE_PUBLIC_PATH, VITE_PROXY } = loadEnv();
import { createRollupPlugin, createVitePlugins } from './build/vite/plugin';
const pkg = require('./package.json');
const viteEnv = loadEnv();
const {
  VITE_PORT,
  VITE_PUBLIC_PATH,
  VITE_PROXY,
  VITE_DROP_CONSOLE,
  // VITE_USE_CDN,
} = viteEnv;
function pathResolve(dir: string) {
  return resolve(__dirname, '.', dir);
}
const rollupPlugins: any[] = [];
const vitePlugins: VitePlugin[] = [];
(() => {
  if (isReportMode() && isProdFn()) {
    // report
    rollupPlugins.push(
      visualizer({ filename: './node_modules/.cache/stats.html', open: true }) as Plugin
    );
  }
  if (isDevFn() && VITE_USE_MOCK) {
    // open mock
    vitePlugins.push(
      createMockServer({
        ignore: /^\_/,
        mockPath: 'mock',
      })
    );
  }
})();
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'
   */
  // TODO build error
  // entry: './public/index.html',
  /**
   * port
   * @default '3000'
   */
  port: VITE_PORT,
  /**
   * 服务地址
   * @default 'localhost'
   */
  hostname: 'localhost',
  /**
   * 运行自动打开浏览器·
   * Run to open the browser automatically
   * @default 'false'
   */
  open: false,
  /**
   * 压缩代码
   *  boolean | 'terser' | 'esbuild'
   * Set to `false` to disable minification, or specify the minifier to use.
   * Available options are 'terser' or 'esbuild'.
   * @default 'terser'
   */
  minify: isDevFn() ? false : 'terser',
  minify: isDevFn() ? 'esbuild' : 'terser',
  /**
   * 基本公共路径
   * Base public path when served in production.
   * @default '/'
   */
  base: VITE_PUBLIC_PATH,
  /**
   * 打包输入路径
   * Directory relative from `root` where build output will be placed. If the
   * directory exists, it will be removed before the build.
   * @default 'dist'
   */
  outDir: 'dist',
  /**
   * @default 'false'
   * Whether to generate sourcemap
   * @default false
   */
  sourcemap: false,
  /**
   * 资源输出路径
   * Directory relative from `outDir` where the built js/css/image assets will
   * be placed.
   * @default '_assets'
   */
  assetsDir: '_assets',
  /**
   * 静态资源小于该大小将会内联,默认4096kb
   * @default '4096'
   * 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
   */
  assetsInlineLimit: 4096,
  /**
   * esbuild转换目标。
   * @default 'es2019'
   * Transpile target for esbuild.
   * @default 'es2020'
   */
  esbuildTarget: 'es2019',
  esbuildTarget: 'es2020',
  /**
   * Whether to log asset info to console
   * @default false
   */
  silent: false,
  // 别名
  /**
   * 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**.
   * ```
   */
  alias: {
    '/@/': pathResolve('src'),
  },
  // define: {
  //   __ENV__: 'value',
  // },
  // css预处理
  // terser options
  terserOptions: {
    compress: {
      drop_console: VITE_DROP_CONSOLE,
    },
  },
  define: {
    __VERSION__: pkg.version,
  },
  cssPreprocessOptions: {
    less: {
      modifyVars: modifyVars,
      javascriptEnabled: true,
    },
  },
  // 配置Dep优化行为
  // 会使用 rollup 对 包重新编译,将编译成符合 esm 模块规范的新的包放入 node_modules 下的 .
  // 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
  optimizeDeps: {
    include: [
      'echarts',
@@ -116,14 +127,18 @@
      'moment/locale/zh-cn',
    ],
  },
  // 本地跨域代理
  proxy: createProxy(VITE_PROXY),
  plugins: [PurgeIcons(), ...vitePlugins],
  rollupOutputOptions: {},
  // Local cross-domain proxy
  proxy: createProxy(VITE_PROXY),
  plugins: createVitePlugins(viteEnv),
  rollupInputOptions: {
    plugins: rollupPlugins,
    // TODO
    // external: VITE_USE_CDN ? externals : [],
    plugins: createRollupPlugin(),
  },
};
export default viteConfig;
export default {
  ...viteConfig,
  transforms: [globbyTransform(viteConfig)],
} as UserConfig;