UNPKG

gatsby-plugin-image

Version:

Adding responsive images to your site while maintaining high performance scores can be difficult to do manually. The Gatsby Image plugin handles the hard parts of producing images in multiple sizes and formats for you!

104 lines (103 loc) 3.76 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.extractStaticImageProps = exports.babelParseToAst = exports.getBabelParserOptions = void 0; var traverse_1 = __importDefault(require("@babel/traverse")); var parser_1 = require("@babel/parser"); var gatsby_core_utils_1 = require("gatsby-core-utils"); var babel_helpers_1 = require("../babel-helpers"); var PARSER_OPTIONS = { allowImportExportEverywhere: true, allowReturnOutsideFunction: true, allowSuperOutsideMethod: true, sourceType: "unambiguous", plugins: [ "jsx", "flow", "doExpressions", "objectRestSpread", [ "decorators", { decoratorsBeforeExport: true, }, ], "classProperties", "classPrivateProperties", "classPrivateMethods", "exportDefaultFrom", "exportNamespaceFrom", "asyncGenerators", "functionBind", "functionSent", "dynamicImport", "numericSeparator", "optionalChaining", "importMeta", "bigInt", "optionalCatchBinding", "throwExpressions", [ "pipelineOperator", { proposal: "minimal", }, ], "nullishCoalescingOperator", ], }; function getBabelParserOptions(filePath) { // Flow and TypeScript plugins can't be enabled simultaneously if (/\.tsx?/.test(filePath)) { var plugins = PARSER_OPTIONS.plugins; return __assign(__assign({}, PARSER_OPTIONS), { plugins: (plugins || []).map(function (plugin) { return plugin === "flow" ? "typescript" : plugin; }) }); } return PARSER_OPTIONS; } exports.getBabelParserOptions = getBabelParserOptions; function babelParseToAst(contents, filePath) { return (0, parser_1.parse)(contents, getBabelParserOptions(filePath)); } exports.babelParseToAst = babelParseToAst; /** * Traverses the parsed source, looking for StaticImage components. * Extracts and returns the props from any that are found */ var extractStaticImageProps = function (ast, filename, onError) { var images = new Map(); (0, traverse_1.default)(ast, { JSXOpeningElement: function (nodePath) { // Is this a StaticImage? if (!nodePath .get("name") .referencesImport("gatsby-plugin-image", "StaticImage")) { return; } var image = (0, babel_helpers_1.evaluateImageAttributes)( // There's a conflict between the definition of NodePath in @babel/core and @babel/traverse nodePath, onError); // When the image props are the same for multiple StaticImage but they are in different locations // the hash will be the same then. We need to make sure that the hash is unique. // The filename should already be normalized but better safe than sorry. image.filename = (0, gatsby_core_utils_1.slash)(filename); images.set((0, babel_helpers_1.hashOptions)(image), image); }, }); return images; }; exports.extractStaticImageProps = extractStaticImageProps;