SymlinkPlugin.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 getPaths = require("./getPaths");
  8. const { getType, PathType } = 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 = getPaths(/** @type {string} */ (request.path));
  33. const pathSegments = pathsResult.segments;
  34. const paths = pathsResult.paths;
  35. let containsSymlink = false;
  36. let idx = -1;
  37. forEachBail(
  38. paths,
  39. /**
  40. * @param {string} path path
  41. * @param {(err?: null|Error, result?: null|number) => void} callback callback
  42. * @returns {void}
  43. */
  44. (path, callback) => {
  45. idx++;
  46. if (resolveContext.fileDependencies)
  47. resolveContext.fileDependencies.add(path);
  48. fs.readlink(path, (err, result) => {
  49. if (!err && result) {
  50. pathSegments[idx] = /** @type {string} */ (result);
  51. containsSymlink = true;
  52. // Shortcut when absolute symlink found
  53. const resultType = getType(result.toString());
  54. if (
  55. resultType === PathType.AbsoluteWin ||
  56. resultType === PathType.AbsolutePosix
  57. ) {
  58. return callback(null, idx);
  59. }
  60. }
  61. callback();
  62. });
  63. },
  64. /**
  65. * @param {null|Error} [err] error
  66. * @param {null|number} [idx] result
  67. * @returns {void}
  68. */
  69. (err, idx) => {
  70. if (!containsSymlink) return callback();
  71. const resultSegments =
  72. typeof idx === "number"
  73. ? pathSegments.slice(0, idx + 1)
  74. : pathSegments.slice();
  75. const result = resultSegments.reduceRight((a, b) => {
  76. return resolver.join(a, b);
  77. });
  78. /** @type {ResolveRequest} */
  79. const obj = {
  80. ...request,
  81. path: result
  82. };
  83. resolver.doResolve(
  84. target,
  85. obj,
  86. "resolved symlink to " + result,
  87. resolveContext,
  88. callback
  89. );
  90. }
  91. );
  92. });
  93. }
  94. };