absolute.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. 'use strict';
  2. var path = require('path'),
  3. fs = require('fs');
  4. /**
  5. * Codec for absolute paths.
  6. * @type {{name:string, decode: function, encode: function, root: function}}
  7. */
  8. module.exports = {
  9. name : 'absolute',
  10. decode: decode,
  11. encode: encode,
  12. root : root
  13. };
  14. /**
  15. * Decode the given uri.
  16. * Any path with leading slash is tested in an absolute sense.
  17. * @this {{options: object}} A loader or compilation
  18. * @param {string} uri A source uri to decode
  19. * @returns {boolean|string} False where unmatched else the decoded path
  20. */
  21. function decode(uri) {
  22. return path.isAbsolute(uri) && fs.existsSync(uri) && fs.statSync(uri).isFile() && uri;
  23. }
  24. /**
  25. * Encode the given file path.
  26. * @this {{options: object}} A loader or compilation
  27. * @returns {string} A uri
  28. */
  29. function encode(absolute) {
  30. return absolute;
  31. }
  32. /**
  33. * The source-map root where relevant.
  34. * @this {{options: object}} A loader or compilation
  35. * @returns {string|undefined} The source-map root applicable to any encoded uri
  36. */
  37. function root() {
  38. }