You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

57 lines
1.8 KiB

4 years ago
  1. 'use strict';
  2. const PACKAGE_NAME = require('../../package.json').name,
  3. PADDING = (new Array(11)).join(' ');
  4. /**
  5. * Format a debug message
  6. * @param {{resourcePath:string, loaders:Array, loaderIndex:number}} context A loader or compilation
  7. * @param {{input:Array.<string>, absolute:Array.<string>, output:Array.<string>, root:string}} info Source-map info
  8. * @returns {string} An encoded debug string
  9. */
  10. function debugMessage(context, info) {
  11. return [
  12. ' ',
  13. PACKAGE_NAME + ':',
  14. ' ' + context.resourcePath,
  15. formatField('@', precedingRequest(context)),
  16. formatField('INPUT', info.input || '(source-map absent)'),
  17. formatField('ABSOLUTE', info.absolute),
  18. formatField('OUTPUT', info.output),
  19. formatField('ROOT', info.root)
  20. ]
  21. .filter(Boolean)
  22. .join('\n');
  23. }
  24. module.exports = debugMessage;
  25. /**
  26. * Find the request that precedes this loader in the loader chain
  27. * @param {{loaders:Array, loaderIndex:number}} loader The loader context
  28. * @returns {string} The request of the preceding loader
  29. */
  30. function precedingRequest(loader) {
  31. var isLoader = ('loaderIndex' in loader) && ('loaders' in loader) && Array.isArray(loader.loaders);
  32. if (isLoader) {
  33. var index = loader.loaderIndex + 1;
  34. return (index in loader.loaders) ? loader.loaders[index].request : '(no preceding loader)';
  35. }
  36. }
  37. /**
  38. * Where the data is truthy then format it with a right-aligned title.
  39. * @param {string} title
  40. * @param {*} data The data to display
  41. * @returns {boolean|string} False where data is falsey, else formatted message
  42. */
  43. function formatField(title, data) {
  44. return !!data && (rightAlign(title) + formatData(data));
  45. function rightAlign(text) {
  46. return (PADDING + text + ' ').slice(-PADDING.length);
  47. }
  48. function formatData(data) {
  49. return Array.isArray(data) ? data.join('\n' + PADDING) : data;
  50. }
  51. }