Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

SymlinkPlugin.js 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const getPaths = require("./getPaths");
  7. const forEachBail = require("./forEachBail");
  8. module.exports = class SymlinkPlugin {
  9. constructor(source, target) {
  10. this.source = source;
  11. this.target = target;
  12. }
  13. apply(resolver) {
  14. const target = resolver.ensureHook(this.target);
  15. const fs = resolver.fileSystem;
  16. resolver.getHook(this.source).tapAsync("SymlinkPlugin", (request, resolveContext, callback) => {
  17. const pathsResult = getPaths(request.path);
  18. const pathSeqments = pathsResult.seqments;
  19. const paths = pathsResult.paths;
  20. let containsSymlink = false;
  21. forEachBail.withIndex(paths, (path, idx, callback) => {
  22. fs.readlink(path, (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. }, (err, idx) => {
  33. if(!containsSymlink) return callback();
  34. const resultSeqments = typeof idx === "number" ? pathSeqments.slice(0, idx + 1) : pathSeqments.slice();
  35. const result = resultSeqments.reverse().reduce((a, b) => {
  36. return resolver.join(a, b);
  37. });
  38. const obj = Object.assign({}, request, {
  39. path: result
  40. });
  41. resolver.doResolve(target, obj, "resolved symlink to " + result, resolveContext, callback);
  42. });
  43. });
  44. }
  45. };