vue.config.js 5.6 KB

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