步骤
找出 RN 项目中所有没有使用到的资源文件:
找出资源文件
最好将图片放到统一的文件夹下(src/img
)
$ find PROJECT_ROOT/src/img -type f > ~/Downloads/org
找出所有代码资源引用
最好使用 require
代替图片 import
$ find . -type f -name "*.ts" -or -name "*.tsx" -or -name "*.js" -not -path "*node_modules*" | xargs grep "require(" > ~/Downloads/key
- 代码有 ts, tsx 和 js
- 使用
grep
获取包含requre(
的行
格式化 1 和 2
格式化 1
org:
./t.png./b/bb.png./a.jpg./c/cc.mp4
去掉头:
$ gsed -i "s/\.\///g" org
排序:
$ sort org -u > org.sorted
格式化 2
key:
file1.ts blablarequire('../img/a.png')adlakfile2.tsx blablarequire('../img/b.jpg') blablafile3.js blablarequire('../img/c/c.png') blabla require('../img/c/a.mp4')
获取关键字符(img/abc.png
):
$ grep -oE "img\/.*?\.(png|jpg|mp4)" key > key2
key2:
img/a.pngimg/b.jpgimg/c/c.pngimg/c/a.mp4
删掉前缀:
$ gsed -i "s/img\///g" key2
排序:
$ sort key2 -u > key2.sorted
diff
$ diff org.sorted key2.sorted > unused.file
unused.file
1d0< .DS_Store8d6< arrow_up.png14d11< ask_icon.png20d16< bang.png25d20< billing_normal.png27d21< bindphone.png31d24< cancel-gray.png34,35d26< chat.png< company/.DS_Store37d27< company/call.png39d28< company/friendCircle.png41d29< company/link.png52d39< company/share.png54d40< company/wechat.png61,63d46< cpy_call.png< cpy_gz.png< cpy_more.png