vue.config.js 5.6 KB

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