vue.config.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. '/api': {
  40. target: 'http://observe-server.cestong.com.cn',// 目标服务器地址
  41. changeOrigin: true// 是否改变源地址
  42. },
  43. '/app': {
  44. target: 'http://app.cestong.com.cn',
  45. changeOrigin: true,
  46. pathRewrite: {
  47. '^/app': ''// 重写路径
  48. }
  49. }
  50. }
  51. },
  52. configureWebpack: {
  53. plugins: [
  54. new CompressionPlugin({
  55. cache:false,
  56. algorithm: 'gzip',
  57. test: /\.js$|\.html$|\.css/, // 匹配文件名
  58. threshold: 10240, // 对超过10kb的数据进行压缩
  59. deleteOriginalAssets: false, // 是否删除原文件
  60. minRatio: 0.8
  61. })
  62. // new MonacoWebpackPlugin()
  63. ],
  64. name: name,
  65. resolve: {
  66. alias: {
  67. '@': resolve('src')
  68. }
  69. }
  70. },
  71. chainWebpack(config) {
  72. // it can improve the speed of the first screen, it is recommended to turn on preload
  73. // config.plugins.delete('preload')
  74. // when there are many pages, it will cause too many meaningless requests
  75. config.plugins.delete('prefetch') //
  76. config.module
  77. .rule('svg')
  78. .exclude.add(resolve('src/icons'))
  79. .end()
  80. config.module
  81. .rule('icons')
  82. .test(/\.svg$/)
  83. .include.add(resolve('src/icons'))
  84. .end()
  85. .use('svg-sprite-loader')
  86. .loader('svg-sprite-loader')
  87. .options({
  88. symbolId: 'icon-[name]'
  89. })
  90. .end()
  91. // set preserveWhitespace
  92. config.module
  93. .rule('vue')
  94. .use('vue-loader')
  95. .loader('vue-loader')
  96. .tap(options => {
  97. options.compilerOptions.preserveWhitespace = true
  98. return options
  99. })
  100. .end()
  101. config
  102. .when(process.env.NODE_ENV === 'development',
  103. config => config.devtool('cheap-source-map')
  104. )
  105. config
  106. .when(process.env.NODE_ENV !== 'development',
  107. config => {
  108. config
  109. .plugin('ScriptExtHtmlWebpackPlugin')
  110. .after('html')
  111. .use('script-ext-html-webpack-plugin', [{
  112. // `runtime` must same as runtimeChunk name. default is `runtime`
  113. // inline: /runtime\..*\.js$/ //由于网络报错404,因此注释掉的
  114. }])
  115. .end()
  116. config
  117. .optimization.splitChunks({
  118. chunks: 'all',
  119. cacheGroups: {
  120. libs: {
  121. name: 'chunk-libs',
  122. test: /[\\/]node_modules[\\/]/,
  123. priority: 10,
  124. chunks: 'initial' // only package third parties that are initially dependent
  125. },
  126. elementUI: {
  127. name: 'chunk-elementUI', // split elementUI into a single package
  128. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  129. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  130. },
  131. commons: {
  132. name: 'chunk-commons',
  133. test: resolve('src/components'), // can customize your rules
  134. minChunks: 3, // minimum common number
  135. priority: 5,
  136. reuseExistingChunk: true
  137. }
  138. }
  139. })
  140. config.optimization.runtimeChunk('single')
  141. }
  142. )
  143. config.plugin("html").tap(args => {
  144. // 可以只配置只在生产环境中显示
  145. // if (process.env.NODE_ENV === "production") {
  146. const date = new Date();
  147. args[0].builtTime = date.toLocaleString();
  148. // }
  149. return args;
  150. })
  151. },
  152. css: {
  153. loaderOptions: {
  154. less: {
  155. modifyVars: {
  156. // less vars,customize ant design theme
  157. // 'primary-color': '#F5222D',
  158. // 'link-color': '#F5222D',
  159. 'border-radius-base': '2px'
  160. },
  161. // DO NOT REMOVE THIS LINE
  162. javascriptEnabled: true
  163. }
  164. }
  165. }
  166. }