createAsset.js 909 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. /**
  2. * @typedef {Object} WebpackAsset
  3. * @param {Function} size
  4. * @param {Function} source
  5. */
  6. /**
  7. * Creates a webpack asset object from a string. If the asset is falsy, the function returns
  8. * undefined.
  9. *
  10. * @param {string} assetContent - the content for the webpack asset
  11. * @param {string} [target] - a target name to be included in an eventual warning
  12. * @return {WebpackAsset|undefined}
  13. */
  14. function createAsset (assetContent, target) {
  15. if (!assetContent) {
  16. const message = [
  17. 'the asset content for the target',
  18. `'${target}'`,
  19. 'is empty'
  20. ]
  21. .filter((part) => !!part)
  22. .join(' ');
  23. console.warn(message);
  24. return;
  25. }
  26. return {
  27. size () {
  28. return assetContent.length;
  29. },
  30. source () {
  31. return assetContent;
  32. }
  33. };
  34. }
  35. module.exports = createAsset;