Lete114 / hexo-minify

Hexo-minify is a Hexo compression plug-in that compresses HTML, CSS, JS, Font and Image(jpg,png,gif,webp,svg) (Hexo-minify 是一款 Hexo 压缩插件,它可以压缩 HTML、CSS、JS、Font、Image(jpg,png,gif,webp,svg))
37 stars 1 forks source link

为什么自动生成了许多webp? #5

Closed Nihility-Ming closed 1 year ago

Nihility-Ming commented 1 year ago

一个图片文件生成了一个对应的同名webp,这是为何?

Nihility-Ming commented 1 year ago

是不是要手动把文章里的图片链接全部替换成webp格式后缀名才能够使用?

Lete114 commented 1 year ago

如果你不需要webp你可以在配置文件中把webp设置为false

Nihility-Ming commented 1 year ago
webp:
      enable: false
      ## 详细配置: https://github.com/imagemin/imagemin-webp#options
      options: {}

设置成false还是不行,会自动生成新的webp,我的图片文件里面都是一些jpg/png,webp是新生成的,而且和原文件同名,只是后缀是webp

Lete114 commented 1 year ago

怎么可能,你的配置是怎么写的?先执行hexo cl 然后执行 hexo g

Nihility-Ming commented 1 year ago

没法,还是不行,我已经写脚本解决了,就是麻烦了一点:

#!/bin/bash

echo ""
echo "把Nihility-Ming.github.io项目推送到GitHub服务器"
echo ""

read -p "请输入更新注释: " inputText

hexo_path="/c/Users/Administrator/Desktop/hexo"
website_path="/c/Users/Administrator/Desktop/Nihility-Ming.github.io"

cd $hexo_path
hexo clean
hexo g
cd ./public
find . -name "*.webp" -type f -delete
cp -R * $website_path
cd $website_path
git pull
git add .
git commit -m "$inputText"
git push origin

echo ""
echo ""
echo "成功把项目推送到GitHub服务器!"
echo ""
read -p "按任意键继续..."
Lete114 commented 1 year ago

在hexo配置文件里把minify的配置的webp禁用就可以了

Nihility-Ming commented 1 year ago

在hexo配置文件里把minify的配置的webp禁用就可以了

我试过了哦,hexo是最新版本前几天下载的,根目录的_config.yml我加了以下代码:

## Hexo-minify Default Config Options
minify:
  preview: false ## 本地预览时是否压缩
  exclude: ['*.min.*']
  js:
    enable: true
    sourceMap:
      enable: false ## 生成 sourceMap
      ## 将 sourceMappingURL 插入压缩后的 js 文件,如果为 false 则需要在浏览器开发者工具中手动添加 sourceMap
      sourceMappingURL: false ## //# sourceMappingURL=xxx.js.map
    ## 详细配置: https://github.com/terser/terser#minify-options
    options: {}
  css:
    enable: true
    ## 详细配置: https://github.com/clean-css/clean-css#compatibility-modes
    options: {}
  html:
    enable: true
    ## 详细配置: https://github.com/kangax/html-minifier#options-quick-reference
    options:
      minifyJS: true # Compressed JavaScript
      minifyCSS: true # CSS Compressed
      removeComments: true # Remove the comments
      collapseWhitespace: true # Delete any extra space
      removeAttributeQuotes: true # Delete attribute quotes
  image:
    enable: true
    svg:
      enable: true
      ## 详细配置: https://github.com/imagemin/imagemin-svgo#imageminsvgooptionsbuffer
      options: {}
    jpg:
      enable: true
      ## 详细配置: https://github.com/imagemin/imagemin-jpegtran#options
      options: {}
    png:
      enable: true
      ## 详细配置: https://github.com/imagemin/imagemin-pngquant#options
      options: {}
    gif:
      enable: true
      ## 详细配置: https://www.npmjs.com/package/imagemin-gifsicle#options
      options: {}
    webp:
      enable: false
      ## 详细配置: https://github.com/imagemin/imagemin-webp#options
      options: {}
  font:
    enable: false
    ## 详细配置: https://github.com/Lete114/fontmin-spider#api
    options: {}

还是不行,难道我的themes问题?但估计也没有影响吧,还是感谢你!

Lete114 commented 1 year ago

确实没啥影响。者配置是对的,我这里复制了你的配置是正常的,不会生成webp

Lete114 commented 1 year ago

请确保你的 hexo-minify 版本为 1.8.0

Nihility-Ming commented 1 year ago

请确保你的 hexo-minify 版本为 1.8.0

奇怪了根目录的package.json里面是这样的:

{
  "name": "hexo-site",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "build": "hexo generate",
    "clean": "hexo clean",
    "deploy": "hexo deploy",
    "server": "hexo server"
  },
  "hexo": {
    "version": "6.3.0"
  },
  "dependencies": {
    "hexo": "^6.3.0",
    "hexo-generator-archive": "^2.0.0",
    "hexo-generator-category": "^2.0.0",
    "hexo-generator-feed": "^3.0.0",
    "hexo-generator-index": "^3.0.0",
    "hexo-generator-tag": "^2.0.0",
    "hexo-minify": "^1.8.0",
    "hexo-renderer-ejs": "^2.0.0",
    "hexo-renderer-marked": "^6.0.0",
    "hexo-renderer-stylus": "^3.0.0",
    "hexo-server": "^3.0.0",
    "hexo-theme-landscape": "^1.0.0"
  }
}

这里"hexo-minify": "^1.8.0",应该也没问题。。

Lete114 commented 1 year ago

emmm,如果还是不行,建议你在配置文件整忽略掉图片

## Hexo-minify Default Config Options
minify:
  preview: false ## 本地预览时是否压缩
  exclude: ['*.min.*','*.jpg','*.png']
Nihility-Ming commented 1 year ago

emmm,如果还是不行,建议你在配置文件整忽略掉图片

## Hexo-minify Default Config Options
minify:
  preview: false ## 本地预览时是否压缩
  exclude: ['*.min.*','*.jpg','*.png']

好的,感谢您!

Nihility-Ming commented 1 year ago
    if (ext !== '.webp') {
      const newBuffer = await imagemin.buffer(buffer, { plugins: [pluginMaps['.webp']] })
      const writePath = path.replace(new RegExp(`${ext}$`), '.webp')
      await writeFile(writePath, newBuffer)
    }

原因找到了,在libfilter.js看看这段代码:

对于非 .webp 格式的图片,首先将其转换为 .webp 格式并保存为一个新文件。然后,使用相应的 imagemin 插件对原始图片进行压缩和优化。最后,将处理后的图片覆盖原始文件。 这样,每个图片文件都会被压缩和优化,同时为非 .webp 格式的图片生成一个对应的 .webp 图片。

Lete114 commented 1 year ago

是的,这段代码有问题,感谢你的反馈。 此外,我发现存在配置覆盖的问题,这些问题将会在下个版本中进行修复

Lete114 commented 1 year ago

此 bug 已修复,请更新到 1.8.1 版本,感谢你的反馈