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 |
|
---|
11 | /** @typedef {import("./Resolver")} Resolver */
|
---|
12 | /** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
|
---|
13 | /** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
|
---|
14 |
|
---|
15 | module.exports = class ModulesInHierarchicalDirectoriesPlugin {
|
---|
16 | /**
|
---|
17 | * @param {string | ResolveStepHook} source source
|
---|
18 | * @param {string | Array<string>} directories directories
|
---|
19 | * @param {string | ResolveStepHook} target target
|
---|
20 | */
|
---|
21 | constructor(source, directories, target) {
|
---|
22 | this.source = source;
|
---|
23 | this.directories = /** @type {Array<string>} */ ([]).concat(directories);
|
---|
24 | this.target = target;
|
---|
25 | }
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * @param {Resolver} resolver the resolver
|
---|
29 | * @returns {void}
|
---|
30 | */
|
---|
31 | apply(resolver) {
|
---|
32 | const target = resolver.ensureHook(this.target);
|
---|
33 | resolver
|
---|
34 | .getHook(this.source)
|
---|
35 | .tapAsync(
|
---|
36 | "ModulesInHierarchicalDirectoriesPlugin",
|
---|
37 | (request, resolveContext, callback) => {
|
---|
38 | const fs = resolver.fileSystem;
|
---|
39 | const addrs = getPaths(/** @type {string} */ (request.path))
|
---|
40 | .paths.map(p => {
|
---|
41 | return this.directories.map(d => resolver.join(p, d));
|
---|
42 | })
|
---|
43 | .reduce((array, p) => {
|
---|
44 | array.push.apply(array, p);
|
---|
45 | return array;
|
---|
46 | }, []);
|
---|
47 | forEachBail(
|
---|
48 | addrs,
|
---|
49 | /**
|
---|
50 | * @param {string} addr addr
|
---|
51 | * @param {(err?: null|Error, result?: null|ResolveRequest) => void} callback callback
|
---|
52 | * @returns {void}
|
---|
53 | */
|
---|
54 | (addr, callback) => {
|
---|
55 | fs.stat(addr, (err, stat) => {
|
---|
56 | if (!err && stat && stat.isDirectory()) {
|
---|
57 | /** @type {ResolveRequest} */
|
---|
58 | const obj = {
|
---|
59 | ...request,
|
---|
60 | path: addr,
|
---|
61 | request: "./" + request.request,
|
---|
62 | module: false
|
---|
63 | };
|
---|
64 | const message = "looking for modules in " + addr;
|
---|
65 | return resolver.doResolve(
|
---|
66 | target,
|
---|
67 | obj,
|
---|
68 | message,
|
---|
69 | resolveContext,
|
---|
70 | callback
|
---|
71 | );
|
---|
72 | }
|
---|
73 | if (resolveContext.log)
|
---|
74 | resolveContext.log(
|
---|
75 | addr + " doesn't exist or is not a directory"
|
---|
76 | );
|
---|
77 | if (resolveContext.missingDependencies)
|
---|
78 | resolveContext.missingDependencies.add(addr);
|
---|
79 | return callback();
|
---|
80 | });
|
---|
81 | },
|
---|
82 | callback
|
---|
83 | );
|
---|
84 | }
|
---|
85 | );
|
---|
86 | }
|
---|
87 | };
|
---|