vue.config.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. const {
  2. defineConfig
  3. } = require('@vue/cli-service')
  4. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  5. // 代码压缩
  6. const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
  7. const path = require('path')
  8. const cdn = {
  9. js: [
  10. `https://cdn.bootcdn.net/ajax/libs/vue/2.6.14/vue.min.js`,
  11. `https://cdn.bootcdn.net/ajax/libs/element-ui/2.15.12/index.min.js`,
  12. `https://cdn.bootcdn.net/ajax/libs/axios/0.17.1/axios.min.js`,
  13. `https://cdn.bootcdn.net/ajax/libs/vue-router/3.5.1/vue-router.min.js`,
  14. ]
  15. };
  16. module.exports = defineConfig({
  17. transpileDependencies: true,
  18. lintOnSave: false, //关闭eslint检查
  19. // 具体使用情况还需要看项目的配置
  20. publicPath: process.env.NODE_ENV === 'production' ? './' : '/',
  21. assetsDir: 'static',
  22. configureWebpack: {
  23. externals: {
  24. vue: 'Vue',
  25. 'vue-router': 'VueRouter',
  26. axios: 'axios',
  27. 'element-ui': 'ELEMENT',
  28. },
  29. plugins: [
  30. new CompressionWebpackPlugin({
  31. algorithm: "gzip",
  32. test: /\.js$|\.html$|\.css$/, // 匹配文件名
  33. threshold: 10240, // 对超过10k的数据压缩
  34. deleteOriginalAssets: false, // 不删除源文件
  35. minRatio: 0.8 // 压缩比
  36. }),
  37. new UglifyJsPlugin({
  38. uglifyOptions: {
  39. compress: {
  40. drop_debugger: true,
  41. drop_console: true,
  42. pure_funcs: ['console.log']
  43. },
  44. output: {
  45. // 去掉注释内容
  46. comments: true
  47. }
  48. },
  49. sourceMap: false,
  50. parallel: true
  51. }),
  52. ],
  53. },
  54. chainWebpack: config => {
  55. config.plugin("html").tap(args => {
  56. args[0].cdn = cdn;
  57. return args;
  58. });
  59. }
  60. })