vue.config.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. 'use strict'
  2. const path = require('path')
  3. const CompressionPlugin = require('compression-webpack-plugin')// 引入gzip压缩插件
  4. const defaultSettings = require('./src/settings.js')
  5. function resolve(dir) {
  6. return path.join(__dirname, dir)
  7. }
  8. const name = defaultSettings.title || 'go-dmin' // page title
  9. // If your port is set to 80,
  10. // use administrator privileges to execute the command line.
  11. // For example, Mac: sudo npm run
  12. // You can change the port by the following method:
  13. // port = 9527 npm run dev OR npm run dev --port = 9527
  14. const port = process.env.port || process.env.npm_config_port || 9527 // dev port
  15. // const MonacoWebpackPlugin = require('monaco-editor-webpack-plugin')
  16. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  17. module.exports = {
  18. /**
  19. * You will need to set publicPath if you plan to deploy your site under a sub path,
  20. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  21. * then publicPath should be set to "/bar/".
  22. * In most cases please use '/' !!!
  23. * Detail: https://cli.vuejs.org/config/#publicpath
  24. */
  25. publicPath: '/',
  26. outputDir: 'dist',
  27. // assetsDir: '../../static/admin',
  28. // assetsDir: '/',
  29. lintOnSave: process.env.NODE_ENV === 'development',
  30. productionSourceMap: false,
  31. devServer: {
  32. port: port,
  33. open: false,
  34. overlay: {
  35. warnings: false,
  36. errors: true
  37. },
  38. proxy: {
  39. '/re': {
  40. target: 'http://observe-server.cestong.com.cn',// 目标服务器地址
  41. changeOrigin: true, // 是否改变源地址
  42. pathRewrite: {
  43. '^/re': ''// 重写路径
  44. }
  45. },
  46. '/app': {
  47. target: 'http://app.cestong.com.cn',
  48. changeOrigin: true,
  49. pathRewrite: {
  50. '^/app': ''// 重写路径
  51. }
  52. }
  53. }
  54. },
  55. configureWebpack: {
  56. plugins: [
  57. new CompressionPlugin({
  58. cache:false,
  59. algorithm: 'gzip',
  60. test: /\.js$|\.html$|\.css/, // 匹配文件名
  61. threshold: 10240, // 对超过10kb的数据进行压缩
  62. deleteOriginalAssets: false, // 是否删除原文件
  63. minRatio: 0.8
  64. })
  65. // new MonacoWebpackPlugin()
  66. ],
  67. name: name,
  68. resolve: {
  69. alias: {
  70. '@': resolve('src')
  71. }
  72. }
  73. },
  74. chainWebpack(config) {
  75. // it can improve the speed of the first screen, it is recommended to turn on preload
  76. // config.plugins.delete('preload')
  77. // when there are many pages, it will cause too many meaningless requests
  78. config.plugins.delete('prefetch') //
  79. config.module
  80. .rule('svg')
  81. .exclude.add(resolve('src/icons'))
  82. .end()
  83. config.module
  84. .rule('icons')
  85. .test(/\.svg$/)
  86. .include.add(resolve('src/icons'))
  87. .end()
  88. .use('svg-sprite-loader')
  89. .loader('svg-sprite-loader')
  90. .options({
  91. symbolId: 'icon-[name]'
  92. })
  93. .end()
  94. // set preserveWhitespace
  95. config.module
  96. .rule('vue')
  97. .use('vue-loader')
  98. .loader('vue-loader')
  99. .tap(options => {
  100. options.compilerOptions.preserveWhitespace = true
  101. return options
  102. })
  103. .end()
  104. config
  105. .when(process.env.NODE_ENV === 'development',
  106. config => config.devtool('cheap-source-map')
  107. )
  108. config
  109. .when(process.env.NODE_ENV !== 'development',
  110. config => {
  111. config
  112. .plugin('ScriptExtHtmlWebpackPlugin')
  113. .after('html')
  114. .use('script-ext-html-webpack-plugin', [{
  115. // `runtime` must same as runtimeChunk name. default is `runtime`
  116. // inline: /runtime\..*\.js$/ //由于网络报错404,因此注释掉的
  117. }])
  118. .end()
  119. config
  120. .optimization.splitChunks({
  121. chunks: 'all',
  122. cacheGroups: {
  123. libs: {
  124. name: 'chunk-libs',
  125. test: /[\\/]node_modules[\\/]/,
  126. priority: 10,
  127. chunks: 'initial' // only package third parties that are initially dependent
  128. },
  129. elementUI: {
  130. name: 'chunk-elementUI', // split elementUI into a single package
  131. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  132. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  133. },
  134. commons: {
  135. name: 'chunk-commons',
  136. test: resolve('src/components'), // can customize your rules
  137. minChunks: 3, // minimum common number
  138. priority: 5,
  139. reuseExistingChunk: true
  140. }
  141. }
  142. })
  143. config.optimization.runtimeChunk('single')
  144. }
  145. )
  146. config.plugin("html").tap(args => {
  147. // 可以只配置只在生产环境中显示
  148. // if (process.env.NODE_ENV === "production") {
  149. const date = new Date();
  150. args[0].builtTime = date.toLocaleString();
  151. // }
  152. return args;
  153. })
  154. },
  155. css: {
  156. loaderOptions: {
  157. less: {
  158. modifyVars: {
  159. // less vars,customize ant design theme
  160. // 'primary-color': '#F5222D',
  161. // 'link-color': '#F5222D',
  162. 'border-radius-base': '2px'
  163. },
  164. // DO NOT REMOVE THIS LINE
  165. javascriptEnabled: true
  166. }
  167. }
  168. }
  169. }