fix: 优化浏览器兼容策略

This commit is contained in:
landaiqing
2024-05-06 01:00:14 +08:00
parent 564ff40406
commit feb965b11d
3 changed files with 188 additions and 164 deletions

27
.babelrc.json Normal file
View File

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