You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

108 lines
4.2 KiB

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