vue.config.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. const path = require('path')
  2. function resolve(dir) {
  3. return path.join(__dirname, dir)
  4. }
  5. // 导入compression-webpack-plugin
  6. const CompressionWebpackPlugin = require('compression-webpack-plugin')
  7. // 定义压缩文件类型
  8. const productionGzipExtensions = ['js', 'css']
  9. // 导入代码混淆插件
  10. const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
  11. module.exports = {
  12. // publicPath: '/',
  13. assetsDir: 'static',
  14. lintOnSave: true,
  15. // 生产环境是否生成 sourceMap 文件
  16. productionSourceMap: false,
  17. // 开启 CSS source maps?
  18. css: {
  19. sourceMap: true
  20. },
  21. // transpileDependencies: ['swiper', 'dom7', 'element-ui', 'nprogress', 'axios', 'dayjs', 'vue-i18n'],
  22. chainWebpack: config => {
  23. config.resolve.alias
  24. .set('@', resolve('src'))
  25. .set('api', resolve('src/api'))
  26. .set('utils', resolve('src/utils'))
  27. .set('components', resolve('src/components'))
  28. // 图片压缩
  29. config.module.rule('images')
  30. .use('image-webpack-loader')
  31. .loader('image-webpack-loader')
  32. .options({
  33. mozjpeg: { progressive: true, quality: 65 },
  34. optipng: { enabled: false },
  35. pngquant: { quality: '65-90', speed: 4 },
  36. gifsicle: { interlaced: false },
  37. webp: { quality: 75 }
  38. })
  39. // .end()
  40. },
  41. configureWebpack: config => {
  42. if (process.env.NODE_ENV === 'production') {
  43. // 将每个依赖包打包成单独的js文件
  44. const plugins = []
  45. // 生产环境 gzi压缩
  46. plugins.push(
  47. new CompressionWebpackPlugin({
  48. filename: '[path].gz[query]',
  49. algorithm: 'gzip',
  50. test: new RegExp('\\.(' + productionGzipExtensions.join('|') + ')$'),
  51. threshold: 10240,
  52. minRatio: 0.8
  53. })
  54. )
  55. plugins.push(
  56. new UglifyJsPlugin({
  57. uglifyOptions: {
  58. mangle: {
  59. toplevel: true
  60. },
  61. warnings: false,
  62. compress: {
  63. drop_console: true,
  64. drop_debugger: false,
  65. pure_funcs: ['console.log'] // 移除console
  66. }
  67. },
  68. sourceMap: false,
  69. parallel: true
  70. })
  71. )
  72. config.plugins = [
  73. ...config.plugins,
  74. ...plugins
  75. ]
  76. }
  77. }
  78. // {
  79. // test: /\.js$/,
  80. // loader: 'babel-loader',
  81. // include: [
  82. // resolve('src'),
  83. // resolve('test'),
  84. // resolve('node_modules/element-ui/src/mixins/emitter.js'),//<------add
  85. // ]
  86. // }
  87. }