Skip to content

Commit

Permalink
fix: use URI-decoded pathname when emitting file (#689)
Browse files Browse the repository at this point in the history
  • Loading branch information
pzerelles authored Mar 24, 2024
1 parent 395a8db commit 755912c
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-books-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-imagetools': patch
---

fix: use URI-decoded pathname when emitting file
5 changes: 3 additions & 2 deletions packages/vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,14 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): 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
Expand Down Expand Up @@ -131,7 +132,7 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): 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'
})
Expand Down

0 comments on commit 755912c

Please sign in to comment.