svg-to-png.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * 将 static/icons 下的 tab 相关 SVG 转为 81x81 PNG,供 tabBar 使用
  3. * 运行: node scripts/svg-to-png.js
  4. */
  5. const fs = require('fs');
  6. const path = require('path');
  7. const dir = path.join(__dirname, '..', 'static', 'icons');
  8. const names = [
  9. 'tab-message',
  10. 'tab-message-active',
  11. 'tab-contacts',
  12. 'tab-contacts-active',
  13. 'tab-app',
  14. 'tab-app-active'
  15. ];
  16. async function run() {
  17. let sharp;
  18. try {
  19. sharp = require('sharp');
  20. } catch (e) {
  21. console.error('请先安装 sharp: npm install sharp');
  22. process.exit(1);
  23. }
  24. const size = 81;
  25. for (const name of names) {
  26. const svgPath = path.join(dir, name + '.svg');
  27. const pngPath = path.join(dir, name + '.png');
  28. if (!fs.existsSync(svgPath)) {
  29. console.warn('跳过(不存在):', svgPath);
  30. continue;
  31. }
  32. const svg = fs.readFileSync(svgPath);
  33. await sharp(svg)
  34. .resize(size, size)
  35. .png()
  36. .toFile(pngPath);
  37. console.log('生成:', pngPath);
  38. }
  39. // 同步到构建输出目录,避免 tab 图标不更新
  40. const distDir = path.join(__dirname, '..', 'unpackage', 'dist', 'dev', 'app-plus', 'static', 'icons');
  41. if (fs.existsSync(distDir)) {
  42. for (const name of names) {
  43. const src = path.join(dir, name + '.png');
  44. const dest = path.join(distDir, name + '.png');
  45. if (fs.existsSync(src)) fs.copyFileSync(src, dest);
  46. }
  47. console.log('已同步到 unpackage/dist/dev/app-plus/static/icons/');
  48. }
  49. console.log('完成。');
  50. }
  51. run().catch((err) => {
  52. console.error(err);
  53. process.exit(1);
  54. });