src/loader.js
/**
   * Downloads an image and writes it into disk cache (if option enabled).
   * Implements "options.beforeFetch" to allow customizing fetch parameters.
   * 
   * @param {String} imageUrl - The url of the remote image
   * @returns {Promise<Buffer>}
   */
Downloads an image and writes it into disk cache (if option enabled).
Implements "options.beforeFetch" to allow customizing fetch parameters.

@param {String} imageUrl - The url of the remote image
@returns {Promise}
Downloads an image and writes it into disk cache ( if option enabled ) . Implements " options . beforeFetch " to allow customizing fetch parameters . @param { String } imageUrl - The url of the remote image @returns { Promise }
async downloadImage(imageUrl) {
    let customOptions;
    if (typeof this.options.beforeFetch === 'function') {
      const result = await this.options.beforeFetch(imageUrl, this.fileInfo.resource);
      const makeError = (msg) => new LoaderError(`Result of "options.beforeFetch" ${msg}`).setFatal();
      if (result instanceof URL) {
        imageUrl = result.href;
      } else if (isStringNE(result)) {
        imageUrl = result;
      } else if (isPlainObject(result)) {
        const { fetchUrl, fetchOptions } = result;
        imageUrl = (fetchUrl instanceof URL) ? fetchUrl.href : fetchUrl;
        assert(isStringNE(imageUrl), makeError('has no or invalid value for key "fetchUrl".'));
        assert(isPlainObject(fetchOptions), makeError('has no or invalid value for key "fetchOptions".'));
        if (Object.keys(fetchOptions).length) {
          customOptions = { fetchOptions };
        }
      } else {
        assert.fail(makeError(`must be a non-empty instance of <String>|<URL>|<Object>. Got "${typeof result}".`));
      }
    }
    this.debug('download', {
      fetchFile: this.fileInfo.resource,
      fetchUrl: imageUrl,
      ...customOptions
    });
    return this.eleventyCache(imageUrl, {
      directory: this.options.cacheDir,
      duration: this.options.cacheDuration,
      dryRun: this.options.cacheDownloads ? false : true,
      type: "buffer",
      ...customOptions
    });
  }
async downloadImage ( imageUrl ) { let customOptions ; if ( typeof this . options . beforeFetch === 'function' ) { const result = await this . options . beforeFetch ( imageUrl , this . fileInfo . resource ) ; const makeError = ( msg ) => new LoaderError ( ` ${ msg } ` ) . setFatal ( ) ; if ( result instanceof URL ) { imageUrl = result . href ; } else if ( isStringNE ( result ) ) { imageUrl = result ; } else if ( isPlainObject ( result ) ) { const { fetchUrl , fetchOptions } = result ; imageUrl = ( fetchUrl instanceof URL ) ? fetchUrl . href : fetchUrl ; assert ( isStringNE ( imageUrl ) , makeError ( 'has no or invalid value for key "fetchUrl".' ) ) ; assert ( isPlainObject ( fetchOptions ) , makeError ( 'has no or invalid value for key "fetchOptions".' ) ) ; if ( Object . keys ( fetchOptions ) . length ) { customOptions = { fetchOptions } ; } } else { assert . fail ( makeError ( ` ${ typeof result } ` ) ) ; } } this . debug ( 'download' , { fetchFile : this . fileInfo . resource , fetchUrl : imageUrl , ... customOptions } ) ; return this . eleventyCache ( imageUrl , { directory : this . options . cacheDir , duration : this . options . cacheDuration , dryRun : this . options . cacheDownloads ? false : true , type : "buffer" , ... customOptions } ) ; }
Downloads an image and writes it into disk cache (if option enabled).
Downloads an image and writes it into disk cache ( if option enabled ) .
// add fetchOptions only if we have at least one option set // https://www.npmjs.com/package/node-fetch#options // Download image using @11ty/eleventy-cache-assets // Store the image in cache (or read from) if option is enabled // (https://www.11ty.dev/docs/plugins/cache)