Vue CLI4.0 Vue.config.js标准配置

前言:

Vue.js CLI4.0给人最直白的感受就是没有了build文件夹和config文件夹,所有的配置都在Vue.config.js(需要手动创建)完成。那么该文件的配置至关重要。现在我们来看一下最新配置是怎么配置的。

安装

  1. npm i -d vue-cli-configjs

vue.config.js

  1. // vue.config.js
  2. const path = require('path');
  3. const CompressionWebpackPlugin = require("compression-webpack-plugin"); // 开启gzip压缩, 按需引用
  4. const productionGzipExtensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i; // 开启gzip压缩, 按需写入
  5. const BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin; // 打包分析
  6. const IS_PROD = ['production'].includes(process.env.NODE_ENV);
  7. const resolve = (dir) => path.join(__dirname, dir);
  8. module.exports = {
  9. publicPath: process.env.NODE_ENV === 'production' ? '/site/vue-demo/' : '/', // 公共路径
  10. indexPath: 'index.html' , // 相对于打包路径index.html的路径
  11. outputDir: process.env.outputDir || 'dist', // 'dist', 生产环境构建文件的目录
  12. assetsDir: 'static', // 相对于outputDir的静态资源(js、css、img、fonts)目录
  13. lintOnSave: false, // 是否在开发环境下通过 eslint-loader 在每次保存时 lint 代码
  14. runtimeCompiler: true, // 是否使用包含运行时编译器的 Vue 构建版本
  15. productionSourceMap: !IS_PROD, // 生产环境的 source map
  16. parallel: require("os").cpus().length > 1, // 是否为 Babel 或 TypeScript 使用 thread-loader。该选项在系统的 CPU 有多于一个内核时自动启用,仅作用于生产构建。
  17. pwa: {}, // 向 PWA 插件传递选项。
  18. chainWebpack: config => {
  19. config.resolve.symlinks(true); // 修复热更新失效
  20. // 如果使用多页面打包,使用vue inspect --plugins查看html是否在结果数组中
  21. config.plugin("html").tap(args => {
  22. // 修复 Lazy loading routes Error
  23. args[0].chunksSortMode = "none";
  24. return args;
  25. });
  26. config.resolve.alias // 添加别名
  27. .set('@', resolve('src'))
  28. .set('@assets', resolve('src/assets'))
  29. .set('@components', resolve('src/components'))
  30. .set('@views', resolve('src/views'))
  31. .set('@store', resolve('src/store'));
  32. // 压缩图片
  33. // 需要 npm i -D image-webpack-loader
  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. // 打包分析, 打包之后自动生成一个名叫report.html文件(可忽视)
  46. if (IS_PROD) {
  47. config.plugin("webpack-report").use(BundleAnalyzerPlugin, [
  48. {
  49. analyzerMode: "static"
  50. }
  51. ]);
  52. }
  53. },
  54. configureWebpack: config => {
  55. // 开启 gzip 压缩
  56. // 需要 npm i -D compression-webpack-plugin
  57. const plugins = [];
  58. if (IS_PROD) {
  59. plugins.push(
  60. new CompressionWebpackPlugin({
  61. filename: "[path].gz[query]",
  62. algorithm: "gzip",
  63. test: productionGzipExtensions,
  64. threshold: 10240,
  65. minRatio: 0.8
  66. })
  67. );
  68. }
  69. config.plugins = [...config.plugins, ...plugins];
  70. },
  71. css: {
  72. extract: IS_PROD,
  73. requireModuleExtension: false,// 去掉文件名中的 .module
  74. loaderOptions: {
  75. // 给 less-loader 传递 Less.js 相关选项
  76. less: {
  77. // `globalVars` 定义全局对象,可加入全局变量
  78. globalVars: {
  79. primary: '#333'
  80. }
  81. }
  82. }
  83. },
  84. devServer: {
  85. overlay: { // 让浏览器 overlay 同时显示警告和错误
  86. warnings: true,
  87. errors: true
  88. },
  89. host: "localhost",
  90. port: 8080, // 端口号
  91. https: false, // https:{type:Boolean}
  92. open: false, //配置自动启动浏览器
  93. hotOnly: true, // 热更新
  94. // proxy: 'http://localhost:8080' // 配置跨域处理,只有一个代理
  95. proxy: { //配置多个跨域
  96. "/api": {
  97. target: "http://172.11.11.11:7071",
  98. changeOrigin: true,
  99. // ws: true,//websocket支持
  100. secure: false,
  101. pathRewrite: {
  102. "^/api": "/"
  103. }
  104. },
  105. "/api2": {
  106. target: "http://172.12.12.12:2018",
  107. changeOrigin: true,
  108. //ws: true,//websocket支持
  109. secure: false,
  110. pathRewrite: {
  111. "^/api2": "/"
  112. }
  113. },
  114. }
  115. }
  116. }
  117. 复制代码

结语

上述代码可以直接复制,也可以按需引入,一般都用的到,注意里面需要安装的依赖。

  1. cnpm install --save-dev compression-webpack-plugin
  2. 复制代码
  1. cnpm install --save-dev image-webpack-loader

对于vue cli3.0的问题可以参考:https://segmentfault.com/a/1190000016101954