source: imaps-frontend/node_modules/enhanced-resolve/lib/ExtensionAliasPlugin.js

main
Last change on this file was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago

F4 Finalna Verzija

  • Property mode set to 100644
File size: 3.0 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
8const forEachBail = require("./forEachBail");
9
10/** @typedef {import("./Resolver")} Resolver */
11/** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
12/** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
13/** @typedef {{ alias: string|string[], extension: string }} ExtensionAliasOption */
14
15module.exports = class ExtensionAliasPlugin {
16 /**
17 * @param {string | ResolveStepHook} source source
18 * @param {ExtensionAliasOption} options options
19 * @param {string | ResolveStepHook} target target
20 */
21 constructor(source, options, target) {
22 this.source = source;
23 this.options = options;
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 const { extension, alias } = this.options;
34 resolver
35 .getHook(this.source)
36 .tapAsync("ExtensionAliasPlugin", (request, resolveContext, callback) => {
37 const requestPath = request.request;
38 if (!requestPath || !requestPath.endsWith(extension)) return callback();
39 const isAliasString = typeof alias === "string";
40 /**
41 * @param {string} alias extension alias
42 * @param {(err?: null|Error, result?: null|ResolveRequest) => void} callback callback
43 * @param {number} [index] index
44 * @returns {void}
45 */
46 const resolve = (alias, callback, index) => {
47 const newRequest = `${requestPath.slice(
48 0,
49 -extension.length
50 )}${alias}`;
51
52 return resolver.doResolve(
53 target,
54 {
55 ...request,
56 request: newRequest,
57 fullySpecified: true
58 },
59 `aliased from extension alias with mapping '${extension}' to '${alias}'`,
60 resolveContext,
61 (err, result) => {
62 // Throw error if we are on the last alias (for multiple aliases) and it failed, always throw if we are not an array or we have only one alias
63 if (!isAliasString && index) {
64 if (index !== this.options.alias.length) {
65 if (resolveContext.log) {
66 resolveContext.log(
67 `Failed to alias from extension alias with mapping '${extension}' to '${alias}' for '${newRequest}': ${err}`
68 );
69 }
70
71 return callback(null, result);
72 }
73
74 return callback(err, result);
75 } else {
76 callback(err, result);
77 }
78 }
79 );
80 };
81 /**
82 * @param {null|Error} [err] error
83 * @param {null|ResolveRequest} [result] result
84 * @returns {void}
85 */
86 const stoppingCallback = (err, result) => {
87 if (err) return callback(err);
88 if (result) return callback(null, result);
89 // Don't allow other aliasing or raw request
90 return callback(null, null);
91 };
92 if (isAliasString) {
93 resolve(alias, stoppingCallback);
94 } else if (alias.length > 1) {
95 forEachBail(alias, resolve, stoppingCallback);
96 } else {
97 resolve(alias[0], stoppingCallback);
98 }
99 });
100 }
101};
Note: See TracBrowser for help on using the repository browser.