放肆青春的博客
首页
前端
算法
网络
面试
技术
后端
运维
杂项
数据库
工具
网址
电脑
个人
文章
  • 分类
  • 标签
  • 归档
github (opens new window)
gitee (opens new window)

放肆青春

一个前端菜鸟的技术成长之路
首页
前端
算法
网络
面试
技术
后端
运维
杂项
数据库
工具
网址
电脑
个人
文章
  • 分类
  • 标签
  • 归档
github (opens new window)
gitee (opens new window)
  • 前端

    • 前端 概览
    • 前端汇总

    • front 博文

    • front 项目总结

    • front 高级

    • front tools

  • vue

    • vue 概览
    • vue 汇总

    • vue 博文

    • vue 项目总结

      • vue 配置
        • vue 配置
          • 去除 console log
          • 图片压缩
        • vue-cli3 配置
      • vue环境
      • vue国际化
      • vue指令
      • vue页面刷新
      • vue混入
      • vue-other
      • vue二维码
      • vue防抖节流
      • vue svg
      • vue图片
      • vue echarts图表
      • vue复制
    • vue 高级

  • html

    • html 概览
    • html 汇总

    • html 博文

  • css

    • css 概览
    • css 汇总

    • css 博文

    • sass

    • less

  • js

    • javascript 概览
    • JS 汇总

    • ES6

    • JS 博文

    • JS 工具

  • node

    • node 概览
    • node 汇总

    • node 框架

    • node 博文

  • react

    • react 概览
    • react 汇总

    • react 博文

    • react 高级

  • 微信小程序

    • 微信小程序 概览
    • 微信小程序总结
    • 微信小程序文章
    • 微信小程序 博文

    • 微信小程序 高级

  • 微信公众号

    • 微信公众号 概览
    • 微信公众号总结
    • 微信公众号文章
  • 多端开发

    • 多端开发
    • dsbridge 概览
    • jsbridge 概览
    • webview
    • uniapp

      • uniapp 概览
    • taro

      • taro 概览
    • flutter

      • flutter 概览
      • flutter 环境搭建
    • electron

      • electron 概览
  • front
放肆青春
2020-07-09

vue 配置

# vue 配置

# 去除 console log

注意:使用 uglifyjs-webpack-plugin 插件打包会报错

  1. 配置optimization.minimizer
if (process.env.VUE_APP_CONSOLE === "true") {
  config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true;
  config.optimization.minimizer[0].options.terserOptions.compress.drop_debugger = true;
}
1
2
3
4
  1. 使用babel-plugin-transform-remove-console

安装:npm install babel-plugin-transform-remove-console --save-dev

配置:babel.config.js

const plugins = ["@vue/babel-plugin-transform-vue-jsx"]
// 生产环境移除console
if(process.env.NODE_ENV === 'production') {
  plugins.push("transform-remove-console")
}
module.exports = {
  plugins: plugins,
  presets: [
    [
      '@vue/app', {
        modules: false,
        targets: {
          browsers: ["> 1%", "last 2 versions", "not ie <= 8", "Android >= 4", "iOS >= 8"]
        },
        useBuiltIns: 'entry',
      }
    ]
  ]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

优点:打包体积小

# 图片压缩

  1. url-loader
chainWebpack: (config) => {
    const imagesRule = config.module.rule("images")
    imagesRule
      .use('url-loader')
        .loader('url-loader')
        .tap(options => Object.assign(options, { limit: 6144 }))
}
1
2
3
4
5
6
7

# vue-cli3 配置

vue.config.js

const UglifyJsPlugin = require("uglifyjs-webpack-plugin");

const path = require("path");
function resolve(dir) {
  return path.join(__dirname, dir);
}

module.exports = {
  // 部署应用包时的基本 URL
  publicPath: process.env.VUE_APP_BASE_URL,
  devServer: {
    /* 自动打开浏览器 */
    open: false,
    port: 8095,
    proxy: {
      "/proxy": {
        target: "https://baidu..com",
        ws: false,
        changeOrigin: true,
        pathRewrite: {
          "^/proxy": "",
        },
      },
      "/cipher/generate": {
        target: "https://baidu..com", // 密钥服务地址 - 测试环境
        ws: false,
        changeOrigin: true,
        // pathRewrite: {
        //   '^/encrype': '/'
        // }
      },
    },
  },
  // 生产环境是否生成 sourceMap 文件
  productionSourceMap: false,
  css: {
    // 开启 CSS source maps?
    sourceMap: false,
    // css预设器配置项
    loaderOptions: {
      sass: {
        prependData: `@import "@/styles/Theme.scss";`,
      },
    },
  },
  // 简单/基础配置,比如引入一个新插件
  configureWebpack: (config) => {
    config.resolve.alias["@asset"] = resolve("src/assets");
    config.resolve.alias["@"] = resolve("src");

    // 删除log
    if (process.env.VUE_APP_CONSOLE === "true") {
      config.optimization.minimizer[0].options.terserOptions.compress.drop_console = true;
      config.optimization.minimizer[0].options.terserOptions.compress.drop_debugger = true;
    }

    // 按照自己项目需要配置
    config["externals"] = {
      vue: "Vue",
      vuex: "Vuex",
      "vue-router": "VueRouter",
      axios: "axios",
      "crypto-js/crypto-js": "CryptoJS",
      "jsencrypt/bin/jsencrypt": "JSEncrypt",
      echarts: "echarts",
      vant: "vant",
      moment: "moment",
      lodash: "_",
      "vue-awesome-swiper": "VueAwesomeSwiper",
    };
  },
  // 链式配置
  chainWebpack: (config) => {
    // 移除 prefetch 插件
    config.plugins.delete("prefetch");
    // 移除 preload 插件
    config.plugins.delete("preload");

    // 替换 svg loader
    const svgRule = config.module.rule("svg");

    // 清除已有的所有 loader。
    // 如果你不这样做,接下来的 loader 会附加在该规则现有的 loader 之后。
    svgRule.uses.clear();

    // 添加要替换的 loader
    svgRule
      .test(/\.svg$/)
      .include.add(resolve("src/icons"))
      .end()
      .use("svg-sprite-loader")
      .loader("svg-sprite-loader")
      .tap((options) => {
        // eslint-disable-next-line no-param-reassign
        options = {
          symbolId: "icon-[name]",
        };
        return options;
      });

    const imagesRule = config.module.rule("images");
    imagesRule
      .use("url-loader")
      .loader("url-loader")
      .tap((options) => Object.assign(options, { limit: 6144 })); // 低于6kb的图片全部被内联,高于6kb的图片会放在单独的img文件夹中
  },
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
更新时间: 4/30/2021, 5:04:21 PM
vue2 hook
vue环境

← vue2 hook vue环境→

最近更新
01
前端权限管理
02-24
02
vue2指令
02-24
03
vue2 hook
02-24
更多文章>
Theme by Vdoing | Copyright © 2019-2022 放肆青春
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式