source: imaps-frontend/node_modules/enhanced-resolve/lib/ParsePlugin.js@ 79a0317

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

F4 Finalna Verzija

  • Property mode set to 100644
File size: 2.3 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
8/** @typedef {import("./Resolver")} Resolver */
9/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
10/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
11
12module.exports = class ParsePlugin {
13 /**
14 * @param {string | ResolveStepHook} source source
15 * @param {Partial<ResolveRequest>} requestOptions request options
16 * @param {string | ResolveStepHook} target target
17 */
18 constructor(source, requestOptions, target) {
19 this.source = source;
20 this.requestOptions = requestOptions;
21 this.target = target;
22 }
23
24 /**
25 * @param {Resolver} resolver the resolver
26 * @returns {void}
27 */
28 apply(resolver) {
29 const target = resolver.ensureHook(this.target);
30 resolver
31 .getHook(this.source)
32 .tapAsync("ParsePlugin", (request, resolveContext, callback) => {
33 const parsed = resolver.parse(/** @type {string} */ (request.request));
34 /** @type {ResolveRequest} */
35 const obj = { ...request, ...parsed, ...this.requestOptions };
36 if (request.query && !parsed.query) {
37 obj.query = request.query;
38 }
39 if (request.fragment && !parsed.fragment) {
40 obj.fragment = request.fragment;
41 }
42 if (parsed && resolveContext.log) {
43 if (parsed.module) resolveContext.log("Parsed request is a module");
44 if (parsed.directory)
45 resolveContext.log("Parsed request is a directory");
46 }
47 // There is an edge-case where a request with # can be a path or a fragment -> try both
48 if (obj.request && !obj.query && obj.fragment) {
49 const directory = obj.fragment.endsWith("/");
50 /** @type {ResolveRequest} */
51 const alternative = {
52 ...obj,
53 directory,
54 request:
55 obj.request +
56 (obj.directory ? "/" : "") +
57 (directory ? obj.fragment.slice(0, -1) : obj.fragment),
58 fragment: ""
59 };
60 resolver.doResolve(
61 target,
62 alternative,
63 null,
64 resolveContext,
65 (err, result) => {
66 if (err) return callback(err);
67 if (result) return callback(null, result);
68 resolver.doResolve(target, obj, null, resolveContext, callback);
69 }
70 );
71 return;
72 }
73 resolver.doResolve(target, obj, null, resolveContext, callback);
74 });
75 }
76};
Note: See TracBrowser for help on using the repository browser.