packages/workbox-build/src/lib/runtime-caching-converter.js
function getOptionsString(options = {}) {
  let plugins = [];
  if (options.plugins) {
    // Using libs because JSON.stringify won't handle functions.
    plugins = options.plugins.map(stringifyWithoutComments);
    delete options.plugins;
  }

  // Pull handler-specific config from the options object, since they are
  // not directly used to construct a Plugin instance. If set, need to be
  // passed as options to the handler constructor instead.
  const handlerOptionKeys = [
    'cacheName',
    'networkTimeoutSeconds',
    'fetchOptions',
    'matchOptions',
  ];
  const handlerOptions = {};
  for (const key of handlerOptionKeys) {
    if (key in options) {
      handlerOptions[key] = options[key];
      delete options[key];
    }
  }

  const pluginsMapping = {
    backgroundSync: 'workbox.backgroundSync.Plugin',
    broadcastUpdate: 'workbox.broadcastUpdate.Plugin',
    expiration: 'workbox.expiration.Plugin',
    cacheableResponse: 'workbox.cacheableResponse.Plugin',
  };

  for (const [pluginName, pluginConfig] of Object.entries(options)) {
    // Ensure that we have some valid configuration to pass to Plugin().
    if (Object.keys(pluginConfig).length === 0) {
      continue;
    }

    const pluginString = pluginsMapping[pluginName];
    if (!pluginString) {
      throw new Error(`${errors['bad-runtime-caching-config']} ${pluginName}`);
    }

    let pluginCode;
    switch (pluginName) {
    // Special case logic for plugins that have a required parameter, and then
    // an additional optional config parameter.
      case 'backgroundSync': {
        const name = pluginConfig.name;
        pluginCode = `new ${pluginString}(${JSON.stringify(name)}`;
        if ('options' in pluginConfig) {
          pluginCode += `, ${stringifyWithoutComments(pluginConfig.options)}`;
        }
        pluginCode += `)`;

        break;
      }

      case 'broadcastUpdate': {
        const channelName = pluginConfig.channelName;
        const opts = Object.assign({channelName}, pluginConfig.options);
        pluginCode = `new ${pluginString}(${stringifyWithoutComments(opts)})`;

        break;
      }

      // For plugins that just pass in an Object to the constructor, like
      // expiration and cacheableResponse
      default: {
        pluginCode = `new ${pluginString}(${stringifyWithoutComments(
            pluginConfig
        )})`;
      }
    }

    plugins.push(pluginCode);
  }

  if (Object.keys(handlerOptions).length > 0 || plugins.length > 0) {
    const optionsString = JSON.stringify(handlerOptions).slice(1, -1);
    return ol`{
      ${optionsString ? optionsString + ',' : ''}
      plugins: [${plugins.join(', ')}]
    }`;
  } else {
    return '';
  }
}
function getOptionsString(options = {}) {
  let plugins = [];
  if (options.plugins) {
    // Using libs because JSON.stringify won't handle functions.
    plugins = options.plugins.map(stringifyWithoutComments);
    delete options.plugins;
  }

  // Pull handler-specific config from the options object, since they are
  // not directly used to construct a Plugin instance. If set, need to be
  // passed as options to the handler constructor instead.
  const handlerOptionKeys = [
    'cacheName',
    'networkTimeoutSeconds',
    'fetchOptions',
    'matchOptions',
  ];
  const handlerOptions = {};
  for (const key of handlerOptionKeys) {
    if (key in options) {
      handlerOptions[key] = options[key];
      delete options[key];
    }
  }

  const pluginsMapping = {
    backgroundSync: 'workbox.backgroundSync.Plugin',
    broadcastUpdate: 'workbox.broadcastUpdate.Plugin',
    expiration: 'workbox.expiration.Plugin',
    cacheableResponse: 'workbox.cacheableResponse.Plugin',
  };

  for (const [pluginName, pluginConfig] of Object.entries(options)) {
    // Ensure that we have some valid configuration to pass to Plugin().
    if (Object.keys(pluginConfig).length === 0) {
      continue;
    }

    const pluginString = pluginsMapping[pluginName];
    if (!pluginString) {
      throw new Error(`${errors['bad-runtime-caching-config']} ${pluginName}`);
    }

    let pluginCode;
    switch (pluginName) {
    // Special case logic for plugins that have a required parameter, and then
    // an additional optional config parameter.
      case 'backgroundSync': {
        const name = pluginConfig.name;
        pluginCode = `new ${pluginString}(${JSON.stringify(name)}`;
        if ('options' in pluginConfig) {
          pluginCode += `, ${stringifyWithoutComments(pluginConfig.options)}`;
        }
        pluginCode += `)`;

        break;
      }

      case 'broadcastUpdate': {
        const channelName = pluginConfig.channelName;
        const opts = Object.assign({channelName}, pluginConfig.options);
        pluginCode = `new ${pluginString}(${stringifyWithoutComments(opts)})`;

        break;
      }

      // For plugins that just pass in an Object to the constructor, like
      // expiration and cacheableResponse
      default: {
        pluginCode = `new ${pluginString}(${stringifyWithoutComments(
            pluginConfig
        )})`;
      }
    }

    plugins.push(pluginCode);
  }

  if (Object.keys(handlerOptions).length > 0 || plugins.length > 0) {
    const optionsString = JSON.stringify(handlerOptions).slice(1, -1);
    return ol`{
      ${optionsString ? optionsString + ',' : ''}
      plugins: [${plugins.join(', ')}]
    }`;
  } else {
    return '';
  }
}
function getOptionsString ( options = { } ) { let plugins = [ ] ; if ( options . plugins ) { // Using libs because JSON.stringify won't handle functions. plugins = options . plugins . map ( stringifyWithoutComments ) ; delete options . plugins ; } // Pull handler-specific config from the options object, since they are // not directly used to construct a Plugin instance. If set, need to be // passed as options to the handler constructor instead. const handlerOptionKeys = [ 'cacheName' , 'networkTimeoutSeconds' , 'fetchOptions' , 'matchOptions' , ] ; const handlerOptions = { } ; for ( const key of handlerOptionKeys ) { if ( key in options ) { handlerOptions [ key ] = options [ key ] ; delete options [ key ] ; } } const pluginsMapping = { backgroundSync : 'workbox.backgroundSync.Plugin' , broadcastUpdate : 'workbox.broadcastUpdate.Plugin' , expiration : 'workbox.expiration.Plugin' , cacheableResponse : 'workbox.cacheableResponse.Plugin' , } ; for ( const [ pluginName , pluginConfig ] of Object . entries ( options ) ) { // Ensure that we have some valid configuration to pass to Plugin(). if ( Object . keys ( pluginConfig ) . length === 0 ) { continue ; } const pluginString = pluginsMapping [ pluginName ] ; if ( ! pluginString ) { throw new Error ( ` ${ errors [ 'bad-runtime-caching-config' ] } ${ pluginName } ` ) ; } let pluginCode ; switch ( pluginName ) { // Special case logic for plugins that have a required parameter, and then // an additional optional config parameter. case 'backgroundSync' : { const name = pluginConfig . name ; pluginCode = ` ${ pluginString } ${ JSON . stringify ( name ) } ` ; if ( 'options' in pluginConfig ) { pluginCode += ` ${ stringifyWithoutComments ( pluginConfig . options ) } ` ; } pluginCode += ` ` ; break ; } case 'broadcastUpdate' : { const channelName = pluginConfig . channelName ; const opts = Object . assign ( { channelName } , pluginConfig . options ) ; pluginCode = ` ${ pluginString } ${ stringifyWithoutComments ( opts ) } ` ; break ; } // For plugins that just pass in an Object to the constructor, like // expiration and cacheableResponse default : { pluginCode = ` ${ pluginString } ${ stringifyWithoutComments ( pluginConfig ) } ` ; } } plugins . push ( pluginCode ) ; } if ( Object . keys ( handlerOptions ) . length > 0 || plugins . length > 0 ) { const optionsString = JSON . stringify ( handlerOptions ) . slice ( 1 , - 1 ) ; return ol ` ${ optionsString ? optionsString + ',' : '' } ${ plugins . join ( ', ' ) } ` ; } else { return '' ; } }
Given a set of options that configures `sw-toolbox`'s behavior, convert it
into a string that would configure equivalent `workbox-sw` behavior.

@param {Object} options See
https://googlechromelabs.github.io/sw-toolbox/api.html#options
@return {string} A JSON string representing the equivalent options.

@private
Given a set of options that configures sw - toolbox s behavior convert it into a string that would configure equivalent workbox - sw behavior .