前端优化--webpack等

104 篇文章 0 订阅
  • https://juejin.cn/post/6844904150988226574

splitChunks

const path = require('path')
const BundleAnalyzerPlugin = require('webpack-bundle-analyzer').BundleAnalyzerPlugin

// const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
function resolve (dir) {
  return path.join(__dirname, dir)
}
module.exports = {
  productionSourceMap: false,
  lintOnSave: false,
  devServer: {
    port: '8080',
    open: true,
    hot: true,
    disableHostCheck: true,
    clientLogLevel: 'warning',
    overlay: {
      warnings: false,
      errors: true
    }
  },
  configureWebpack: {
    devtool: 'source-map',
    name: 'dfdf',
    resolve: {
      alias: {
        '@': resolve('src')
      }
    },
    plugins: [
      // new UglifyJsPlugin()
    ]
  },
  chainWebpack: config => {
    // config.plugin('BundleAnalyzerPlugin').use(BundleAnalyzerPlugin).tap(() => [
    //   {
    //     rel: 'BundleAnalyzerPlugin',
    //     analyzerMode: 'server', // 'server': 启动端口服务;'static': 生成 report.html;'disabled': 配合 generateStatsFile 使用;
    //     generateStatsFile: false, // 是否生成stats.json文件
    //     analyzerHost: '127.0.0.1',
    //     analyzerPort: '8877',
    //     reportFilename: 'report.html',
    //     defaultSizes: 'parsed',
    //     openAnalyzer: false,
    //     statsFilename: 'stats.json',
    //     statsOptions: null,
    //     excludeAssets: null
    //   }])
    // 分包处理--加快前端速度
    config.optimization.splitChunks({
      chunks: 'all',
      cacheGroups: {
        html2canvas: {
          name: 'chunk-html2canvas',
          test: /[/]node_modules[/]_?html2canvas(.*)/,
          priority: 10
        },
        swiper: {
          name: 'chunk-swiper',
          test: /[/]node_modules[/]_?swiper(.*)/,
          priority: 10
        },
        vant: {
          name: 'chunk-vant',
          priority: 20,
          // 正则规则验证,如果符合就提取 chunk
          test: /[/]node_modules[/]_?vant(.*)/
        },
        commons: {
          name: 'chunk-commons', // 要缓存的 分隔出来的 chunk 名称
          minChunks: 2, // 最小 chunk ,默认1
          priority: 5, // 缓存组优先级
          chunks: 'initial', // 必须三选一: "initial" | "all" | "async"(默认就是async)
          reuseExistingChunk: true// 可设置是否重用该chunk
        },
        corejs: {
          name: 'chunk-corejs',
          test: /[/]node_modules[/]_?core-js(.*)/,
          priority: 10
        }
      }
    })
    // 添加新的svg-sprite-loader处理svgIcon
    config.module
      .rule('svgIcon')
      .test(/\.svg$/)
      .include
      .add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader') // 一定要添加use
      .loader('svg-sprite-loader')
      .tap(options => {
        options = {
          symbolId: 'icon-[name]'
        }
        return options
      })

    // 原有的svg图像处理loader添加exclude
    config.module
      .rule('svg')
      .exclude
      .add(resolve('src/icons'))
      .end()
    config
      .plugin('html')
      .tap(args => {
        args[0].title = '珍客企微版'
        return args
      })
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值