gulp-imagemin版本9图片压缩

由于网上大多数的博文已经比较久,参考性不大

版本

  • gulp

    PS D:\XXX\github\hexo> gulp -v
    CLI version: 2.3.0
    Local version: 4.0.2
    
  • gulp-imagemin

    9.0.0
    
  • node

    PS D:\XXX\github\hexo> node -v
    v20.10.0
    PS D:\XXX\github\hexo> npm -v
    10.2.3
    PS D:\XXX\github\hexo>
    

gulpfile.js

import gulp from 'gulp';
import imagemin from 'gulp-imagemin';
let imgSrc = 'source/images/*.*'
let imgDist = 'source/images2'
// 压缩图片
export default (cb) => {
	return gulp.src([imgSrc])
	.pipe(imagemin())
    .pipe(gulp.dest(imgDist));
}

如果需要对某种类型进行配置

import gulp from 'gulp';
import imagemin, {optipng} from 'gulp-imagemin';
let imgSrc = 'source/images/*.*'
let imgDist = 'source/images2'
// 压缩图片
export default (cb) => {
	return gulp.src([imgSrc])
	.pipe(imagemin(optipng({optimizationLevel: 5}))) 
    .pipe(gulp.dest(imgDist));
}

如果希望已经压缩过的图片不再次压缩,可以结合gulp-cache

import gulp from 'gulp';
import imagemin from 'gulp-imagemin';
import cache from 'gulp-cache';
let imgSrc = 'source/images3/*.*'
let imgDist = 'source/images2'
// 压缩图片
export default (cb) => {
	return gulp.src([imgSrc])
	.pipe(cache(imagemin()))
    .pipe(gulp.dest(imgDist));
}

输出对比结果

// 第一次执行
PS D:\XXX\github\hexo> gulp
[16:55:41] Using gulpfile D:\XXX\github\hexo\gulpfile.js
[16:55:41] Starting 'default'...
gulp-imagemin: Minified 1 image (saved 116 kB - 8.7%)
[16:55:48] Finished 'default' after 7.23 s

// 再次执行命令
PS D:\XXX\github\hexo> gulp
[16:55:55] Using gulpfile D:\XXX\github\hexo\gulpfile.js
[16:55:55] Starting 'default'...
gulp-imagemin: Minified 0 images
[16:55:55] Finished 'default' after 98 ms

新增一个图片测试

PS D:\XXX\github\hexo> gulp
[16:58:15] Using gulpfile D:\XXX\github\hexo\gulpfile.js
[16:58:15] Starting 'default'...
gulp-imagemin: Minified 1 image (saved 208 kB - 40.4%)
[16:58:20] Finished 'default' after 5 s

这里发现一个问题是,有些压缩效果并不明显

gulp-imagemin: Minified 1 image (saved 116 kB - 8.7%)
[16:55:48] Finished 'default' after 7.23 s

但如果你上传到tinypng, 压缩了差不多一半

遇到问题

  • Cannot use import statement outside a module

    D:\XXX\github\hexo\gulpfile.js:17
    import gulp from 'gulp';
    ^^^^^^
    
    SyntaxError: Cannot use import statement outside a module
        at Object.compileFunction (node:vm:352:18)
        at wrapSafe (node:internal/modules/cjs/loader:1033:15)
        at Module._compile (node:internal/modules/cjs/loader:1069:27)
        at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
        at Module.load (node:internal/modules/cjs/loader:981:32)
        at Function.Module._load (node:internal/modules/cjs/loader:822:12)
        at Module.require (node:internal/modules/cjs/loader:1005:19)
        at require (node:internal/modules/cjs/helpers:102:18)
    

    需要在package.json加上"type": "module"

    {
        "name": "hexo-site",
        "version": "0.0.0",
        "private": true,
        "type": "module",
        "devDependencies": {
            "gulp": "^4.0.2",
            "gulp-imagemin": "^9.0.0"
        }
    }
    
  • 一、Cannot find module 'optipng-bin'

    er Error: Cannot find module 'optipng-bin'
    Require stack:
    - D:\XXX\github\hexo\node_modules\imagemin-optipng\index.js
        at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
        at Module._load (node:internal/modules/cjs/loader:985:27)
        at Module.require (node:internal/modules/cjs/loader:1235:19)
        at require (node:internal/modules/helpers:176:18)
        at Object.<anonymous> (D:\XXX\github\hexo\node_modules\imagemin-optipng\index.js:4:17)
        at Module._compile (node:internal/modules/cjs/loader:1376:14)
        at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
        at Module.load (node:internal/modules/cjs/loader:1207:32)
        at Module._load (node:internal/modules/cjs/loader:1023:12)
        at cjsLoader (node:internal/modules/esm/translators:345:17) {
      code: 'MODULE_NOT_FOUND',
      requireStack: [
        'D:\\XXX\\github\\hexo\\node_modules\\imagemin-optipng\\index.js'
      ]
    }
    
  • 二、gulp-imagemin: Could not load default plugin ***

    [11:32:18] Using gulpfile D:\XXX\github\hexo\gulpfile.js
    [11:32:18] Starting 'default'...
    er Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'mozjpeg' imported from D:\XXX\github\hexo\node_modules\imagemin-mozjpeg\index.js
        at packageResolve (node:internal/modules/esm/resolve:844:9)
        at moduleResolve (node:internal/modules/esm/resolve:901:20)
        at defaultResolve (node:internal/modules/esm/resolve:1121:11)
        at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:396:12)
        at ModuleLoader.resolve (node:internal/modules/esm/loader:365:25)
        at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38)
        at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:85:39)
        at link (node:internal/modules/esm/module_job:84:36) {
      code: 'ERR_MODULE_NOT_FOUND'
    }
    gulp-imagemin: Could not load default plugin `mozjpeg`
    er Error: Cannot find module 'gifsicle'
    Require stack:
    - D:\XXX\github\hexo\node_modules\imagemin-gifsicle\index.js
        at Module._resolveFilename (node:internal/modules/cjs/loader:1144:15)
        at Module._load (node:internal/modules/cjs/loader:985:27)
        at Module.require (node:internal/modules/cjs/loader:1235:19)
        at require (node:internal/modules/helpers:176:18)
        at Object.<anonymous> (D:\XXX\github\hexo\node_modules\imagemin-gifsicle\index.js:3:18)
        at Module._compile (node:internal/modules/cjs/loader:1376:14)
        at Module._extensions..js (node:internal/modules/cjs/loader:1435:10)
        at Module.load (node:internal/modules/cjs/loader:1207:32)
        at Module._load (node:internal/modules/cjs/loader:1023:12)
        at cjsLoader (node:internal/modules/esm/translators:345:17) {
      code: 'MODULE_NOT_FOUND',
      requireStack: [
        'D:\\XXX\\github\\hexo\\node_modules\\imagemin-gifsicle\\index.js'
      ]
    }
    gulp-imagemin: Could not load default plugin `gifsicle`
    er Error [ERR_REQUIRE_ESM]: require() of ES Module D:\XXX\github\hexo\node_modules\optipng-bin\index.js from D:\XXX\github\hexo\node_modules\imagemin-optipng\index.js not supported.
    Instead change the require of D:\XXX\github\hexo\node_modules\optipng-bin\index.js in D:\XXX\github\hexo\node_modules\imagemin-optipng\index.js to a dynamic import() which is available in all CommonJS modules.
        at Object.<anonymous> (D:\XXX\github\hexo\node_modules\imagemin-optipng\index.js:4:17) {
      code: 'ERR_REQUIRE_ESM'
    }
    gulp-imagemin: Could not load default plugin `optipng`
    [11:32:22] 'default' errored after 3.81 s
    [11:32:22] Error in plugin "gulp-imagemin"
    TypeError: function_ is not a function
        at file:///D:/XXX/github/hexo/node_modules/p-pipe/index.js:10:25
        at imagemin.buffer (file:///D:/XXX/github/hexo/node_modules/imagemin/index.js:75:26)
        at gulpPlugin.onFinish (file:///D:/XXX/github/hexo/node_modules/gulp-imagemin/index.js:59:31)
        at async file:///D:/XXX/github/hexo/node_modules/gulp-plugin-extras/index.js:19:12
        at async file:///D:/XXX/github/hexo/node_modules/easy-transform-stream/index.js:14:20
    
  • 三、Error in plugin "gulp-imagemin"

    [19:04:18] Using gulpfile D:\XXX\github\hexo\gulpfile.js
    [19:04:18] Starting 'default'...
    [19:04:22] 'default' errored after 3.61 s
    [19:04:22] Error in plugin "gulp-imagemin"
    Error: spawn D:\XXX\github\hexo\node_modules\.pnpm\optipng-bin@7.0.1\node_modules\optipng-bin\vendor\optipng.exe ENOENT
        at notFoundError (D:\XXX\github\hexo\node_modules\.pnpm\cross-spawn@5.1.0\node_modules\cross-spawn\lib\enoent.js:11:11)
        at verifyENOENT (D:\XXX\github\hexo\node_modules\.pnpm\cross-spawn@5.1.0\node_modules\cross-spawn\lib\enoent.js:46:16)
        at cp.emit (D:\XXX\github\hexo\node_modules\.pnpm\cross-spawn@5.1.0\node_modules\cross-spawn\lib\enoent.js:33:19)
        at ChildProcess._handle.onexit (node:internal/child_process:294:12)
        at Process.callbackTrampoline (node:internal/async_hooks:130:17)
    

    一、二、三同种问题,由于使用npm/yarn/pnpm install安装的时候并没有完全安装成功

    // pnpm失败跳出
    node_modules/.pnpm/optipng-bin@7.0.1/node_modules/optipng-bin: Running postinstall script, failed in 7.3s (skipped as optional)dules/.pnpm/mozjpeg@8.0.0/node_modules/mozjpeg: Running postinstall script, failed in 4.6s (skipped as optional)
    node_modules/.pnpm/gifsicle@5.3.0/node_modules/gifsicle: Running postinstall script, failed in 3s (skipped as optional)
    
    // yarn失败跳出
    [4/4] Building fresh packages...
    [-/9]  waiting...
    [2/9]  hexo-util
    [8/9]  optipng-bin
    [7/9]  mozjpeg
    warning Error running install script for optional dependency: "D:\\XXX\\github\\hexo\\node_modules\\gifsicle: Command failed.
    Exit code: 1
    Command: node lib/install.js
    Arguments: 
    Directory: D:\\XXX\\github\\hexo\\node_modules\\gifsicle
    Output:
    unable to verify the first certificate
    gifsicle pre-build test failed
    compiling from source
    Error: Command failed: C:\\Windows\\system32\\cmd.exe /s /c \"autoreconf -ivf\"
    'autoreconf' �����ڲ����ⲿ���Ҳ���ǿ����еij���
    ���������ļ���
    
    
    [-/9] ⠈ waiting...
    [-/9] ⠈ waiting...
    [8/9] ⠈ optipng-bin
    [7/9] ⠁ mozjpeg
    warning Error running install script for optional dependency: "D:\\XXX\\github\\hexo\\node_modules\\mozjpeg: Command failed.
    Exit code: 1
    Command: node lib/install.js
    Arguments:
    Directory: D:\\XXX\\github\\hexo\\node_modules\\mozjpeg
    Output:
    unable to verify the first certificate
    mozjpeg pre-build test failed
    compiling from source
    Error: Command failed: C:\\Windows\\system32\\cmd.exe /s /c \"autoreconf -fiv\"
    'autoreconf' �����ڲ����ⲿ���Ҳ���ǿ����еij���
    ���������ļ���
    
    
    [-/9] ⡀ waiting...
    [-/9] ⡀ waiting...
    [8/9] ⡀ optipng-bin
    [-/9] ⢀ waiting...
    warning Error running install script for optional dependency: "D:\\XXX\\github\\hexo\\node_modules\\optipng-bin: Command failed.
    Exit code: 1
    Command: node lib/install.js
    Arguments:
    Directory: D:\\XXX\\github\\hexo\\node_modules\\optipng-bin
    Output:
    unable to verify the first certificate
    optipng pre-build test failed
    compiling from source
    

    解决方案:重新安装

    // 如果曾经安装过gulp-imagemin,有时需要删除才可以
    rm -rf node_modules
    // !!!记住顺序
    cnpm i optipng-bin -D
    cnpm i gulp-imagemin -D
    
  • EPERM: operation not permitted(npm install出现)

    npm WARN cleanup Failed to remove some directories [
    npm WARN cleanup   [
    npm WARN cleanup     'D:\\XXX\\github\\hexo\\node_modules',
    npm WARN cleanup     [Error: EPERM: operation not permitted, rmdir 'D:\XXX\github\hexo\node_modules\readable-web-to-node-stream\node_modules\readable-stream\lib\internal'] {
    npm WARN cleanup       errno: -4048,
    npm WARN cleanup       code: 'EPERM',
    npm WARN cleanup       syscall: 'rmdir',
    npm WARN cleanup       path: 'D:\\XXX\\github\\hexo\\node_modules\\readable-web-to-node-stream\\node_modules\\readable-stream\\lib\\internal'
    npm WARN cleanup     }
    npm WARN cleanup   ],
    npm WARN cleanup   [
    npm WARN cleanup     'D:\\XXX\\github\\hexo\\node_modules\\htmlparser2',        
    npm WARN cleanup     [Error: EPERM: operation not permitted, rmdir 'D:\XXX\github\hexo\node_modules\htmlparser2\node_modules'] {
    npm WARN cleanup       errno: -4048,
    npm WARN cleanup       code: 'EPERM',
    npm WARN cleanup       syscall: 'rmdir',
    npm WARN cleanup       path: 'D:\\XXX\\github\\hexo\\node_modules\\htmlparser2\\node_modules'
    npm WARN cleanup     }
    npm WARN cleanup   ],
    npm WARN cleanup   [
    npm WARN cleanup     'D:\\XXX\\github\\hexo\\node_modules\\jsdom',
    npm WARN cleanup     [Error: EPERM: operation not permitted, rmdir 'D:\XXX\github\hexo\node_modules\jsdom'] {
    npm WARN cleanup       errno: -4048,
    npm WARN cleanup       code: 'EPERM',
    npm WARN cleanup       syscall: 'rmdir',
    npm WARN cleanup       path: 'D:\\XXX\\github\\hexo\\node_modules\\jsdom'       
    npm WARN cleanup     }
    npm WARN cleanup   ],
    npm WARN cleanup   [
    npm WARN cleanup     'D:\\XXX\\github\\hexo\\node_modules\\gulp-imagemin',      
    npm WARN cleanup     [Error: EPERM: operation not permitted, rmdir 'D:\XXX\github\hexo\node_modules\gulp-imagemin\node_modules'] {
    npm WARN cleanup       errno: -4048,
    npm WARN cleanup       code: 'EPERM',
    npm WARN cleanup       syscall: 'rmdir',
    npm WARN cleanup       path: 'D:\\XXX\\github\\hexo\\node_modules\\gulp-imagemin\\node_modules'
    npm WARN cleanup     }
    npm WARN cleanup   ]
    npm WARN cleanup ]
    npm ERR! code 1
    npm ERR! path D:\XXX\github\hexo\node_modules\optipng-bin
    npm ERR! command failed
    npm ERR! command C:\Windows\system32\cmd.exe /d /s /c node lib/install.js        
    npm ERR! compiling from source
    npm ERR! unable to verify the first certificate
    npm ERR! optipng pre-build test failed
    npm ERR! Error: Command failed: C:\Windows\system32\cmd.exe /s /c "./configure --with-system-zlib --prefix="D:\XXX\github\hexo\node_modules\optipng-bin\vendor" --bindir="D:\XXX\github\hexo\node_modules\optipng-bin\vendor""
    

    package.json中先删除gulp-imagemin和optipng-bin

    {
      "devDependencies": {
        "gulp": "^4.0.2",
        "gulp-imagemin": "^9.0.0",
        "optipng-bin": "^9.0.0"
      }
    }
    

    再重新安装

    rm -rf node_modules
    yarn install
    cnpm i optipng-bin -D
    cnpm i gulp-imagemin -D
    
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,214评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,307评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,543评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,221评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,224评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,007评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,313评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,956评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,441评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,925评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,018评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,685评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,234评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,240评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,464评论 1 261
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,467评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,762评论 2 345

推荐阅读更多精彩内容