fix: 优化浏览器兼容策略

This commit is contained in:
landaiqing
2024-05-06 01:07:14 +08:00
parent feb965b11d
commit c4bd8f6be0
3 changed files with 171 additions and 187 deletions

View File

@@ -1,27 +0,0 @@
{
"plugins": [
[
"@babel/plugin-transform-runtime",
{
"corejs": 3
}
]
],
"presets": [
[
"@babel/preset-env",
{
"targets": {
"ie": "11",
"chrome": "58",
"safari": "11.1",
"firefox": "63",
"edge": "16"
},
"corejs": 3,
"useBuiltIns": "usage",
"modules": false
}
]
]
}

View File

@@ -6,8 +6,6 @@ import routeConfig from './router'
import 'virtual:svg-icons-register' import 'virtual:svg-icons-register'
import { Provider as MobxProvider } from 'mobx-react' import { Provider as MobxProvider } from 'mobx-react'
import { RootStore } from '@/store' import { RootStore } from '@/store'
import 'core-js';
import 'regenerator-runtime/runtime';
const router = createBrowserRouter(routeConfig) const router = createBrowserRouter(routeConfig)
ReactDOM.createRoot(document.getElementById('root')!).render( ReactDOM.createRoot(document.getElementById('root')!).render(
// <React.StrictMode> // <React.StrictMode>

View File

@@ -1,170 +1,183 @@
import {defineConfig, loadEnv} from 'vite' import { defineConfig, loadEnv } from 'vite'
import react from '@vitejs/plugin-react' import react from '@vitejs/plugin-react'
import {resolve} from 'path' import { resolve } from 'path'
// icons plugin // icons plugin
import {createSvgIconsPlugin} from 'vite-plugin-svg-icons' import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import * as path from 'path' import * as path from 'path'
import imagemin from 'unplugin-imagemin/vite' import imagemin from 'unplugin-imagemin/vite'
import viteCompression from 'vite-plugin-compression' import viteCompression from 'vite-plugin-compression'
import {createHtmlPlugin} from 'vite-plugin-html' import { createHtmlPlugin } from 'vite-plugin-html'
import legacy from '@vitejs/plugin-legacy' import legacy from '@vitejs/plugin-legacy'
import postcssPresetEnv from 'postcss-preset-env' import postcssPresetEnv from 'postcss-preset-env'
import autoprefixer from 'autoprefixer' import autoprefixer from 'autoprefixer'
// eslint-disable-next-line @typescript-eslint/ban-ts-comment // eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error // @ts-expect-error
//配置参数 //配置参数
export default defineConfig(({mode}) => { export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd()) const env = loadEnv(mode, process.cwd())
return { return {
base: '/', base: '/',
plugins: [ plugins: [
react(), react(),
legacy({ legacy({
targets: ['ie >= 11', 'chrome 52', 'Chrome > 70', 'Safari 12.1', 'last 2 versions and since 2018 and > 0.5%', 'iOS >= 9','Android >= 4.4','last 2 versions'], targets: [
additionalLegacyPolyfills: ['regenerator-runtime/runtime'], 'ie >= 11',
renderLegacyChunks: true, 'chrome 52',
polyfills: [ 'Chrome > 70',
'es.promise.all-settled', 'Safari 12.1',
'es.symbol', 'last 2 versions and since 2018 and > 0.5%',
'es.array.filter', 'iOS >= 9',
'es.promise', 'Android >= 4.4',
'es.promise.finally', 'last 2 versions',
'es/map', ],
'es/set', additionalLegacyPolyfills: ['regenerator-runtime/runtime'],
'es.array.for-each', renderLegacyChunks: true,
'es.object.define-properties', polyfills: [
'es.object.define-property', 'es.promise.all-settled',
'es.object.get-own-property-descriptor', 'es.symbol',
'es.object.get-own-property-descriptors', 'es.array.filter',
'es.object.keys', 'es.promise',
'es.object.to-string', 'es.promise.finally',
'web.dom-collections.for-each', 'es/map',
'esnext.global-this', 'es/set',
'esnext.string.match-all' 'es.array.for-each',
], 'es.object.define-properties',
modernPolyfills: ['es.promise.all-settled', 'es.object.entries', 'es.string.replace-all'], 'es.object.define-property',
}), 'es.object.get-own-property-descriptor',
// 修改 icons 相关配置 'es.object.get-own-property-descriptors',
createSvgIconsPlugin({ 'es.object.keys',
// 指定需要缓存的图标文件夹 'es.object.to-string',
iconDirs: [path.resolve(__dirname, './src/assets/icons')], 'web.dom-collections.for-each',
// 指定symbolId格式 'esnext.global-this',
symbolId: 'icon-[dir]-[name]', 'esnext.string.match-all',
}), ],
imagemin({ modernPolyfills: [
// Default mode sharp. support squoosh and sharp 'es.promise.all-settled',
mode: 'sharp', 'es.object.entries',
beforeBundle: true, 'es.string.replace-all',
// Default configuration options for compressing different pictures ],
compress: { }),
jpg: { // 修改 icons 相关配置
quality: 10, createSvgIconsPlugin({
}, // 指定需要缓存的图标文件夹
jpeg: { iconDirs: [path.resolve(__dirname, './src/assets/icons')],
quality: 10, // 指定symbolId格式
}, symbolId: 'icon-[dir]-[name]',
png: { }),
quality: 10, imagemin({
}, // Default mode sharp. support squoosh and sharp
webp: { mode: 'sharp',
quality: 10, beforeBundle: true,
}, // Default configuration options for compressing different pictures
}, compress: {
conversion: [ jpg: {
{from: 'jpeg', to: 'webp'}, quality: 10,
{from: 'png', to: 'webp'}, },
{from: 'JPG', to: 'jpeg'}, jpeg: {
], quality: 10,
}), },
viteCompression({ png: {
verbose: true, // 是否在控制台中输出压缩结果 quality: 10,
disable: false, },
threshold: 10240, // 如果体积大于阈值将被压缩单位为b体积过小时请不要压缩以免适得其反 webp: {
algorithm: 'gzip', // 压缩算法,可选['gzip'' brotliccompress ''deflate ''deflateRaw'] quality: 10,
ext: '.gz', },
deleteOriginFile: true, // 源文件压缩后是否删除 },
}), conversion: [
createHtmlPlugin({ { from: 'jpeg', to: 'webp' },
minify: true, { from: 'png', to: 'webp' },
/** { from: 'JPG', to: 'jpeg' },
* 在这里写entry后你将不需要在`index.html`内添加 script 标签,原有标签需要删除 ],
* @default src/main.ts }),
*/ viteCompression({
entry: 'src/main.tsx', verbose: true, // 是否在控制台中输出压缩结果
/** disable: false,
* 如果你想将 `index.html`存放在指定文件夹,可以修改它,否则不需要配置 threshold: 10240, // 如果体积大于阈值将被压缩单位为b体积过小时请不要压缩以免适得其反
* @default index.html algorithm: 'gzip', // 压缩算法,可选['gzip'' brotliccompress ''deflate ''deflateRaw']
*/ ext: '.gz',
template: 'index.html', deleteOriginFile: true, // 源文件压缩后是否删除
/** }),
* 需要注入 index.html ejs 模版的数据 createHtmlPlugin({
*/ minify: true,
inject: { /**
data: { * 在这里写entry后你将不需要在`index.html`内添加 script 标签,原有标签需要删除
title: env.VITE_TITLE_NAME, * @default src/main.ts
}, */
}, entry: 'src/main.tsx',
}), /**
], * 如果你想将 `index.html`存放在指定文件夹,可以修改它,否则不需要配置
resolve: { * @default index.html
alias: { */
'@': resolve(__dirname, 'src'), template: 'index.html',
}, /**
extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json'], // 默认值,这些文件引入时不需要写后缀 * 需要注入 index.html ejs 模版的数据
}, */
css: { inject: {
modules: { data: {
// 一般我们可以通过 generateScopedName 属性来对生成的类名进行自定义 title: env.VITE_TITLE_NAME,
// 其中name 表示当前文件名local 表示类名 },
generateScopedName: '[name]__[local]___[hash:base64:5]', },
}, }),
postcss: { ],
plugins: [ resolve: {
postcssPresetEnv(), alias: {
autoprefixer({ '@': resolve(__dirname, 'src'),
// 自动添加前缀 },
overrideBrowserslist: [ extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json'], // 默认值,这些文件引入时不需要写后缀
'Android 4.1', },
'iOS 7.1', css: {
'Chrome > 31', modules: {
'ff > 31', // 一般我们可以通过 generateScopedName 属性来对生成的类名进行自定义
'ie >= 8', // 其中name 表示当前文件名local 表示类名
], generateScopedName: '[name]__[local]___[hash:base64:5]',
}), },
], postcss: {
}, plugins: [
preprocessorOptions: { postcssPresetEnv(),
less: { autoprefixer({
javascriptEnabled: true, // 自动添加前缀
}, overrideBrowserslist: [
}, 'Android 4.1',
}, 'iOS 7.1',
esbuild: { 'Chrome > 31',
drop: ['console', 'debugger'], 'ff > 31',
}, 'ie >= 8',
build: { ],
outDir: 'dist', // 指定输出路径 }),
assetsDir: 'assets', // 指定生成静态文件目录 ],
assetsInlineLimit: '4096', // 小于此阈值的导入或引用资源将内联为 base64 编码 },
cssCodeSplit: true, // 启用 CSS 代码拆分 preprocessorOptions: {
sourcemap: false, // 构建后是否生成 source map 文件 less: {
minify: 'esbuild', // 指定使用哪种混淆器 javascriptEnabled: true,
write: true, // 启用将构建后的文件写入磁盘 },
emptyOutDir: true, // 构建时清空该目录 },
brotliSize: true, // 启用 brotli 压缩大小报告 },
chunkSizeWarningLimit: 2000, // chunk 大小警告的限制 esbuild: {
watch: null, // 设置为 {} 则会启用 rollup 的监听器 drop: ['console', 'debugger'],
}, },
server: { build: {
proxy: { outDir: 'dist', // 指定输出路径
[env.VITE_APP_BASE_API]: { assetsDir: 'assets', // 指定生成静态文件目录
//后端接口的baseurl assetsInlineLimit: '4096', // 小于此阈值的导入或引用资源将内联为 base64 编码
target: env.VITE_API_BASE_URL, cssCodeSplit: true, // 启用 CSS 代码拆分
//是否允许跨域 sourcemap: false, // 构建后是否生成 source map 文件
changeOrigin: true, minify: 'esbuild', // 指定使用哪种混淆器
rewrite: (path) => path.replace(RegExp(`^${env.VITE_APP_BASE_API}`), ''), write: true, // 启用将构建后的文件写入磁盘
}, emptyOutDir: true, // 构建时清空该目录
}, brotliSize: true, // 启用 brotli 压缩大小报告
}, chunkSizeWarningLimit: 2000, // chunk 大小警告的限制
} watch: null, // 设置为 {} 则会启用 rollup 的监听器
},
server: {
proxy: {
[env.VITE_APP_BASE_API]: {
//后端接口的baseurl
target: env.VITE_API_BASE_URL,
//是否允许跨域
changeOrigin: true,
rewrite: (path) => path.replace(RegExp(`^${env.VITE_APP_BASE_API}`), ''),
},
},
},
}
}) })