-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #642 from simonihmig/vite-lqip
Add LQIP support to vite-plugin
- Loading branch information
Showing
13 changed files
with
448 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@responsive-image/vite-plugin': minor | ||
--- | ||
|
||
Add LQIP support to vite-plugin |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import sharp from 'sharp'; | ||
import type { Plugin } from 'vite'; | ||
import type { Options } from '../types'; | ||
import { getPathname, parseQuery, parseURL } from '../utils'; | ||
|
||
export const name = 'responsive-image/lqip/color-css'; | ||
|
||
export default function lqipColorCssPlugin( | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
userOptions: Partial<Options> = {}, | ||
): Plugin { | ||
return { | ||
name, | ||
resolveId(source) { | ||
const { _plugin } = parseQuery(parseURL(source).searchParams); | ||
|
||
if (_plugin !== name) { | ||
return null; | ||
} | ||
|
||
// return the same module id to make vite think this file exists and is a .css file | ||
// we will load the actually existing file without .css in the load hook | ||
return source; | ||
}, | ||
async load(id) { | ||
const { className, _plugin } = parseQuery(parseURL(id).searchParams); | ||
|
||
if (_plugin !== name) { | ||
return; | ||
} | ||
|
||
if (typeof className !== 'string') { | ||
throw new Error('Missing className'); | ||
} | ||
|
||
const file = getPathname(id).replace(/\.css$/, ''); | ||
const image = sharp(file); | ||
const { dominant } = await image.stats(); | ||
const colorHex = | ||
dominant.r.toString(16) + | ||
dominant.g.toString(16) + | ||
dominant.b.toString(16); | ||
const color = '#' + colorHex; | ||
|
||
const cssRule = `.${className} { background-color: ${color}; }`; | ||
|
||
return cssRule; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { Plugin } from 'vite'; | ||
import type { Options } from '../types'; | ||
import { | ||
META_KEY, | ||
generateLqipClassName, | ||
getInput, | ||
getOptions, | ||
getPathname, | ||
} from '../utils'; | ||
import { name as colorCssPluginName } from './color-css'; | ||
|
||
export default function lqipColorPlugin( | ||
userOptions: Partial<Options> = {}, | ||
): Plugin { | ||
return { | ||
name: 'responsive-image/lqip/color', | ||
async transform(code, id) { | ||
const input = getInput(this, id); | ||
|
||
// Bail out if our loader didn't handle this module | ||
if (!input) { | ||
return; | ||
} | ||
|
||
const options = getOptions(id, userOptions); | ||
|
||
if (options.lqip?.type !== 'color') { | ||
return; | ||
} | ||
|
||
const pathname = getPathname(id); | ||
const className = generateLqipClassName(id); | ||
const importCSS = `${ | ||
pathname | ||
}.css?_plugin=${colorCssPluginName}&className=${encodeURIComponent(className)}`; | ||
|
||
const result = { | ||
...input, | ||
lqip: { type: 'color', class: className }, | ||
imports: [...input.imports, importCSS], | ||
}; | ||
|
||
return { | ||
// Only the export plugin will actually return ESM code | ||
code: '', | ||
meta: { | ||
[META_KEY]: result, | ||
}, | ||
}; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import sharp, { Metadata } from 'sharp'; | ||
import type { Plugin } from 'vite'; | ||
import type { Options } from '../types'; | ||
import { | ||
blurrySvg, | ||
dataUri, | ||
getAspectRatio, | ||
getPathname, | ||
parseQuery, | ||
parseURL, | ||
} from '../utils'; | ||
|
||
export const name = 'responsive-image/lqip/inline-css'; | ||
|
||
export default function lqipInlineCssPlugin( | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
userOptions: Partial<Options> = {}, | ||
): Plugin { | ||
return { | ||
name, | ||
resolveId(source) { | ||
const { _plugin } = parseQuery(parseURL(source).searchParams); | ||
|
||
if (_plugin !== name) { | ||
return null; | ||
} | ||
|
||
// return the same module id to make vite think this file exists and is a .css file | ||
// we will load the actually existing file without .css in the load hook | ||
return source; | ||
}, | ||
async load(id) { | ||
const { className, targetPixels, _plugin } = parseQuery( | ||
parseURL(id).searchParams, | ||
); | ||
|
||
if (_plugin !== name) { | ||
return; | ||
} | ||
|
||
if (typeof className !== 'string') { | ||
throw new Error('Missing className'); | ||
} | ||
|
||
if (typeof targetPixels !== 'string') { | ||
throw new Error('Missing targetPixels'); | ||
} | ||
|
||
const file = getPathname(id).replace(/\.css$/, ''); | ||
const image = sharp(file); | ||
const meta = await image.metadata(); | ||
|
||
if (meta.width === undefined || meta.height === undefined) { | ||
throw new Error('Missing image meta data'); | ||
} | ||
|
||
const { width, height } = await getLqipDimensions( | ||
parseInt(targetPixels, 10), | ||
meta, | ||
); | ||
|
||
const lqi = await image | ||
.resize(width, height, { | ||
withoutEnlargement: true, | ||
fit: 'fill', | ||
}) | ||
.png(); | ||
|
||
const uri = dataUri( | ||
blurrySvg( | ||
dataUri(await lqi.toBuffer(), 'image/png'), | ||
meta.width, | ||
meta.height, | ||
), | ||
'image/svg+xml', | ||
); | ||
|
||
return `.${className} { background-image: url(${uri}); }`; | ||
}, | ||
}; | ||
} | ||
|
||
async function getLqipDimensions( | ||
targetPixels: number, | ||
meta: Metadata, | ||
): Promise<{ width: number; height: number }> { | ||
const aspectRatio = getAspectRatio(meta) ?? 1; | ||
|
||
// taken from https://github.com/google/eleventy-high-performance-blog/blob/5ed39db7fd3f21ae82ac1a8e833bf283355bd3d0/_11ty/blurry-placeholder.js#L74-L92 | ||
let bitmapHeight = targetPixels / aspectRatio; | ||
bitmapHeight = Math.sqrt(bitmapHeight); | ||
const bitmapWidth = targetPixels / bitmapHeight; | ||
return { width: Math.round(bitmapWidth), height: Math.round(bitmapHeight) }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import type { Plugin } from 'vite'; | ||
import type { Options } from '../types'; | ||
import { | ||
META_KEY, | ||
generateLqipClassName, | ||
getInput, | ||
getOptions, | ||
getPathname, | ||
} from '../utils'; | ||
import { name as inlineCssPluginName } from './inline-css'; | ||
|
||
export default function lqipLinlinePlugin( | ||
userOptions: Partial<Options> = {}, | ||
): Plugin { | ||
return { | ||
name: 'responsive-image/lqip/inline', | ||
async transform(code, id) { | ||
const input = getInput(this, id); | ||
|
||
// Bail out if our loader didn't handle this module | ||
if (!input) { | ||
return; | ||
} | ||
|
||
const options = getOptions(id, userOptions); | ||
|
||
if (options.lqip?.type !== 'inline') { | ||
return; | ||
} | ||
|
||
const pathname = getPathname(id); | ||
const className = generateLqipClassName(id); | ||
const targetPixels = options.lqip.targetPixels ?? 60; | ||
const importCSS = `${ | ||
pathname | ||
}.css?_plugin=${inlineCssPluginName}&className=${encodeURIComponent(className)}&targetPixels=${targetPixels}`; | ||
|
||
const result = { | ||
...input, | ||
lqip: { type: 'inline', class: className }, | ||
imports: [...input.imports, importCSS], | ||
}; | ||
|
||
return { | ||
// Only the export plugin will actually return ESM code | ||
code: '', | ||
meta: { | ||
[META_KEY]: result, | ||
}, | ||
}; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.