mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2026-05-26 20:49:55 +00:00
Properly inline blurthumbs in build more
Also reduces code duplication
This commit is contained in:
@@ -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,39 +166,17 @@ const thumbHash = (options: Options = {}): Plugin => {
|
|||||||
if (isThumbHash(id)) {
|
if (isThumbHash(id)) {
|
||||||
const cleanedId = cleanId(id)
|
const cleanedId = cleanId(id)
|
||||||
|
|
||||||
if (config.command === 'serve') {
|
if (cache.has(id)) {
|
||||||
if (devCache.has(id)) {
|
let loadedSource = cache.get(id) as importItem
|
||||||
return devCache.get(id)
|
if (config.command === 'serve') {
|
||||||
|
const originalRefId = this.emitFile({
|
||||||
|
type: 'asset',
|
||||||
|
name: basename(cleanedId),
|
||||||
|
source: await readFile(cleanedId),
|
||||||
|
})
|
||||||
|
loadedSource.originalSrc = buildViteAsset(originalRefId);
|
||||||
}
|
}
|
||||||
|
return loader(loadedSource)
|
||||||
const { rgba, width, height, originalHeight, originalWidth } =
|
|
||||||
await loadImageAndConvertToRgba(cleanedId)
|
|
||||||
|
|
||||||
const buffer = fromRGBAToImageBuffer(
|
|
||||||
rgba,
|
|
||||||
bufferMimeType,
|
|
||||||
width,
|
|
||||||
height
|
|
||||||
)
|
|
||||||
|
|
||||||
const dataURL = buildDataURL(buffer, bufferMimeType)
|
|
||||||
|
|
||||||
const loadedSource = loader({
|
|
||||||
thumbSrc: dataURL,
|
|
||||||
thumbWidth: width,
|
|
||||||
thumbHeight: height,
|
|
||||||
originalSrc: relative(config.root, cleanedId),
|
|
||||||
originalWidth: originalWidth,
|
|
||||||
originalHeight: originalHeight,
|
|
||||||
})
|
|
||||||
|
|
||||||
devCache.set(id, loadedSource)
|
|
||||||
|
|
||||||
return loadedSource
|
|
||||||
}
|
|
||||||
|
|
||||||
if (buildCache.has(id)) {
|
|
||||||
return buildCache.get(id)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const { rgba, width, height, originalHeight, originalWidth } =
|
const { rgba, width, height, originalHeight, originalWidth } =
|
||||||
@@ -205,35 +189,43 @@ const thumbHash = (options: Options = {}): Plugin => {
|
|||||||
height
|
height
|
||||||
)
|
)
|
||||||
|
|
||||||
const referenceId = this.emitFile({
|
const dataURL = buildDataURL(buffer, bufferMimeType)
|
||||||
type: 'asset',
|
|
||||||
name: basename(cleanedId).replace(
|
// const referenceId = this.emitFile({
|
||||||
/\.(jpg)|(jpeg)|(png)|(webp)|(avif)/g,
|
// type: 'asset',
|
||||||
`.${outputExtension}`
|
// name: basename(cleanedId).replace(
|
||||||
),
|
// /\.(jpg)|(jpeg)|(png)|(webp)|(avif)|(svg)/g,
|
||||||
source: buffer,
|
// `.${outputExtension}`
|
||||||
})
|
// ),
|
||||||
|
// source: buffer,
|
||||||
|
// })
|
||||||
|
const originalSrc = relative(config.root, cleanedId);
|
||||||
|
const loadedSource = {
|
||||||
|
thumbSrc: dataURL,
|
||||||
|
thumbWidth: width,
|
||||||
|
thumbHeight: height,
|
||||||
|
originalSrc,
|
||||||
|
originalWidth: originalWidth,
|
||||||
|
originalHeight: originalHeight,
|
||||||
|
}
|
||||||
|
|
||||||
|
cache.set(id, loadedSource)
|
||||||
|
|
||||||
|
if (config.command === 'serve') {
|
||||||
|
const originalRefId = this.emitFile({
|
||||||
|
type: 'asset',
|
||||||
|
name: basename(cleanedId),
|
||||||
|
source: await readFile(cleanedId),
|
||||||
|
})
|
||||||
|
loadedSource.originalSrc = buildViteAsset(originalRefId);
|
||||||
|
}
|
||||||
|
|
||||||
|
return loader(loadedSource)
|
||||||
|
|
||||||
|
|
||||||
const originalRefId = this.emitFile({
|
|
||||||
type: 'asset',
|
|
||||||
name: basename(cleanedId),
|
|
||||||
source: await readFile(cleanedId),
|
|
||||||
})
|
|
||||||
|
|
||||||
// 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
|
||||||
|
|||||||
Reference in New Issue
Block a user