vue.config.js 3.7 KB

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