source: trip-planner-front/node_modules/enhanced-resolve/lib/PnpPlugin.js@ eed0bf8

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

initial commit

  • Property mode set to 100644
File size: 2.8 KB
Line 
1/*
2 MIT License http://www.opensource.org/licenses/mit-license.php
3 Author Maël Nison @arcanis
4*/
5
6"use strict";
7
8/** @typedef {import("./Resolver")} Resolver */
9/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
10/**
11 * @typedef {Object} PnpApiImpl
12 * @property {function(string, string, object): string} resolveToUnqualified
13 */
14
15module.exports = class PnpPlugin {
16 /**
17 * @param {string | ResolveStepHook} source source
18 * @param {PnpApiImpl} pnpApi pnpApi
19 * @param {string | ResolveStepHook} target target
20 */
21 constructor(source, pnpApi, target) {
22 this.source = source;
23 this.pnpApi = pnpApi;
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("PnpPlugin", (request, resolveContext, callback) => {
36 const req = request.request;
37 if (!req) return callback();
38
39 // The trailing slash indicates to PnP that this value is a folder rather than a file
40 const issuer = `${request.path}/`;
41
42 const packageMatch = /^(@[^/]+\/)?[^/]+/.exec(req);
43 if (!packageMatch) return callback();
44
45 const packageName = packageMatch[0];
46 const innerRequest = `.${req.slice(packageName.length)}`;
47
48 let resolution;
49 let apiResolution;
50 try {
51 resolution = this.pnpApi.resolveToUnqualified(packageName, issuer, {
52 considerBuiltins: false
53 });
54 if (resolveContext.fileDependencies) {
55 apiResolution = this.pnpApi.resolveToUnqualified("pnpapi", issuer, {
56 considerBuiltins: false
57 });
58 }
59 } catch (error) {
60 if (
61 error.code === "MODULE_NOT_FOUND" &&
62 error.pnpCode === "UNDECLARED_DEPENDENCY"
63 ) {
64 // This is not a PnP managed dependency.
65 // Try to continue resolving with our alternatives
66 if (resolveContext.log) {
67 resolveContext.log(`request is not managed by the pnpapi`);
68 for (const line of error.message.split("\n").filter(Boolean))
69 resolveContext.log(` ${line}`);
70 }
71 return callback();
72 }
73 return callback(error);
74 }
75
76 if (resolution === packageName) return callback();
77
78 if (apiResolution && resolveContext.fileDependencies) {
79 resolveContext.fileDependencies.add(apiResolution);
80 }
81
82 const obj = {
83 ...request,
84 path: resolution,
85 request: innerRequest,
86 ignoreSymlinks: true,
87 fullySpecified: request.fullySpecified && innerRequest !== "."
88 };
89 resolver.doResolve(
90 target,
91 obj,
92 `resolved by pnp to ${resolution}`,
93 resolveContext,
94 (err, result) => {
95 if (err) return callback(err);
96 if (result) return callback(null, result);
97 // Skip alternatives
98 return callback(null, null);
99 }
100 );
101 });
102 }
103};
Note: See TracBrowser for help on using the repository browser.