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.

128 lines
4.2 KiB

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