@nuxt/image
Version:
Nuxt Image Module
52 lines (51 loc) • 1.25 kB
JavaScript
import { joinURL, encodePath } from "ufo";
import { createOperationsGenerator } from "../utils/index.js";
import { defineProvider } from "../utils/provider.js";
const operationsGenerator = createOperationsGenerator({
keyMap: {
width: "w",
height: "h",
quality: "cmpr",
format: "f",
fit: "m",
passThrough: "pass",
sharpen: "s",
rotate: "r",
screenPercent: "pc",
crop: "cr",
inline: "in",
metadata: "meta",
maxDpr: "maxdpr",
download: "dl"
},
valueMap: {
fit: {
cover: "cropbox",
contain: "letterbox",
fill: "stretch",
inside: "box",
outside: "box",
productletterbox: "productletterbox"
},
format: {
jpeg: "jpg"
},
quality(value) {
let compression = 100 - Number.parseInt(value, 10);
if (compression === 100) {
compression = 99;
}
return compression.toString();
}
},
joinWith: "/",
formatter: (key, value) => encodePath(`${key}_${value}`)
});
export default defineProvider({
getImage: (src, { modifiers, baseURL = "/" }) => {
const operations = operationsGenerator(modifiers);
return {
url: joinURL(baseURL, src + (operations ? "?imgeng=/" + operations : ""))
};
}
});