[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 | /** @typedef {import("./Resolver")} Resolver */
|
---|
| 9 | /** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
|
---|
| 10 |
|
---|
| 11 | module.exports = class FileExistsPlugin {
|
---|
| 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 | const fs = resolver.fileSystem;
|
---|
| 28 | resolver
|
---|
| 29 | .getHook(this.source)
|
---|
| 30 | .tapAsync("FileExistsPlugin", (request, resolveContext, callback) => {
|
---|
| 31 | const file = request.path;
|
---|
| 32 | if (!file) return callback();
|
---|
| 33 | fs.stat(file, (err, stat) => {
|
---|
| 34 | if (err || !stat) {
|
---|
| 35 | if (resolveContext.missingDependencies)
|
---|
| 36 | resolveContext.missingDependencies.add(file);
|
---|
| 37 | if (resolveContext.log) resolveContext.log(file + " doesn't exist");
|
---|
| 38 | return callback();
|
---|
| 39 | }
|
---|
| 40 | if (!stat.isFile()) {
|
---|
| 41 | if (resolveContext.missingDependencies)
|
---|
| 42 | resolveContext.missingDependencies.add(file);
|
---|
| 43 | if (resolveContext.log) resolveContext.log(file + " is not a file");
|
---|
| 44 | return callback();
|
---|
| 45 | }
|
---|
| 46 | if (resolveContext.fileDependencies)
|
---|
| 47 | resolveContext.fileDependencies.add(file);
|
---|
| 48 | resolver.doResolve(
|
---|
| 49 | target,
|
---|
| 50 | request,
|
---|
| 51 | "existing file: " + file,
|
---|
| 52 | resolveContext,
|
---|
| 53 | callback
|
---|
| 54 | );
|
---|
| 55 | });
|
---|
| 56 | });
|
---|
| 57 | }
|
---|
| 58 | };
|
---|