SymlinkPlugin.js 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. var getPaths = require("./getPaths");
  6. var forEachBail = require("./forEachBail");
  7. function SymlinkPlugin(source, target) {
  8. this.source = source;
  9. this.target = target;
  10. }
  11. module.exports = SymlinkPlugin;
  12. SymlinkPlugin.prototype.apply = function(resolver) {
  13. var target = this.target;
  14. resolver.plugin(this.source, function(request, callback) {
  15. var _this = this;
  16. var fs = _this.fileSystem;
  17. var pathsResult = getPaths(request.path);
  18. var pathSeqments = pathsResult.seqments;
  19. var paths = pathsResult.paths;
  20. var containsSymlink = false;
  21. forEachBail.withIndex(paths, function(path, idx, callback) {
  22. fs.readlink(path, function(err, result) {
  23. if(!err && result) {
  24. pathSeqments[idx] = result;
  25. containsSymlink = true;
  26. // Shortcut when absolute symlink found
  27. if(/^(\/|[a-zA-z]:($|\\))/.test(result))
  28. return callback(null, idx);
  29. }
  30. callback();
  31. });
  32. }, function(err, idx) {
  33. if(!containsSymlink) return callback();
  34. var resultSeqments = typeof idx === "number" ? pathSeqments.slice(0, idx + 1) : pathSeqments.slice();
  35. var result = resultSeqments.reverse().reduce(function(a, b) {
  36. return _this.join(a, b);
  37. });
  38. var obj = Object.assign({}, request, {
  39. path: result
  40. });
  41. resolver.doResolve(target, obj, "resolved symlink to " + result, callback);
  42. });
  43. });
  44. };