vue.config.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // vue.config.js
  2. const path = require('path');
  3. const IS_PROD = ['production', 'prod'].includes(process.env.NODE_ENV);
  4. const resolve = (dir) => path.join(__dirname, dir);
  5. const UglifyJsPlugin = require("uglifyjs-webpack-plugin"); // 去掉 console.log
  6. //打包分析
  7. const BundleAnalyzerPlugin = require("webpack-bundle-analyzer")
  8. .BundleAnalyzerPlugin;
  9. module.exports = {
  10. publicPath: "./", // 默认'/',部署应用包时的基本 URL
  11. indexPath: 'index.html', // 相对于打包路径index.html的路径
  12. outputDir: 'dist', // 'dist', 生产环境构建文件的目录
  13. assetsDir: 'static', // 相对于outputDir的静态资源(js、css、img、fonts)目录
  14. lintOnSave: false, // 是否在开发环境下通过 eslint-loader 在每次保存时 lint 代码
  15. runtimeCompiler: true, // 是否使用包含运行时编译器的 Vue 构建版本
  16. productionSourceMap: !IS_PROD, // 生产环境的 source map
  17. chainWebpack: config => {
  18. config.resolve.symlinks(true); // 修复热更新失效
  19. // 添加别名
  20. config.resolve.alias
  21. .set('@', resolve('src'))
  22. .set('@api', resolve('src/api'))
  23. .set('@tools', resolve('src/tools'))
  24. .set('@assets', resolve('src/assets'))
  25. .set('@components', resolve('src/components'))
  26. .set('@views', resolve('src/views'))
  27. .set('@router', resolve('src/router'))
  28. .set('@store', resolve('src/store'))
  29. .set('@layout',resolve('src/layout'))
  30. .set('@utils',resolve('src/utils'))
  31. .set('@jsonData', resolve('src/jsonData'));
  32. // 压缩图片
  33. if (IS_PROD) {
  34. config.module
  35. .rule("images")
  36. .use("image-webpack-loader")
  37. .loader("image-webpack-loader")
  38. .options({
  39. mozjpeg: { progressive: true, quality: 65 },
  40. optipng: { enabled: false },
  41. pngquant: { quality: [0.65, 0.9], speed: 4 },
  42. gifsicle: { interlaced: false }
  43. // webp: { quality: 75 }
  44. });
  45. }
  46. },
  47. css: {
  48. loaderOptions: {
  49. sass: {
  50. prependData: `@import "./src/assets/css/default";`,
  51. },
  52. },
  53. },
  54. // 去掉console.log
  55. configureWebpack: config => {
  56. if (IS_PROD) {
  57. const plugins = [];
  58. plugins.push(
  59. new UglifyJsPlugin({
  60. uglifyOptions: {
  61. compress: {
  62. drop_console: true,
  63. drop_debugger: false,
  64. pure_funcs: ["console.log"] //移除console
  65. }
  66. },
  67. sourceMap: false,
  68. parallel: true
  69. })
  70. );
  71. config.plugins = [...config.plugins, ...plugins];
  72. }
  73. },
  74. devServer: {
  75. //代理配置
  76. proxy:{
  77. //这里的api表示请求的地址有apizzzs,就触发代理机制
  78. '/apizzz':{
  79. target:'www.baidu.com',//我们要代理的地址
  80. changeOrigin:true,//是否跨域
  81. pathRewrite:{
  82. //'^/api',''//假设想把 localhost:8080/api/login 变成www.baidu.com/login就需要这么做
  83. }
  84. }
  85. },
  86. open: true, // 是否打开浏览器
  87. },
  88. //打包分析
  89. chainWebpack: config => {
  90. // 打包分析
  91. if (IS_PROD) {
  92. config.plugin("webpack-report").use(BundleAnalyzerPlugin, [
  93. {
  94. analyzerMode: "static"
  95. }
  96. ]);
  97. }
  98. }
  99. }