{"version":3,"sources":["../../../../src/shared/lib/image-loader.ts"],"sourcesContent":["import type { ImageLoaderPropsWithConfig } from './image-config'\nimport { findClosestQuality } from './find-closest-quality'\nimport { getDeploymentId } from './deployment-id'\n\nfunction defaultLoader({\n  config,\n  src,\n  width,\n  quality,\n}: ImageLoaderPropsWithConfig): string {\n  if (process.env.NODE_ENV !== 'production') {\n    const missingValues = []\n\n    // these should always be provided but make sure they are\n    if (!src) missingValues.push('src')\n    if (!width) missingValues.push('width')\n\n    if (missingValues.length > 0) {\n      throw new Error(\n        `Next Image Optimization requires ${missingValues.join(\n          ', '\n        )} to be provided. Make sure you pass them as props to the \\`next/image\\` component. Received: ${JSON.stringify(\n          { src, width, quality }\n        )}`\n      )\n    }\n  }\n\n  // Extract dpl parameter early so validation uses the clean URL.\n  // If a immutable asset token should be used, it was already added as a query parameter and will\n  // be extracted and reused here.\n  let deploymentId = getDeploymentId()\n  if (src.startsWith('/') && !src.startsWith('//')) {\n    // We unfortunately can't easily use `new URL()` here, because it normalizes the URL which causes\n    // double-encoding with the `encodeURIComponent(src)` below\n    const qIndex = src.indexOf('?')\n    if (qIndex !== -1) {\n      const params = new URLSearchParams(src.slice(qIndex + 1))\n      const srcDpl = params.get('dpl')\n      if (srcDpl) {\n        deploymentId = srcDpl\n        params.delete('dpl')\n        const remaining = params.toString()\n        src = src.slice(0, qIndex) + (remaining ? '?' + remaining : '')\n      }\n    }\n  }\n\n  if (\n    src.startsWith('/') &&\n    src.includes('?') &&\n    config.localPatterns?.length === 1 &&\n    config.localPatterns[0].pathname === '**' &&\n    config.localPatterns[0].search === ''\n  ) {\n    throw new Error(\n      `Image with src \"${src}\" is using a query string which is not configured in images.localPatterns.` +\n        `\\nRead more: https://nextjs.org/docs/messages/next-image-unconfigured-localpatterns`\n    )\n  }\n\n  if (process.env.NODE_ENV !== 'production') {\n    if (src.startsWith('//')) {\n      throw new Error(\n        `Failed to parse src \"${src}\" on \\`next/image\\`, protocol-relative URL (//) must be changed to an absolute URL (http:// or https://)`\n      )\n    }\n\n    if (src.startsWith('/') && config.localPatterns) {\n      if (\n        process.env.NODE_ENV !== 'test' &&\n        // micromatch isn't compatible with edge runtime\n        process.env.NEXT_RUNTIME !== 'edge'\n      ) {\n        // We use dynamic require because this should only error in development\n        const { hasLocalMatch } =\n          require('./match-local-pattern') as typeof import('./match-local-pattern')\n        if (!hasLocalMatch(config.localPatterns, src)) {\n          throw new Error(\n            `Invalid src prop (${src}) on \\`next/image\\` does not match \\`images.localPatterns\\` configured in your \\`next.config.js\\`\\n` +\n              `See more info: https://nextjs.org/docs/messages/next-image-unconfigured-localpatterns`\n          )\n        }\n      }\n    }\n\n    if (!src.startsWith('/') && (config.domains || config.remotePatterns)) {\n      let parsedSrc: URL\n      try {\n        parsedSrc = new URL(src)\n      } catch (err) {\n        console.error(err)\n        throw new Error(\n          `Failed to parse src \"${src}\" on \\`next/image\\`, if using relative image it must start with a leading slash \"/\" or be an absolute URL (http:// or https://)`\n        )\n      }\n\n      if (\n        process.env.NODE_ENV !== 'test' &&\n        // micromatch isn't compatible with edge runtime\n        process.env.NEXT_RUNTIME !== 'edge'\n      ) {\n        // We use dynamic require because this should only error in development\n        const { hasRemoteMatch } =\n          require('./match-remote-pattern') as typeof import('./match-remote-pattern')\n        if (\n          !hasRemoteMatch(config.domains!, config.remotePatterns!, parsedSrc)\n        ) {\n          throw new Error(\n            `Invalid src prop (${src}) on \\`next/image\\`, hostname \"${parsedSrc.hostname}\" is not configured under images in your \\`next.config.js\\`\\n` +\n              `See more info: https://nextjs.org/docs/messages/next-image-unconfigured-host`\n          )\n        }\n      }\n    }\n  }\n\n  const q = findClosestQuality(quality, config)\n\n  return `${config.path}?url=${encodeURIComponent(src)}&w=${width}&q=${q}${\n    src.startsWith('/') && deploymentId ? `&dpl=${deploymentId}` : ''\n  }`\n}\n\n// We use this to determine if the import is the default loader\n// or a custom loader defined by the user in next.config.js\ndefaultLoader.__next_img_default = true\n\nexport default defaultLoader\n"],"names":["findClosestQuality","getDeploymentId","defaultLoader","config","src","width","quality","process","env","NODE_ENV","missingValues","push","length","Error","join","JSON","stringify","deploymentId","startsWith","qIndex","indexOf","params","URLSearchParams","slice","srcDpl","get","delete","remaining","toString","includes","localPatterns","pathname","search","NEXT_RUNTIME","hasLocalMatch","require","domains","remotePatterns","parsedSrc","URL","err","console","error","hasRemoteMatch","hostname","q","path","encodeURIComponent","__next_img_default"],"mappings":"AACA,SAASA,kBAAkB,QAAQ,yBAAwB;AAC3D,SAASC,eAAe,QAAQ,kBAAiB;AAEjD,SAASC,cAAc,EACrBC,MAAM,EACNC,GAAG,EACHC,KAAK,EACLC,OAAO,EACoB;IAC3B,IAAIC,QAAQC,GAAG,CAACC,QAAQ,KAAK,cAAc;QACzC,MAAMC,gBAAgB,EAAE;QAExB,yDAAyD;QACzD,IAAI,CAACN,KAAKM,cAAcC,IAAI,CAAC;QAC7B,IAAI,CAACN,OAAOK,cAAcC,IAAI,CAAC;QAE/B,IAAID,cAAcE,MAAM,GAAG,GAAG;YAC5B,MAAM,qBAML,CANK,IAAIC,MACR,CAAC,iCAAiC,EAAEH,cAAcI,IAAI,CACpD,MACA,6FAA6F,EAAEC,KAAKC,SAAS,CAC7G;gBAAEZ;gBAAKC;gBAAOC;YAAQ,IACrB,GALC,qBAAA;uBAAA;4BAAA;8BAAA;YAMN;QACF;IACF;IAEA,gEAAgE;IAChE,gGAAgG;IAChG,gCAAgC;IAChC,IAAIW,eAAehB;IACnB,IAAIG,IAAIc,UAAU,CAAC,QAAQ,CAACd,IAAIc,UAAU,CAAC,OAAO;QAChD,iGAAiG;QACjG,2DAA2D;QAC3D,MAAMC,SAASf,IAAIgB,OAAO,CAAC;QAC3B,IAAID,WAAW,CAAC,GAAG;YACjB,MAAME,SAAS,IAAIC,gBAAgBlB,IAAImB,KAAK,CAACJ,SAAS;YACtD,MAAMK,SAASH,OAAOI,GAAG,CAAC;YAC1B,IAAID,QAAQ;gBACVP,eAAeO;gBACfH,OAAOK,MAAM,CA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