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