source: trip-planner-front/node_modules/enhanced-resolve/lib/DescriptionFilePlugin.js@ fa375fe

Last change on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago

initial commit

  • Property mode set to 100644
File size: 2.5 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Tobias Koppers @sokra
4*/
5
6"use strict";
7
8const DescriptionFileUtils = require("./DescriptionFileUtils");
9
10/** @typedef {import("./Resolver")} Resolver */
11/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
12
13module.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};
Note: See TracBrowser for help on using the repository browser.