vue.config.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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: process.env.VUE_APP_CLI_PORT,
  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: `${process.env.VUE_APP_BASE_PATH}:${process.env.VUE_APP_SERVER_PORT}/`, // 代理到 目标路径
  27. changeOrigin: true,
  28. pathRewrite: { // 修改路径数据
  29. ['^' + process.env.VUE_APP_BASE_API]: '' // 举例 '^/api:""' 把路径中的/api字符串删除
  30. }
  31. }
  32. }
  33. },
  34. configureWebpack: {
  35. // @路径走src文件夹
  36. resolve: {
  37. alias: {
  38. '@': resolve('src')
  39. }
  40. }
  41. },
  42. chainWebpack(config) {
  43. config
  44. // https://webpack.js.org/configuration/devtool/#development
  45. .when(process.env.NODE_ENV === 'development',
  46. config => config.devtool('cheap-source-map')
  47. )
  48. config
  49. .when(process.env.NODE_ENV !== 'development',
  50. config => {
  51. // 不打包 begin
  52. // 1.目前已经测试通过[vue,axios,echarts]可以cdn引用,其它组件测试通过后可继续添加
  53. // 2.此处添加不打包后,需在public/index.html head中添加相应cdn资源链接
  54. config.set('externals', buildConf.cdns.reduce((p, a) => {
  55. p[a.name] = a.scope
  56. return p
  57. }, {}))
  58. // 不打包 end
  59. config.plugin('html')
  60. .tap(args => {
  61. if (buildConf.title) {
  62. args[0].title = buildConf.title
  63. }
  64. if (buildConf.cdns.length > 0) {
  65. args[0].cdns = buildConf.cdns.map(conf => {
  66. if (conf.path) {
  67. conf.js = `${buildConf.baseCdnUrl}${conf.path}`
  68. } else {
  69. conf.js = `${buildConf.baseCdnUrl}/${conf.name}/${packageConf.dependencies[conf.name].replace('^', '')}/${conf.name}.min.js`
  70. }
  71. return conf
  72. })
  73. }
  74. return args
  75. })
  76. config
  77. .plugin('ScriptExtHtmlWebpackPlugin')
  78. .after('html')
  79. .use('script-ext-html-webpack-plugin', [{
  80. // `runtime` must same as runtimeChunk name. default is `runtime`
  81. inline: /single\..*\.js$/
  82. }])
  83. .end()
  84. config
  85. .optimization.splitChunks({
  86. chunks: 'all',
  87. cacheGroups: {
  88. libs: {
  89. name: 'chunk-libs',
  90. test: /[\\/]node_modules[\\/]/,
  91. priority: 10,
  92. chunks: 'initial' // only package third parties that are initially dependent
  93. },
  94. commons: {
  95. name: 'chunk-commons',
  96. test: resolve('src/components'), // can customize your rules
  97. minChunks: 3, // minimum common number
  98. priority: 5,
  99. reuseExistingChunk: true
  100. }
  101. }
  102. })
  103. config.optimization.runtimeChunk('single')
  104. }
  105. )
  106. }
  107. }