Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

webpack的性能优化 #86

Open
GGXXMM opened this issue Jan 12, 2021 · 0 comments
Open

webpack的性能优化 #86

GGXXMM opened this issue Jan 12, 2021 · 0 comments
Labels
webpack webpack knowledge

Comments

@GGXXMM
Copy link
Owner

GGXXMM commented Jan 12, 2021

一、编译速度构建优化

1、编译速度分析

使用speed-measure-webpack-plugin 分析每个loader和plugin执行耗时,如图所示:
image

2、缩小文件的搜索范围

1)配置loader,通过test、exclude、include缩小搜索范围
2)配置module.noParse,告诉webpack哪些模块不必解析

3、多进程并行压缩代码

4、使用tree shaking擦除无用的JavaScript、CSS代码

5、充分利用缓存提升二次构建的速度

使用上述优化策略,构建速度基本可以提升70~80%左右。例如一个vue项目进行优化,编译速度前后对比如下:
image
image

编译速度提升75%

二、构建体积优化

1、打包内容分析

使用webpack-bundle-analyzer 分析Bundle的每个模块体积大小,如图所示:
image

2、配置external,提取第三方工具包不进行webpack打包

3、开启TreeShaking

三、加载速度优化

1、使用Imagemin,对图片进行压缩

2、限制chunck数量,合理控制http请求

3、使用Scope Hoisting

在线分析工具

  • 生成统计文件 stats.json
    在 webpack 启动时带入参数:--profile --json > stats.json,例如:
webpack --config webpack.config.prod.js --profile --json > stats.json
@GGXXMM GGXXMM added the webpack webpack knowledge label Dec 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
webpack webpack knowledge
Projects
None yet
Development

No branches or pull requests

1 participant