vue.config.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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. transpileDependencies: [
  19. 'zone.js',
  20. ],
  21. /**
  22. * You will need to set publicPath if you plan to deploy your site under a sub path,
  23. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  24. * then publicPath should be set to "/bar/".
  25. * In most cases please use '/' !!!
  26. * Detail: https://cli.vuejs.org/config/#publicpath
  27. */
  28. publicPath: '/',
  29. outputDir: 'dist',
  30. // assetsDir: '../../static/admin',
  31. // assetsDir: '/',
  32. lintOnSave: process.env.NODE_ENV === 'development',
  33. productionSourceMap: false,
  34. devServer: {
  35. port: port,
  36. open: false,
  37. overlay: {
  38. warnings: false,
  39. errors: true
  40. },
  41. proxy: {
  42. '/re': {
  43. target: 'http://observe-server.cestong.com.cn', // 目标服务器地址
  44. changeOrigin: true, // 是否改变源地址
  45. pathRewrite: {
  46. '^/re': ''// 重写路径
  47. }
  48. },
  49. '/app': {
  50. target: 'http://app.cestong.com.cn',
  51. changeOrigin: true,
  52. pathRewrite: {
  53. '^/app': ''// 重写路径
  54. }
  55. },
  56. '/core': {
  57. target: 'http://observe-front.cestong.com.cn/core',
  58. changeOrigin: true,
  59. pathRewrite: {
  60. '^/core': ''// 重写路径
  61. }
  62. }
  63. }
  64. },
  65. configureWebpack: {
  66. plugins: [
  67. new CompressionPlugin({
  68. cache: false,
  69. algorithm: 'gzip',
  70. test: /\.js$|\.html$|\.css/, // 匹配文件名
  71. threshold: 10240, // 对超过10kb的数据进行压缩
  72. deleteOriginalAssets: false, // 是否删除原文件
  73. minRatio: 0.8
  74. }),
  75. // new MonacoWebpackPlugin()
  76. ],
  77. name: name,
  78. resolve: {
  79. alias: {
  80. '@': resolve('src')
  81. }
  82. }
  83. },
  84. chainWebpack(config) {
  85. // it can improve the speed of the first screen, it is recommended to turn on preload
  86. // config.plugins.delete('preload')
  87. config.module
  88. .rule('js') // 针对 .js 文件
  89. .test(/\.js$/)
  90. .use('babel-loader') // 使用 babel-loader
  91. .tap(options => {
  92. // 合并选项
  93. if (!options) {
  94. options = {};
  95. }
  96. options.presets = (options.presets || []).concat(['@babel/preset-env']);// 使用 Babel 预设
  97. options.plugins = (options.plugins || []).concat([
  98. '@babel/plugin-proposal-class-static-block',
  99. '@babel/plugin-proposal-class-properties'
  100. ]);
  101. return options;
  102. });
  103. // when there are many pages, it will cause too many meaningless requests
  104. config.plugins.delete('prefetch') //
  105. config.module
  106. .rule('svg')
  107. .exclude.add(resolve('src/icons'))
  108. .end()
  109. config.module
  110. .rule('icons')
  111. .test(/\.svg$/)
  112. .include.add(resolve('src/icons'))
  113. .end()
  114. .use('svg-sprite-loader')
  115. .loader('svg-sprite-loader')
  116. .options({
  117. symbolId: 'icon-[name]'
  118. })
  119. .end()
  120. // set preserveWhitespace
  121. config.module
  122. .rule('vue')
  123. .use('vue-loader')
  124. .loader('vue-loader')
  125. .tap(options => {
  126. options.compilerOptions.preserveWhitespace = true
  127. return options
  128. })
  129. .end()
  130. config
  131. .when(process.env.NODE_ENV === 'development',
  132. config => config.devtool('cheap-source-map')
  133. )
  134. config
  135. .when(process.env.NODE_ENV !== 'development',
  136. config => {
  137. config
  138. .plugin('ScriptExtHtmlWebpackPlugin')
  139. .after('html')
  140. .use('script-ext-html-webpack-plugin', [{
  141. // `runtime` must same as runtimeChunk name. default is `runtime`
  142. // inline: /runtime\..*\.js$/ //由于网络报错404,因此注释掉的
  143. }])
  144. .end()
  145. config
  146. .optimization.splitChunks({
  147. chunks: 'all',
  148. cacheGroups: {
  149. libs: {
  150. name: 'chunk-libs',
  151. test: /[\\/]node_modules[\\/]/,
  152. priority: 10,
  153. chunks: 'initial' // only package third parties that are initially dependent
  154. },
  155. elementUI: {
  156. name: 'chunk-elementUI', // split elementUI into a single package
  157. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  158. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  159. },
  160. commons: {
  161. name: 'chunk-commons',
  162. test: resolve('src/components'), // can customize your rules
  163. minChunks: 3, // minimum common number
  164. priority: 5,
  165. reuseExistingChunk: true
  166. }
  167. }
  168. })
  169. config.optimization.runtimeChunk('single')
  170. }
  171. )
  172. config.plugin("html").tap(args => {
  173. // 可以只配置只在生产环境中显示
  174. // if (process.env.NODE_ENV === "production") {
  175. const date = new Date();
  176. args[0].builtTime = date.toLocaleString();
  177. // }
  178. return args;
  179. })
  180. },
  181. css: {
  182. loaderOptions: {
  183. less: {
  184. modifyVars: {
  185. // less vars,customize ant design theme
  186. // 'primary-color': '#F5222D',
  187. // 'link-color': '#F5222D',
  188. 'border-radius-base': '2px'
  189. },
  190. // DO NOT REMOVE THIS LINE
  191. javascriptEnabled: true
  192. },
  193. sass: {
  194. sassOptions: {
  195. outputStyle: 'expanded'
  196. }
  197. }
  198. }
  199. }
  200. }