vue.config.js 5.5 KB

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