[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 DescriptionFileUtils = require("./DescriptionFileUtils");
|
---|
| 9 |
|
---|
| 10 | /** @typedef {import("./Resolver")} Resolver */
|
---|
| 11 | /** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
|
---|
| 12 |
|
---|
| 13 | module.exports = class DescriptionFilePlugin {
|
---|
| 14 | /**
|
---|
| 15 | * @param {string | ResolveStepHook} source source
|
---|
| 16 | * @param {string[]} filenames filenames
|
---|
| 17 | * @param {boolean} pathIsFile pathIsFile
|
---|
| 18 | * @param {string | ResolveStepHook} target target
|
---|
| 19 | */
|
---|
| 20 | constructor(source, filenames, pathIsFile, target) {
|
---|
| 21 | this.source = source;
|
---|
| 22 | this.filenames = filenames;
|
---|
| 23 | this.pathIsFile = pathIsFile;
|
---|
| 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 | "DescriptionFilePlugin",
|
---|
| 37 | (request, resolveContext, callback) => {
|
---|
| 38 | const path = request.path;
|
---|
| 39 | if (!path) return callback();
|
---|
| 40 | const directory = this.pathIsFile
|
---|
| 41 | ? DescriptionFileUtils.cdUp(path)
|
---|
| 42 | : path;
|
---|
| 43 | if (!directory) return callback();
|
---|
| 44 | DescriptionFileUtils.loadDescriptionFile(
|
---|
| 45 | resolver,
|
---|
| 46 | directory,
|
---|
| 47 | this.filenames,
|
---|
| 48 | request.descriptionFilePath
|
---|
| 49 | ? {
|
---|
| 50 | path: request.descriptionFilePath,
|
---|
| 51 | content: request.descriptionFileData,
|
---|
| 52 | directory: /** @type {string} */ (request.descriptionFileRoot)
|
---|
| 53 | }
|
---|
| 54 | : undefined,
|
---|
| 55 | resolveContext,
|
---|
| 56 | (err, result) => {
|
---|
| 57 | if (err) return callback(err);
|
---|
| 58 | if (!result) {
|
---|
| 59 | if (resolveContext.log)
|
---|
| 60 | resolveContext.log(
|
---|
| 61 | `No description file found in ${directory} or above`
|
---|
| 62 | );
|
---|
| 63 | return callback();
|
---|
| 64 | }
|
---|
| 65 | const relativePath =
|
---|
| 66 | "." + path.substr(result.directory.length).replace(/\\/g, "/");
|
---|
| 67 | const obj = {
|
---|
| 68 | ...request,
|
---|
| 69 | descriptionFilePath: result.path,
|
---|
| 70 | descriptionFileData: result.content,
|
---|
| 71 | descriptionFileRoot: result.directory,
|
---|
| 72 | relativePath: relativePath
|
---|
| 73 | };
|
---|
| 74 | resolver.doResolve(
|
---|
| 75 | target,
|
---|
| 76 | obj,
|
---|
| 77 | "using description file: " +
|
---|
| 78 | result.path +
|
---|
| 79 | " (relative path: " +
|
---|
| 80 | relativePath +
|
---|
| 81 | ")",
|
---|
| 82 | resolveContext,
|
---|
| 83 | (err, result) => {
|
---|
| 84 | if (err) return callback(err);
|
---|
| 85 |
|
---|
| 86 | // Don't allow other processing
|
---|
| 87 | if (result === undefined) return callback(null, null);
|
---|
| 88 | callback(null, result);
|
---|
| 89 | }
|
---|
| 90 | );
|
---|
| 91 | }
|
---|
| 92 | );
|
---|
| 93 | }
|
---|
| 94 | );
|
---|
| 95 | }
|
---|
| 96 | };
|
---|