jw-12138 / vite-plugin-vsharp

Compress Images for the Web with Vite and Sharp
MIT License
43 stars 0 forks source link

[Bug] `exclude` option not working #7

Closed FatehAK closed 1 year ago

FatehAK commented 1 year ago

The option to exclude certain files does not seem to work. I checked the source code it seems you are splicing the entire array and removing all the entries by thisName.splice(0, 2); as a result thisName ends up being an empty string.

Also, lets say I give exclude like so - exclude: ['thumbsup.gif']

When running vite build the filename contains the hash as well so the output of _path["default"].basename(el).split('.') would be thumbsup-[hash].gif

This will fail the comparison check opts.exclude.includes(thisName + thisExt)

jw-12138 commented 1 year ago

@FatehAK could you please provide the file/folder structure of your project?

FatehAK commented 1 year ago

It looks like this:

image

jw-12138 commented 1 year ago

Still can't reproduce this, if you are using the image directly (using <img> tag) inside index.html or related components, the exclude option should work as expected.

File hash generated by Vite should have no impact with comparison check.

could you please provide more details or a sample project?

FatehAK commented 1 year ago

I'm using an <img> tag inside index.html. You can take a look at this repo -

https://github.com/FatehAK/memrise/tree/vsharp-test

jw-12138 commented 1 year ago

Okay, I think I know where the problem is...

Vite 4 bundles things differently than Vite 2/3, The filename will be <filename>-<hash>.<ext> instead of <filename>.<hash>.<ext>.

for a temporary solution, you can downgrade vite to 3.2.5 from 4.0.0, just run npm i vite@3.

I'll see if I could do something to round this up!

FatehAK commented 1 year ago

Nice catch! No wonder the slicing logic worked fine you would have had 3 elements in the array earlier.

jw-12138 commented 1 year ago

I found a solution in https://github.com/vitejs/vite/issues/378, basically you can add something in your vite.config.js like this if you still want to use Vite 4:

import vsharp from "vite-plugin-vsharp"
export default ({
  // ⬇️ from here
  build: {
    rollupOptions: {
      output: {
        assetFileNames: `assets/[name].[hash].[ext]`
      }
    }
  },
  // ⬆️ to here
  plugins: [
    vsharp()
  ]
})

not very elegant, but it works. 🥲

FatehAK commented 1 year ago

https://rollupjs.org/guide/en/#outputassetfilenames

Seems like Vite 4.0 upgraded to Rollup 3.0 which has changed the default asset names.

Is it possible to handle this within the vsharp plugin itself? Since everyone would have to make the path change to assets/[name].[hash].[ext] 😕

jw-12138 commented 1 year ago

This is now fixed at version >1.4, you can upgrade the vsharp plugin and delete the config items. 😆