vue.config.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. const path = require('path')
  2. const CompressionPlugin = require('compression-webpack-plugin')
  3. function resolve(dir) {
  4. return path.join(__dirname, dir)
  5. }
  6. const name = '云璞信息'
  7. const port = process.env.port || process.env.npm_config_port || 8080
  8. module.exports = {
  9. publicPath: process.env.NODE_ENV === 'production' ? '/' : '/',
  10. outputDir: 'dist',
  11. assetsDir: 'static',
  12. parallel: false,
  13. lintOnSave: process.env.NODE_ENV === 'development',
  14. productionSourceMap: false,
  15. devServer: {
  16. port: port,
  17. open: true,
  18. host: '0.0.0.0',
  19. // proxy: {
  20. // '/api': {
  21. // target: process.env.VUE_APP_BASE_API || 'http://localhost:3000',
  22. // changeOrigin: true,
  23. // pathRewrite: {
  24. // '^/api': ''
  25. // }
  26. // }
  27. // },
  28. disableHostCheck: true
  29. },
  30. css: {
  31. loaderOptions: {
  32. scss: {
  33. additionalData: `@import "@/assets/styles/variables.scss";`
  34. },
  35. // postcss: {
  36. // plugins: [
  37. // require('postcss-pxtorem')({
  38. // rootValue: 200, // 基于375px设计稿
  39. // propList: ['*'],
  40. // selectorBlackList: ['.no-rem'], // 不转换rem的类名
  41. // minPixelValue: 2
  42. // })
  43. // ]
  44. // }
  45. }
  46. },
  47. configureWebpack: (config) => {
  48. config.name = name
  49. config.resolve.alias = {
  50. ...config.resolve.alias,
  51. '@': resolve('src')
  52. }
  53. if (process.env.NODE_ENV === 'production') {
  54. config.plugins.push(
  55. new CompressionPlugin({
  56. test: /\.(js|css|html|svg)?$/i,
  57. filename: '[path][base].gz',
  58. algorithm: 'gzip',
  59. minRatio: 0.8,
  60. deleteOriginalAssets: false
  61. })
  62. )
  63. }
  64. },
  65. chainWebpack(config) {
  66. // 预加载
  67. config.plugins.delete('preload')
  68. config.plugins.delete('prefetch')
  69. // set svg-sprite-loader
  70. config.module
  71. .rule('svg')
  72. .exclude.add(resolve('src/assets/icons'))
  73. .end()
  74. config.module
  75. .rule('icons')
  76. .test(/\.svg$/)
  77. .include.add(resolve('src/assets/icons'))
  78. .end()
  79. .use('svg-sprite-loader')
  80. .loader('svg-sprite-loader')
  81. .options({
  82. symbolId: 'icon-[name]'
  83. })
  84. // 图片压缩
  85. config.module
  86. .rule('images')
  87. .use('url-loader')
  88. .loader('url-loader')
  89. .tap(options => Object.assign(options, {
  90. limit: 10240
  91. }))
  92. // 生产环境配置
  93. config.when(process.env.NODE_ENV === 'production', config => {
  94. config.optimization.splitChunks({
  95. chunks: 'all',
  96. cacheGroups: {
  97. libs: {
  98. name: 'chunk-libs',
  99. test: /[\\/]node_modules[\\/]/,
  100. priority: 10,
  101. chunks: 'initial'
  102. },
  103. vant: {
  104. name: 'chunk-vant',
  105. test: /[\\/]node_modules[\\/]_?vant(.*)/,
  106. priority: 20
  107. },
  108. commons: {
  109. name: 'chunk-commons',
  110. test: resolve('src/components'),
  111. minChunks: 3,
  112. priority: 5,
  113. reuseExistingChunk: true
  114. }
  115. }
  116. })
  117. config.optimization.runtimeChunk('single')
  118. })
  119. }
  120. }