vue.config.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. 'use strict'
  2. const path = require('path')
  3. const webpack = require('webpack')
  4. const { defineConfig } = require('@vue/cli-service')
  5. const name = process.env.VUE_APP_TITLE || '中正企业信息' // 网页标题
  6. const CompressionWebpackPlugin = require("compression-webpack-plugin");
  7. function resolve(dir) {
  8. return path.join(__dirname, dir)
  9. }
  10. module.exports = defineConfig({
  11. transpileDependencies: true,
  12. lintOnSave: false,
  13. devServer: {
  14. host: '0.0.0.0',
  15. port: 5099,
  16. open: true,
  17. },
  18. configureWebpack: {
  19. output: { // 输出重构 打包编译后的 文件名称 【模块名称.版本号.时间戳】
  20. filename: `js/[name].[hash].js`,
  21. chunkFilename: `js/[id].[hash].js`
  22. },
  23. name: name,
  24. // 生产环境,则添加不参与打包的包名和依赖包的名称
  25. externals: {
  26. 'vue': 'Vue',
  27. 'axios': 'axios',
  28. 'element-ui': 'ELEMENT',
  29. 'vue-router': 'VueRouter',
  30. },
  31. resolve: {
  32. alias: {
  33. '@': resolve('src')
  34. }
  35. }
  36. },
  37. chainWebpack(config) {
  38. // 生产环境,开启js\css压缩
  39. if (process.env.NODE_ENV !== 'development') {
  40. config.plugin('compressionPlugin').use(new CompressionWebpackPlugin({
  41. test: /\.(js|css|less|map)$/, // 匹配文件名
  42. threshold: 1024, // 对超过10k的数据压缩
  43. minRatio: 0.8,
  44. }))
  45. }
  46. config.plugin('provide').use(webpack.ProvidePlugin, [{
  47. 'window.Quill': 'quill'
  48. }])
  49. config.module
  50. .rule('svg')
  51. .exclude.add(resolve('src/assets/icons'))
  52. .end()
  53. config.module
  54. .rule('icons')
  55. .test(/\.svg$/)
  56. .include.add(resolve('src/assets/icons'))
  57. .end()
  58. .use('svg-sprite-loader')
  59. .loader('svg-sprite-loader')
  60. .options({
  61. symbolId: 'icon-[name]'
  62. })
  63. .end()
  64. config
  65. .when(process.env.NODE_ENV !== 'development',
  66. config => {
  67. config
  68. .plugin('ScriptExtHtmlWebpackPlugin')
  69. .after('html')
  70. .use('script-ext-html-webpack-plugin', [{
  71. // `runtime` must same as runtimeChunk name. default is `runtime`
  72. inline: /runtime\..*\.js$/
  73. }])
  74. .end()
  75. config
  76. .optimization.splitChunks({
  77. chunks: 'all',
  78. minSize: 1005000,
  79. maxSize: 3005000,
  80. cacheGroups: {
  81. libs: {
  82. name: 'chunk-libs',
  83. test: /[\\/]node_modules[\\/]/,
  84. priority: 10,
  85. chunks: 'initial' // only package third parties that are initially dependent
  86. },
  87. elementUI: {
  88. name: 'chunk-elementUI', // split elementUI into a single package
  89. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  90. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  91. },
  92. commons: {
  93. name: 'chunk-commons',
  94. test: resolve('src/components'), // can customize your rules
  95. minChunks: 3, // minimum common number
  96. priority: 5,
  97. reuseExistingChunk: true
  98. }
  99. }
  100. })
  101. config.optimization.runtimeChunk('single'),
  102. config.optimization.providedExports = true,
  103. config.optimization.usedExports = true,
  104. config.optimization.innerGraph = true,
  105. config.optimization.sideEffects = true,
  106. {
  107. from: path.resolve(__dirname, './public/robots.txt'), //防爬虫文件
  108. to: './' //到根目录下
  109. }
  110. }
  111. )
  112. }
  113. })