Webpack cl4 配置

基本配置:

module.exports = defineConfig({
  transpileDependencies: false,
  lintOnSave: false,
  outputDir: process.env.VUE_APP_DIST, // 打包后文件的目录
  publicPath: './', // 静态资源路径(默认/,打包后会白屏)
  assetsDir: 'static', //outputDir的静态资源(js、css、img、fonts)目录
)}

一.打正式环境包时设置productionSourceMap为false:

  • productionSourceMap: true

    • 当设置为 true 时,在生产模式下会生成用于调试的源映射文件。这样,如果在生产环境出现错误,浏览器的开发者工具将能够还原回源代码,方便开发者进行调试。
    • 生成的源映射文件可能会增加构建的体积,但对于排查线上问题非常有帮助。
  • productionSourceMap: false

    • 当设置为 false 时,在生产模式下不会生成源映射文件。这样能够减小构建产物的体积,提高生产环境的性能。但在出现问题时,调试时将不再能够直接追踪到源代码。

二.autoprefixer 样式自动添加浏览器前缀:

        npm install autoprefixer

const autoprefixer = require('autoprefixer');
// 添加样式浏览器前缀
  css: {
    loaderOptions: {
      postcss: {
        postcssOptions: {
          plugins: [autoprefixer()]
        }
      }
    }
  },

 三.打正式包去掉注释/黄色警告/console.log

       安装 uglifyjs-webpack-plugin 插件:

        npm install uglifyjs-webpack-plugin

const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const isProduction = process.env.NODE_ENV === 'production'; //判断打包生产环境
configureWebpack: config => {
    // const plugins = [];
    if (isProduction) {
      config.plugins.push(
        new UglifyJsPlugin({
          uglifyOptions: {
            output: {
              comments: false, // 去掉注释
            },
            warnings: false, //去除黄色警告
            compress: {
              drop_console: true,
              drop_debugger: false,// 防止利用debugger调试
              pure_funcs: ['console.log']//移除console
            }
          }
        })
      )
    }
  }

 四.配置路径别名

 // 配置路径别名
  chainWebpack: (config) => {
    config.resolve.alias
      .set('@', resolve('./src'))
      .set('components', resolve('./src/components'))
      .set('assets', resolve('./src/assets'))
  },

 四.配置多个代理

        详细介绍:

                项目环境配置 本地/测试/预发/生产_预发布环境部署-CSDN博客

                vue项目配置多个代理-CSDN博客

// 代理
  devServer: {
    proxy: {
      "/iot": {
        target: process.env.VUE_APP_API_PATH_ONE,
        changeOrigin: true,
        pathRewrite: {
          '^/': '',
        }
      },
      "/V1": {
        target: process.env.VUE_APP_API_PATH_TWO,
        changeOrigin: true,
        pathRewrite: {
          '^/': '',
        }
      },
      "/runbayunapi": {
        target: process.env.VUE_APP_API_PATH,
        changeOrigin: true,
        pathRewrite: {
          '^/': '',
        }
      },
    },
  },

完整代码:

const { defineConfig } = require('@vue/cli-service')
const autoprefixer = require('autoprefixer');
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
const isProduction = process.env.NODE_ENV === 'production';
const path = require('path');//引入path模块
function resolve(dir) {
  return path.join(__dirname, dir)//path.join(__dirname)设置绝对路径
}
module.exports = defineConfig({
  transpileDependencies: false,
  lintOnSave: false,
  outputDir: process.env.VUE_APP_DIST, // 打包后文件的目录
  publicPath: './', // 静态资源路径(默认/,打包后会白屏)
  assetsDir: 'static', //outputDir的静态资源(js、css、img、fonts)目录
  // 打包正式环境去除.map文件
  productionSourceMap: false,
  // 添加样式浏览器前缀
  css: {
    loaderOptions: {
      postcss: {
        postcssOptions: {
          plugins: [autoprefixer()]
        }
      }
    }
  },
  configureWebpack: config => {
    // const plugins = [];
    if (isProduction) {
      config.plugins.push(
        new UglifyJsPlugin({
          uglifyOptions: {
            output: {
              comments: false, // 去掉注释
            },
            warnings: false, //去除黄色警告
            compress: {
              drop_console: true,
              drop_debugger: false,// 防止利用debugger调试
              pure_funcs: ['console.log']//移除console
            }
          }
        })
      )
    }
  },
  // 配置路径别名
  chainWebpack: (config) => {
    config.resolve.alias
      .set('@', resolve('./src'))
      .set('components', resolve('./src/components'))
      .set('assets', resolve('./src/assets'))
  },
  // 代理
  devServer: {
    proxy: {
      "/iot": {
        target: process.env.VUE_APP_API_PATH_ONE,
        changeOrigin: true,
        pathRewrite: {
          '^/': '',
        }
      },
      "/V1": {
        target: process.env.VUE_APP_API_PATH_TWO,
        changeOrigin: true,
        pathRewrite: {
          '^/': '',
        }
      },
      "/runbayunapi": {
        target: process.env.VUE_APP_API_PATH,
        changeOrigin: true,
        pathRewrite: {
          '^/': '',
        }
      },
    },
  },
})

相关推荐

  1. Webpack cl4 配置

    2023-12-11 20:26:03       73 阅读
  2. GTK4 环境配置

    2023-12-11 20:26:03       52 阅读
  3. neo4j配置详解

    2023-12-11 20:26:03       65 阅读
  4. pnp4nagios 配置 nagios

    2023-12-11 20:26:03       45 阅读
  5. Oracle T4-4小型机上配置Ldom部署rac

    2023-12-11 20:26:03       48 阅读
  6. Vue3框架搭建4配置说明-eslint配置

    2023-12-11 20:26:03       23 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-11 20:26:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-11 20:26:03       101 阅读
  3. 在Django里面运行非项目文件

    2023-12-11 20:26:03       82 阅读
  4. Python语言-面向对象

    2023-12-11 20:26:03       91 阅读

热门阅读

  1. C语言-水仙花数

    2023-12-11 20:26:03       58 阅读
  2. C++ 把引用作为参数

    2023-12-11 20:26:03       57 阅读
  3. 算法基础十

    2023-12-11 20:26:03       51 阅读
  4. 以为回调函数是同步的(js的问题)

    2023-12-11 20:26:03       85 阅读
  5. K8S学习指南-minikube的安装

    2023-12-11 20:26:03       49 阅读
  6. angular material mat-error 失效不展示

    2023-12-11 20:26:03       54 阅读
  7. CSS height auto 过渡

    2023-12-11 20:26:03       56 阅读
  8. 常用的C语言宏定义

    2023-12-11 20:26:03       63 阅读
  9. 牛客挑战赛 B - 树上博弈 -- 题解

    2023-12-11 20:26:03       71 阅读
  10. Python:合并两个PDF文件为一个PDF

    2023-12-11 20:26:03       57 阅读