vue.config.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. 'use strict'
  2. const path = require('path')
  3. const defaultSettings = require('./src/settings.js')
  4. function resolve(dir) {
  5. return path.join(__dirname, dir)
  6. }
  7. const name = defaultSettings.title || 'vue Admin Template' // page title
  8. // If your port is set to 80,
  9. // use administrator privileges to execute the command line.
  10. // For example, Mac: sudo npm run
  11. // You can change the port by the following methods:
  12. // port = 9528 npm run dev OR npm run dev --port = 9528
  13. const port = process.env.port || process.env.npm_config_port || 9527 // dev port
  14. const Version = new Date().getTime()
  15. // All configuration item explanations can be find in https://cli.vuejs.org/config/
  16. module.exports = {
  17. /**
  18. * You will need to set publicPath if you plan to deploy your site under a sub path,
  19. * for example GitHub Pages. If you plan to deploy your site to https://foo.github.io/bar/,
  20. * then publicPath should be set to "/bar/".
  21. * In most cases please use '/' !!!
  22. * Detail: https://cli.vuejs.org/config/#publicpath
  23. */
  24. publicPath: '/',
  25. outputDir: 'dist',
  26. assetsDir: 'assets',
  27. // lintOnSave: process.env.NODE_ENV === 'development',
  28. lintOnSave: false,
  29. productionSourceMap: false,
  30. pages: {
  31. index: {
  32. entry: './src/main.js',
  33. template: './public/index.html',
  34. filename: 'index.html'
  35. }
  36. },
  37. devServer: {
  38. port: port,
  39. open: false,
  40. overlay: {
  41. warnings: false,
  42. errors: true
  43. },
  44. proxy: {
  45. // change xxx-api/login => mock/login
  46. // detail: https://cli.vuejs.org/config/#devserver-proxy
  47. [process.env.VUE_APP_BASE_API]: {
  48. // target: `http://127.0.0.1:${port}/mock`,
  49. target: 'http://localhost:8888',
  50. changeOrigin: true,
  51. pathRewrite: {
  52. ['^' + process.env.VUE_APP_BASE_API]: ''
  53. }
  54. }
  55. }
  56. },
  57. configureWebpack: {
  58. devtool: 'source-map',
  59. // provide the app's title in webpack's name field, so that
  60. // it can be accessed in index.html to inject the correct title.
  61. name: name,
  62. resolve: {
  63. alias: {
  64. '@': resolve('src')
  65. }
  66. },
  67. output: { // 输出重构 打包编译后的 文件名称 【模块名称.版本号.时间戳】
  68. // filename: utils.assetsPath('js/[name].[chunkhash].'+Version+'js'),
  69. // chunkFilename: utils.assetsPath('js/[id].[chunkhash].'+Version+'js')
  70. filename: 'static/js/[name].${Version}.js', // js打包文件,添加时间戳
  71. chunkFilename: 'static/js/[name].${Version}.js'
  72. }
  73. },
  74. chainWebpack(config) {
  75. config.plugins.delete('preload') // TODO: need test
  76. config.plugins.delete('prefetch') // TODO: need test
  77. // set svg-sprite-loader
  78. config.module
  79. .rule('svg')
  80. .exclude.add(resolve('src/icons'))
  81. .end()
  82. config.module
  83. .rule('icons')
  84. .test(/\.svg$/)
  85. .include.add(resolve('src/icons'))
  86. .end()
  87. .use('svg-sprite-loader')
  88. .loader('svg-sprite-loader')
  89. .options({
  90. symbolId: 'icon-[name]'
  91. })
  92. .end()
  93. // set preserveWhitespace
  94. config.module
  95. .rule('vue')
  96. .use('vue-loader')
  97. .loader('vue-loader')
  98. .tap(options => {
  99. options.compilerOptions.preserveWhitespace = true
  100. return options
  101. })
  102. .end()
  103. // img的文件名修改 // img打包文件,添加时间戳
  104. config.module
  105. .rule('images')
  106. .use('url-loader')
  107. .tap(options => {
  108. options.name = `static/img/[name].${Version}.[ext]`
  109. options.fallback = {
  110. loader: 'file-loader',
  111. options: {
  112. name: `static/img/[name].${Version}.[ext]`
  113. }
  114. }
  115. return options
  116. })
  117. config
  118. // https://webpack.js.org/configuration/devtool/#development
  119. .when(process.env.NODE_ENV === 'development',
  120. config => config.devtool('cheap-source-map')
  121. )
  122. config
  123. .when(process.env.NODE_ENV !== 'development',
  124. config => {
  125. config
  126. .plugin('ScriptExtHtmlWebpackPlugin')
  127. .after('html')
  128. .use('script-ext-html-webpack-plugin', [{
  129. // `runtime` must same as runtimeChunk name. default is `runtime`
  130. inline: /runtime\..*\.js$/
  131. }])
  132. .end()
  133. /* config
  134. .optimization.splitChunks({
  135. chunks: 'all',
  136. cacheGroups: {
  137. libs: {
  138. name: 'chunk-libs',
  139. test: /[\\/]node_modules[\\/]/,
  140. priority: 10,
  141. chunks: 'initial' // only package third parties that are initially dependent
  142. },
  143. elementUI: {
  144. name: 'chunk-elementUI', // split elementUI into a single package
  145. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  146. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  147. },
  148. commons: {
  149. name: 'chunk-commons',
  150. test: resolve('src/main/frontend/components'), // can customize your rules
  151. minChunks: 3, // minimum common number
  152. priority: 5,
  153. reuseExistingChunk: true
  154. }
  155. }
  156. })
  157. config.optimization.runtimeChunk('single')*/
  158. }
  159. )
  160. }
  161. }