Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

vue.config.js 5.0KB

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