gatsby-plugin-image 2.19.0-next.2 → 2.19.0

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.
Files changed (32) hide show
  1. package/dist/babel-helpers.js +66 -66
  2. package/dist/babel-plugin-parse-static-images.js +80 -80
  3. package/dist/components/gatsby-image.browser.js +173 -173
  4. package/dist/components/gatsby-image.server.js +106 -106
  5. package/dist/components/hooks.js +250 -250
  6. package/dist/components/intersection-observer.js +53 -53
  7. package/dist/components/layout-wrapper.js +95 -95
  8. package/dist/components/lazy-hydrate.js +218 -218
  9. package/dist/components/main-image.js +28 -28
  10. package/dist/components/picture.js +103 -103
  11. package/dist/components/placeholder.js +78 -78
  12. package/dist/components/static-image.server.js +111 -111
  13. package/dist/gatsby-node.js +58 -58
  14. package/dist/global.js +2 -2
  15. package/dist/image-utils.js +442 -442
  16. package/dist/index.js +32 -32
  17. package/dist/node-apis/image-processing.js +276 -276
  18. package/dist/node-apis/node-utils.js +11 -11
  19. package/dist/node-apis/parser.js +98 -98
  20. package/dist/node-apis/preprocess-source.js +117 -117
  21. package/dist/node-apis/watcher.js +129 -129
  22. package/dist/resolver-utils.js +120 -120
  23. package/dist/src/babel-helpers.d.ts +6 -6
  24. package/dist/src/babel-plugin-parse-static-images.d.ts +10 -10
  25. package/dist/src/gatsby-node.d.ts +5 -5
  26. package/dist/src/node-apis/image-processing.d.ts +29 -29
  27. package/dist/src/node-apis/node-utils.d.ts +1 -1
  28. package/dist/src/node-apis/parser.d.ts +11 -11
  29. package/dist/src/node-apis/preprocess-source.d.ts +2 -2
  30. package/dist/src/node-apis/watcher.d.ts +12 -12
  31. package/dist/src/resolver-utils.d.ts +21 -21
  32. package/package.json +5 -5
@@ -1,95 +1,95 @@
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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
18
- if (k2 === undefined) k2 = k;
19
- var desc = Object.getOwnPropertyDescriptor(m, k);
20
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
21
- desc = { enumerable: true, get: function() { return m[k]; } };
22
- }
23
- Object.defineProperty(o, k2, desc);
24
- }) : (function(o, m, k, k2) {
25
- if (k2 === undefined) k2 = k;
26
- o[k2] = m[k];
27
- }));
28
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
29
- Object.defineProperty(o, "default", { enumerable: true, value: v });
30
- }) : function(o, v) {
31
- o["default"] = v;
32
- });
33
- var __importStar = (this && this.__importStar) || function (mod) {
34
- if (mod && mod.__esModule) return mod;
35
- var result = {};
36
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
37
- __setModuleDefault(result, mod);
38
- return result;
39
- };
40
- var __rest = (this && this.__rest) || function (s, e) {
41
- var t = {};
42
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
43
- t[p] = s[p];
44
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
45
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
46
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
47
- t[p[i]] = s[p[i]];
48
- }
49
- return t;
50
- };
51
- var __importDefault = (this && this.__importDefault) || function (mod) {
52
- return (mod && mod.__esModule) ? mod : { "default": mod };
53
- };
54
- exports.__esModule = true;
55
- exports.LayoutWrapper = exports.getSizer = void 0;
56
- var react_1 = __importStar(require("react"));
57
- var terser_macro_1 = __importDefault(require("../../macros/terser.macro"));
58
- var NativeScriptLoading = function () { return (react_1["default"].createElement("script", { type: "module", dangerouslySetInnerHTML: {
59
- __html: (0, terser_macro_1["default"])(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\nconst hasNativeLazyLoadSupport = typeof HTMLImageElement !== \"undefined\" && \"loading\" in HTMLImageElement.prototype;\nif (hasNativeLazyLoadSupport) {\n const gatsbyImages = document.querySelectorAll('img[data-main-image]');\n for (let mainImage of gatsbyImages) {\n if (mainImage.dataset.src) {\n mainImage.setAttribute('src', mainImage.dataset.src)\n mainImage.removeAttribute('data-src')\n }\n if (mainImage.dataset.srcset) {\n mainImage.setAttribute('srcset', mainImage.dataset.srcset)\n mainImage.removeAttribute('data-srcset')\n }\n\n const sources = mainImage.parentNode.querySelectorAll('source[data-srcset]');\n for (let source of sources) {\n source.setAttribute('srcset', source.dataset.srcset)\n source.removeAttribute('data-srcset')\n }\n\n if (mainImage.complete) {\n mainImage.style.opacity = 1;\n\n // also hide the placeholder\n mainImage.parentNode.parentNode.querySelector('[data-placeholder-image]').style.opacity = 0;\n }\n }\n}\n"], ["\nconst hasNativeLazyLoadSupport = typeof HTMLImageElement !== \"undefined\" && \"loading\" in HTMLImageElement.prototype;\nif (hasNativeLazyLoadSupport) {\n const gatsbyImages = document.querySelectorAll('img[data-main-image]');\n for (let mainImage of gatsbyImages) {\n if (mainImage.dataset.src) {\n mainImage.setAttribute('src', mainImage.dataset.src)\n mainImage.removeAttribute('data-src')\n }\n if (mainImage.dataset.srcset) {\n mainImage.setAttribute('srcset', mainImage.dataset.srcset)\n mainImage.removeAttribute('data-srcset')\n }\n\n const sources = mainImage.parentNode.querySelectorAll('source[data-srcset]');\n for (let source of sources) {\n source.setAttribute('srcset', source.dataset.srcset)\n source.removeAttribute('data-srcset')\n }\n\n if (mainImage.complete) {\n mainImage.style.opacity = 1;\n\n // also hide the placeholder\n mainImage.parentNode.parentNode.querySelector('[data-placeholder-image]').style.opacity = 0;\n }\n }\n}\n"])))
60
- } })); };
61
- function getSizer(layout, width, height) {
62
- var sizer = "";
63
- if (layout === "fullWidth") {
64
- sizer = "<div aria-hidden=\"true\" style=\"padding-top: ".concat((height / width) * 100, "%;\"></div>");
65
- }
66
- if (layout === "constrained") {
67
- sizer = "<div style=\"max-width: ".concat(width, "px; display: block;\"><img alt=\"\" role=\"presentation\" aria-hidden=\"true\" src=\"data:image/svg+xml;charset=utf-8,%3Csvg height='").concat(height, "' width='").concat(width, "' xmlns='http://www.w3.org/2000/svg' version='1.1'%3E%3C/svg%3E\" style=\"max-width: 100%; display: block; position: static;\"></div>");
68
- }
69
- return sizer;
70
- }
71
- exports.getSizer = getSizer;
72
- var Sizer = function Sizer(_a) {
73
- var layout = _a.layout, width = _a.width, height = _a.height;
74
- if (layout === "fullWidth") {
75
- return (react_1["default"].createElement("div", { "aria-hidden": true, style: { paddingTop: "".concat((height / width) * 100, "%") } }));
76
- }
77
- if (layout === "constrained") {
78
- return (react_1["default"].createElement("div", { style: { maxWidth: width, display: "block" } },
79
- react_1["default"].createElement("img", { alt: "", role: "presentation", "aria-hidden": "true", src: "data:image/svg+xml;charset=utf-8,%3Csvg height='".concat(height, "' width='").concat(width, "' xmlns='http://www.w3.org/2000/svg' version='1.1'%3E%3C/svg%3E"), style: {
80
- maxWidth: "100%",
81
- display: "block",
82
- position: "static"
83
- } })));
84
- }
85
- return null;
86
- };
87
- var LayoutWrapper = function LayoutWrapper(_a) {
88
- var children = _a.children, props = __rest(_a, ["children"]);
89
- return (react_1["default"].createElement(react_1.Fragment, null,
90
- react_1["default"].createElement(Sizer, __assign({}, props)),
91
- children,
92
- SERVER ? react_1["default"].createElement(NativeScriptLoading, null) : null));
93
- };
94
- exports.LayoutWrapper = LayoutWrapper;
95
- 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 __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
18
+ if (k2 === undefined) k2 = k;
19
+ var desc = Object.getOwnPropertyDescriptor(m, k);
20
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
21
+ desc = { enumerable: true, get: function() { return m[k]; } };
22
+ }
23
+ Object.defineProperty(o, k2, desc);
24
+ }) : (function(o, m, k, k2) {
25
+ if (k2 === undefined) k2 = k;
26
+ o[k2] = m[k];
27
+ }));
28
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
29
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
30
+ }) : function(o, v) {
31
+ o["default"] = v;
32
+ });
33
+ var __importStar = (this && this.__importStar) || function (mod) {
34
+ if (mod && mod.__esModule) return mod;
35
+ var result = {};
36
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
37
+ __setModuleDefault(result, mod);
38
+ return result;
39
+ };
40
+ var __rest = (this && this.__rest) || function (s, e) {
41
+ var t = {};
42
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
43
+ t[p] = s[p];
44
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
45
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
46
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
47
+ t[p[i]] = s[p[i]];
48
+ }
49
+ return t;
50
+ };
51
+ var __importDefault = (this && this.__importDefault) || function (mod) {
52
+ return (mod && mod.__esModule) ? mod : { "default": mod };
53
+ };
54
+ exports.__esModule = true;
55
+ exports.LayoutWrapper = exports.getSizer = void 0;
56
+ var react_1 = __importStar(require("react"));
57
+ var terser_macro_1 = __importDefault(require("../../macros/terser.macro"));
58
+ var NativeScriptLoading = function () { return (react_1["default"].createElement("script", { type: "module", dangerouslySetInnerHTML: {
59
+ __html: (0, terser_macro_1["default"])(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\nconst hasNativeLazyLoadSupport = typeof HTMLImageElement !== \"undefined\" && \"loading\" in HTMLImageElement.prototype;\nif (hasNativeLazyLoadSupport) {\n const gatsbyImages = document.querySelectorAll('img[data-main-image]');\n for (let mainImage of gatsbyImages) {\n if (mainImage.dataset.src) {\n mainImage.setAttribute('src', mainImage.dataset.src)\n mainImage.removeAttribute('data-src')\n }\n if (mainImage.dataset.srcset) {\n mainImage.setAttribute('srcset', mainImage.dataset.srcset)\n mainImage.removeAttribute('data-srcset')\n }\n\n const sources = mainImage.parentNode.querySelectorAll('source[data-srcset]');\n for (let source of sources) {\n source.setAttribute('srcset', source.dataset.srcset)\n source.removeAttribute('data-srcset')\n }\n\n if (mainImage.complete) {\n mainImage.style.opacity = 1;\n\n // also hide the placeholder\n mainImage.parentNode.parentNode.querySelector('[data-placeholder-image]').style.opacity = 0;\n }\n }\n}\n"], ["\nconst hasNativeLazyLoadSupport = typeof HTMLImageElement !== \"undefined\" && \"loading\" in HTMLImageElement.prototype;\nif (hasNativeLazyLoadSupport) {\n const gatsbyImages = document.querySelectorAll('img[data-main-image]');\n for (let mainImage of gatsbyImages) {\n if (mainImage.dataset.src) {\n mainImage.setAttribute('src', mainImage.dataset.src)\n mainImage.removeAttribute('data-src')\n }\n if (mainImage.dataset.srcset) {\n mainImage.setAttribute('srcset', mainImage.dataset.srcset)\n mainImage.removeAttribute('data-srcset')\n }\n\n const sources = mainImage.parentNode.querySelectorAll('source[data-srcset]');\n for (let source of sources) {\n source.setAttribute('srcset', source.dataset.srcset)\n source.removeAttribute('data-srcset')\n }\n\n if (mainImage.complete) {\n mainImage.style.opacity = 1;\n\n // also hide the placeholder\n mainImage.parentNode.parentNode.querySelector('[data-placeholder-image]').style.opacity = 0;\n }\n }\n}\n"])))
60
+ } })); };
61
+ function getSizer(layout, width, height) {
62
+ var sizer = "";
63
+ if (layout === "fullWidth") {
64
+ sizer = "<div aria-hidden=\"true\" style=\"padding-top: ".concat((height / width) * 100, "%;\"></div>");
65
+ }
66
+ if (layout === "constrained") {
67
+ sizer = "<div style=\"max-width: ".concat(width, "px; display: block;\"><img alt=\"\" role=\"presentation\" aria-hidden=\"true\" src=\"data:image/svg+xml;charset=utf-8,%3Csvg height='").concat(height, "' width='").concat(width, "' xmlns='http://www.w3.org/2000/svg' version='1.1'%3E%3C/svg%3E\" style=\"max-width: 100%; display: block; position: static;\"></div>");
68
+ }
69
+ return sizer;
70
+ }
71
+ exports.getSizer = getSizer;
72
+ var Sizer = function Sizer(_a) {
73
+ var layout = _a.layout, width = _a.width, height = _a.height;
74
+ if (layout === "fullWidth") {
75
+ return (react_1["default"].createElement("div", { "aria-hidden": true, style: { paddingTop: "".concat((height / width) * 100, "%") } }));
76
+ }
77
+ if (layout === "constrained") {
78
+ return (react_1["default"].createElement("div", { style: { maxWidth: width, display: "block" } },
79
+ react_1["default"].createElement("img", { alt: "", role: "presentation", "aria-hidden": "true", src: "data:image/svg+xml;charset=utf-8,%3Csvg height='".concat(height, "' width='").concat(width, "' xmlns='http://www.w3.org/2000/svg' version='1.1'%3E%3C/svg%3E"), style: {
80
+ maxWidth: "100%",
81
+ display: "block",
82
+ position: "static"
83
+ } })));
84
+ }
85
+ return null;
86
+ };
87
+ var LayoutWrapper = function LayoutWrapper(_a) {
88
+ var children = _a.children, props = __rest(_a, ["children"]);
89
+ return (react_1["default"].createElement(react_1.Fragment, null,
90
+ react_1["default"].createElement(Sizer, __assign({}, props)),
91
+ children,
92
+ SERVER ? react_1["default"].createElement(NativeScriptLoading, null) : null));
93
+ };
94
+ exports.LayoutWrapper = LayoutWrapper;
95
+ var templateObject_1;
@@ -1,218 +1,218 @@
1
- "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
- if (k2 === undefined) k2 = k;
15
- var desc = Object.getOwnPropertyDescriptor(m, k);
16
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
17
- desc = { enumerable: true, get: function() { return m[k]; } };
18
- }
19
- Object.defineProperty(o, k2, desc);
20
- }) : (function(o, m, k, k2) {
21
- if (k2 === undefined) k2 = k;
22
- o[k2] = m[k];
23
- }));
24
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
25
- Object.defineProperty(o, "default", { enumerable: true, value: v });
26
- }) : function(o, v) {
27
- o["default"] = v;
28
- });
29
- var __importStar = (this && this.__importStar) || function (mod) {
30
- if (mod && mod.__esModule) return mod;
31
- var result = {};
32
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
33
- __setModuleDefault(result, mod);
34
- return result;
35
- };
36
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
37
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
38
- return new (P || (P = Promise))(function (resolve, reject) {
39
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
40
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
41
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
42
- step((generator = generator.apply(thisArg, _arguments || [])).next());
43
- });
44
- };
45
- var __generator = (this && this.__generator) || function (thisArg, body) {
46
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
47
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
48
- function verb(n) { return function (v) { return step([n, v]); }; }
49
- function step(op) {
50
- if (f) throw new TypeError("Generator is already executing.");
51
- while (_) try {
52
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
53
- if (y = 0, t) op = [op[0] & 2, t.value];
54
- switch (op[0]) {
55
- case 0: case 1: t = op; break;
56
- case 4: _.label++; return { value: op[1], done: false };
57
- case 5: _.label++; y = op[1]; op = [0]; continue;
58
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
59
- default:
60
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
61
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
62
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
63
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
64
- if (t[2]) _.ops.pop();
65
- _.trys.pop(); continue;
66
- }
67
- op = body.call(thisArg, _);
68
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
69
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
70
- }
71
- };
72
- var __rest = (this && this.__rest) || function (s, e) {
73
- var t = {};
74
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
75
- t[p] = s[p];
76
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
77
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
78
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
79
- t[p[i]] = s[p[i]];
80
- }
81
- return t;
82
- };
83
- var __importDefault = (this && this.__importDefault) || function (mod) {
84
- return (mod && mod.__esModule) ? mod : { "default": mod };
85
- };
86
- exports.__esModule = true;
87
- exports.renderImageToString = exports.swapPlaceholderImage = void 0;
88
- var react_1 = __importDefault(require("react"));
89
- var server_1 = require("react-dom/server");
90
- var layout_wrapper_1 = require("./layout-wrapper");
91
- var placeholder_1 = require("./placeholder");
92
- var main_image_1 = require("./main-image");
93
- var hooks_1 = require("./hooks");
94
- var intersection_observer_1 = require("./intersection-observer");
95
- function applyPolyfill(element) {
96
- return __awaiter(this, void 0, void 0, function () {
97
- return __generator(this, function (_a) {
98
- switch (_a.label) {
99
- case 0:
100
- if (!!("objectFitPolyfill" in window)) return [3 /*break*/, 2];
101
- return [4 /*yield*/, Promise.resolve().then(function () { return __importStar(require(
102
- // @ts-ignore typescript can't find the module for some reason ¯\_(ツ)_/¯
103
- /* webpackChunkName: "gatsby-plugin-image-objectfit-polyfill" */ "objectFitPolyfill")); })];
104
- case 1:
105
- _a.sent();
106
- _a.label = 2;
107
- case 2:
108
- ;
109
- window.objectFitPolyfill(element);
110
- return [2 /*return*/];
111
- }
112
- });
113
- });
114
- }
115
- function toggleLoaded(mainImage, placeholderImage) {
116
- mainImage.style.opacity = "1";
117
- if (placeholderImage) {
118
- placeholderImage.style.opacity = "0";
119
- }
120
- }
121
- function startLoading(element, cacheKey, imageCache, onStartLoad, onLoad, onError) {
122
- var mainImage = element.querySelector("[data-main-image]");
123
- var placeholderImage = element.querySelector("[data-placeholder-image]");
124
- var isCached = imageCache.has(cacheKey);
125
- function onImageLoaded(e) {
126
- var _this = this;
127
- // eslint-disable-next-line @babel/no-invalid-this
128
- this.removeEventListener("load", onImageLoaded);
129
- var target = e.currentTarget;
130
- var img = new Image();
131
- img.src = target.currentSrc;
132
- if (img.decode) {
133
- // Decode the image through javascript to support our transition
134
- img
135
- .decode()
136
- .then(function () {
137
- // eslint-disable-next-line @babel/no-invalid-this
138
- toggleLoaded(_this, placeholderImage);
139
- onLoad === null || onLoad === void 0 ? void 0 : onLoad({
140
- wasCached: isCached
141
- });
142
- })["catch"](function (e) {
143
- // eslint-disable-next-line @babel/no-invalid-this
144
- toggleLoaded(_this, placeholderImage);
145
- onError === null || onError === void 0 ? void 0 : onError(e);
146
- });
147
- }
148
- else {
149
- // eslint-disable-next-line @babel/no-invalid-this
150
- toggleLoaded(this, placeholderImage);
151
- onLoad === null || onLoad === void 0 ? void 0 : onLoad({
152
- wasCached: isCached
153
- });
154
- }
155
- }
156
- mainImage.addEventListener("load", onImageLoaded);
157
- onStartLoad === null || onStartLoad === void 0 ? void 0 : onStartLoad({
158
- wasCached: isCached
159
- });
160
- Array.from(mainImage.parentElement.children).forEach(function (child) {
161
- var src = child.getAttribute("data-src");
162
- var srcSet = child.getAttribute("data-srcset");
163
- if (src) {
164
- child.removeAttribute("data-src");
165
- child.setAttribute("src", src);
166
- }
167
- if (srcSet) {
168
- child.removeAttribute("data-srcset");
169
- child.setAttribute("srcset", srcSet);
170
- }
171
- });
172
- imageCache.add(cacheKey);
173
- // Load times not always fires - mostly when it's a 304
174
- // We check if the image is already completed and if so we trigger onload.
175
- if (mainImage.complete) {
176
- onImageLoaded.call(mainImage, {
177
- currentTarget: mainImage
178
- });
179
- }
180
- return function () {
181
- if (mainImage) {
182
- mainImage.removeEventListener("load", onImageLoaded);
183
- }
184
- };
185
- }
186
- function swapPlaceholderImage(element, cacheKey, imageCache, style, onStartLoad, onLoad, onError) {
187
- var _a, _b;
188
- if (!(0, hooks_1.hasNativeLazyLoadSupport)()) {
189
- var cleanup_1;
190
- var io = (0, intersection_observer_1.createIntersectionObserver)(function () {
191
- cleanup_1 = startLoading(element, cacheKey, imageCache, onStartLoad, onLoad, onError);
192
- });
193
- var unobserve_1 = io(element);
194
- // Polyfill "object-fit" if unsupported (mostly IE)
195
- if (!("objectFit" in document.documentElement.style)) {
196
- element.dataset.objectFit = (_a = style.objectFit) !== null && _a !== void 0 ? _a : "cover";
197
- element.dataset.objectPosition = "".concat((_b = style.objectPosition) !== null && _b !== void 0 ? _b : "50% 50%");
198
- applyPolyfill(element);
199
- }
200
- return function () {
201
- if (cleanup_1) {
202
- cleanup_1();
203
- }
204
- unobserve_1();
205
- };
206
- }
207
- return startLoading(element, cacheKey, imageCache, onStartLoad, onLoad, onError);
208
- }
209
- exports.swapPlaceholderImage = swapPlaceholderImage;
210
- function renderImageToString(_a) {
211
- var image = _a.image, _b = _a.loading, loading = _b === void 0 ? "lazy" : _b, isLoading = _a.isLoading, isLoaded = _a.isLoaded, imgClassName = _a.imgClassName, _c = _a.imgStyle, imgStyle = _c === void 0 ? {} : _c, objectPosition = _a.objectPosition, backgroundColor = _a.backgroundColor, _d = _a.objectFit, objectFit = _d === void 0 ? "cover" : _d, props = __rest(_a, ["image", "loading", "isLoading", "isLoaded", "imgClassName", "imgStyle", "objectPosition", "backgroundColor", "objectFit"]);
212
- var width = image.width, height = image.height, layout = image.layout, images = image.images, placeholder = image.placeholder, wrapperBackgroundColor = image.backgroundColor;
213
- imgStyle = __assign({ objectFit: objectFit, objectPosition: objectPosition, backgroundColor: backgroundColor }, imgStyle);
214
- return (0, server_1.renderToStaticMarkup)(react_1["default"].createElement(layout_wrapper_1.LayoutWrapper, { layout: layout, width: width, height: height },
215
- react_1["default"].createElement(placeholder_1.Placeholder, __assign({}, (0, hooks_1.getPlaceholderProps)(placeholder, isLoaded, layout, width, height, wrapperBackgroundColor, objectFit, objectPosition))),
216
- react_1["default"].createElement(main_image_1.MainImage, __assign({}, props, { width: width, height: height, className: imgClassName }, (0, hooks_1.getMainProps)(isLoading, isLoaded, images, loading, imgStyle)))));
217
- }
218
- exports.renderImageToString = renderImageToString;
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14
+ if (k2 === undefined) k2 = k;
15
+ var desc = Object.getOwnPropertyDescriptor(m, k);
16
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
17
+ desc = { enumerable: true, get: function() { return m[k]; } };
18
+ }
19
+ Object.defineProperty(o, k2, desc);
20
+ }) : (function(o, m, k, k2) {
21
+ if (k2 === undefined) k2 = k;
22
+ o[k2] = m[k];
23
+ }));
24
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
25
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
26
+ }) : function(o, v) {
27
+ o["default"] = v;
28
+ });
29
+ var __importStar = (this && this.__importStar) || function (mod) {
30
+ if (mod && mod.__esModule) return mod;
31
+ var result = {};
32
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
33
+ __setModuleDefault(result, mod);
34
+ return result;
35
+ };
36
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
37
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
38
+ return new (P || (P = Promise))(function (resolve, reject) {
39
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
40
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
41
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
42
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
43
+ });
44
+ };
45
+ var __generator = (this && this.__generator) || function (thisArg, body) {
46
+ var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
47
+ return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
48
+ function verb(n) { return function (v) { return step([n, v]); }; }
49
+ function step(op) {
50
+ if (f) throw new TypeError("Generator is already executing.");
51
+ while (_) try {
52
+ if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
53
+ if (y = 0, t) op = [op[0] & 2, t.value];
54
+ switch (op[0]) {
55
+ case 0: case 1: t = op; break;
56
+ case 4: _.label++; return { value: op[1], done: false };
57
+ case 5: _.label++; y = op[1]; op = [0]; continue;
58
+ case 7: op = _.ops.pop(); _.trys.pop(); continue;
59
+ default:
60
+ if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
61
+ if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
62
+ if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
63
+ if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
64
+ if (t[2]) _.ops.pop();
65
+ _.trys.pop(); continue;
66
+ }
67
+ op = body.call(thisArg, _);
68
+ } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
69
+ if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
70
+ }
71
+ };
72
+ var __rest = (this && this.__rest) || function (s, e) {
73
+ var t = {};
74
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
75
+ t[p] = s[p];
76
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
77
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
78
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
79
+ t[p[i]] = s[p[i]];
80
+ }
81
+ return t;
82
+ };
83
+ var __importDefault = (this && this.__importDefault) || function (mod) {
84
+ return (mod && mod.__esModule) ? mod : { "default": mod };
85
+ };
86
+ exports.__esModule = true;
87
+ exports.renderImageToString = exports.swapPlaceholderImage = void 0;
88
+ var react_1 = __importDefault(require("react"));
89
+ var server_1 = require("react-dom/server");
90
+ var layout_wrapper_1 = require("./layout-wrapper");
91
+ var placeholder_1 = require("./placeholder");
92
+ var main_image_1 = require("./main-image");
93
+ var hooks_1 = require("./hooks");
94
+ var intersection_observer_1 = require("./intersection-observer");
95
+ function applyPolyfill(element) {
96
+ return __awaiter(this, void 0, void 0, function () {
97
+ return __generator(this, function (_a) {
98
+ switch (_a.label) {
99
+ case 0:
100
+ if (!!("objectFitPolyfill" in window)) return [3 /*break*/, 2];
101
+ return [4 /*yield*/, Promise.resolve().then(function () { return __importStar(require(
102
+ // @ts-ignore typescript can't find the module for some reason ¯\_(ツ)_/¯
103
+ /* webpackChunkName: "gatsby-plugin-image-objectfit-polyfill" */ "objectFitPolyfill")); })];
104
+ case 1:
105
+ _a.sent();
106
+ _a.label = 2;
107
+ case 2:
108
+ ;
109
+ window.objectFitPolyfill(element);
110
+ return [2 /*return*/];
111
+ }
112
+ });
113
+ });
114
+ }
115
+ function toggleLoaded(mainImage, placeholderImage) {
116
+ mainImage.style.opacity = "1";
117
+ if (placeholderImage) {
118
+ placeholderImage.style.opacity = "0";
119
+ }
120
+ }
121
+ function startLoading(element, cacheKey, imageCache, onStartLoad, onLoad, onError) {
122
+ var mainImage = element.querySelector("[data-main-image]");
123
+ var placeholderImage = element.querySelector("[data-placeholder-image]");
124
+ var isCached = imageCache.has(cacheKey);
125
+ function onImageLoaded(e) {
126
+ var _this = this;
127
+ // eslint-disable-next-line @babel/no-invalid-this
128
+ this.removeEventListener("load", onImageLoaded);
129
+ var target = e.currentTarget;
130
+ var img = new Image();
131
+ img.src = target.currentSrc;
132
+ if (img.decode) {
133
+ // Decode the image through javascript to support our transition
134
+ img
135
+ .decode()
136
+ .then(function () {
137
+ // eslint-disable-next-line @babel/no-invalid-this
138
+ toggleLoaded(_this, placeholderImage);
139
+ onLoad === null || onLoad === void 0 ? void 0 : onLoad({
140
+ wasCached: isCached
141
+ });
142
+ })["catch"](function (e) {
143
+ // eslint-disable-next-line @babel/no-invalid-this
144
+ toggleLoaded(_this, placeholderImage);
145
+ onError === null || onError === void 0 ? void 0 : onError(e);
146
+ });
147
+ }
148
+ else {
149
+ // eslint-disable-next-line @babel/no-invalid-this
150
+ toggleLoaded(this, placeholderImage);
151
+ onLoad === null || onLoad === void 0 ? void 0 : onLoad({
152
+ wasCached: isCached
153
+ });
154
+ }
155
+ }
156
+ mainImage.addEventListener("load", onImageLoaded);
157
+ onStartLoad === null || onStartLoad === void 0 ? void 0 : onStartLoad({
158
+ wasCached: isCached
159
+ });
160
+ Array.from(mainImage.parentElement.children).forEach(function (child) {
161
+ var src = child.getAttribute("data-src");
162
+ var srcSet = child.getAttribute("data-srcset");
163
+ if (src) {
164
+ child.removeAttribute("data-src");
165
+ child.setAttribute("src", src);
166
+ }
167
+ if (srcSet) {
168
+ child.removeAttribute("data-srcset");
169
+ child.setAttribute("srcset", srcSet);
170
+ }
171
+ });
172
+ imageCache.add(cacheKey);
173
+ // Load times not always fires - mostly when it's a 304
174
+ // We check if the image is already completed and if so we trigger onload.
175
+ if (mainImage.complete) {
176
+ onImageLoaded.call(mainImage, {
177
+ currentTarget: mainImage
178
+ });
179
+ }
180
+ return function () {
181
+ if (mainImage) {
182
+ mainImage.removeEventListener("load", onImageLoaded);
183
+ }
184
+ };
185
+ }
186
+ function swapPlaceholderImage(element, cacheKey, imageCache, style, onStartLoad, onLoad, onError) {
187
+ var _a, _b;
188
+ if (!(0, hooks_1.hasNativeLazyLoadSupport)()) {
189
+ var cleanup_1;
190
+ var io = (0, intersection_observer_1.createIntersectionObserver)(function () {
191
+ cleanup_1 = startLoading(element, cacheKey, imageCache, onStartLoad, onLoad, onError);
192
+ });
193
+ var unobserve_1 = io(element);
194
+ // Polyfill "object-fit" if unsupported (mostly IE)
195
+ if (!("objectFit" in document.documentElement.style)) {
196
+ element.dataset.objectFit = (_a = style.objectFit) !== null && _a !== void 0 ? _a : "cover";
197
+ element.dataset.objectPosition = "".concat((_b = style.objectPosition) !== null && _b !== void 0 ? _b : "50% 50%");
198
+ applyPolyfill(element);
199
+ }
200
+ return function () {
201
+ if (cleanup_1) {
202
+ cleanup_1();
203
+ }
204
+ unobserve_1();
205
+ };
206
+ }
207
+ return startLoading(element, cacheKey, imageCache, onStartLoad, onLoad, onError);
208
+ }
209
+ exports.swapPlaceholderImage = swapPlaceholderImage;
210
+ function renderImageToString(_a) {
211
+ var image = _a.image, _b = _a.loading, loading = _b === void 0 ? "lazy" : _b, isLoading = _a.isLoading, isLoaded = _a.isLoaded, imgClassName = _a.imgClassName, _c = _a.imgStyle, imgStyle = _c === void 0 ? {} : _c, objectPosition = _a.objectPosition, backgroundColor = _a.backgroundColor, _d = _a.objectFit, objectFit = _d === void 0 ? "cover" : _d, props = __rest(_a, ["image", "loading", "isLoading", "isLoaded", "imgClassName", "imgStyle", "objectPosition", "backgroundColor", "objectFit"]);
212
+ var width = image.width, height = image.height, layout = image.layout, images = image.images, placeholder = image.placeholder, wrapperBackgroundColor = image.backgroundColor;
213
+ imgStyle = __assign({ objectFit: objectFit, objectPosition: objectPosition, backgroundColor: backgroundColor }, imgStyle);
214
+ return (0, server_1.renderToStaticMarkup)(react_1["default"].createElement(layout_wrapper_1.LayoutWrapper, { layout: layout, width: width, height: height },
215
+ react_1["default"].createElement(placeholder_1.Placeholder, __assign({}, (0, hooks_1.getPlaceholderProps)(placeholder, isLoaded, layout, width, height, wrapperBackgroundColor, objectFit, objectPosition))),
216
+ react_1["default"].createElement(main_image_1.MainImage, __assign({}, props, { width: width, height: height, className: imgClassName }, (0, hooks_1.getMainProps)(isLoading, isLoaded, images, loading, imgStyle)))));
217
+ }
218
+ exports.renderImageToString = renderImageToString;