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 |
|
---|
12 | const namespaceStartCharCode = "@".charCodeAt(0);
|
---|
13 |
|
---|
14 | module.exports = class JoinRequestPartPlugin {
|
---|
15 | /**
|
---|
16 | * @param {string | ResolveStepHook} source source
|
---|
17 | * @param {string | ResolveStepHook} target target
|
---|
18 | */
|
---|
19 | constructor(source, target) {
|
---|
20 | this.source = source;
|
---|
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(
|
---|
33 | "JoinRequestPartPlugin",
|
---|
34 | (request, resolveContext, callback) => {
|
---|
35 | const req = request.request || "";
|
---|
36 | let i = req.indexOf("/", 3);
|
---|
37 |
|
---|
38 | if (i >= 0 && req.charCodeAt(2) === namespaceStartCharCode) {
|
---|
39 | i = req.indexOf("/", i + 1);
|
---|
40 | }
|
---|
41 |
|
---|
42 | /** @type {string} */
|
---|
43 | let moduleName;
|
---|
44 | /** @type {string} */
|
---|
45 | let remainingRequest;
|
---|
46 | /** @type {boolean} */
|
---|
47 | let fullySpecified;
|
---|
48 | if (i < 0) {
|
---|
49 | moduleName = req;
|
---|
50 | remainingRequest = ".";
|
---|
51 | fullySpecified = false;
|
---|
52 | } else {
|
---|
53 | moduleName = req.slice(0, i);
|
---|
54 | remainingRequest = "." + req.slice(i);
|
---|
55 | fullySpecified = /** @type {boolean} */ (request.fullySpecified);
|
---|
56 | }
|
---|
57 | /** @type {ResolveRequest} */
|
---|
58 | const obj = {
|
---|
59 | ...request,
|
---|
60 | path: resolver.join(
|
---|
61 | /** @type {string} */
|
---|
62 | (request.path),
|
---|
63 | moduleName
|
---|
64 | ),
|
---|
65 | relativePath:
|
---|
66 | request.relativePath &&
|
---|
67 | resolver.join(request.relativePath, moduleName),
|
---|
68 | request: remainingRequest,
|
---|
69 | fullySpecified
|
---|
70 | };
|
---|
71 | resolver.doResolve(target, obj, null, resolveContext, callback);
|
---|
72 | }
|
---|
73 | );
|
---|
74 | }
|
---|
75 | };
|
---|