vue.config.js 5.2 KB

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