replaceAssets.js 825 B

1234567891011121314151617181920212223242526
  1. /**
  2. * The regular expression to find asset names in a string
  3. *
  4. * @type {RegExp}
  5. */
  6. const defaultAssetRegex = /[{]{2}([^}]+)[}]{2}/g;
  7. /**
  8. * Replaces asset names with the first of the asset files from a webpack compilation.
  9. *
  10. * @param {string} targetSource - a string in which to replace the assets
  11. * @param {Object} assetFiles - a map of available asset names and their containing files
  12. * @return {string}
  13. */
  14. function replaceAssets (targetSource, assetFiles) {
  15. return targetSource.replace(defaultAssetRegex, function replaceAsset (match, assetName) {
  16. if (!(assetFiles[assetName] && assetFiles[assetName].length)) {
  17. console.warn(`no asset for '${assetName}' was found`);
  18. return match;
  19. }
  20. return assetFiles[assetName][0];
  21. });
  22. }
  23. module.exports = replaceAssets;