'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); const ImgixClient = require('@imgix/js-core'); const paramCase = require('param-case'); const gatsbyPluginImage = require('gatsby-plugin-image'); const React = require('react'); function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; } function _interopNamespace(e) { if (e && e.__esModule) return e; const n = Object.create(null); if (e) { for (const k in e) { if (k !== 'default') { const d = Object.getOwnPropertyDescriptor(e, k); Object.defineProperty(n, k, d.get ? d : { enumerable: true, get: function () { return e[k]; } }); } } } n["default"] = e; return Object.freeze(n); } const ImgixClient__default = /*#__PURE__*/_interopDefaultLegacy(ImgixClient); const React__namespace = /*#__PURE__*/_interopNamespace(React); const paramCaseObject = (obj) => { const result = {}; for (const key in obj) { const paramKey = paramCase.paramCase(key); result[paramKey] = obj[key]; } return result; }; const parseArParam = (ar) => { const [antecedent, consequent] = ar.split(":"); return Number.parseFloat(antecedent) / Number.parseFloat(consequent); }; const DEFAULT_FIXED_WIDTH = 400; const DEFAULT_FLUID_MAX_WIDTH = 800; const DEFAULT_FLUID_SRC_SET_BREAKPOINT_FACTORS = [0.25, 0.5, 1.5, 2]; const DEFAULT_IMGIX_PARAMS = { fit: "crop" }; const DEFAULT_PLACEHOLDER_IMGIX_PARAMS = { w: 20, h: void 0, blur: 15, q: 20 }; var GraphQLTypeName; (function(GraphQLTypeName2) { GraphQLTypeName2["GatsbyImageDataPlaceholderEnum"] = "GatsbyImageDataPlaceholder"; GraphQLTypeName2["ImgixParamsInputObject"] = "ImgixParams"; GraphQLTypeName2["GatsbyImageDataObject"] = "GatsbyImageData"; GraphQLTypeName2["FixedObject"] = "Fixed"; GraphQLTypeName2["FluidObject"] = "Fluid"; })(GraphQLTypeName || (GraphQLTypeName = {})); var GatsbyImageDataPlaceholderKind; (function(GatsbyImageDataPlaceholderKind2) { GatsbyImageDataPlaceholderKind2["Blurred"] = "blurred"; GatsbyImageDataPlaceholderKind2["DominantColor"] = "dominantColor"; GatsbyImageDataPlaceholderKind2["None"] = "none"; })(GatsbyImageDataPlaceholderKind || (GatsbyImageDataPlaceholderKind = {})); var GatsbyImageDataLayoutKind; (function(GatsbyImageDataLayoutKind2) { GatsbyImageDataLayoutKind2["Constrained"] = "constrained"; GatsbyImageDataLayoutKind2["Fixed"] = "fixed"; GatsbyImageDataLayoutKind2["FullWidth"] = "fullWidth"; })(GatsbyImageDataLayoutKind || (GatsbyImageDataLayoutKind = {})); const resolveFixed = (source, options = {}, config = {}) => { var _a; const url = new URL(source.url); const filename = decodeURIComponent(url.pathname); const client = new ImgixClient__default["default"]({ domain: url.hostname, ...config.imgixClientConfig }); let aspectRatio = source.width / source.height; if (typeof ((_a = options.imgixParams) == null ? void 0 : _a.ar) === "string") { const parsedAr = parseArParam(options.imgixParams.ar); if (!Number.isNaN(parsedAr)) { aspectRatio = parsedAr; } } let width = DEFAULT_FIXED_WIDTH; let height = Math.round(width / aspectRatio); if (options.width != null) { width = options.width; if (options.height != null) { height = options.height; } else { height = Math.round(width / aspectRatio); } } else if (options.height != null) { height = options.height; if (options.width != null) { width = options.width; } else { width = Math.round(height * aspectRatio); } } const imgixParams = paramCaseObject({ ...DEFAULT_IMGIX_PARAMS, ...options.imgixParams, w: width, h: height }); const imgixParamsWebp = { ...imgixParams, fm: "webp" }; const placeholderImgixParams = paramCaseObject({ ...DEFAULT_IMGIX_PARAMS, ...options.imgixParams, ...DEFAULT_PLACEHOLDER_IMGIX_PARAMS, ...options.placeholderImgixParams }); return { width, height, base64: client.buildURL(filename, placeholderImgixParams), src: client.buildURL(filename, imgixParams), srcWebp: client.buildURL(filename, imgixParamsWebp), srcSet: client.buildSrcSet(filename, imgixParams), srcSetWebp: client.buildSrcSet(filename, imgixParamsWebp) }; }; const buildFixedImageData = (src, imgixParams, options = {}) => { return resolveFixed({ url: src, width: imgixParams.w, height: imgixParams.h }, { width: imgixParams.w, height: imgixParams.h, imgixParams }, { imgixClientConfig: { includeLibraryParam: options.includeLibraryParam } }); }; const resolveFluid = (source, options = {}, config = {}) => { var _a; const url = new URL(source.url); const filename = decodeURIComponent(url.pathname); const client = new ImgixClient__default["default"]({ domain: url.hostname, ...config.imgixClientConfig }); let aspectRatio = source.width / source.height; if (typeof ((_a = options.imgixParams) == null ? void 0 : _a.ar) === "string") { const parsedAr = parseArParam(options.imgixParams.ar); if (!Number.isNaN(parsedAr)) { aspectRatio = parsedAr; } } else if (options.maxWidth != null && options.maxHeight != null) { aspectRatio = options.maxWidth / options.maxHeight; } let maxWidth = DEFAULT_FLUID_MAX_WIDTH; if (options.maxWidth != null) { maxWidth = options.maxWidth; } else if (options.maxHeight != null) { maxWidth = Math.round(options.maxHeight * aspectRatio); } const imgixParams = paramCaseObject({ ...DEFAULT_IMGIX_PARAMS, ...options.imgixParams, w: maxWidth, ar: `${aspectRatio}:1` }); const imgixParamsWebp = { ...imgixParams, fm: "webp" }; const placeholderImgixParams = paramCaseObject({ ...imgixParams, ...DEFAULT_PLACEHOLDER_IMGIX_PARAMS, ...options.placeholderImgixParams }); const srcSetOptions = { maxWidth, widths: options.srcSetBreakpoints || DEFAULT_FLUID_SRC_SET_BREAKPOINT_FACTORS.map((factor) => { var _a2; return ((_a2 = options.maxWidth) != null ? _a2 : DEFAULT_FLUID_MAX_WIDTH) * factor; }) }; return { aspectRatio, base64: client.buildURL(filename, placeholderImgixParams), src: client.buildURL(filename, imgixParams), srcWebp: client.buildURL(filename, imgixParamsWebp), srcSet: client.buildSrcSet(filename, imgixParams, srcSetOptions), srcSetWebp: client.buildSrcSet(filename, imgixParamsWebp, srcSetOptions), sizes: "(min-width: 8192px) 8192px 100vw" }; }; const buildFluidImageData = (src, imgixParams, options = {}) => { var _a; const aspectRatio = parseArParam(imgixParams.ar); const fluid = resolveFluid({ url: src, width: aspectRatio, height: 1 }, { imgixParams }, { imgixClientConfig: { includeLibraryParam: options.includeLibraryParam } }); fluid.sizes = (_a = options.sizes) != null ? _a : fluid.sizes; return fluid; }; var name = "gatsby-plugin-imgix-lite"; const generateGatsbyImageDataSource = (config) => { return (sourceUrl, width, height, format, _fit, options) => { const url = new URL(sourceUrl); const filename = decodeURIComponent(url.pathname); const client = new ImgixClient__default["default"]({ domain: url.hostname, ...config.imgixClientConfig }); const imgixParams = paramCaseObject({ ...DEFAULT_IMGIX_PARAMS, ...options == null ? void 0 : options.imgixParams, w: width, h: height }); if (format && format !== "auto") { imgixParams.fm = format; } return { src: client.buildURL(filename, imgixParams), width, height, format }; }; }; const getGatsbyImageData = (config) => { let sourceWidth; let sourceHeight; if ("sourceWidth" in config && "sourceHeight" in config) { sourceWidth = config.sourceWidth; sourceHeight = config.sourceHeight; } else { sourceWidth = config.width; sourceHeight = config.width / config.aspectRatio; } return gatsbyPluginImage.getImageData({ ...config, baseUrl: config.src, urlBuilder: (args) => { const gatsbyImageDataSource = generateGatsbyImageDataSource({ imgixClientConfig: void 0 })(args.baseUrl, args.width, args.height, args.format, void 0, args.options); return gatsbyImageDataSource.src; }, sourceWidth, sourceHeight, pluginName: name, options: { imgixParams: config.imgixParams } }); }; const ImgixGatsbyImage = (props) => { const data = getGatsbyImageData(props); return /* @__PURE__ */ React__namespace.createElement(gatsbyPluginImage.GatsbyImage, { image: data, ...props }); }; exports.ImgixGatsbyImage = ImgixGatsbyImage; exports.buildFixedImageData = buildFixedImageData; exports.buildFluidImageData = buildFluidImageData; exports.getGatsbyImageData = getGatsbyImageData; //# sourceMappingURL=index.cjs.map