SymlinkPlugin.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const forEachBail = require("./forEachBail");
  7. const { getPathsCached } = require("./getPaths");
  8. const { PathType, getType } = require("./util/path");
  9. /** @typedef {import("./Resolver")} Resolver */
  10. /** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
  11. /** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
  12. module.exports = class SymlinkPlugin {
  13. /**
  14. * @param {string | ResolveStepHook} source source
  15. * @param {string | ResolveStepHook} target target
  16. */
  17. constructor(source, target) {
  18. this.source = source;
  19. this.target = target;
  20. }
  21. /**
  22. * @param {Resolver} resolver the resolver
  23. * @returns {void}
  24. */
  25. apply(resolver) {
  26. const target = resolver.ensureHook(this.target);
  27. const fs = resolver.fileSystem;
  28. resolver
  29. .getHook(this.source)
  30. .tapAsync("SymlinkPlugin", (request, resolveContext, callback) => {
  31. if (request.ignoreSymlinks) return callback();
  32. const pathsResult = getPathsCached(
  33. fs,
  34. /** @type {string} */ (request.path),
  35. );
  36. const { paths, segments } = pathsResult;
  37. // `pathsResult.segments` is shared across callers via the cache.
  38. // The only place we need to mutate is `pathSegments[idx] = result`
  39. // when `fs.readlink` succeeds — which is rare (the vast majority
  40. // of paths contain no symlinks, e.g. every resolve on
  41. // `cache-predicate`'s no-symlink fixture). Defer the copy until
  42. // we actually see a symlink so the common no-symlink path stays
  43. // allocation-free.
  44. /** @type {string[] | null} */
  45. let pathSegments = null;
  46. let containsSymlink = false;
  47. let idx = -1;
  48. forEachBail(
  49. paths,
  50. /**
  51. * @param {string} path path
  52. * @param {(err?: null | Error, result?: null | number) => void} callback callback
  53. * @returns {void}
  54. */
  55. (path, callback) => {
  56. idx++;
  57. if (resolveContext.fileDependencies) {
  58. resolveContext.fileDependencies.add(path);
  59. }
  60. fs.readlink(path, (err, result) => {
  61. if (!err && result) {
  62. // First symlink seen — take our own copy now, so
  63. // the cached `segments` array stays pristine for
  64. // sibling resolves.
  65. if (pathSegments === null) {
  66. pathSegments = [...segments];
  67. }
  68. pathSegments[idx] = /** @type {string} */ (result);
  69. containsSymlink = true;
  70. // Shortcut when absolute symlink found
  71. const resultType = getType(result.toString());
  72. if (
  73. resultType === PathType.AbsoluteWin ||
  74. resultType === PathType.AbsolutePosix
  75. ) {
  76. return callback(null, idx);
  77. }
  78. }
  79. callback();
  80. });
  81. },
  82. /**
  83. * @param {null | Error=} err error
  84. * @param {null | number=} idx result
  85. * @returns {void}
  86. */
  87. (err, idx) => {
  88. if (!containsSymlink) return callback();
  89. // `containsSymlink === true` implies we took a copy in
  90. // `pathSegments` already, so it's non-null. The copy is
  91. // our own, so `slice` to trim is fine and spreading to
  92. // "unshare" is no longer necessary.
  93. const own = /** @type {string[]} */ (pathSegments);
  94. const resultSegments =
  95. typeof idx === "number" ? own.slice(0, idx + 1) : own;
  96. const result = resultSegments.reduceRight((a, b) =>
  97. resolver.join(a, b),
  98. );
  99. /** @type {ResolveRequest} */
  100. const obj = {
  101. ...request,
  102. path: result,
  103. };
  104. resolver.doResolve(
  105. target,
  106. obj,
  107. `resolved symlink to ${result}`,
  108. resolveContext,
  109. callback,
  110. );
  111. },
  112. );
  113. });
  114. }
  115. };