You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

vue.config.js 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. 'use strict'
  2. const path = require('path')
  3. const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
  4. const defaultSettings = require('./src/settings.js')
  5. function resolve(dir) {
  6. return path.join(__dirname, dir)
  7. }
  8. const name = defaultSettings.title || 'vue Admin Template' // page title
  9. const resfulurl = process.env.VUE_APP_BASE_RESFUL
  10. // If your port is set to 80,
  11. // use administrator privileges to execute the command line.
  12. // For example, Mac: sudo npm run
  13. // You can change the port by the following methods:
  14. // port = 9528 npm run dev OR npm run dev --port = 9528
  15. const port = process.env.port || process.env.npm_config_port || 9528 // dev port
  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',
  28. lintOnSave: process.env.NODE_ENV === 'development',
  29. productionSourceMap: false,
  30. devServer: { // 配置服务器
  31. port: 8181,
  32. open: false,
  33. https: false,
  34. host: '',
  35. disableHostCheck: true, //需要做一下内网穿透,代理都配置
  36. proxy: {
  37. '/': {
  38. target: resfulurl, // 目标 API 地址
  39. ws: true, // 是否代理 websockets
  40. changOrigin: true, // 跨域配置
  41. pathRewrite: {
  42. '^/': '/'
  43. }
  44. }
  45. }
  46. },
  47. configureWebpack: {
  48. // provide the app's title in webpack's name field, so that
  49. // it can be accessed in index.html to inject the correct title.
  50. name: name,
  51. resolve: {
  52. alias: {
  53. '@': resolve('src')
  54. }
  55. }
  56. },
  57. chainWebpack(config) {
  58. const plugins = [];
  59. // it can improve the speed of the first screen, it is recommended to turn on preload
  60. config.plugin('preload').tap(() => [
  61. {
  62. rel: 'preload',
  63. // to ignore runtime.js
  64. // https://github.com/vuejs/vue-cli/blob/dev/packages/@vue/cli-service/lib/config/app.js#L171
  65. fileBlacklist: [/\.map$/, /hot-update\.js$/, /runtime\..*\.js$/],
  66. include: 'initial'
  67. }
  68. ])
  69. // when there are many pages, it will cause too many meaningless requests
  70. config.plugins.delete('prefetch')
  71. // set svg-sprite-loader
  72. config.module
  73. .rule('svg')
  74. .exclude.add(resolve('src/icons'))
  75. .end()
  76. config.module
  77. .rule('icons')
  78. .test(/\.svg$/)
  79. .include.add(resolve('src/icons'))
  80. .end()
  81. .use('svg-sprite-loader')
  82. .loader('svg-sprite-loader')
  83. .options({
  84. symbolId: 'icon-[name]'
  85. })
  86. .end()
  87. config
  88. .when(process.env.NODE_ENV !== 'development',
  89. config => {
  90. config
  91. .plugin('ScriptExtHtmlWebpackPlugin')
  92. .after('html')
  93. .use('script-ext-html-webpack-plugin', [{
  94. // `runtime` must same as runtimeChunk name. default is `runtime`
  95. inline: /runtime\..*\.js$/
  96. }])
  97. .end()
  98. config
  99. .optimization.splitChunks({
  100. chunks: 'all',
  101. cacheGroups: {
  102. libs: {
  103. name: 'chunk-libs',
  104. test: /[\\/]node_modules[\\/]/,
  105. priority: 10,
  106. chunks: 'initial' // only package third parties that are initially dependent
  107. },
  108. elementUI: {
  109. name: 'chunk-elementUI', // split elementUI into a single package
  110. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  111. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  112. },
  113. commons: {
  114. name: 'chunk-commons',
  115. test: resolve('src/components'), // can customize your rules
  116. minChunks: 3, // minimum common number
  117. priority: 5,
  118. reuseExistingChunk: true
  119. }
  120. }
  121. })
  122. // https:// webpack.js.org/configuration/optimization/#optimizationruntimechunk
  123. config.optimization.runtimeChunk('single')
  124. }
  125. )
  126. if(process.env.NODE_ENV=='production'){
  127. config.mode = 'production'
  128. config.optimization.minimizer = [
  129. new UglifyJsPlugin({
  130. uglifyOptions: {
  131. // 删除注释
  132. output: {
  133. comments: false
  134. },
  135. // 删除console debugger 删除警告
  136. compress: {
  137. drop_console: true, // console
  138. drop_debugger: false,
  139. pure_funcs: ['console.log']// 移除console
  140. },
  141. warnings: false, // 这样写就不报错
  142. }
  143. })
  144. ]
  145. }
  146. },
  147. }