vue.config.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/settings.js')
  4. const px2rem = require('postcss-px2rem')
  5. const TerserPlugin = require('terser-webpack-plugin');
  6. const postcss = px2rem({
  7. remUnit: 32, //基准大小 baseSize,需要和rem.js中相同
  8. exclude: /node_modules|folder_name/i
  9. })
  10. function resolve(dir) {
  11. return path.join(__dirname, dir)
  12. }
  13. const name = defaultSettings.title || 'LeenkSee Admin' // page title
  14. // If your port is set to 80,
  15. // use administrator privileges to execute the command line.
  16. // For example, Mac: sudo npm run
  17. // You can change the port by the following method:
  18. // port = 9527 npm run dev OR npm run dev --port = 9527
  19. const port = process.env.port || process.env.npm_config_port || 9527 // dev port
  20. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  21. module.exports = {
  22. /**
  23. * You will need to set publicPath if you plan to deploy your site under a sub path,
  24. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  25. * then publicPath should be set to "/bar/".
  26. * In most cases please use '/' !!!
  27. * Detail: https://cli.vuejs.org/config/#publicpath
  28. */
  29. css: {
  30. loaderOptions: {
  31. postcss: {
  32. plugins: [
  33. postcss
  34. ]
  35. }
  36. }
  37. },
  38. publicPath: '/',
  39. outputDir: 'dist',
  40. assetsDir: 'static',
  41. lintOnSave: process.env.NODE_ENV === 'development',
  42. productionSourceMap: false,
  43. devServer: {
  44. port: port,
  45. open: true,
  46. overlay: {
  47. warnings: false,
  48. errors: true
  49. },
  50. // before: require('./mock/mock-server.js')
  51. },
  52. configureWebpack: {
  53. // provide the app's title in webpack's name field, so that
  54. // it can be accessed in index.html to inject the correct title.
  55. name: name,
  56. resolve: {
  57. alias: {
  58. '@': resolve('src')
  59. }
  60. },
  61. optimization: {
  62. minimize: process.env.NODE_ENV === 'production',
  63. minimizer: [
  64. new TerserPlugin({
  65. terserOptions: {
  66. ecma: undefined,
  67. warnings: false,
  68. parse: {},
  69. compress: {
  70. drop_console: true,
  71. drop_debugger: true,
  72. pure_funcs: ['console.log'], // 移除console
  73. },
  74. },
  75. }),
  76. ],
  77. }
  78. },
  79. // css: {
  80. // loaderOptions: {
  81. // sass: {
  82. // data: `@import "@/styles/mixin.scss";`
  83. // }
  84. // }
  85. // },
  86. chainWebpack(config) {
  87. config.plugins.delete('preload') // TODO: need test
  88. config.plugins.delete('prefetch') // TODO: need test
  89. // set svg-sprite-loader
  90. config.module
  91. .rule('svg')
  92. .exclude.add(resolve('src/icons'))
  93. .end()
  94. config.module
  95. .rule('icons')
  96. .test(/\.svg$/)
  97. .include.add(resolve('src/icons'))
  98. .end()
  99. .use('svg-sprite-loader')
  100. .loader('svg-sprite-loader')
  101. .options({
  102. symbolId: 'icon-[name]'
  103. })
  104. .end()
  105. // set preserveWhitespace
  106. config.module
  107. .rule('vue')
  108. .use('vue-loader')
  109. .loader('vue-loader')
  110. .tap(options => {
  111. options.compilerOptions.preserveWhitespace = true
  112. return options
  113. })
  114. .end()
  115. config
  116. // https://webpack.js.org/configuration/devtool/#development
  117. .when(process.env.NODE_ENV === 'development',
  118. config => config.devtool('cheap-source-map')
  119. )
  120. config
  121. .when(process.env.NODE_ENV !== 'development',
  122. config => {
  123. config
  124. .plugin('ScriptExtHtmlWebpackPlugin')
  125. .after('html')
  126. .use('script-ext-html-webpack-plugin', [{
  127. // `runtime` must same as runtimeChunk name. default is `runtime`
  128. inline: /runtime\..*\.js$/
  129. }])
  130. .end()
  131. config
  132. .optimization.splitChunks({
  133. chunks: 'all',
  134. cacheGroups: {
  135. libs: {
  136. name: 'chunk-libs',
  137. test: /[\\/]node_modules[\\/]/,
  138. priority: 10,
  139. chunks: 'initial' // only package third parties that are initially dependent
  140. },
  141. elementUI: {
  142. name: 'chunk-elementUI', // split elementUI into a single package
  143. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  144. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  145. },
  146. commons: {
  147. name: 'chunk-commons',
  148. test: resolve('src/components'), // can customize your rules
  149. minChunks: 3, // minimum common number
  150. priority: 5,
  151. reuseExistingChunk: true
  152. }
  153. }
  154. })
  155. config.optimization.runtimeChunk('single')
  156. }
  157. )
  158. }
  159. }