[6a3a178] | 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").ResolveStepHook} ResolveStepHook */
|
---|
| 14 |
|
---|
| 15 | module.exports = class SymlinkPlugin {
|
---|
| 16 | /**
|
---|
| 17 | * @param {string | ResolveStepHook} source source
|
---|
| 18 | * @param {string | ResolveStepHook} target target
|
---|
| 19 | */
|
---|
| 20 | constructor(source, target) {
|
---|
| 21 | this.source = source;
|
---|
| 22 | this.target = target;
|
---|
| 23 | }
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
| 26 | * @param {Resolver} resolver the resolver
|
---|
| 27 | * @returns {void}
|
---|
| 28 | */
|
---|
| 29 | apply(resolver) {
|
---|
| 30 | const target = resolver.ensureHook(this.target);
|
---|
| 31 | const fs = resolver.fileSystem;
|
---|
| 32 | resolver
|
---|
| 33 | .getHook(this.source)
|
---|
| 34 | .tapAsync("SymlinkPlugin", (request, resolveContext, callback) => {
|
---|
| 35 | if (request.ignoreSymlinks) return callback();
|
---|
| 36 | const pathsResult = getPaths(request.path);
|
---|
| 37 | const pathSeqments = pathsResult.seqments;
|
---|
| 38 | const paths = pathsResult.paths;
|
---|
| 39 |
|
---|
| 40 | let containsSymlink = false;
|
---|
| 41 | let idx = -1;
|
---|
| 42 | forEachBail(
|
---|
| 43 | paths,
|
---|
| 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 | pathSeqments[idx] = 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 | (err, idx) => {
|
---|
| 65 | if (!containsSymlink) return callback();
|
---|
| 66 | const resultSeqments =
|
---|
| 67 | typeof idx === "number"
|
---|
| 68 | ? pathSeqments.slice(0, idx + 1)
|
---|
| 69 | : pathSeqments.slice();
|
---|
| 70 | const result = resultSeqments.reduceRight((a, b) => {
|
---|
| 71 | return resolver.join(a, b);
|
---|
| 72 | });
|
---|
| 73 | const obj = {
|
---|
| 74 | ...request,
|
---|
| 75 | path: result
|
---|
| 76 | };
|
---|
| 77 | resolver.doResolve(
|
---|
| 78 | target,
|
---|
| 79 | obj,
|
---|
| 80 | "resolved symlink to " + result,
|
---|
| 81 | resolveContext,
|
---|
| 82 | callback
|
---|
| 83 | );
|
---|
| 84 | }
|
---|
| 85 | );
|
---|
| 86 | });
|
---|
| 87 | }
|
---|
| 88 | };
|
---|