All files / src loader.js

100% Statements 8/8
87.5% Branches 7/8
100% Functions 1/1
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32    1x 1x             1x 2x 2x           2x         2x       2x        
'use strict'
 
const { getOptions, interpolateName } = require('loader-utils')
const spriteStore = require('./sprite-store')
 
/**
 * The loader handles:
 * 1. Transforming each imported SVG into a valid JS export.
 * 1. Adding the imported SVG into the sprite store.
 */
module.exports = function svgSymbolSpriteLoader(source) {
  const { resourcePath } = this
  const options = getOptions(this)
 
  // If provided, call the symbolId option to get a customized ID
  // Otherwise interpolate the name to pull the file name out of the resource path and
  // use it as the svg symbol id
  const symbolId =
    options && typeof options.symbolId === 'function'
      ? options.symbolId(resourcePath)
      : interpolateName({ resourcePath }, '[name]', { content: '' })
 
  // Sprite store returns data about added sprite that we use as the JS export value
  const svgData = spriteStore.addSVG(resourcePath, source, symbolId)
 
  // If a template was configured for the loader, call it with svgData to get
  // module value, otherwise return raw data as export
  return options && options.loaderTemplate
    ? options.loaderTemplate(svgData)
    : `export default ${JSON.stringify(svgData)}`
}