vue.config.js 5.5 KB

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