1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const forEachBail = require("./forEachBail");
|
---|
9 | const getPaths = require("./getPaths");
|
---|
10 | const { getType, PathType } = require("./util/path");
|
---|
11 |
|
---|
12 | /** @typedef {import("./Resolver")} Resolver */
|
---|
13 | /** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
|
---|
14 | /** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
|
---|
15 |
|
---|
16 | module.exports = class SymlinkPlugin {
|
---|
17 | /**
|
---|
18 | * @param {string | ResolveStepHook} source source
|
---|
19 | * @param {string | ResolveStepHook} target target
|
---|
20 | */
|
---|
21 | constructor(source, target) {
|
---|
22 | this.source = source;
|
---|
23 | this.target = target;
|
---|
24 | }
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * @param {Resolver} resolver the resolver
|
---|
28 | * @returns {void}
|
---|
29 | */
|
---|
30 | apply(resolver) {
|
---|
31 | const target = resolver.ensureHook(this.target);
|
---|
32 | const fs = resolver.fileSystem;
|
---|
33 | resolver
|
---|
34 | .getHook(this.source)
|
---|
35 | .tapAsync("SymlinkPlugin", (request, resolveContext, callback) => {
|
---|
36 | if (request.ignoreSymlinks) return callback();
|
---|
37 | const pathsResult = getPaths(/** @type {string} */ (request.path));
|
---|
38 | const pathSegments = pathsResult.segments;
|
---|
39 | const paths = pathsResult.paths;
|
---|
40 |
|
---|
41 | let containsSymlink = false;
|
---|
42 | let idx = -1;
|
---|
43 | forEachBail(
|
---|
44 | paths,
|
---|
45 | /**
|
---|
46 | * @param {string} path path
|
---|
47 | * @param {(err?: null|Error, result?: null|number) => void} callback callback
|
---|
48 | * @returns {void}
|
---|
49 | */
|
---|
50 | (path, callback) => {
|
---|
51 | idx++;
|
---|
52 | if (resolveContext.fileDependencies)
|
---|
53 | resolveContext.fileDependencies.add(path);
|
---|
54 | fs.readlink(path, (err, result) => {
|
---|
55 | if (!err && result) {
|
---|
56 | pathSegments[idx] = /** @type {string} */ (result);
|
---|
57 | containsSymlink = true;
|
---|
58 | // Shortcut when absolute symlink found
|
---|
59 | const resultType = getType(result.toString());
|
---|
60 | if (
|
---|
61 | resultType === PathType.AbsoluteWin ||
|
---|
62 | resultType === PathType.AbsolutePosix
|
---|
63 | ) {
|
---|
64 | return callback(null, idx);
|
---|
65 | }
|
---|
66 | }
|
---|
67 | callback();
|
---|
68 | });
|
---|
69 | },
|
---|
70 | /**
|
---|
71 | * @param {null|Error} [err] error
|
---|
72 | * @param {null|number} [idx] result
|
---|
73 | * @returns {void}
|
---|
74 | */
|
---|
75 | (err, idx) => {
|
---|
76 | if (!containsSymlink) return callback();
|
---|
77 | const resultSegments =
|
---|
78 | typeof idx === "number"
|
---|
79 | ? pathSegments.slice(0, idx + 1)
|
---|
80 | : pathSegments.slice();
|
---|
81 | const result = resultSegments.reduceRight((a, b) => {
|
---|
82 | return resolver.join(a, b);
|
---|
83 | });
|
---|
84 | /** @type {ResolveRequest} */
|
---|
85 | const obj = {
|
---|
86 | ...request,
|
---|
87 | path: result
|
---|
88 | };
|
---|
89 | resolver.doResolve(
|
---|
90 | target,
|
---|
91 | obj,
|
---|
92 | "resolved symlink to " + result,
|
---|
93 | resolveContext,
|
---|
94 | callback
|
---|
95 | );
|
---|
96 | }
|
---|
97 | );
|
---|
98 | });
|
---|
99 | }
|
---|
100 | };
|
---|