当前位置: 移动技术网 > IT编程>开发语言>JavaScript > Vue CLI4 Vue.config.js标准配置(最全注释)

Vue CLI4 Vue.config.js标准配置(最全注释)

2020年06月14日  | 移动技术网IT编程  | 我要评论

前言:

vue.js cli工具 不知不觉发展到了4.0时代,cli给人最直白的感受是没有了build文件夹跟config文件夹,所有的配置都在vue.config.js完成。那么该文件的配置至关重要。现在我们来看一下最新配置是怎么配置的。

安装

npm i -d vue-cli-configjs
// vue.config.js
const path = require('path');
const compressionwebpackplugin = require("compression-webpack-plugin"); // 开启gzip压缩, 按需引用
const productiongzipextensions = /\.(js|css|json|txt|html|ico|svg)(\?.*)?$/i; // 开启gzip压缩, 按需写入
const bundleanalyzerplugin = require("webpack-bundle-analyzer").bundleanalyzerplugin; // 打包分析
const is_prod = ['production', 'prod'].includes(process.env.node_env);
const resolve = (dir) => path.join(__dirname, dir);
module.exports = {
  publicpath: process.env.node_env === 'production' ? '/site/vue-demo/' : '/', // 公共路径
  indexpath: '' , // 相对于打包路径的路径
  outputdir: process.env.outputdir || 'dist', // 'dist', 生产环境构建文件的目录
  assetsdir: 'static', // 相对于outputdir的静态资源(js、css、img、fonts)目录
  lintonsave: false, // 是否在开发环境下通过 eslint-loader 在每次保存时 lint 代码
  runtimecompiler: true, // 是否使用包含运行时编译器的 vue 构建版本
  productionsourcemap: !is_prod, // 生产环境的 source map
  parallel: require("os").cpus().length > 1, // 是否为 babel 或 typescript 使用 thread-loader。该选项在系统的 cpu 有多于一个内核时自动启用,仅作用于生产构建。
  pwa: {}, // 向 pwa 插件传递选项。
  chainwebpack: config => {
    config.resolve.symlinks(true); // 修复热更新失效
    // 如果使用多页面打包,使用vue inspect --plugins查看html是否在结果数组中
    config.plugin("html").tap(args => {
      // 修复 lazy loading routes error
      args[0].chunkssortmode = "none";
      return args;
    });
    config.resolve.alias // 添加别名
      .set('@', resolve('src'))
      .set('@assets', resolve('src/assets'))
      .set('@components', resolve('src/components'))
      .set('@views', resolve('src/views'))
      .set('@store', resolve('src/store'));
    // 压缩图片
    // 需要 npm i -d image-webpack-loader
    config.module
      .rule("images")
      .use("image-webpack-loader")
      .loader("image-webpack-loader")
      .options({
        mozjpeg: { progressive: true, quality: 65 },
        optipng: { enabled: false },
        pngquant: { quality: [0.65, 0.9], speed: 4 },
        gifsicle: { interlaced: false },
        webp: { quality: 75 }
      });
    // 打包分析, 打包之后自动生成一个名叫report.html文件(可忽视)
    if (is_prod) {
      config.plugin("webpack-report").use(bundleanalyzerplugin, [
        {
          analyzermode: "static"
        }
      ]);
    }
  },
  configurewebpack: config => {
    // 开启 gzip 压缩
    // 需要 npm i -d compression-webpack-plugin
    const plugins = [];
    if (is_prod) {
      plugins.push(
        new compressionwebpackplugin({
          filename: "[path].gz[query]",
          algorithm: "gzip",
          test: productiongzipextensions,
          threshold: 10240,
          minratio: 0.8
        })
      );
    }
    config.plugins = [...config.plugins, ...plugins];
  },
  css: {
    extract: is_prod,
    requiremoduleextension: false,// 去掉文件名中的 .module
    loaderoptions: {
        // 给 less-loader 传递 less.js 相关选项
        less: {
          // `globalvars` 定义全局对象,可加入全局变量
          globalvars: {
            primary: '#333'
          }
        }
    }
  },
  devserver: {
      overlay: { // 让浏览器 overlay 同时显示警告和错误
       warnings: true,
       errors: true
      },
      host: "localhost",
      port: 8080, // 端口号
      https: false, // https:{type:boolean}
      open: false, //配置自动启动浏览器
      hotonly: true, // 热更新
      // proxy: 'http://localhost:8080'  // 配置跨域处理,只有一个代理
      proxy: { //配置多个跨域
        "/api": {
          target: "http://172.11.11.11:7071",
          changeorigin: true,
          // ws: true,//websocket支持
          secure: false,
          pathrewrite: {
            "^/api": "/"
          }
        },
        "/api2": {
          target: "http://172.12.12.12:2018",
          changeorigin: true,
          //ws: true,//websocket支持
          secure: false,
          pathrewrite: {
            "^/api2": "/"
          }
        },
      }
    }
}

结语

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

cnpm install --save-dev compression-webpack-plugin 
cnpm install --save-dev image-webpack-loader 

到此这篇关于vue cli4 vue.config.js标准配置(最全注释)的文章就介绍到这了,更多相关vue.config.js标准配置内容请搜索移动技术网以前的文章或继续浏览下面的相关文章希望大家以后多多支持移动技术网!,希望大家以后多多支持移动技术网!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
  2020 -06-21 15:58:18
less: { // globalVars 定义全局对象,可加入全局变量 globalVars: { primary: ''#333'' } 这个地方配置只能用于less-loader5.0 如果是安装的less-loader6.0 就会报错
支持: 0 反对: 0 回复
移动技术网