diff --git a/.changeset/afraid-books-notice.md b/.changeset/afraid-books-notice.md new file mode 100644 index 00000000..ede73ab7 --- /dev/null +++ b/.changeset/afraid-books-notice.md @@ -0,0 +1,5 @@ +--- +'vite-imagetools': patch +--- + +fix: use URI-decoded pathname when emitting file diff --git a/packages/vite/src/index.ts b/packages/vite/src/index.ts index d7d13e51..bc4f5b76 100644 --- a/packages/vite/src/index.ts +++ b/packages/vite/src/index.ts @@ -64,13 +64,14 @@ export function imagetools(userOptions: Partial = {}): Plugin if (!filter(id)) return null const srcURL = parseURL(id) + const pathname = decodeURIComponent(srcURL.pathname) // lazy loaders so that we can load the metadata in defaultDirectives if needed // but if there are no directives then we can just skip loading let lazyImg: Sharp const lazyLoadImage = () => { if (lazyImg) return lazyImg - return (lazyImg = sharp(decodeURIComponent(srcURL.pathname))) + return (lazyImg = sharp(pathname)) } let lazyMetadata: Metadata @@ -131,7 +132,7 @@ export function imagetools(userOptions: Partial = {}): Plugin metadata.src = basePath + id } else { const fileHandle = this.emitFile({ - name: basename(srcURL.pathname, extname(srcURL.pathname)) + `.${metadata.format}`, + name: basename(pathname, extname(pathname)) + `.${metadata.format}`, source: await image.toBuffer(), type: 'asset' })