utils.js 773 B

123456789101112131415161718192021222324252627
  1. const { interpolateName, getHashDigest } = require('loader-utils');
  2. /**
  3. * @param {PostHTMLTree} tree
  4. * @return {Object} Node
  5. */
  6. exports.getRoot = function getRoot(tree) {
  7. return tree.find(node => typeof node === 'object' && 'tag' in node);
  8. };
  9. /**
  10. * @param {string|Function} pattern
  11. * @param {string} resourcePath
  12. * @param {Object} [options]
  13. * @param {string} options.context
  14. * @param {string} options.content
  15. * @param {string} options.regExp
  16. */
  17. exports.interpolate = function interpolate(pattern, resourcePath, options = null) {
  18. const opts = Object.assign({ context: process.cwd() }, options);
  19. return interpolateName({ resourcePath }, pattern, opts);
  20. };
  21. exports.getHash = function getHash(content) {
  22. return getHashDigest(content, 'md5', 'hex', 6);
  23. };