背景
我们在执行 yarn start/build
的时候会实时检测打包更新,有了缓存之后可以对一些没有变换的文件不进行更新,大大节省了打包速度。
缓存类型
metro 的缓存实现在 node_modules/metro-cache
中,主要有两种缓存机制:
- 服务端缓存:HttpStore
- 本地缓存:FileStore
服务端主要是通过服务器来缓存相关内容,优势是不用担心缓存的大小和时间限制,可以灵活的设置缓存策略,不过这个我们目前没有使用到,后期可以考虑。
metro 默认的缓存机制是 FileStore,这也是我们目前使用的缓存机制,FileStore 实际上就是将编译转换后的文件缓存起来,以便下一次编译的时候能够避免重复转换,加快编译速度。
FileStore缓存
缓存地址为:echo $TMPDIR
拼接上 /metro-cache
缓存导致的问题
执行 yarn start
yarn ios
可能会有缓存问题
- 团队有人修改了依赖库,有的人就会报错
yarn ios
打包报错找不到 metro-cahce/T/xx 文件
清除缓存
1、重启电脑
2、删除文件夹: echo $TMPDIR
拼接上 /metro-cache
3、react-native start --reset-cache
4、ignite 脚手架提供清缓存命令,具体可以看这个包 https://github.com/subratamal/react-native-clean-project
参考资料
https://github.com/facebook/metro
https://www.jianshu.com/p/52620bc4b728