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").ResolveStepHook} ResolveStepHook */
|
---|
10 |
|
---|
11 | const namespaceStartCharCode = "@".charCodeAt(0);
|
---|
12 |
|
---|
13 | module.exports = class JoinRequestPartPlugin {
|
---|
14 | /**
|
---|
15 | * @param {string | ResolveStepHook} source source
|
---|
16 | * @param {string | ResolveStepHook} target target
|
---|
17 | */
|
---|
18 | constructor(source, target) {
|
---|
19 | this.source = source;
|
---|
20 | this.target = target;
|
---|
21 | }
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * @param {Resolver} resolver the resolver
|
---|
25 | * @returns {void}
|
---|
26 | */
|
---|
27 | apply(resolver) {
|
---|
28 | const target = resolver.ensureHook(this.target);
|
---|
29 | resolver
|
---|
30 | .getHook(this.source)
|
---|
31 | .tapAsync(
|
---|
32 | "JoinRequestPartPlugin",
|
---|
33 | (request, resolveContext, callback) => {
|
---|
34 | const req = request.request || "";
|
---|
35 | let i = req.indexOf("/", 3);
|
---|
36 |
|
---|
37 | if (i >= 0 && req.charCodeAt(2) === namespaceStartCharCode) {
|
---|
38 | i = req.indexOf("/", i + 1);
|
---|
39 | }
|
---|
40 |
|
---|
41 | let moduleName, remainingRequest, fullySpecified;
|
---|
42 | if (i < 0) {
|
---|
43 | moduleName = req;
|
---|
44 | remainingRequest = ".";
|
---|
45 | fullySpecified = false;
|
---|
46 | } else {
|
---|
47 | moduleName = req.slice(0, i);
|
---|
48 | remainingRequest = "." + req.slice(i);
|
---|
49 | fullySpecified = request.fullySpecified;
|
---|
50 | }
|
---|
51 | const obj = {
|
---|
52 | ...request,
|
---|
53 | path: resolver.join(request.path, moduleName),
|
---|
54 | relativePath:
|
---|
55 | request.relativePath &&
|
---|
56 | resolver.join(request.relativePath, moduleName),
|
---|
57 | request: remainingRequest,
|
---|
58 | fullySpecified
|
---|
59 | };
|
---|
60 | resolver.doResolve(target, obj, null, resolveContext, callback);
|
---|
61 | }
|
---|
62 | );
|
---|
63 | }
|
---|
64 | };
|
---|