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