Skip to content

Commit

Permalink
fix: support Vite's base config (#489)
Browse files Browse the repository at this point in the history
  • Loading branch information
DeedleFake authored Jan 20, 2023
1 parent c0e5fd2 commit 6291c71
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-yaks-jam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vite-imagetools': patch
---

fix: support Vite's `base` config
8 changes: 5 additions & 3 deletions packages/vite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): Plugin
: builtinOutputFormats

let viteConfig: ResolvedConfig
let basePath: string

const generatedImages = new Map()

Expand All @@ -45,6 +46,7 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): Plugin
enforce: 'pre',
configResolved(cfg) {
viteConfig = cfg
basePath = viteConfig.base?.replace(/\/$/, '') || ''
},
async load(id) {
if (!filter(id)) return null
Expand Down Expand Up @@ -88,7 +90,7 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): Plugin

metadata.src = `__VITE_ASSET__${fileHandle}__`
} else {
metadata.src = posix.join('/@imagetools', id)
metadata.src = posix.join(basePath, '@imagetools', id)
}

metadata.image = image
Expand Down Expand Up @@ -118,8 +120,8 @@ export function imagetools(userOptions: Partial<VitePluginOptions> = {}): Plugin

configureServer(server) {
server.middlewares.use((req, res, next) => {
if (req.url?.startsWith('/@imagetools/')) {
const [, id] = req.url.split('/@imagetools/')
if (req.url?.startsWith(`${basePath}/@imagetools/`)) {
const [, id] = req.url.split(`${basePath}/@imagetools/`)

const image = generatedImages.get(id)

Expand Down

0 comments on commit 6291c71

Please sign in to comment.