vue.config.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. 'use strict'
  2. const path = require('path')
  3. const buildConf = require('./build.config')
  4. const packageConf = require('./package.json')
  5. function resolve(dir) {
  6. return path.join(__dirname, dir)
  7. }
  8. module.exports = {
  9. // 基础配置 详情看文档
  10. publicPath: './',
  11. outputDir: 'dist',
  12. assetsDir: 'static',
  13. lintOnSave: process.env.NODE_ENV === 'development',
  14. productionSourceMap: false,
  15. devServer: {
  16. port: 8180,
  17. open: true,
  18. overlay: {
  19. warnings: false,
  20. errors: true
  21. },
  22. proxy: {
  23. // 把key的路径代理到target位置
  24. // detail: https://cli.vuejs.org/config/#devserver-proxy
  25. [process.env.VUE_APP_BASE_API]: { //需要代理的路径 例如 '/api'
  26. target: `http://127.0.0.1:8889/`, //代理到 目标路径
  27. changeOrigin: true,
  28. // pathRewrite: { // 修改路径数据
  29. // ['^' + process.env.VUE_APP_BASE_API]: '' // 举例 '^/api:""' 把路径中的/api字符串删除
  30. // }
  31. },
  32. '/uploads':{
  33. target: `http://127.0.0.1:8888/`, //代理到 目标路径
  34. changeOrigin: true,
  35. pathRewrite:{
  36. "/uploads" :'/uploads'
  37. }
  38. }
  39. },
  40. },
  41. configureWebpack: {
  42. // @路径走src文件夹
  43. resolve: {
  44. alias: {
  45. '@': resolve('src')
  46. }
  47. }
  48. },
  49. chainWebpack(config) {
  50. // set preserveWhitespace
  51. config.module
  52. .rule('vue')
  53. .use('vue-loader')
  54. .loader('vue-loader')
  55. .tap(options => {
  56. options.compilerOptions.preserveWhitespace = true
  57. return options
  58. })
  59. .end()
  60. config
  61. // https://webpack.js.org/configuration/devtool/#development
  62. .when(process.env.NODE_ENV === 'development',
  63. config => config.devtool('cheap-source-map')
  64. )
  65. config
  66. .when(process.env.NODE_ENV !== 'development',
  67. config => {
  68. // 不打包 begin
  69. // 1.目前已经测试通过[vue,axios,echarts]可以cdn引用,其它组件测试通过后可继续添加
  70. // 2.此处添加不打包后,需在public/index.html head中添加相应cdn资源链接
  71. config.set('externals', buildConf.cdns.reduce((p, a) => {
  72. p[a.name] = a.scope
  73. return p
  74. },{}))
  75. config.plugins.delete('prefetch')
  76. // 不打包 end
  77. config.plugin('html')
  78. .tap(args => {
  79. if(buildConf.title) {
  80. args[0].title = buildConf.title
  81. }
  82. if(buildConf.cdns.length > 0) {
  83. args[0].cdns = buildConf.cdns.map(conf => {
  84. if (conf.path) {
  85. conf.js = `${buildConf.baseCdnUrl}${conf.path}`
  86. } else {
  87. conf.js = `${buildConf.baseCdnUrl}/${conf.name}/${packageConf.dependencies[conf.name].replace('^', '')}/${conf.name}.min.js`
  88. }
  89. return conf
  90. })
  91. }
  92. return args
  93. })
  94. config
  95. .plugin('ScriptExtHtmlWebpackPlugin')
  96. .after('html')
  97. .use('script-ext-html-webpack-plugin', [{
  98. // `runtime` must same as runtimeChunk name. default is `runtime`
  99. inline: /single\..*\.js$/
  100. }])
  101. .end()
  102. config
  103. .optimization.splitChunks({
  104. chunks: 'all',
  105. cacheGroups: {
  106. libs: {
  107. name: 'chunk-libs',
  108. test: /[\\/]node_modules[\\/]/,
  109. priority: 10,
  110. chunks: 'initial' // only package third parties that are initially dependent
  111. },
  112. elementUI: {
  113. name: 'chunk-elementUI', // split elementUI into a single package
  114. priority: 20, // the weight needs to be larger than libs and app or it will be packaged into libs or app
  115. test: /[\\/]node_modules[\\/]_?element-ui(.*)/ // in order to adapt to cnpm
  116. },
  117. commons: {
  118. name: 'chunk-commons',
  119. test: resolve('src/components'), // can customize your rules
  120. minChunks: 3, // minimum common number
  121. priority: 5,
  122. // maxChunks: 30,
  123. maxAsyncRequests: 6,
  124. reuseExistingChunk: true
  125. }
  126. }
  127. })
  128. config.optimization.runtimeChunk('single')
  129. }
  130. )
  131. }
  132. }