• Vue3兼容低版本浏览器(ie11,chrome63)


    1、插件安装

    为了使你的项目兼容 Chrome 63,你需要确保包含适当的 polyfills 和插件配置。你已经在使用 legacy 插件,但在代码中可能缺少一些配置或插件顺序有问题。以下是几个可能的改进:

    1. 安装 @vitejs/plugin-legacy 插件: 确保你已经安装了 @vitejs/plugin-legacy 插件:
    • npm install @vitejs/plugin-legacy --save-dev

    1. 安装plugin-babel插件
    • npm install --save-dev @babel/core @babel/preset-env @rollup/plugin-babel

    2、更新配置文件

    确保 @vitejs/plugin-legacy 插件在 vite.config.js 中被正确引入和配置。

    1. import { defineConfig, loadEnv } from "vite";
    2. import path from "path";
    3. import createVitePlugins from "./vite/plugins";
    4. import legacy from '@vitejs/plugin-legacy';
    5. import babel from '@rollup/plugin-babel';
    6. // https://vitejs.dev/config/
    7. export default defineConfig(({ mode, command }) => {
    8. const env = loadEnv(mode, process.cwd());
    9. return {
    10. // 部署生产环境和开发环境下的URL。
    11. // 默认情况下,vite 会假设你的应用是被部署在一个域名的根路径上
    12. // 例如 https://www.tianzhu.vip/。如果应用被部署在一个子路径上,你就需要用这个选项指定这个子路径。例如,如果你的应用被部署在 https://www.tianzhu.vip/admin/,则设置 baseUrl 为 /admin/
    13. base: env.VITE_APP_CONTEXT_PATH,
    14. // plugins: createVitePlugins(env, command === "build"),
    15. plugins: [
    16. createVitePlugins(env, command === "build"),
    17. legacy({
    18. targets: ['defaults', 'ie >= 11','chrome 63'],
    19. additionalLegacyPolyfills: ['regenerator-runtime/runtime'], // 面向IE11时需要此插件
    20. modernPolyfills: true,
    21. polyfills: [
    22. 'es.object.values',
    23. 'es.object.entries',
    24. 'es.array.includes',
    25. 'es.promise.finally',
    26. 'es.string.includes',
    27. 'es.array.flat-map'
    28. ]
    29. })
    30. ],
    31. resolve: {
    32. // https://cn.vitejs.dev/config/#resolve-alias
    33. alias: {
    34. // 设置路径
    35. "~": path.resolve(__dirname, "./"),
    36. // 设置别名
    37. "@": path.resolve(__dirname, "./src"),
    38. },
    39. extensions: [".mjs", ".js", ".ts", ".jsx", ".tsx", ".json", ".vue"],
    40. },
    41. build: {
    42. chunkSizeWarningLimit: 50000,
    43. },
    44. // vite 相关配置
    45. server: {
    46. port: 8091,
    47. host: true,
    48. open: true,
    49. proxy: {
    50. // https://cn.vitejs.dev/config/#server-proxy
    51. "/dev-api": {
    52. // target: 'http://127.0.0.1:8080',
    53. target: "http://192.168.2.130:9001/api",
    54. changeOrigin: true,
    55. rewrite: (p) => p.replace(/^\/dev-api/, ""),
    56. },
    57. },
    58. },
    59. //fix:error:stdin>:7356:1: warning: "@charset" must be the first rule in the file
    60. css: {
    61. postcss: {
    62. plugins: [
    63. {
    64. postcssPlugin: "internal:charset-removal",
    65. AtRule: {
    66. charset: (atRule) => {
    67. if (atRule.name === "charset") {
    68. atRule.remove();
    69. }
    70. },
    71. },
    72. },
    73. ],
    74. },
    75. },
    76. };
    77. });

    3、创建本地babelrc文件

    1. {
    2. "presets": [
    3. ["@babel/preset-env", {
    4. "targets": {
    5. "chrome": "63"
    6. },
    7. "useBuiltIns": "entry",
    8. "corejs": 3
    9. }]
    10. ]
    11. }

    4、修改package.json构建大小

    build构建的时候,可能会出现内存溢出的情况,以下构建调整最大内存大小

    1. "scripts": {
    2. "dev": "vite",
    3. "build:prod": "node --max_old_space_size=8192 node_modules/vite/bin/vite.js build",
    4. "preview": "vite preview"
    5. },

    5、打包构建、运行

      • npm run build:prod

  • 相关阅读:
    纸条折痕问题
    【KVM-6】KVM/QEMU软件栈
    C++ STL进阶与补充(基础)
    【面经】华为车BU面经
    try-except 搭配装饰器使用
    性能优化:TCP连接优化之三次握手
    CODING DevOps产品认证笔记
    ssm+java+vue面向企事业单位的项目申报小程序#毕业设计
    车间生产设备管理有哪些问题?低代码来助力
    python tkinter 使用(三)
  • 原文地址:https://blog.csdn.net/chajinglong/article/details/139284000