gatsby-plugin-image 2.9.0 → 2.10.0-next.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,449 +1,449 @@
1
- "use strict";
2
- var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
3
- if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
4
- return cooked;
5
- };
6
- var __assign = (this && this.__assign) || function () {
7
- __assign = Object.assign || function(t) {
8
- for (var s, i = 1, n = arguments.length; i < n; i++) {
9
- s = arguments[i];
10
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
11
- t[p] = s[p];
12
- }
13
- return t;
14
- };
15
- return __assign.apply(this, arguments);
16
- };
17
- var __read = (this && this.__read) || function (o, n) {
18
- var m = typeof Symbol === "function" && o[Symbol.iterator];
19
- if (!m) return o;
20
- var i = m.call(o), r, ar = [], e;
21
- try {
22
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
23
- }
24
- catch (error) { e = { error: error }; }
25
- finally {
26
- try {
27
- if (r && !r.done && (m = i["return"])) m.call(i);
28
- }
29
- finally { if (e) throw e.error; }
30
- }
31
- return ar;
32
- };
33
- var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
34
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
35
- if (ar || !(i in from)) {
36
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
37
- ar[i] = from[i];
38
- }
39
- }
40
- return to.concat(ar || Array.prototype.slice.call(from));
41
- };
42
- var __importDefault = (this && this.__importDefault) || function (mod) {
43
- return (mod && mod.__esModule) ? mod : { "default": mod };
44
- };
45
- exports.__esModule = true;
46
- exports.getDimensionsAndAspectRatio = exports.responsiveImageSizes = exports.fixedImageSizes = exports.calculateImageSizes = exports.generateImageData = exports.getLowResolutionImageURL = exports.setDefaultDimensions = exports.formatFromFilename = exports.getSrcSet = exports.getSizes = exports.EVERY_BREAKPOINT = exports.DEFAULT_BREAKPOINTS = void 0;
47
- /* eslint-disable no-unused-expressions */
48
- var common_tags_1 = require("common-tags");
49
- var camelcase_1 = __importDefault(require("camelcase"));
50
- var DEFAULT_PIXEL_DENSITIES = [0.25, 0.5, 1, 2];
51
- exports.DEFAULT_BREAKPOINTS = [750, 1080, 1366, 1920];
52
- exports.EVERY_BREAKPOINT = [
53
- 320, 654, 768, 1024, 1366, 1600, 1920, 2048, 2560, 3440, 3840, 4096,
54
- ];
55
- var DEFAULT_FLUID_WIDTH = 800;
56
- var DEFAULT_FIXED_WIDTH = 800;
57
- var DEFAULT_ASPECT_RATIO = 4 / 3;
58
- var warn = function (message) { return console.warn(message); };
59
- var sortNumeric = function (a, b) { return a - b; };
60
- var getSizes = function (width, layout) {
61
- switch (layout) {
62
- // If screen is wider than the max size, image width is the max size,
63
- // otherwise it's the width of the screen
64
- case "constrained":
65
- return "(min-width: ".concat(width, "px) ").concat(width, "px, 100vw");
66
- // Image is always the same width, whatever the size of the screen
67
- case "fixed":
68
- return "".concat(width, "px");
69
- // Image is always the width of the screen
70
- case "fullWidth":
71
- return "100vw";
72
- default:
73
- return undefined;
74
- }
75
- };
76
- exports.getSizes = getSizes;
77
- var getSrcSet = function (images) {
78
- return images.map(function (image) { return "".concat(image.src, " ").concat(image.width, "w"); }).join(",\n");
79
- };
80
- exports.getSrcSet = getSrcSet;
81
- function formatFromFilename(filename) {
82
- var dot = filename.lastIndexOf(".");
83
- if (dot !== -1) {
84
- var ext = filename.substr(dot + 1);
85
- if (ext === "jpeg") {
86
- return "jpg";
87
- }
88
- if (ext.length === 3 || ext.length === 4) {
89
- return ext;
90
- }
91
- }
92
- return undefined;
93
- }
94
- exports.formatFromFilename = formatFromFilename;
95
- function setDefaultDimensions(args) {
96
- var _a = args.layout, layout = _a === void 0 ? "constrained" : _a, width = args.width, height = args.height, sourceMetadata = args.sourceMetadata, breakpoints = args.breakpoints, aspectRatio = args.aspectRatio, _b = args.formats, formats = _b === void 0 ? ["auto", "webp"] : _b;
97
- formats = formats.map(function (format) { return format.toLowerCase(); });
98
- layout = (0, camelcase_1["default"])(layout);
99
- if (width && height) {
100
- return __assign(__assign({}, args), { formats: formats, layout: layout, aspectRatio: width / height });
101
- }
102
- if (sourceMetadata.width && sourceMetadata.height && !aspectRatio) {
103
- aspectRatio = sourceMetadata.width / sourceMetadata.height;
104
- }
105
- if (layout === "fullWidth") {
106
- width = width || sourceMetadata.width || breakpoints[breakpoints.length - 1];
107
- height = height || Math.round(width / (aspectRatio || DEFAULT_ASPECT_RATIO));
108
- }
109
- else {
110
- if (!width) {
111
- if (height && aspectRatio) {
112
- width = height * aspectRatio;
113
- }
114
- else if (sourceMetadata.width) {
115
- width = sourceMetadata.width;
116
- }
117
- else if (height) {
118
- width = Math.round(height / DEFAULT_ASPECT_RATIO);
119
- }
120
- else {
121
- width = DEFAULT_FIXED_WIDTH;
122
- }
123
- }
124
- if (aspectRatio && !height) {
125
- height = Math.round(width / aspectRatio);
126
- }
127
- else if (!aspectRatio) {
128
- aspectRatio = width / height;
129
- }
130
- }
131
- return __assign(__assign({}, args), { width: width, height: height, aspectRatio: aspectRatio, layout: layout, formats: formats });
132
- }
133
- exports.setDefaultDimensions = setDefaultDimensions;
134
- /**
135
- * Use this for getting an image for the blurred placeholder. This ensures the
136
- * aspect ratio and crop match the main image
137
- */
138
- function getLowResolutionImageURL(args, width) {
139
- var _a;
140
- if (width === void 0) { width = 20; }
141
- args = setDefaultDimensions(args);
142
- var generateImageSource = args.generateImageSource, filename = args.filename, aspectRatio = args.aspectRatio;
143
- return (_a = generateImageSource(filename, width, Math.round(width / aspectRatio), args.sourceMetadata.format || "jpg", args.fit, args.options)) === null || _a === void 0 ? void 0 : _a.src;
144
- }
145
- exports.getLowResolutionImageURL = getLowResolutionImageURL;
146
- function generateImageData(args) {
147
- args = setDefaultDimensions(args);
148
- var pluginName = args.pluginName, sourceMetadata = args.sourceMetadata, generateImageSource = args.generateImageSource, layout = args.layout, fit = args.fit, options = args.options, width = args.width, height = args.height, filename = args.filename, _a = args.reporter, reporter = _a === void 0 ? { warn: warn } : _a, backgroundColor = args.backgroundColor, placeholderURL = args.placeholderURL;
149
- if (!pluginName) {
150
- reporter.warn("[gatsby-plugin-image] \"generateImageData\" was not passed a plugin name");
151
- }
152
- if (typeof generateImageSource !== "function") {
153
- throw new Error("generateImageSource must be a function");
154
- }
155
- if (!sourceMetadata || (!sourceMetadata.width && !sourceMetadata.height)) {
156
- // No metadata means we let the CDN handle max size etc, aspect ratio etc
157
- sourceMetadata = {
158
- width: width,
159
- height: height,
160
- format: (sourceMetadata === null || sourceMetadata === void 0 ? void 0 : sourceMetadata.format) || formatFromFilename(filename) || "auto"
161
- };
162
- }
163
- else if (!sourceMetadata.format) {
164
- sourceMetadata.format = formatFromFilename(filename);
165
- }
166
- var formats = new Set(args.formats);
167
- if (formats.size === 0 || formats.has("auto") || formats.has("")) {
168
- formats["delete"]("auto");
169
- formats["delete"]("");
170
- formats.add(sourceMetadata.format);
171
- }
172
- if (formats.has("jpg") && formats.has("png")) {
173
- reporter.warn("[".concat(pluginName, "] Specifying both 'jpg' and 'png' formats is not supported. Using 'auto' instead"));
174
- if (sourceMetadata.format === "jpg") {
175
- formats["delete"]("png");
176
- }
177
- else {
178
- formats["delete"]("jpg");
179
- }
180
- }
181
- var imageSizes = calculateImageSizes(__assign(__assign({}, args), { sourceMetadata: sourceMetadata }));
182
- var result = {
183
- sources: []
184
- };
185
- var sizes = args.sizes;
186
- if (!sizes) {
187
- sizes = (0, exports.getSizes)(imageSizes.presentationWidth, layout);
188
- }
189
- formats.forEach(function (format) {
190
- var _a;
191
- var images = imageSizes.sizes
192
- .map(function (size) {
193
- var imageSrc = generateImageSource(filename, size, Math.round(size / imageSizes.aspectRatio), format, fit, options);
194
- if (!(imageSrc === null || imageSrc === void 0 ? void 0 : imageSrc.width) ||
195
- !imageSrc.height ||
196
- !imageSrc.src ||
197
- !imageSrc.format) {
198
- reporter.warn("[".concat(pluginName, "] The resolver for image ").concat(filename, " returned an invalid value."));
199
- return undefined;
200
- }
201
- return imageSrc;
202
- })
203
- .filter(Boolean);
204
- if (format === "jpg" || format === "png" || format === "auto") {
205
- var unscaled = images.find(function (img) { return img.width === imageSizes.unscaledWidth; }) || images[0];
206
- if (unscaled) {
207
- result.fallback = {
208
- src: unscaled.src,
209
- srcSet: (0, exports.getSrcSet)(images),
210
- sizes: sizes
211
- };
212
- }
213
- }
214
- else {
215
- (_a = result.sources) === null || _a === void 0 ? void 0 : _a.push({
216
- srcSet: (0, exports.getSrcSet)(images),
217
- sizes: sizes,
218
- type: "image/".concat(format)
219
- });
220
- }
221
- });
222
- var imageProps = {
223
- images: result,
224
- layout: layout,
225
- backgroundColor: backgroundColor
226
- };
227
- if (placeholderURL) {
228
- imageProps.placeholder = { fallback: placeholderURL };
229
- }
230
- switch (layout) {
231
- case "fixed":
232
- imageProps.width = imageSizes.presentationWidth;
233
- imageProps.height = imageSizes.presentationHeight;
234
- break;
235
- case "fullWidth":
236
- imageProps.width = 1;
237
- imageProps.height = 1 / imageSizes.aspectRatio;
238
- break;
239
- case "constrained":
240
- imageProps.width = args.width || imageSizes.presentationWidth || 1;
241
- imageProps.height = (imageProps.width || 1) / imageSizes.aspectRatio;
242
- }
243
- return imageProps;
244
- }
245
- exports.generateImageData = generateImageData;
246
- var dedupeAndSortDensities = function (values) {
247
- return Array.from(new Set(__spreadArray([1], __read(values), false))).sort(sortNumeric);
248
- };
249
- function calculateImageSizes(args) {
250
- var width = args.width, height = args.height, filename = args.filename, _a = args.layout, layout = _a === void 0 ? "constrained" : _a, imgDimensions = args.sourceMetadata, _b = args.reporter, reporter = _b === void 0 ? { warn: warn } : _b, _c = args.breakpoints, breakpoints = _c === void 0 ? exports.DEFAULT_BREAKPOINTS : _c;
251
- // check that all dimensions provided are positive
252
- var userDimensions = { width: width, height: height };
253
- var erroneousUserDimensions = Object.entries(userDimensions).filter(function (_a) {
254
- var _b = __read(_a, 2), _ = _b[0], size = _b[1];
255
- return typeof size === "number" && size < 1;
256
- });
257
- if (erroneousUserDimensions.length) {
258
- throw new Error("Specified dimensions for images must be positive numbers (> 0). Problem dimensions you have are ".concat(erroneousUserDimensions
259
- .map(function (dim) { return dim.join(": "); })
260
- .join(", ")));
261
- }
262
- if (layout === "fixed") {
263
- return fixedImageSizes(args);
264
- }
265
- else if (layout === "constrained") {
266
- return responsiveImageSizes(args);
267
- }
268
- else if (layout === "fullWidth") {
269
- return responsiveImageSizes(__assign({ breakpoints: breakpoints }, args));
270
- }
271
- else {
272
- reporter.warn("No valid layout was provided for the image at ".concat(filename, ". Valid image layouts are fixed, fullWidth, and constrained. Found ").concat(layout));
273
- return {
274
- sizes: [imgDimensions.width],
275
- presentationWidth: imgDimensions.width,
276
- presentationHeight: imgDimensions.height,
277
- aspectRatio: imgDimensions.width / imgDimensions.height,
278
- unscaledWidth: imgDimensions.width
279
- };
280
- }
281
- }
282
- exports.calculateImageSizes = calculateImageSizes;
283
- function fixedImageSizes(_a) {
284
- var filename = _a.filename, imgDimensions = _a.sourceMetadata, width = _a.width, height = _a.height, _b = _a.fit, fit = _b === void 0 ? "cover" : _b, _c = _a.outputPixelDensities, outputPixelDensities = _c === void 0 ? DEFAULT_PIXEL_DENSITIES : _c, _d = _a.reporter, reporter = _d === void 0 ? { warn: warn } : _d;
285
- var aspectRatio = imgDimensions.width / imgDimensions.height;
286
- // Sort, dedupe and ensure there's a 1
287
- var densities = dedupeAndSortDensities(outputPixelDensities);
288
- // If both are provided then we need to check the fit
289
- if (width && height) {
290
- var calculated = getDimensionsAndAspectRatio(imgDimensions, {
291
- width: width,
292
- height: height,
293
- fit: fit
294
- });
295
- width = calculated.width;
296
- height = calculated.height;
297
- aspectRatio = calculated.aspectRatio;
298
- }
299
- if (!width) {
300
- if (!height) {
301
- width = DEFAULT_FIXED_WIDTH;
302
- }
303
- else {
304
- width = Math.round(height * aspectRatio);
305
- }
306
- }
307
- else if (!height) {
308
- height = Math.round(width / aspectRatio);
309
- }
310
- var originalWidth = width; // will use this for presentationWidth, don't want to lose it
311
- var isTopSizeOverriden = imgDimensions.width < width || imgDimensions.height < height;
312
- // If the image is smaller than requested, warn the user that it's being processed as such
313
- // print out this message with the necessary information before we overwrite it for sizing
314
- if (isTopSizeOverriden) {
315
- var fixedDimension = imgDimensions.width < width ? "width" : "height";
316
- reporter.warn((0, common_tags_1.stripIndent)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n The requested ", " \"", "px\" for the image ", " was larger than the actual image ", " of ", "px. If possible, replace the current image with a larger one."], ["\n The requested ", " \"", "px\" for the image ", " was larger than the actual image ", " of ", "px. If possible, replace the current image with a larger one."])), fixedDimension, fixedDimension === "width" ? width : height, filename, fixedDimension, imgDimensions[fixedDimension]));
317
- if (fixedDimension === "width") {
318
- width = imgDimensions.width;
319
- height = Math.round(width / aspectRatio);
320
- }
321
- else {
322
- height = imgDimensions.height;
323
- width = height * aspectRatio;
324
- }
325
- }
326
- var sizes = densities
327
- .filter(function (size) { return size >= 1; }) // remove smaller densities because fixed images don't need them
328
- .map(function (density) { return Math.round(density * width); })
329
- .filter(function (size) { return size <= imgDimensions.width; });
330
- return {
331
- sizes: sizes,
332
- aspectRatio: aspectRatio,
333
- presentationWidth: originalWidth,
334
- presentationHeight: Math.round(originalWidth / aspectRatio),
335
- unscaledWidth: width
336
- };
337
- }
338
- exports.fixedImageSizes = fixedImageSizes;
339
- function responsiveImageSizes(_a) {
340
- var imgDimensions = _a.sourceMetadata, width = _a.width, height = _a.height, _b = _a.fit, fit = _b === void 0 ? "cover" : _b, _c = _a.outputPixelDensities, outputPixelDensities = _c === void 0 ? DEFAULT_PIXEL_DENSITIES : _c, breakpoints = _a.breakpoints, layout = _a.layout;
341
- var sizes;
342
- var aspectRatio = imgDimensions.width / imgDimensions.height;
343
- // Sort, dedupe and ensure there's a 1
344
- var densities = dedupeAndSortDensities(outputPixelDensities);
345
- // If both are provided then we need to check the fit
346
- if (width && height) {
347
- var calculated = getDimensionsAndAspectRatio(imgDimensions, {
348
- width: width,
349
- height: height,
350
- fit: fit
351
- });
352
- width = calculated.width;
353
- height = calculated.height;
354
- aspectRatio = calculated.aspectRatio;
355
- }
356
- // Case 1: width of height were passed in, make sure it isn't larger than the actual image
357
- width = width && Math.min(width, imgDimensions.width);
358
- height = height && Math.min(height, imgDimensions.height);
359
- // Case 2: neither width or height were passed in, use default size
360
- if (!width && !height) {
361
- width = Math.min(DEFAULT_FLUID_WIDTH, imgDimensions.width);
362
- height = width / aspectRatio;
363
- }
364
- // if it still hasn't been found, calculate width from the derived height.
365
- // TS isn't smart enough to realise the type for height has been narrowed here
366
- if (!width) {
367
- width = height * aspectRatio;
368
- }
369
- var originalWidth = width;
370
- var isTopSizeOverriden = imgDimensions.width < width || imgDimensions.height < height;
371
- if (isTopSizeOverriden) {
372
- width = imgDimensions.width;
373
- height = imgDimensions.height;
374
- }
375
- width = Math.round(width);
376
- if ((breakpoints === null || breakpoints === void 0 ? void 0 : breakpoints.length) > 0) {
377
- sizes = breakpoints.filter(function (size) { return size <= imgDimensions.width; });
378
- // If a larger breakpoint has been filtered-out, add the actual image width instead
379
- if (sizes.length < breakpoints.length &&
380
- !sizes.includes(imgDimensions.width)) {
381
- sizes.push(imgDimensions.width);
382
- }
383
- }
384
- else {
385
- sizes = densities.map(function (density) { return Math.round(density * width); });
386
- sizes = sizes.filter(function (size) { return size <= imgDimensions.width; });
387
- }
388
- // ensure that the size passed in is included in the final output
389
- if (layout === "constrained" && !sizes.includes(width)) {
390
- sizes.push(width);
391
- }
392
- sizes = sizes.sort(sortNumeric);
393
- return {
394
- sizes: sizes,
395
- aspectRatio: aspectRatio,
396
- presentationWidth: originalWidth,
397
- presentationHeight: Math.round(originalWidth / aspectRatio),
398
- unscaledWidth: width
399
- };
400
- }
401
- exports.responsiveImageSizes = responsiveImageSizes;
402
- function getDimensionsAndAspectRatio(dimensions, options) {
403
- // Calculate the eventual width/height of the image.
404
- var imageAspectRatio = dimensions.width / dimensions.height;
405
- var width = options.width;
406
- var height = options.height;
407
- switch (options.fit) {
408
- case "fill": {
409
- width = options.width ? options.width : dimensions.width;
410
- height = options.height ? options.height : dimensions.height;
411
- break;
412
- }
413
- case "inside": {
414
- var widthOption = options.width
415
- ? options.width
416
- : Number.MAX_SAFE_INTEGER;
417
- var heightOption = options.height
418
- ? options.height
419
- : Number.MAX_SAFE_INTEGER;
420
- width = Math.min(widthOption, Math.round(heightOption * imageAspectRatio));
421
- height = Math.min(heightOption, Math.round(widthOption / imageAspectRatio));
422
- break;
423
- }
424
- case "outside": {
425
- var widthOption = options.width ? options.width : 0;
426
- var heightOption = options.height ? options.height : 0;
427
- width = Math.max(widthOption, Math.round(heightOption * imageAspectRatio));
428
- height = Math.max(heightOption, Math.round(widthOption / imageAspectRatio));
429
- break;
430
- }
431
- default: {
432
- if (options.width && !options.height) {
433
- width = options.width;
434
- height = Math.round(options.width / imageAspectRatio);
435
- }
436
- if (options.height && !options.width) {
437
- width = Math.round(options.height * imageAspectRatio);
438
- height = options.height;
439
- }
440
- }
441
- }
442
- return {
443
- width: width,
444
- height: height,
445
- aspectRatio: width / height
446
- };
447
- }
448
- exports.getDimensionsAndAspectRatio = getDimensionsAndAspectRatio;
449
- var templateObject_1;
1
+ "use strict";
2
+ var __makeTemplateObject = (this && this.__makeTemplateObject) || function (cooked, raw) {
3
+ if (Object.defineProperty) { Object.defineProperty(cooked, "raw", { value: raw }); } else { cooked.raw = raw; }
4
+ return cooked;
5
+ };
6
+ var __assign = (this && this.__assign) || function () {
7
+ __assign = Object.assign || function(t) {
8
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
9
+ s = arguments[i];
10
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
11
+ t[p] = s[p];
12
+ }
13
+ return t;
14
+ };
15
+ return __assign.apply(this, arguments);
16
+ };
17
+ var __read = (this && this.__read) || function (o, n) {
18
+ var m = typeof Symbol === "function" && o[Symbol.iterator];
19
+ if (!m) return o;
20
+ var i = m.call(o), r, ar = [], e;
21
+ try {
22
+ while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
23
+ }
24
+ catch (error) { e = { error: error }; }
25
+ finally {
26
+ try {
27
+ if (r && !r.done && (m = i["return"])) m.call(i);
28
+ }
29
+ finally { if (e) throw e.error; }
30
+ }
31
+ return ar;
32
+ };
33
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
34
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
35
+ if (ar || !(i in from)) {
36
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
37
+ ar[i] = from[i];
38
+ }
39
+ }
40
+ return to.concat(ar || Array.prototype.slice.call(from));
41
+ };
42
+ var __importDefault = (this && this.__importDefault) || function (mod) {
43
+ return (mod && mod.__esModule) ? mod : { "default": mod };
44
+ };
45
+ exports.__esModule = true;
46
+ exports.getDimensionsAndAspectRatio = exports.responsiveImageSizes = exports.fixedImageSizes = exports.calculateImageSizes = exports.generateImageData = exports.getLowResolutionImageURL = exports.setDefaultDimensions = exports.formatFromFilename = exports.getSrcSet = exports.getSizes = exports.EVERY_BREAKPOINT = exports.DEFAULT_BREAKPOINTS = void 0;
47
+ /* eslint-disable no-unused-expressions */
48
+ var common_tags_1 = require("common-tags");
49
+ var camelcase_1 = __importDefault(require("camelcase"));
50
+ var DEFAULT_PIXEL_DENSITIES = [0.25, 0.5, 1, 2];
51
+ exports.DEFAULT_BREAKPOINTS = [750, 1080, 1366, 1920];
52
+ exports.EVERY_BREAKPOINT = [
53
+ 320, 654, 768, 1024, 1366, 1600, 1920, 2048, 2560, 3440, 3840, 4096,
54
+ ];
55
+ var DEFAULT_FLUID_WIDTH = 800;
56
+ var DEFAULT_FIXED_WIDTH = 800;
57
+ var DEFAULT_ASPECT_RATIO = 4 / 3;
58
+ var warn = function (message) { return console.warn(message); };
59
+ var sortNumeric = function (a, b) { return a - b; };
60
+ var getSizes = function (width, layout) {
61
+ switch (layout) {
62
+ // If screen is wider than the max size, image width is the max size,
63
+ // otherwise it's the width of the screen
64
+ case "constrained":
65
+ return "(min-width: ".concat(width, "px) ").concat(width, "px, 100vw");
66
+ // Image is always the same width, whatever the size of the screen
67
+ case "fixed":
68
+ return "".concat(width, "px");
69
+ // Image is always the width of the screen
70
+ case "fullWidth":
71
+ return "100vw";
72
+ default:
73
+ return undefined;
74
+ }
75
+ };
76
+ exports.getSizes = getSizes;
77
+ var getSrcSet = function (images) {
78
+ return images.map(function (image) { return "".concat(image.src, " ").concat(image.width, "w"); }).join(",\n");
79
+ };
80
+ exports.getSrcSet = getSrcSet;
81
+ function formatFromFilename(filename) {
82
+ var dot = filename.lastIndexOf(".");
83
+ if (dot !== -1) {
84
+ var ext = filename.substr(dot + 1);
85
+ if (ext === "jpeg") {
86
+ return "jpg";
87
+ }
88
+ if (ext.length === 3 || ext.length === 4) {
89
+ return ext;
90
+ }
91
+ }
92
+ return undefined;
93
+ }
94
+ exports.formatFromFilename = formatFromFilename;
95
+ function setDefaultDimensions(args) {
96
+ var _a = args.layout, layout = _a === void 0 ? "constrained" : _a, width = args.width, height = args.height, sourceMetadata = args.sourceMetadata, breakpoints = args.breakpoints, aspectRatio = args.aspectRatio, _b = args.formats, formats = _b === void 0 ? ["auto", "webp"] : _b;
97
+ formats = formats.map(function (format) { return format.toLowerCase(); });
98
+ layout = (0, camelcase_1["default"])(layout);
99
+ if (width && height) {
100
+ return __assign(__assign({}, args), { formats: formats, layout: layout, aspectRatio: width / height });
101
+ }
102
+ if (sourceMetadata.width && sourceMetadata.height && !aspectRatio) {
103
+ aspectRatio = sourceMetadata.width / sourceMetadata.height;
104
+ }
105
+ if (layout === "fullWidth") {
106
+ width = width || sourceMetadata.width || breakpoints[breakpoints.length - 1];
107
+ height = height || Math.round(width / (aspectRatio || DEFAULT_ASPECT_RATIO));
108
+ }
109
+ else {
110
+ if (!width) {
111
+ if (height && aspectRatio) {
112
+ width = height * aspectRatio;
113
+ }
114
+ else if (sourceMetadata.width) {
115
+ width = sourceMetadata.width;
116
+ }
117
+ else if (height) {
118
+ width = Math.round(height / DEFAULT_ASPECT_RATIO);
119
+ }
120
+ else {
121
+ width = DEFAULT_FIXED_WIDTH;
122
+ }
123
+ }
124
+ if (aspectRatio && !height) {
125
+ height = Math.round(width / aspectRatio);
126
+ }
127
+ else if (!aspectRatio) {
128
+ aspectRatio = width / height;
129
+ }
130
+ }
131
+ return __assign(__assign({}, args), { width: width, height: height, aspectRatio: aspectRatio, layout: layout, formats: formats });
132
+ }
133
+ exports.setDefaultDimensions = setDefaultDimensions;
134
+ /**
135
+ * Use this for getting an image for the blurred placeholder. This ensures the
136
+ * aspect ratio and crop match the main image
137
+ */
138
+ function getLowResolutionImageURL(args, width) {
139
+ var _a;
140
+ if (width === void 0) { width = 20; }
141
+ args = setDefaultDimensions(args);
142
+ var generateImageSource = args.generateImageSource, filename = args.filename, aspectRatio = args.aspectRatio;
143
+ return (_a = generateImageSource(filename, width, Math.round(width / aspectRatio), args.sourceMetadata.format || "jpg", args.fit, args.options)) === null || _a === void 0 ? void 0 : _a.src;
144
+ }
145
+ exports.getLowResolutionImageURL = getLowResolutionImageURL;
146
+ function generateImageData(args) {
147
+ args = setDefaultDimensions(args);
148
+ var pluginName = args.pluginName, sourceMetadata = args.sourceMetadata, generateImageSource = args.generateImageSource, layout = args.layout, fit = args.fit, options = args.options, width = args.width, height = args.height, filename = args.filename, _a = args.reporter, reporter = _a === void 0 ? { warn: warn } : _a, backgroundColor = args.backgroundColor, placeholderURL = args.placeholderURL;
149
+ if (!pluginName) {
150
+ reporter.warn("[gatsby-plugin-image] \"generateImageData\" was not passed a plugin name");
151
+ }
152
+ if (typeof generateImageSource !== "function") {
153
+ throw new Error("generateImageSource must be a function");
154
+ }
155
+ if (!sourceMetadata || (!sourceMetadata.width && !sourceMetadata.height)) {
156
+ // No metadata means we let the CDN handle max size etc, aspect ratio etc
157
+ sourceMetadata = {
158
+ width: width,
159
+ height: height,
160
+ format: (sourceMetadata === null || sourceMetadata === void 0 ? void 0 : sourceMetadata.format) || formatFromFilename(filename) || "auto"
161
+ };
162
+ }
163
+ else if (!sourceMetadata.format) {
164
+ sourceMetadata.format = formatFromFilename(filename);
165
+ }
166
+ var formats = new Set(args.formats);
167
+ if (formats.size === 0 || formats.has("auto") || formats.has("")) {
168
+ formats["delete"]("auto");
169
+ formats["delete"]("");
170
+ formats.add(sourceMetadata.format);
171
+ }
172
+ if (formats.has("jpg") && formats.has("png")) {
173
+ reporter.warn("[".concat(pluginName, "] Specifying both 'jpg' and 'png' formats is not supported. Using 'auto' instead"));
174
+ if (sourceMetadata.format === "jpg") {
175
+ formats["delete"]("png");
176
+ }
177
+ else {
178
+ formats["delete"]("jpg");
179
+ }
180
+ }
181
+ var imageSizes = calculateImageSizes(__assign(__assign({}, args), { sourceMetadata: sourceMetadata }));
182
+ var result = {
183
+ sources: []
184
+ };
185
+ var sizes = args.sizes;
186
+ if (!sizes) {
187
+ sizes = (0, exports.getSizes)(imageSizes.presentationWidth, layout);
188
+ }
189
+ formats.forEach(function (format) {
190
+ var _a;
191
+ var images = imageSizes.sizes
192
+ .map(function (size) {
193
+ var imageSrc = generateImageSource(filename, size, Math.round(size / imageSizes.aspectRatio), format, fit, options);
194
+ if (!(imageSrc === null || imageSrc === void 0 ? void 0 : imageSrc.width) ||
195
+ !imageSrc.height ||
196
+ !imageSrc.src ||
197
+ !imageSrc.format) {
198
+ reporter.warn("[".concat(pluginName, "] The resolver for image ").concat(filename, " returned an invalid value."));
199
+ return undefined;
200
+ }
201
+ return imageSrc;
202
+ })
203
+ .filter(Boolean);
204
+ if (format === "jpg" || format === "png" || format === "auto") {
205
+ var unscaled = images.find(function (img) { return img.width === imageSizes.unscaledWidth; }) || images[0];
206
+ if (unscaled) {
207
+ result.fallback = {
208
+ src: unscaled.src,
209
+ srcSet: (0, exports.getSrcSet)(images),
210
+ sizes: sizes
211
+ };
212
+ }
213
+ }
214
+ else {
215
+ (_a = result.sources) === null || _a === void 0 ? void 0 : _a.push({
216
+ srcSet: (0, exports.getSrcSet)(images),
217
+ sizes: sizes,
218
+ type: "image/".concat(format)
219
+ });
220
+ }
221
+ });
222
+ var imageProps = {
223
+ images: result,
224
+ layout: layout,
225
+ backgroundColor: backgroundColor
226
+ };
227
+ if (placeholderURL) {
228
+ imageProps.placeholder = { fallback: placeholderURL };
229
+ }
230
+ switch (layout) {
231
+ case "fixed":
232
+ imageProps.width = imageSizes.presentationWidth;
233
+ imageProps.height = imageSizes.presentationHeight;
234
+ break;
235
+ case "fullWidth":
236
+ imageProps.width = 1;
237
+ imageProps.height = 1 / imageSizes.aspectRatio;
238
+ break;
239
+ case "constrained":
240
+ imageProps.width = args.width || imageSizes.presentationWidth || 1;
241
+ imageProps.height = (imageProps.width || 1) / imageSizes.aspectRatio;
242
+ }
243
+ return imageProps;
244
+ }
245
+ exports.generateImageData = generateImageData;
246
+ var dedupeAndSortDensities = function (values) {
247
+ return Array.from(new Set(__spreadArray([1], __read(values), false))).sort(sortNumeric);
248
+ };
249
+ function calculateImageSizes(args) {
250
+ var width = args.width, height = args.height, filename = args.filename, _a = args.layout, layout = _a === void 0 ? "constrained" : _a, imgDimensions = args.sourceMetadata, _b = args.reporter, reporter = _b === void 0 ? { warn: warn } : _b, _c = args.breakpoints, breakpoints = _c === void 0 ? exports.DEFAULT_BREAKPOINTS : _c;
251
+ // check that all dimensions provided are positive
252
+ var userDimensions = { width: width, height: height };
253
+ var erroneousUserDimensions = Object.entries(userDimensions).filter(function (_a) {
254
+ var _b = __read(_a, 2), _ = _b[0], size = _b[1];
255
+ return typeof size === "number" && size < 1;
256
+ });
257
+ if (erroneousUserDimensions.length) {
258
+ throw new Error("Specified dimensions for images must be positive numbers (> 0). Problem dimensions you have are ".concat(erroneousUserDimensions
259
+ .map(function (dim) { return dim.join(": "); })
260
+ .join(", ")));
261
+ }
262
+ if (layout === "fixed") {
263
+ return fixedImageSizes(args);
264
+ }
265
+ else if (layout === "constrained") {
266
+ return responsiveImageSizes(args);
267
+ }
268
+ else if (layout === "fullWidth") {
269
+ return responsiveImageSizes(__assign({ breakpoints: breakpoints }, args));
270
+ }
271
+ else {
272
+ reporter.warn("No valid layout was provided for the image at ".concat(filename, ". Valid image layouts are fixed, fullWidth, and constrained. Found ").concat(layout));
273
+ return {
274
+ sizes: [imgDimensions.width],
275
+ presentationWidth: imgDimensions.width,
276
+ presentationHeight: imgDimensions.height,
277
+ aspectRatio: imgDimensions.width / imgDimensions.height,
278
+ unscaledWidth: imgDimensions.width
279
+ };
280
+ }
281
+ }
282
+ exports.calculateImageSizes = calculateImageSizes;
283
+ function fixedImageSizes(_a) {
284
+ var filename = _a.filename, imgDimensions = _a.sourceMetadata, width = _a.width, height = _a.height, _b = _a.fit, fit = _b === void 0 ? "cover" : _b, _c = _a.outputPixelDensities, outputPixelDensities = _c === void 0 ? DEFAULT_PIXEL_DENSITIES : _c, _d = _a.reporter, reporter = _d === void 0 ? { warn: warn } : _d;
285
+ var aspectRatio = imgDimensions.width / imgDimensions.height;
286
+ // Sort, dedupe and ensure there's a 1
287
+ var densities = dedupeAndSortDensities(outputPixelDensities);
288
+ // If both are provided then we need to check the fit
289
+ if (width && height) {
290
+ var calculated = getDimensionsAndAspectRatio(imgDimensions, {
291
+ width: width,
292
+ height: height,
293
+ fit: fit
294
+ });
295
+ width = calculated.width;
296
+ height = calculated.height;
297
+ aspectRatio = calculated.aspectRatio;
298
+ }
299
+ if (!width) {
300
+ if (!height) {
301
+ width = DEFAULT_FIXED_WIDTH;
302
+ }
303
+ else {
304
+ width = Math.round(height * aspectRatio);
305
+ }
306
+ }
307
+ else if (!height) {
308
+ height = Math.round(width / aspectRatio);
309
+ }
310
+ var originalWidth = width; // will use this for presentationWidth, don't want to lose it
311
+ var isTopSizeOverriden = imgDimensions.width < width || imgDimensions.height < height;
312
+ // If the image is smaller than requested, warn the user that it's being processed as such
313
+ // print out this message with the necessary information before we overwrite it for sizing
314
+ if (isTopSizeOverriden) {
315
+ var fixedDimension = imgDimensions.width < width ? "width" : "height";
316
+ reporter.warn((0, common_tags_1.stripIndent)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n The requested ", " \"", "px\" for the image ", " was larger than the actual image ", " of ", "px. If possible, replace the current image with a larger one."], ["\n The requested ", " \"", "px\" for the image ", " was larger than the actual image ", " of ", "px. If possible, replace the current image with a larger one."])), fixedDimension, fixedDimension === "width" ? width : height, filename, fixedDimension, imgDimensions[fixedDimension]));
317
+ if (fixedDimension === "width") {
318
+ width = imgDimensions.width;
319
+ height = Math.round(width / aspectRatio);
320
+ }
321
+ else {
322
+ height = imgDimensions.height;
323
+ width = height * aspectRatio;
324
+ }
325
+ }
326
+ var sizes = densities
327
+ .filter(function (size) { return size >= 1; }) // remove smaller densities because fixed images don't need them
328
+ .map(function (density) { return Math.round(density * width); })
329
+ .filter(function (size) { return size <= imgDimensions.width; });
330
+ return {
331
+ sizes: sizes,
332
+ aspectRatio: aspectRatio,
333
+ presentationWidth: originalWidth,
334
+ presentationHeight: Math.round(originalWidth / aspectRatio),
335
+ unscaledWidth: width
336
+ };
337
+ }
338
+ exports.fixedImageSizes = fixedImageSizes;
339
+ function responsiveImageSizes(_a) {
340
+ var imgDimensions = _a.sourceMetadata, width = _a.width, height = _a.height, _b = _a.fit, fit = _b === void 0 ? "cover" : _b, _c = _a.outputPixelDensities, outputPixelDensities = _c === void 0 ? DEFAULT_PIXEL_DENSITIES : _c, breakpoints = _a.breakpoints, layout = _a.layout;
341
+ var sizes;
342
+ var aspectRatio = imgDimensions.width / imgDimensions.height;
343
+ // Sort, dedupe and ensure there's a 1
344
+ var densities = dedupeAndSortDensities(outputPixelDensities);
345
+ // If both are provided then we need to check the fit
346
+ if (width && height) {
347
+ var calculated = getDimensionsAndAspectRatio(imgDimensions, {
348
+ width: width,
349
+ height: height,
350
+ fit: fit
351
+ });
352
+ width = calculated.width;
353
+ height = calculated.height;
354
+ aspectRatio = calculated.aspectRatio;
355
+ }
356
+ // Case 1: width of height were passed in, make sure it isn't larger than the actual image
357
+ width = width && Math.min(width, imgDimensions.width);
358
+ height = height && Math.min(height, imgDimensions.height);
359
+ // Case 2: neither width or height were passed in, use default size
360
+ if (!width && !height) {
361
+ width = Math.min(DEFAULT_FLUID_WIDTH, imgDimensions.width);
362
+ height = width / aspectRatio;
363
+ }
364
+ // if it still hasn't been found, calculate width from the derived height.
365
+ // TS isn't smart enough to realise the type for height has been narrowed here
366
+ if (!width) {
367
+ width = height * aspectRatio;
368
+ }
369
+ var originalWidth = width;
370
+ var isTopSizeOverriden = imgDimensions.width < width || imgDimensions.height < height;
371
+ if (isTopSizeOverriden) {
372
+ width = imgDimensions.width;
373
+ height = imgDimensions.height;
374
+ }
375
+ width = Math.round(width);
376
+ if ((breakpoints === null || breakpoints === void 0 ? void 0 : breakpoints.length) > 0) {
377
+ sizes = breakpoints.filter(function (size) { return size <= imgDimensions.width; });
378
+ // If a larger breakpoint has been filtered-out, add the actual image width instead
379
+ if (sizes.length < breakpoints.length &&
380
+ !sizes.includes(imgDimensions.width)) {
381
+ sizes.push(imgDimensions.width);
382
+ }
383
+ }
384
+ else {
385
+ sizes = densities.map(function (density) { return Math.round(density * width); });
386
+ sizes = sizes.filter(function (size) { return size <= imgDimensions.width; });
387
+ }
388
+ // ensure that the size passed in is included in the final output
389
+ if (layout === "constrained" && !sizes.includes(width)) {
390
+ sizes.push(width);
391
+ }
392
+ sizes = sizes.sort(sortNumeric);
393
+ return {
394
+ sizes: sizes,
395
+ aspectRatio: aspectRatio,
396
+ presentationWidth: originalWidth,
397
+ presentationHeight: Math.round(originalWidth / aspectRatio),
398
+ unscaledWidth: width
399
+ };
400
+ }
401
+ exports.responsiveImageSizes = responsiveImageSizes;
402
+ function getDimensionsAndAspectRatio(dimensions, options) {
403
+ // Calculate the eventual width/height of the image.
404
+ var imageAspectRatio = dimensions.width / dimensions.height;
405
+ var width = options.width;
406
+ var height = options.height;
407
+ switch (options.fit) {
408
+ case "fill": {
409
+ width = options.width ? options.width : dimensions.width;
410
+ height = options.height ? options.height : dimensions.height;
411
+ break;
412
+ }
413
+ case "inside": {
414
+ var widthOption = options.width
415
+ ? options.width
416
+ : Number.MAX_SAFE_INTEGER;
417
+ var heightOption = options.height
418
+ ? options.height
419
+ : Number.MAX_SAFE_INTEGER;
420
+ width = Math.min(widthOption, Math.round(heightOption * imageAspectRatio));
421
+ height = Math.min(heightOption, Math.round(widthOption / imageAspectRatio));
422
+ break;
423
+ }
424
+ case "outside": {
425
+ var widthOption = options.width ? options.width : 0;
426
+ var heightOption = options.height ? options.height : 0;
427
+ width = Math.max(widthOption, Math.round(heightOption * imageAspectRatio));
428
+ height = Math.max(heightOption, Math.round(widthOption / imageAspectRatio));
429
+ break;
430
+ }
431
+ default: {
432
+ if (options.width && !options.height) {
433
+ width = options.width;
434
+ height = Math.round(options.width / imageAspectRatio);
435
+ }
436
+ if (options.height && !options.width) {
437
+ width = Math.round(options.height * imageAspectRatio);
438
+ height = options.height;
439
+ }
440
+ }
441
+ }
442
+ return {
443
+ width: width,
444
+ height: height,
445
+ aspectRatio: width / height
446
+ };
447
+ }
448
+ exports.getDimensionsAndAspectRatio = getDimensionsAndAspectRatio;
449
+ var templateObject_1;