Skip to content

Commit a79920e

Browse files
committed
fix(vite-plugin): honor server origin and merge client types with Vite's
1 parent aebf2f7 commit a79920e

2 files changed

Lines changed: 6 additions & 19 deletions

File tree

‎packages/vite-plugin/client.d.ts‎

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* oxlint-disable trigen/import-order */
22
/**
33
* Ambient module declarations for image imports handled by @srcset/vite-plugin.
4+
* Only the named exports are declared: the default url export
5+
* comes from the `vite/client` asset types.
46
*
57
* Usage: add to tsconfig `types` or reference directly:
68
* /// <reference types="@srcset/vite-plugin/client" />
@@ -9,71 +11,53 @@
911
declare module '*.jpg' {
1012
import type { SrcSetEntry } from '@srcset/runtime'
1113

12-
const url: string
13-
1414
export const src: SrcSetEntry
1515
export const srcSet: SrcSetEntry[]
1616
export const srcMap: Record<string, string>
1717
export const placeholder: string | undefined
18-
export default url
1918
}
2019

2120
declare module '*.jpeg' {
2221
import type { SrcSetEntry } from '@srcset/runtime'
2322

24-
const url: string
25-
2623
export const src: SrcSetEntry
2724
export const srcSet: SrcSetEntry[]
2825
export const srcMap: Record<string, string>
2926
export const placeholder: string | undefined
30-
export default url
3127
}
3228

3329
declare module '*.png' {
3430
import type { SrcSetEntry } from '@srcset/runtime'
3531

36-
const url: string
37-
3832
export const src: SrcSetEntry
3933
export const srcSet: SrcSetEntry[]
4034
export const srcMap: Record<string, string>
4135
export const placeholder: string | undefined
42-
export default url
4336
}
4437

4538
declare module '*.webp' {
4639
import type { SrcSetEntry } from '@srcset/runtime'
4740

48-
const url: string
49-
5041
export const src: SrcSetEntry
5142
export const srcSet: SrcSetEntry[]
5243
export const srcMap: Record<string, string>
5344
export const placeholder: string | undefined
54-
export default url
5545
}
5646

5747
declare module '*.avif' {
5848
import type { SrcSetEntry } from '@srcset/runtime'
5949

60-
const url: string
61-
6250
export const src: SrcSetEntry
6351
export const srcSet: SrcSetEntry[]
6452
export const srcMap: Record<string, string>
6553
export const placeholder: string | undefined
66-
export default url
6754
}
6855

6956
declare module '*.gif' {
7057
import type { SrcSetEntry } from '@srcset/runtime'
7158

72-
const url: string
73-
7459
export const src: SrcSetEntry
7560
export const srcSet: SrcSetEntry[]
7661
export const srcMap: Record<string, string>
7762
export const placeholder: string | undefined
78-
export default url
7963
}

‎packages/vite-plugin/src/plugin.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export function srcset(options: SrcSetVitePluginOptions = {}): Plugin {
4949
const matchesLoadFilter = createFilter(loadFilter.id.include, loadFilter.id.exclude)
5050
const devCache: DevCache = new Map()
5151
let base = '/'
52+
let origin = ''
5253
let isBuild = false
5354
const generateModule = async (context: EmitContext, id: string) => {
5455
const { path } = splitId(id)
@@ -77,7 +78,7 @@ export function srcset(options: SrcSetVitePluginOptions = {}): Plugin {
7778

7879
return {
7980
outputPath: devPath,
80-
publicPath: base + devPath
81+
publicPath: origin + base + devPath
8182
}
8283
}
8384

@@ -95,6 +96,8 @@ export function srcset(options: SrcSetVitePluginOptions = {}): Plugin {
9596
enforce: 'pre',
9697
configResolved(config) {
9798
base = config.base
99+
// Vite includes the origin in the dev asset urls for backend integrations.
100+
origin = config.server.origin ?? ''
98101
isBuild = config.command === 'build'
99102
},
100103
configureServer(server) {

0 commit comments

Comments
 (0)