vue.config.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 || 'vue Element Admin' // 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. },
  39. configureWebpack: {
  40. plugins: [
  41. new CompressionPlugin({
  42. algorithm: 'gzip',
  43. test: /\.js$|\.html$|\.css/, // 匹配文件名
  44. threshold: 10240, // 对超过10kb的数据进行压缩
  45. deleteOriginalAssets: false, // 是否删除原文件
  46. minRatio: 0.8
  47. }),
  48. new MonacoWebpackPlugin()
  49. ],
  50. name: name,
  51. resolve: {
  52. alias: {
  53. '@': resolve('src')
  54. }
  55. }
  56. },
  57. chainWebpack(config) {
  58. // it can improve the speed of the first screen, it is recommended to turn on preload
  59. // config.plugins.delete('preload')
  60. // when there are many pages, it will cause too many meaningless requests
  61. config.plugins.delete('prefetch') //
  62. config.module
  63. .rule('svg')
  64. .exclude.add(resolve('src/icons'))
  65. .end()
  66. config.module
  67. .rule('icons')
  68. .test(/\.svg$/)
  69. .include.add(resolve('src/icons'))
  70. .end()
  71. .use('svg-sprite-loader')
  72. .loader('svg-sprite-loader')
  73. .options({
  74. symbolId: 'icon-[name]'
  75. })
  76. .end()
  77. // set preserveWhitespace
  78. config.module
  79. .rule('vue')
  80. .use('vue-loader')
  81. .loader('vue-loader')
  82. .tap(options => {
  83. options.compilerOptions.preserveWhitespace = true
  84. return options
  85. })
  86. .end()
  87. config
  88. .when(process.env.NODE_ENV === 'development',
  89. config => config.devtool('cheap-source-map')
  90. )
  91. config
  92. .when(process.env.NODE_ENV !== 'development',
  93. config => {
  94. config
  95. .plugin('ScriptExtHtmlWebpackPlugin')
  96. .after('html')
  97. .use('script-ext-html-webpack-plugin', [{
  98. // `runtime` must same as runtimeChunk name. default is `runtime`
  99. inline: /runtime\..*\.js$/
  100. }])
  101. .end()
  102. config
  103. .optimization.splitChunks({
  104. chunks: 'all',
  105. cacheGroups: {
  106. libs: {
  107. name: 'chunk-libs',
  108. test: /[\\/]node_modules[\\/]/,
  109. priority: 10,
  110. chunks: 'initial' // only package third parties that are initially dependent
  111. },
  112. elementUI: {
  113. name: 'chunk-elementUI', // split elementUI into a single package
  114. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  115. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  116. },
  117. commons: {
  118. name: 'chunk-commons',
  119. test: resolve('src/components'), // can customize your rules
  120. minChunks: 3, // minimum common number
  121. priority: 5,
  122. reuseExistingChunk: true
  123. }
  124. }
  125. })
  126. config.optimization.runtimeChunk('single')
  127. }
  128. )
  129. },
  130. css: {
  131. loaderOptions: {
  132. less: {
  133. modifyVars: {
  134. // less vars,customize ant design theme
  135. // 'primary-color': '#F5222D',
  136. // 'link-color': '#F5222D',
  137. 'border-radius-base': '2px'
  138. },
  139. // DO NOT REMOVE THIS LINE
  140. javascriptEnabled: true
  141. }
  142. }
  143. }
  144. }