1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Tobias Koppers @sokra
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | /** @typedef {import("./Resolver")} Resolver */
|
---|
9 | /** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
|
---|
10 |
|
---|
11 | module.exports = class DirectoryExistsPlugin {
|
---|
12 | /**
|
---|
13 | * @param {string | ResolveStepHook} source source
|
---|
14 | * @param {string | ResolveStepHook} target target
|
---|
15 | */
|
---|
16 | constructor(source, target) {
|
---|
17 | this.source = source;
|
---|
18 | this.target = target;
|
---|
19 | }
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * @param {Resolver} resolver the resolver
|
---|
23 | * @returns {void}
|
---|
24 | */
|
---|
25 | apply(resolver) {
|
---|
26 | const target = resolver.ensureHook(this.target);
|
---|
27 | resolver
|
---|
28 | .getHook(this.source)
|
---|
29 | .tapAsync(
|
---|
30 | "DirectoryExistsPlugin",
|
---|
31 | (request, resolveContext, callback) => {
|
---|
32 | const fs = resolver.fileSystem;
|
---|
33 | const directory = request.path;
|
---|
34 | if (!directory) return callback();
|
---|
35 | fs.stat(directory, (err, stat) => {
|
---|
36 | if (err || !stat) {
|
---|
37 | if (resolveContext.missingDependencies)
|
---|
38 | resolveContext.missingDependencies.add(directory);
|
---|
39 | if (resolveContext.log)
|
---|
40 | resolveContext.log(directory + " doesn't exist");
|
---|
41 | return callback();
|
---|
42 | }
|
---|
43 | if (!stat.isDirectory()) {
|
---|
44 | if (resolveContext.missingDependencies)
|
---|
45 | resolveContext.missingDependencies.add(directory);
|
---|
46 | if (resolveContext.log)
|
---|
47 | resolveContext.log(directory + " is not a directory");
|
---|
48 | return callback();
|
---|
49 | }
|
---|
50 | if (resolveContext.fileDependencies)
|
---|
51 | resolveContext.fileDependencies.add(directory);
|
---|
52 | resolver.doResolve(
|
---|
53 | target,
|
---|
54 | request,
|
---|
55 | `existing directory ${directory}`,
|
---|
56 | resolveContext,
|
---|
57 | callback
|
---|
58 | );
|
---|
59 | });
|
---|
60 | }
|
---|
61 | );
|
---|
62 | }
|
---|
63 | };
|
---|