source: imaps-frontend/node_modules/enhanced-resolve/lib/MainFieldPlugin.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 4 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 2.4 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 path = require("path");
9const DescriptionFileUtils = require("./DescriptionFileUtils");
10
11/** @typedef {import("./Resolver")} Resolver */
12/** @typedef {import("./Resolver").JsonObject} JsonObject */
13/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
14/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
15
16/** @typedef {{name: string|Array<string>, forceRelative: boolean}} MainFieldOptions */
17
18const alreadyTriedMainField = Symbol("alreadyTriedMainField");
19
20module.exports = class MainFieldPlugin {
21 /**
22 * @param {string | ResolveStepHook} source source
23 * @param {MainFieldOptions} options options
24 * @param {string | ResolveStepHook} target target
25 */
26 constructor(source, options, target) {
27 this.source = source;
28 this.options = options;
29 this.target = target;
30 }
31
32 /**
33 * @param {Resolver} resolver the resolver
34 * @returns {void}
35 */
36 apply(resolver) {
37 const target = resolver.ensureHook(this.target);
38 resolver
39 .getHook(this.source)
40 .tapAsync("MainFieldPlugin", (request, resolveContext, callback) => {
41 if (
42 request.path !== request.descriptionFileRoot ||
43 /** @type {ResolveRequest & { [alreadyTriedMainField]?: string }} */
44 (request)[alreadyTriedMainField] === request.descriptionFilePath ||
45 !request.descriptionFilePath
46 )
47 return callback();
48 const filename = path.basename(request.descriptionFilePath);
49 let mainModule =
50 /** @type {string|null|undefined} */
51 (
52 DescriptionFileUtils.getField(
53 /** @type {JsonObject} */ (request.descriptionFileData),
54 this.options.name
55 )
56 );
57
58 if (
59 !mainModule ||
60 typeof mainModule !== "string" ||
61 mainModule === "." ||
62 mainModule === "./"
63 ) {
64 return callback();
65 }
66 if (this.options.forceRelative && !/^\.\.?\//.test(mainModule))
67 mainModule = "./" + mainModule;
68 /** @type {ResolveRequest & { [alreadyTriedMainField]?: string }} */
69 const obj = {
70 ...request,
71 request: mainModule,
72 module: false,
73 directory: mainModule.endsWith("/"),
74 [alreadyTriedMainField]: request.descriptionFilePath
75 };
76 return resolver.doResolve(
77 target,
78 obj,
79 "use " +
80 mainModule +
81 " from " +
82 this.options.name +
83 " in " +
84 filename,
85 resolveContext,
86 callback
87 );
88 });
89 }
90};
Note: See TracBrowser for help on using the repository browser.