| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- import { reactRouter } from '@react-router/dev/vite';
- import legacy from '@vitejs/plugin-legacy';
- import react from '@vitejs/plugin-react';
- import removeConsole from 'vite-plugin-remove-console';
- // import wasm from 'vite-plugin-wasm'; // 代码中直接 import 'xxx.wasm' 时需要用到此插件
- // import topLevelAwait from 'vite-plugin-top-level-await'; // 使用 vite-plugin-wasm 插件时需要同时引入此插件
- import { viteBuildInfo } from '../buildInfo';
- import { configCompressPlugin } from '../compress';
- import svgConvert from '../svgConvert';
- import { clientChunksPlugin } from './chunks';
- import type { PluginOption } from 'vite';
- /**
- * 组装插件数组。依赖渲染模式(isCSR)、环境(isProd)与 env(压缩方式)。
- * @param isCSR 是否 CSR 模式。CSR 用官方 plugin-react;SSG 用 reactRouter(接管 React 处理与入口)。二者互斥。
- * @param isProd 是否生产构建。生产才启用 removeConsole。
- * @param env 处理后的环境变量(提供 VITE_BUILD_COMPRESSION)。
- */
- export function createPlugins(isCSR: boolean, isProd: boolean, env: ImportMetaEnv): PluginOption[] {
- return [
- // React 处理:CSR 用官方 plugin-react;SSG 用 reactRouter(自带 React 处理并接管入口)。二者互斥。
- isCSR ? react() : reactRouter(),
- // clientChunksPlugin 仅 SSG 需要(framework 分 client/ssr 双环境构建);
- // CSR 单环境构建,vendor 分包直接写在 build.rollupOptions 里。
- !isCSR && clientChunksPlugin,
- // wasm(),
- // topLevelAwait(),
- legacy({
- // 浏览器支持基线(browserslist 语法):chrome87/safari13/firefox78/edge88 这条老线,
- // 与 build.ts 的 CSS_TARGET(esbuild 语法)表达同一基线,需保持一致。
- // legacy() 会为该基线生成 nomodule 的 legacy chunk + polyfill,并用 Babel 降级现代 JS 语法,
- // 使产物可在这些老浏览器上运行(现代浏览器仍走 type=module 的现代 chunk)。
- targets: ['chrome >= 87', 'safari >= 13', 'firefox >= 78', 'edge >= 88', 'not IE 11'],
- }),
- isProd &&
- removeConsole({
- includes: ['log', 'warn'],
- external: ['error'],
- }),
- svgConvert(),
- viteBuildInfo(),
- configCompressPlugin(env.VITE_BUILD_COMPRESSION!),
- ].filter(Boolean) as PluginOption[];
- }
|