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 | /** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
|
---|
11 | /**
|
---|
12 | * @typedef {Object} PnpApiImpl
|
---|
13 | * @property {function(string, string, object): string | null} resolveToUnqualified
|
---|
14 | */
|
---|
15 |
|
---|
16 | module.exports = class PnpPlugin {
|
---|
17 | /**
|
---|
18 | * @param {string | ResolveStepHook} source source
|
---|
19 | * @param {PnpApiImpl} pnpApi pnpApi
|
---|
20 | * @param {string | ResolveStepHook} target target
|
---|
21 | * @param {string | ResolveStepHook} alternateTarget alternateTarget
|
---|
22 | */
|
---|
23 | constructor(source, pnpApi, target, alternateTarget) {
|
---|
24 | this.source = source;
|
---|
25 | this.pnpApi = pnpApi;
|
---|
26 | this.target = target;
|
---|
27 | this.alternateTarget = alternateTarget;
|
---|
28 | }
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * @param {Resolver} resolver the resolver
|
---|
32 | * @returns {void}
|
---|
33 | */
|
---|
34 | apply(resolver) {
|
---|
35 | /** @type {ResolveStepHook} */
|
---|
36 | const target = resolver.ensureHook(this.target);
|
---|
37 | const alternateTarget = resolver.ensureHook(this.alternateTarget);
|
---|
38 | resolver
|
---|
39 | .getHook(this.source)
|
---|
40 | .tapAsync("PnpPlugin", (request, resolveContext, callback) => {
|
---|
41 | const req = request.request;
|
---|
42 | if (!req) return callback();
|
---|
43 |
|
---|
44 | // The trailing slash indicates to PnP that this value is a folder rather than a file
|
---|
45 | const issuer = `${request.path}/`;
|
---|
46 |
|
---|
47 | const packageMatch = /^(@[^/]+\/)?[^/]+/.exec(req);
|
---|
48 | if (!packageMatch) return callback();
|
---|
49 |
|
---|
50 | const packageName = packageMatch[0];
|
---|
51 | const innerRequest = `.${req.slice(packageName.length)}`;
|
---|
52 |
|
---|
53 | /** @type {string|undefined|null} */
|
---|
54 | let resolution;
|
---|
55 | /** @type {string|undefined|null} */
|
---|
56 | let apiResolution;
|
---|
57 | try {
|
---|
58 | resolution = this.pnpApi.resolveToUnqualified(packageName, issuer, {
|
---|
59 | considerBuiltins: false
|
---|
60 | });
|
---|
61 |
|
---|
62 | if (resolution === null) {
|
---|
63 | // This is either not a PnP managed issuer or it's a Node builtin
|
---|
64 | // Try to continue resolving with our alternatives
|
---|
65 | resolver.doResolve(
|
---|
66 | alternateTarget,
|
---|
67 | request,
|
---|
68 | "issuer is not managed by a pnpapi",
|
---|
69 | resolveContext,
|
---|
70 | (err, result) => {
|
---|
71 | if (err) return callback(err);
|
---|
72 | if (result) return callback(null, result);
|
---|
73 | // Skip alternatives
|
---|
74 | return callback(null, null);
|
---|
75 | }
|
---|
76 | );
|
---|
77 | return;
|
---|
78 | }
|
---|
79 |
|
---|
80 | if (resolveContext.fileDependencies) {
|
---|
81 | apiResolution = this.pnpApi.resolveToUnqualified("pnpapi", issuer, {
|
---|
82 | considerBuiltins: false
|
---|
83 | });
|
---|
84 | }
|
---|
85 | } catch (/** @type {unknown} */ error) {
|
---|
86 | if (
|
---|
87 | /** @type {Error & { code: string }} */
|
---|
88 | (error).code === "MODULE_NOT_FOUND" &&
|
---|
89 | /** @type {Error & { pnpCode: string }} */
|
---|
90 | (error).pnpCode === "UNDECLARED_DEPENDENCY"
|
---|
91 | ) {
|
---|
92 | // This is not a PnP managed dependency.
|
---|
93 | // Try to continue resolving with our alternatives
|
---|
94 | if (resolveContext.log) {
|
---|
95 | resolveContext.log(`request is not managed by the pnpapi`);
|
---|
96 | for (const line of /** @type {Error} */ (error).message
|
---|
97 | .split("\n")
|
---|
98 | .filter(Boolean))
|
---|
99 | resolveContext.log(` ${line}`);
|
---|
100 | }
|
---|
101 | return callback();
|
---|
102 | }
|
---|
103 | return callback(/** @type {Error} */ (error));
|
---|
104 | }
|
---|
105 |
|
---|
106 | if (resolution === packageName) return callback();
|
---|
107 |
|
---|
108 | if (apiResolution && resolveContext.fileDependencies) {
|
---|
109 | resolveContext.fileDependencies.add(apiResolution);
|
---|
110 | }
|
---|
111 | /** @type {ResolveRequest} */
|
---|
112 | const obj = {
|
---|
113 | ...request,
|
---|
114 | path: resolution,
|
---|
115 | request: innerRequest,
|
---|
116 | ignoreSymlinks: true,
|
---|
117 | fullySpecified: request.fullySpecified && innerRequest !== "."
|
---|
118 | };
|
---|
119 | resolver.doResolve(
|
---|
120 | target,
|
---|
121 | obj,
|
---|
122 | `resolved by pnp to ${resolution}`,
|
---|
123 | resolveContext,
|
---|
124 | (err, result) => {
|
---|
125 | if (err) return callback(err);
|
---|
126 | if (result) return callback(null, result);
|
---|
127 | // Skip alternatives
|
---|
128 | return callback(null, null);
|
---|
129 | }
|
---|
130 | );
|
---|
131 | });
|
---|
132 | }
|
---|
133 | };
|
---|