vue.config.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. config.plugins.delete('preload') // TODO: need test
  59. config.plugins.delete('prefetch') // TODO: need test
  60. config.module
  61. .rule('svg')
  62. .exclude.add(resolve('src/icons'))
  63. .end()
  64. config.module
  65. .rule('icons')
  66. .test(/\.svg$/)
  67. .include.add(resolve('src/icons'))
  68. .end()
  69. .use('svg-sprite-loader')
  70. .loader('svg-sprite-loader')
  71. .options({
  72. symbolId: 'icon-[name]'
  73. })
  74. .end()
  75. // set preserveWhitespace
  76. config.module
  77. .rule('vue')
  78. .use('vue-loader')
  79. .loader('vue-loader')
  80. .tap(options => {
  81. options.compilerOptions.preserveWhitespace = true
  82. return options
  83. })
  84. .end()
  85. config
  86. .when(process.env.NODE_ENV === 'development',
  87. config => config.devtool('cheap-source-map')
  88. )
  89. config
  90. .when(process.env.NODE_ENV !== 'development',
  91. config => {
  92. config
  93. .plugin('ScriptExtHtmlWebpackPlugin')
  94. .after('html')
  95. .use('script-ext-html-webpack-plugin', [{
  96. // `runtime` must same as runtimeChunk name. default is `runtime`
  97. inline: /runtime\..*\.js$/
  98. }])
  99. .end()
  100. config
  101. .optimization.splitChunks({
  102. chunks: 'all',
  103. cacheGroups: {
  104. libs: {
  105. name: 'chunk-libs',
  106. test: /[\\/]node_modules[\\/]/,
  107. priority: 10,
  108. chunks: 'initial' // only package third parties that are initially dependent
  109. },
  110. elementUI: {
  111. name: 'chunk-elementUI', // split elementUI into a single package
  112. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  113. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  114. },
  115. commons: {
  116. name: 'chunk-commons',
  117. test: resolve('src/components'), // can customize your rules
  118. minChunks: 3, // minimum common number
  119. priority: 5,
  120. reuseExistingChunk: true
  121. }
  122. }
  123. })
  124. config.optimization.runtimeChunk('single')
  125. }
  126. )
  127. },
  128. css: {
  129. loaderOptions: {
  130. less: {
  131. modifyVars: {
  132. // less vars,customize ant design theme
  133. // 'primary-color': '#F5222D',
  134. // 'link-color': '#F5222D',
  135. 'border-radius-base': '2px'
  136. },
  137. // DO NOT REMOVE THIS LINE
  138. javascriptEnabled: true
  139. }
  140. }
  141. }
  142. }