replaceAssets.js 858 B

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