惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园_首页
PCI Perspectives
PCI Perspectives
H
Help Net Security
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
雷峰网
雷峰网
S
Secure Thoughts
Jina AI
Jina AI
Attack and Defense Labs
Attack and Defense Labs
大猫的无限游戏
大猫的无限游戏
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
Security @ Cisco Blogs
阮一峰的网络日志
阮一峰的网络日志
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Recent Commits to openclaw:main
Recent Commits to openclaw:main
The Hacker News
The Hacker News
The Last Watchdog
The Last Watchdog
T
Tor Project blog
小众软件
小众软件
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 叶小钗
博客园 - 【当耐特】
G
Google Developers Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
C
Cyber Attacks, Cyber Crime and Cyber Security
C
Cisco Blogs
T
The Blog of Author Tim Ferriss
I
Intezer
S
Schneier on Security
S
Securelist
W
WeLiveSecurity
C
Cybersecurity and Infrastructure Security Agency CISA
P
Palo Alto Networks Blog
Scott Helme
Scott Helme
Project Zero
Project Zero
Google Online Security Blog
Google Online Security Blog
T
Threatpost
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Engineering at Meta
Engineering at Meta
Blog — PlanetScale
Blog — PlanetScale
V
Visual Studio Blog
Last Week in AI
Last Week in AI
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
人人都是产品经理
人人都是产品经理
Y
Y Combinator Blog
A
Arctic Wolf
GbyAI
GbyAI
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
LangChain Blog

博客园 - 冯叶青

OpenAI vs Anthropic API 对比:响应体、请求体、消息格式与工具调用 GitHub Actions 自动部署流程 Git分支自动合并脚本:基于时间戳的冲突解决方案 Next.js lingui.js 多语言自动提取翻译键 - ats-node React实现短信验证码输入组件 Next.js 中优雅地使用 Lottie 动画 Next.js 路由参数更新最佳实践:从 replaceState 到 nextReplaceState Jenkins构建时SWR模块导入报错解决方案 解决 Jenkins 环境下 Lingui 构建报错 "btoa is not defined" React lingui.js 多语言自动提取翻译键 - ast node html2canvas 解决截图空白问题 react 实现前端发版监测 JS根据文件名获取文件类型 JS获取本机IP地址 适用于react、vue菜单格式化工具函数 git 内容提交 实现大图拦截功能 JS实现视频截图 next.js 利用中间件(middleware.ts)实现PC与移动路由无缝切换 Android生成签名文件及对apk进行签名 JS-SDK 配置,实现微信分享功能
git 提交 实现大图自动压缩功能
冯叶青 · 2024-12-11 · via 博客园 - 冯叶青

场景

在一般的网页开发过程中,往往我们都要下载较多的切图,而这些切图未必都做了压缩,即使做了压缩,效果未必能达到理想效果。

要解决这种图片压缩问题,途径有很多

1. 手动将图片丢到xx站点,压缩好之后再放进项目

2. 让UI给已经压缩好的图

这些方式都得靠人工操作,人工操作往往存在一些不确定性,比如忘记了要压缩,也察觉不出来,如果是多人开发,问题会更大。下面我将列举一些常规的解决方案

1. 当代码提交时,拦截大图的提交,提醒开发人员手动压缩(之前博文有实现过)

2. 当提交代码时,主动压缩大图并自动提交(本期讨论重点)

本期将解决方案2展开讨论:主动压缩大图并自动提交

1.安装 Husky:

确保你已经在项目中安装了 Husky。如果还没有安装,可以使用以下命令:

npm install husky --save-dev

2.启用 Git 钩子:

运行以下命令以启用 Husky 钩子:

3.添加 pre-commit 钩子:

使用 Husky 添加 pre-commit 钩子:

npx husky add .husky/pre-commit "npx lint-staged"

4.安装 lint-staged:

为了配合 Husky,安装 lint-staged 来处理文件:

npm install lint-staged --save-dev

5.配置 lint-staged:

package.json 中添加 lint-staged 配置:

{
  "lint-staged": {
    "**/*.{weapp,jpg,png}": "node scripts/compress-images.mjs"
  }
}

6.编写图片压缩脚本(tinify):

安装相关依赖

pnpm install tinify fs-extra glob --save-dev

在项目中创建一个 scripts 文件夹,并在其中添加 compress-images.mjs 文件,内容如下:

程序所包含的逻辑

1. 压缩使用的是tinify压缩工具,官网 https://tinypng.com/

2. 它只压缩图片大于300kb的图

3. 压缩的图将覆盖原图

4. 只有在 git commit -m 'xx' 时会触发图片压缩

import tinify from 'tinify';
import fs from 'fs-extra';

// 设置你的 Tinify API key
tinify.key = 'xxx';

const SIZE_LIMIT = 300 * 1024; // 300KB

async function compressImage(filePath) {
  try {
    const stats = await fs.stat(filePath);

    // 检查文件大小和压缩标记
    if (stats.size < SIZE_LIMIT) {
      console.log(`跳过 ${filePath} (小于300KB)`);
      return;
    }
    console.log(`压缩中: ${filePath}`);

    // 使用 tinify 压缩图片
    const source = tinify.fromFile(filePath);
    await source.toFile(filePath);


    console.log(`压缩完成: ${filePath}`);
  } catch (error) {
    console.error(`压缩失败 ${filePath}:`, error);
  }
}

async function main() {
  try {
    // 获取待提交的文件列表
    const files = process.argv.slice(2);
    console.log(`找到 ${files.length} 个图片文件`);

    // 并发压缩图片
    await Promise.all(files.map(compressImage));

    console.log('所有图片压缩完成!');
  } catch (error) {
    console.error('发生错误:', error);
    process.exit(1);
  }
}

main();

其他场景

有时候你可能需要再已提交的图片中压缩图片,只需要运行一行命令就能处理整个项目的图片压缩

在package.josn 中 的scripts添加命令

"scripts": {
  "compress": "node scripts/compress-images.mjs"
}