output-root-relative.js 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict';
  2. var relative = require('./output-relative');
  3. /**
  4. * Codec for relative paths with respect to the output directory.
  5. * @type {{name:string, decode: function, encode: function, root: function}}
  6. */
  7. module.exports = {
  8. name : 'outputRootRelative',
  9. decode: decode,
  10. encode: encode,
  11. root : relative.root
  12. };
  13. /**
  14. * Decode the given uri.
  15. * Any path with leading slash is tested against output directory.
  16. * @this {{options: object}} A loader or compilation
  17. * @param {string} uri A source uri to decode
  18. * @returns {boolean|string} False where unmatched else the decoded path
  19. */
  20. function decode(uri) {
  21. /* jshint validthis:true */
  22. return uri.startsWith('/') && relative.decode.call(this, uri.slice(1));
  23. }
  24. /**
  25. * Encode the given file path.
  26. * @this {{options: object}} A loader or compilation
  27. * @param {string} absolute An absolute file path to encode
  28. * @returns {string} A uri with leading slash
  29. */
  30. function encode(absolute) {
  31. /* jshint validthis:true */
  32. return '/' + relative.encode.call(this, absolute);
  33. }