Last change
on this file since fa375fe was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
1.3 KB
|
Line | |
---|
1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | Author Ivan Kopeykin @vankop
|
---|
4 | */
|
---|
5 |
|
---|
6 | "use strict";
|
---|
7 |
|
---|
8 | const forEachBail = require("./forEachBail");
|
---|
9 |
|
---|
10 | /** @typedef {import("./Resolver")} Resolver */
|
---|
11 | /** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
|
---|
12 |
|
---|
13 | class RootsPlugin {
|
---|
14 | /**
|
---|
15 | * @param {string | ResolveStepHook} source source hook
|
---|
16 | * @param {Set<string>} roots roots
|
---|
17 | * @param {string | ResolveStepHook} target target hook
|
---|
18 | */
|
---|
19 | constructor(source, roots, target) {
|
---|
20 | this.roots = Array.from(roots);
|
---|
21 | this.source = source;
|
---|
22 | this.target = target;
|
---|
23 | }
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * @param {Resolver} resolver the resolver
|
---|
27 | * @returns {void}
|
---|
28 | */
|
---|
29 | apply(resolver) {
|
---|
30 | const target = resolver.ensureHook(this.target);
|
---|
31 |
|
---|
32 | resolver
|
---|
33 | .getHook(this.source)
|
---|
34 | .tapAsync("RootsPlugin", (request, resolveContext, callback) => {
|
---|
35 | const req = request.request;
|
---|
36 | if (!req) return callback();
|
---|
37 | if (!req.startsWith("/")) return callback();
|
---|
38 |
|
---|
39 | forEachBail(
|
---|
40 | this.roots,
|
---|
41 | (root, callback) => {
|
---|
42 | const path = resolver.join(root, req.slice(1));
|
---|
43 | const obj = {
|
---|
44 | ...request,
|
---|
45 | path,
|
---|
46 | relativePath: request.relativePath && path
|
---|
47 | };
|
---|
48 | resolver.doResolve(
|
---|
49 | target,
|
---|
50 | obj,
|
---|
51 | `root path ${root}`,
|
---|
52 | resolveContext,
|
---|
53 | callback
|
---|
54 | );
|
---|
55 | },
|
---|
56 | callback
|
---|
57 | );
|
---|
58 | });
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | module.exports = RootsPlugin;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.