gatsby-plugin-image 2.22.0 → 3.0.0-alpha-v5.25

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 (34) hide show
  1. package/dist/babel-helpers.js +67 -67
  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 +251 -251
  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-image.browser.js +1 -0
  14. package/dist/gatsby-image.browser.modern.js +1 -0
  15. package/dist/gatsby-node.js +58 -58
  16. package/dist/global.js +2 -2
  17. package/dist/image-utils.js +442 -442
  18. package/dist/index.js +32 -32
  19. package/dist/node-apis/image-processing.js +276 -276
  20. package/dist/node-apis/node-utils.js +11 -11
  21. package/dist/node-apis/parser.js +98 -98
  22. package/dist/node-apis/preprocess-source.js +117 -117
  23. package/dist/node-apis/watcher.js +129 -129
  24. package/dist/resolver-utils.js +120 -120
  25. package/dist/src/babel-helpers.d.ts +6 -6
  26. package/dist/src/babel-plugin-parse-static-images.d.ts +10 -10
  27. package/dist/src/gatsby-node.d.ts +5 -5
  28. package/dist/src/node-apis/image-processing.d.ts +29 -29
  29. package/dist/src/node-apis/node-utils.d.ts +1 -1
  30. package/dist/src/node-apis/parser.d.ts +11 -11
  31. package/dist/src/node-apis/preprocess-source.d.ts +2 -2
  32. package/dist/src/node-apis/watcher.d.ts +12 -12
  33. package/dist/src/resolver-utils.d.ts +21 -21
  34. package/package.json +7 -5
@@ -1,67 +1,67 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
14
- return (mod && mod.__esModule) ? mod : { "default": mod };
15
- };
16
- exports.__esModule = true;
17
- exports.hashOptions = exports.evaluateImageAttributes = exports.normalizeProps = exports.SHARP_ATTRIBUTES = void 0;
18
- var murmur_1 = require("babel-plugin-remove-graphql-queries/murmur");
19
- var babel_jsx_utils_1 = require("babel-jsx-utils");
20
- var camelcase_1 = __importDefault(require("camelcase"));
21
- exports.SHARP_ATTRIBUTES = new Set([
22
- "src",
23
- "layout",
24
- "formats",
25
- "aspectRatio",
26
- "quality",
27
- "avifOptions",
28
- "jpgOptions",
29
- "pngOptions",
30
- "webpOptions",
31
- "blurredOptions",
32
- "transformOptions",
33
- "width",
34
- "height",
35
- "placeholder",
36
- "tracedSVGOptions",
37
- "sizes",
38
- "backgroundColor",
39
- "breakpoints",
40
- "outputPixelDensities",
41
- ]);
42
- function normalizeProps(props) {
43
- var out = __assign({}, props);
44
- if (out.layout) {
45
- out.layout = (0, camelcase_1["default"])(out.layout);
46
- }
47
- if (out.placeholder) {
48
- out.placeholder = (0, camelcase_1["default"])(out.placeholder);
49
- if (out.placeholder === "tracedSvg") {
50
- out.placeholder = "tracedSVG";
51
- }
52
- }
53
- if (Array.isArray(out.formats)) {
54
- out.formats = out.formats.map(function (format) { return format.toLowerCase(); });
55
- }
56
- return out;
57
- }
58
- exports.normalizeProps = normalizeProps;
59
- function evaluateImageAttributes(nodePath, onError) {
60
- // Only get attributes that we need for generating the images
61
- return normalizeProps((0, babel_jsx_utils_1.getAttributeValues)(nodePath, onError, exports.SHARP_ATTRIBUTES));
62
- }
63
- exports.evaluateImageAttributes = evaluateImageAttributes;
64
- function hashOptions(options) {
65
- return "".concat((0, murmur_1.murmurhash)(JSON.stringify(options)));
66
- }
67
- exports.hashOptions = hashOptions;
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 __importDefault = (this && this.__importDefault) || function (mod) {
14
+ return (mod && mod.__esModule) ? mod : { "default": mod };
15
+ };
16
+ exports.__esModule = true;
17
+ exports.hashOptions = exports.evaluateImageAttributes = exports.normalizeProps = exports.SHARP_ATTRIBUTES = void 0;
18
+ var murmur_1 = require("babel-plugin-remove-graphql-queries/murmur");
19
+ var babel_jsx_utils_1 = require("babel-jsx-utils");
20
+ var camelcase_1 = __importDefault(require("camelcase"));
21
+ exports.SHARP_ATTRIBUTES = new Set([
22
+ "src",
23
+ "layout",
24
+ "formats",
25
+ "aspectRatio",
26
+ "quality",
27
+ "avifOptions",
28
+ "jpgOptions",
29
+ "pngOptions",
30
+ "webpOptions",
31
+ "blurredOptions",
32
+ "transformOptions",
33
+ "width",
34
+ "height",
35
+ "placeholder",
36
+ "tracedSVGOptions",
37
+ "sizes",
38
+ "backgroundColor",
39
+ "breakpoints",
40
+ "outputPixelDensities",
41
+ ]);
42
+ function normalizeProps(props) {
43
+ var out = __assign({}, props);
44
+ if (out.layout) {
45
+ out.layout = (0, camelcase_1["default"])(out.layout);
46
+ }
47
+ if (out.placeholder) {
48
+ out.placeholder = (0, camelcase_1["default"])(out.placeholder);
49
+ if (out.placeholder === "tracedSvg") {
50
+ out.placeholder = "tracedSVG";
51
+ }
52
+ }
53
+ if (Array.isArray(out.formats)) {
54
+ out.formats = out.formats.map(function (format) { return format.toLowerCase(); });
55
+ }
56
+ return out;
57
+ }
58
+ exports.normalizeProps = normalizeProps;
59
+ function evaluateImageAttributes(nodePath, onError) {
60
+ // Only get attributes that we need for generating the images
61
+ return normalizeProps((0, babel_jsx_utils_1.getAttributeValues)(nodePath, onError, exports.SHARP_ATTRIBUTES));
62
+ }
63
+ exports.evaluateImageAttributes = evaluateImageAttributes;
64
+ function hashOptions(options) {
65
+ return "".concat((0, murmur_1.murmurhash)(JSON.stringify(options)));
66
+ }
67
+ exports.hashOptions = hashOptions;
@@ -1,80 +1,80 @@
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 __importDefault = (this && this.__importDefault) || function (mod) {
7
- return (mod && mod.__esModule) ? mod : { "default": mod };
8
- };
9
- exports.__esModule = true;
10
- var babel_helpers_1 = require("./babel-helpers");
11
- var fs_extra_1 = __importDefault(require("fs-extra"));
12
- var path_1 = __importDefault(require("path"));
13
- var gatsby_core_utils_1 = require("gatsby-core-utils");
14
- var common_tags_1 = require("common-tags");
15
- /**
16
- * This is a plugin that finds StaticImage components and injects the image props into the component.
17
- * These props contain the image URLs etc, and were created earlier in the build process
18
- */
19
- function attrs(_a) {
20
- var t = _a.types, template = _a.template;
21
- return {
22
- visitor: {
23
- JSXOpeningElement: function (nodePath) {
24
- var _a;
25
- if (!nodePath
26
- .get("name")
27
- .referencesImport("gatsby-plugin-image", "StaticImage")) {
28
- return;
29
- }
30
- var unresolvedProps = [];
31
- var props = (0, babel_helpers_1.evaluateImageAttributes)(nodePath, function (prop) {
32
- unresolvedProps.push(prop);
33
- });
34
- var error;
35
- if (unresolvedProps.length) {
36
- // TODO: Add a shortlink to docs on the plugin
37
- error = "Could not find values for the following props at build time: ".concat(unresolvedProps.join(", "));
38
- console.warn("[gatsby-plugin-image] ".concat(error));
39
- }
40
- var hash = (0, babel_helpers_1.hashOptions)(props);
41
- var cacheDir = (_a = this.opts) === null || _a === void 0 ? void 0 : _a.cacheDir;
42
- if (!cacheDir) {
43
- console.warn("[gatsby-plugin-image] Couldn't find image data cache file");
44
- }
45
- var filename = path_1["default"].join(cacheDir, "".concat(hash, ".json"));
46
- var data;
47
- // If there's no src prop there's no point in checking if it exists
48
- if (!unresolvedProps.includes("src")) {
49
- try {
50
- data = fs_extra_1["default"].readJSONSync(filename);
51
- }
52
- catch (e) {
53
- // TODO add info about minimum Gatsby version once this is merged
54
- var msg = (0, common_tags_1.stripIndent)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n Could not read image data file \"", "\". \n This may mean that the images in \"", "\" were not processed.\n Please ensure that your gatsby version is at least 2.24.78."], ["\n Could not read image data file \"", "\". \n This may mean that the images in \"", "\" were not processed.\n Please ensure that your gatsby version is at least 2.24.78."])), filename, this.filename);
55
- error += msg;
56
- console.warn("[gatsby-plugin-image] ".concat(msg));
57
- }
58
- }
59
- if (!data) {
60
- console.warn("[gatsby-plugin-image] No data found for image \"".concat(props.src, "\""));
61
- // Add the error message to the component so we can show it in the browser
62
- var newProp_1 = t.jsxAttribute(t.jsxIdentifier("__error"), t.jsxExpressionContainer(t.stringLiteral("No data found for image \"".concat(props.src, "\"\n ").concat(error || ""))));
63
- nodePath.node.attributes.push(newProp_1);
64
- return;
65
- }
66
- if (error) {
67
- // Add the error message to the component so we can show it in the browser
68
- var newProp_2 = t.jsxAttribute(t.jsxIdentifier("__error"), t.stringLiteral(error));
69
- nodePath.node.attributes.push(newProp_2);
70
- }
71
- // `require()` the image data into a component prop
72
- var makeRequire = template.expression("require(\"".concat((0, gatsby_core_utils_1.slash)(filename), "\")"));
73
- var newProp = t.jsxAttribute(t.jsxIdentifier("__imageData"), t.jsxExpressionContainer(makeRequire()));
74
- nodePath.node.attributes.push(newProp);
75
- }
76
- }
77
- };
78
- }
79
- exports["default"] = attrs;
80
- 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 __importDefault = (this && this.__importDefault) || function (mod) {
7
+ return (mod && mod.__esModule) ? mod : { "default": mod };
8
+ };
9
+ exports.__esModule = true;
10
+ var babel_helpers_1 = require("./babel-helpers");
11
+ var fs_extra_1 = __importDefault(require("fs-extra"));
12
+ var path_1 = __importDefault(require("path"));
13
+ var gatsby_core_utils_1 = require("gatsby-core-utils");
14
+ var common_tags_1 = require("common-tags");
15
+ /**
16
+ * This is a plugin that finds StaticImage components and injects the image props into the component.
17
+ * These props contain the image URLs etc, and were created earlier in the build process
18
+ */
19
+ function attrs(_a) {
20
+ var t = _a.types, template = _a.template;
21
+ return {
22
+ visitor: {
23
+ JSXOpeningElement: function (nodePath) {
24
+ var _a;
25
+ if (!nodePath
26
+ .get("name")
27
+ .referencesImport("gatsby-plugin-image", "StaticImage")) {
28
+ return;
29
+ }
30
+ var unresolvedProps = [];
31
+ var props = (0, babel_helpers_1.evaluateImageAttributes)(nodePath, function (prop) {
32
+ unresolvedProps.push(prop);
33
+ });
34
+ var error;
35
+ if (unresolvedProps.length) {
36
+ // TODO: Add a shortlink to docs on the plugin
37
+ error = "Could not find values for the following props at build time: ".concat(unresolvedProps.join(", "));
38
+ console.warn("[gatsby-plugin-image] ".concat(error));
39
+ }
40
+ var hash = (0, babel_helpers_1.hashOptions)(props);
41
+ var cacheDir = (_a = this.opts) === null || _a === void 0 ? void 0 : _a.cacheDir;
42
+ if (!cacheDir) {
43
+ console.warn("[gatsby-plugin-image] Couldn't find image data cache file");
44
+ }
45
+ var filename = path_1["default"].join(cacheDir, "".concat(hash, ".json"));
46
+ var data;
47
+ // If there's no src prop there's no point in checking if it exists
48
+ if (!unresolvedProps.includes("src")) {
49
+ try {
50
+ data = fs_extra_1["default"].readJSONSync(filename);
51
+ }
52
+ catch (e) {
53
+ // TODO add info about minimum Gatsby version once this is merged
54
+ var msg = (0, common_tags_1.stripIndent)(templateObject_1 || (templateObject_1 = __makeTemplateObject(["\n Could not read image data file \"", "\". \n This may mean that the images in \"", "\" were not processed.\n Please ensure that your gatsby version is at least 2.24.78."], ["\n Could not read image data file \"", "\". \n This may mean that the images in \"", "\" were not processed.\n Please ensure that your gatsby version is at least 2.24.78."])), filename, this.filename);
55
+ error += msg;
56
+ console.warn("[gatsby-plugin-image] ".concat(msg));
57
+ }
58
+ }
59
+ if (!data) {
60
+ console.warn("[gatsby-plugin-image] No data found for image \"".concat(props.src, "\""));
61
+ // Add the error message to the component so we can show it in the browser
62
+ var newProp_1 = t.jsxAttribute(t.jsxIdentifier("__error"), t.jsxExpressionContainer(t.stringLiteral("No data found for image \"".concat(props.src, "\"\n ").concat(error || ""))));
63
+ nodePath.node.attributes.push(newProp_1);
64
+ return;
65
+ }
66
+ if (error) {
67
+ // Add the error message to the component so we can show it in the browser
68
+ var newProp_2 = t.jsxAttribute(t.jsxIdentifier("__error"), t.stringLiteral(error));
69
+ nodePath.node.attributes.push(newProp_2);
70
+ }
71
+ // `require()` the image data into a component prop
72
+ var makeRequire = template.expression("require(\"".concat((0, gatsby_core_utils_1.slash)(filename), "\")"));
73
+ var newProp = t.jsxAttribute(t.jsxIdentifier("__imageData"), t.jsxExpressionContainer(makeRequire()));
74
+ nodePath.node.attributes.push(newProp);
75
+ }
76
+ }
77
+ };
78
+ }
79
+ exports["default"] = attrs;
80
+ var templateObject_1;
@@ -1,173 +1,173 @@
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 __rest = (this && this.__rest) || function (s, e) {
37
- var t = {};
38
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
39
- t[p] = s[p];
40
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
41
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
42
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
43
- t[p[i]] = s[p[i]];
44
- }
45
- return t;
46
- };
47
- exports.__esModule = true;
48
- exports.GatsbyImage = void 0;
49
- var react_1 = require("react");
50
- var hooks_1 = require("./hooks");
51
- var layout_wrapper_1 = require("./layout-wrapper");
52
- var gatsby_image_server_1 = require("./gatsby-image.server");
53
- var imageCache = new Set();
54
- var renderImageToStringPromise;
55
- var renderImage;
56
- var GatsbyImageHydrator = function GatsbyImageHydrator(_a) {
57
- var _b = _a.as, as = _b === void 0 ? "div" : _b, image = _a.image, style = _a.style, backgroundColor = _a.backgroundColor, className = _a.className, preactClass = _a["class"], onStartLoad = _a.onStartLoad, onLoad = _a.onLoad, onError = _a.onError, props = __rest(_a, ["as", "image", "style", "backgroundColor", "className", "class", "onStartLoad", "onLoad", "onError"]);
58
- var width = image.width, height = image.height, layout = image.layout;
59
- var _c = (0, hooks_1.getWrapperProps)(width, height, layout), wStyle = _c.style, wClass = _c.className, wrapperProps = __rest(_c, ["style", "className"]);
60
- var root = (0, react_1.useRef)();
61
- var cacheKey = (0, react_1.useMemo)(function () { return JSON.stringify(image.images); }, [image.images]);
62
- // Preact uses class instead of className so we need to check for both
63
- if (preactClass) {
64
- className = preactClass;
65
- }
66
- var sizer = (0, layout_wrapper_1.getSizer)(layout, width, height);
67
- (0, react_1.useEffect)(function () {
68
- if (!renderImageToStringPromise) {
69
- renderImageToStringPromise = Promise.resolve().then(function () { return __importStar(require("./lazy-hydrate")); }).then(function (_a) {
70
- var renderImageToString = _a.renderImageToString, swapPlaceholderImage = _a.swapPlaceholderImage;
71
- renderImage = renderImageToString;
72
- return {
73
- renderImageToString: renderImageToString,
74
- swapPlaceholderImage: swapPlaceholderImage
75
- };
76
- });
77
- }
78
- // The plugin image component is a bit special where if it's server-side rendered, we add extra script tags to support lazy-loading without
79
- // In this case we stop hydration but fire the correct events.
80
- var ssrImage = root.current.querySelector("[data-gatsby-image-ssr]");
81
- if (ssrImage && (0, hooks_1.hasNativeLazyLoadSupport)()) {
82
- if (ssrImage.complete) {
83
- // Trigger onStartload and onLoad events
84
- onStartLoad === null || onStartLoad === void 0 ? void 0 : onStartLoad({
85
- wasCached: true
86
- });
87
- onLoad === null || onLoad === void 0 ? void 0 : onLoad({
88
- wasCached: true
89
- });
90
- // remove ssr key for state updates but add delay to not fight with native code snippt of gatsby-ssr
91
- setTimeout(function () {
92
- ssrImage.removeAttribute("data-gatsby-image-ssr");
93
- }, 0);
94
- }
95
- else {
96
- onStartLoad === null || onStartLoad === void 0 ? void 0 : onStartLoad({
97
- wasCached: true
98
- });
99
- ssrImage.addEventListener("load", function onLoadListener() {
100
- ssrImage.removeEventListener("load", onLoadListener);
101
- onLoad === null || onLoad === void 0 ? void 0 : onLoad({
102
- wasCached: true
103
- });
104
- // remove ssr key for state updates but add delay to not fight with native code snippt of gatsby-ssr
105
- setTimeout(function () {
106
- ssrImage.removeAttribute("data-gatsby-image-ssr");
107
- }, 0);
108
- });
109
- }
110
- imageCache.add(cacheKey);
111
- return;
112
- }
113
- if (renderImage && imageCache.has(cacheKey)) {
114
- return;
115
- }
116
- var animationFrame;
117
- var cleanupCallback;
118
- renderImageToStringPromise.then(function (_a) {
119
- var renderImageToString = _a.renderImageToString, swapPlaceholderImage = _a.swapPlaceholderImage;
120
- if (!root.current) {
121
- return;
122
- }
123
- root.current.innerHTML = renderImageToString(__assign({ isLoading: true, isLoaded: imageCache.has(cacheKey), image: image }, props));
124
- if (!imageCache.has(cacheKey)) {
125
- animationFrame = requestAnimationFrame(function () {
126
- if (root.current) {
127
- cleanupCallback = swapPlaceholderImage(root.current, cacheKey, imageCache, style, onStartLoad, onLoad, onError);
128
- }
129
- });
130
- }
131
- });
132
- // eslint-disable-next-line consistent-return
133
- return function () {
134
- if (animationFrame) {
135
- cancelAnimationFrame(animationFrame);
136
- }
137
- if (cleanupCallback) {
138
- cleanupCallback();
139
- }
140
- };
141
- }, [image]);
142
- // useLayoutEffect is ran before React commits to the DOM. This allows us to make sure our HTML is using our cached image version
143
- (0, react_1.useLayoutEffect)(function () {
144
- if (imageCache.has(cacheKey) && renderImage) {
145
- root.current.innerHTML = renderImage(__assign({ isLoading: imageCache.has(cacheKey), isLoaded: imageCache.has(cacheKey), image: image }, props));
146
- // Trigger onStartload and onLoad events
147
- onStartLoad === null || onStartLoad === void 0 ? void 0 : onStartLoad({
148
- wasCached: true
149
- });
150
- onLoad === null || onLoad === void 0 ? void 0 : onLoad({
151
- wasCached: true
152
- });
153
- }
154
- }, [image]);
155
- // By keeping all props equal React will keep the component in the DOM
156
- return (0, react_1.createElement)(as, __assign(__assign({}, wrapperProps), { style: __assign(__assign(__assign({}, wStyle), style), { backgroundColor: backgroundColor }), className: "".concat(wClass).concat(className ? " ".concat(className) : ""), ref: root, dangerouslySetInnerHTML: {
157
- __html: sizer
158
- }, suppressHydrationWarning: true }));
159
- };
160
- exports.GatsbyImage = (0, react_1.memo)(function GatsbyImage(props) {
161
- if (!props.image) {
162
- if (process.env.NODE_ENV === "development") {
163
- console.warn("[gatsby-plugin-image] Missing image prop");
164
- }
165
- return null;
166
- }
167
- if (!(0, hooks_1.gatsbyImageIsInstalled)() && process.env.NODE_ENV === "development") {
168
- console.warn("[gatsby-plugin-image] You're missing out on some cool performance features. Please add \"gatsby-plugin-image\" to your gatsby-config.js");
169
- }
170
- return (0, react_1.createElement)(GatsbyImageHydrator, props);
171
- });
172
- exports.GatsbyImage.propTypes = gatsby_image_server_1.propTypes;
173
- exports.GatsbyImage.displayName = "GatsbyImage";
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 __rest = (this && this.__rest) || function (s, e) {
37
+ var t = {};
38
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
39
+ t[p] = s[p];
40
+ if (s != null && typeof Object.getOwnPropertySymbols === "function")
41
+ for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
42
+ if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
43
+ t[p[i]] = s[p[i]];
44
+ }
45
+ return t;
46
+ };
47
+ exports.__esModule = true;
48
+ exports.GatsbyImage = void 0;
49
+ var react_1 = require("react");
50
+ var hooks_1 = require("./hooks");
51
+ var layout_wrapper_1 = require("./layout-wrapper");
52
+ var gatsby_image_server_1 = require("./gatsby-image.server");
53
+ var imageCache = new Set();
54
+ var renderImageToStringPromise;
55
+ var renderImage;
56
+ var GatsbyImageHydrator = function GatsbyImageHydrator(_a) {
57
+ var _b = _a.as, as = _b === void 0 ? "div" : _b, image = _a.image, style = _a.style, backgroundColor = _a.backgroundColor, className = _a.className, preactClass = _a["class"], onStartLoad = _a.onStartLoad, onLoad = _a.onLoad, onError = _a.onError, props = __rest(_a, ["as", "image", "style", "backgroundColor", "className", "class", "onStartLoad", "onLoad", "onError"]);
58
+ var width = image.width, height = image.height, layout = image.layout;
59
+ var _c = (0, hooks_1.getWrapperProps)(width, height, layout), wStyle = _c.style, wClass = _c.className, wrapperProps = __rest(_c, ["style", "className"]);
60
+ var root = (0, react_1.useRef)();
61
+ var cacheKey = (0, react_1.useMemo)(function () { return JSON.stringify(image.images); }, [image.images]);
62
+ // Preact uses class instead of className so we need to check for both
63
+ if (preactClass) {
64
+ className = preactClass;
65
+ }
66
+ var sizer = (0, layout_wrapper_1.getSizer)(layout, width, height);
67
+ (0, react_1.useEffect)(function () {
68
+ if (!renderImageToStringPromise) {
69
+ renderImageToStringPromise = Promise.resolve().then(function () { return __importStar(require("./lazy-hydrate")); }).then(function (_a) {
70
+ var renderImageToString = _a.renderImageToString, swapPlaceholderImage = _a.swapPlaceholderImage;
71
+ renderImage = renderImageToString;
72
+ return {
73
+ renderImageToString: renderImageToString,
74
+ swapPlaceholderImage: swapPlaceholderImage
75
+ };
76
+ });
77
+ }
78
+ // The plugin image component is a bit special where if it's server-side rendered, we add extra script tags to support lazy-loading without
79
+ // In this case we stop hydration but fire the correct events.
80
+ var ssrImage = root.current.querySelector("[data-gatsby-image-ssr]");
81
+ if (ssrImage && (0, hooks_1.hasNativeLazyLoadSupport)()) {
82
+ if (ssrImage.complete) {
83
+ // Trigger onStartload and onLoad events
84
+ onStartLoad === null || onStartLoad === void 0 ? void 0 : onStartLoad({
85
+ wasCached: true
86
+ });
87
+ onLoad === null || onLoad === void 0 ? void 0 : onLoad({
88
+ wasCached: true
89
+ });
90
+ // remove ssr key for state updates but add delay to not fight with native code snippt of gatsby-ssr
91
+ setTimeout(function () {
92
+ ssrImage.removeAttribute("data-gatsby-image-ssr");
93
+ }, 0);
94
+ }
95
+ else {
96
+ onStartLoad === null || onStartLoad === void 0 ? void 0 : onStartLoad({
97
+ wasCached: true
98
+ });
99
+ ssrImage.addEventListener("load", function onLoadListener() {
100
+ ssrImage.removeEventListener("load", onLoadListener);
101
+ onLoad === null || onLoad === void 0 ? void 0 : onLoad({
102
+ wasCached: true
103
+ });
104
+ // remove ssr key for state updates but add delay to not fight with native code snippt of gatsby-ssr
105
+ setTimeout(function () {
106
+ ssrImage.removeAttribute("data-gatsby-image-ssr");
107
+ }, 0);
108
+ });
109
+ }
110
+ imageCache.add(cacheKey);
111
+ return;
112
+ }
113
+ if (renderImage && imageCache.has(cacheKey)) {
114
+ return;
115
+ }
116
+ var animationFrame;
117
+ var cleanupCallback;
118
+ renderImageToStringPromise.then(function (_a) {
119
+ var renderImageToString = _a.renderImageToString, swapPlaceholderImage = _a.swapPlaceholderImage;
120
+ if (!root.current) {
121
+ return;
122
+ }
123
+ root.current.innerHTML = renderImageToString(__assign({ isLoading: true, isLoaded: imageCache.has(cacheKey), image: image }, props));
124
+ if (!imageCache.has(cacheKey)) {
125
+ animationFrame = requestAnimationFrame(function () {
126
+ if (root.current) {
127
+ cleanupCallback = swapPlaceholderImage(root.current, cacheKey, imageCache, style, onStartLoad, onLoad, onError);
128
+ }
129
+ });
130
+ }
131
+ });
132
+ // eslint-disable-next-line consistent-return
133
+ return function () {
134
+ if (animationFrame) {
135
+ cancelAnimationFrame(animationFrame);
136
+ }
137
+ if (cleanupCallback) {
138
+ cleanupCallback();
139
+ }
140
+ };
141
+ }, [image]);
142
+ // useLayoutEffect is ran before React commits to the DOM. This allows us to make sure our HTML is using our cached image version
143
+ (0, react_1.useLayoutEffect)(function () {
144
+ if (imageCache.has(cacheKey) && renderImage) {
145
+ root.current.innerHTML = renderImage(__assign({ isLoading: imageCache.has(cacheKey), isLoaded: imageCache.has(cacheKey), image: image }, props));
146
+ // Trigger onStartload and onLoad events
147
+ onStartLoad === null || onStartLoad === void 0 ? void 0 : onStartLoad({
148
+ wasCached: true
149
+ });
150
+ onLoad === null || onLoad === void 0 ? void 0 : onLoad({
151
+ wasCached: true
152
+ });
153
+ }
154
+ }, [image]);
155
+ // By keeping all props equal React will keep the component in the DOM
156
+ return (0, react_1.createElement)(as, __assign(__assign({}, wrapperProps), { style: __assign(__assign(__assign({}, wStyle), style), { backgroundColor: backgroundColor }), className: "".concat(wClass).concat(className ? " ".concat(className) : ""), ref: root, dangerouslySetInnerHTML: {
157
+ __html: sizer
158
+ }, suppressHydrationWarning: true }));
159
+ };
160
+ exports.GatsbyImage = (0, react_1.memo)(function GatsbyImage(props) {
161
+ if (!props.image) {
162
+ if (process.env.NODE_ENV === "development") {
163
+ console.warn("[gatsby-plugin-image] Missing image prop");
164
+ }
165
+ return null;
166
+ }
167
+ if (!(0, hooks_1.gatsbyImageIsInstalled)() && process.env.NODE_ENV === "development") {
168
+ console.warn("[gatsby-plugin-image] You're missing out on some cool performance features. Please add \"gatsby-plugin-image\" to your gatsby-config.js");
169
+ }
170
+ return (0, react_1.createElement)(GatsbyImageHydrator, props);
171
+ });
172
+ exports.GatsbyImage.propTypes = gatsby_image_server_1.propTypes;
173
+ exports.GatsbyImage.displayName = "GatsbyImage";