Properly inline blurthumbs in build more

Also reduces code duplication
This commit is contained in:
Jade Ellis
2024-07-20 14:59:30 +01:00
parent 2db14697be
commit 3b9646dede
+38 -46
View File
@@ -139,9 +139,15 @@ const thumbHash = (options: Options = {}): Plugin => {
let config: ResolvedConfig let config: ResolvedConfig
const devCache = new Map<string, string>() const cache = new Map<string, importItem>()
type importItem = {
const buildCache = new Map<string, string>() thumbSrc: string;
thumbWidth: number;
thumbHeight: number;
originalSrc: string;
originalWidth: number;
originalHeight: number;
}
return { return {
name: 'vite-plugin-thumbhash', name: 'vite-plugin-thumbhash',
@@ -160,9 +166,17 @@ const thumbHash = (options: Options = {}): Plugin => {
if (isThumbHash(id)) { if (isThumbHash(id)) {
const cleanedId = cleanId(id) const cleanedId = cleanId(id)
if (cache.has(id)) {
let loadedSource = cache.get(id) as importItem
if (config.command === 'serve') { if (config.command === 'serve') {
if (devCache.has(id)) { const originalRefId = this.emitFile({
return devCache.get(id) type: 'asset',
name: basename(cleanedId),
source: await readFile(cleanedId),
})
loadedSource.originalSrc = buildViteAsset(originalRefId);
}
return loader(loadedSource)
} }
const { rgba, width, height, originalHeight, originalWidth } = const { rgba, width, height, originalHeight, originalWidth } =
@@ -177,63 +191,41 @@ const thumbHash = (options: Options = {}): Plugin => {
const dataURL = buildDataURL(buffer, bufferMimeType) const dataURL = buildDataURL(buffer, bufferMimeType)
const loadedSource = loader({ // const referenceId = this.emitFile({
// type: 'asset',
// name: basename(cleanedId).replace(
// /\.(jpg)|(jpeg)|(png)|(webp)|(avif)|(svg)/g,
// `.${outputExtension}`
// ),
// source: buffer,
// })
const originalSrc = relative(config.root, cleanedId);
const loadedSource = {
thumbSrc: dataURL, thumbSrc: dataURL,
thumbWidth: width, thumbWidth: width,
thumbHeight: height, thumbHeight: height,
originalSrc: relative(config.root, cleanedId), originalSrc,
originalWidth: originalWidth, originalWidth: originalWidth,
originalHeight: originalHeight, originalHeight: originalHeight,
})
devCache.set(id, loadedSource)
return loadedSource
} }
if (buildCache.has(id)) { cache.set(id, loadedSource)
return buildCache.get(id)
}
const { rgba, width, height, originalHeight, originalWidth } =
await loadImageAndConvertToRgba(cleanedId)
const buffer = fromRGBAToImageBuffer(
rgba,
bufferMimeType,
width,
height
)
const referenceId = this.emitFile({
type: 'asset',
name: basename(cleanedId).replace(
/\.(jpg)|(jpeg)|(png)|(webp)|(avif)/g,
`.${outputExtension}`
),
source: buffer,
})
if (config.command === 'serve') {
const originalRefId = this.emitFile({ const originalRefId = this.emitFile({
type: 'asset', type: 'asset',
name: basename(cleanedId), name: basename(cleanedId),
source: await readFile(cleanedId), source: await readFile(cleanedId),
}) })
loadedSource.originalSrc = buildViteAsset(originalRefId);
}
return loader(loadedSource)
// import.meta.ROLLUP_FILE_URL_ // import.meta.ROLLUP_FILE_URL_
const loadedSource = loader({
thumbSrc: buildViteAsset(referenceId),
thumbWidth: width,
thumbHeight: height,
originalSrc: buildViteAsset(originalRefId),
originalWidth: originalWidth,
originalHeight: originalHeight,
})
buildCache.set(id, loadedSource)
return loadedSource
} }
return null return null