[6a3a178] | 1 | /*
|
---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
| 3 | Author Tobias Koppers @sokra
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | "use strict";
|
---|
| 7 |
|
---|
| 8 | const DescriptionFileUtils = require("./DescriptionFileUtils");
|
---|
| 9 | const getInnerRequest = require("./getInnerRequest");
|
---|
| 10 |
|
---|
| 11 | /** @typedef {import("./Resolver")} Resolver */
|
---|
| 12 | /** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
|
---|
| 13 | /** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
|
---|
| 14 |
|
---|
| 15 | module.exports = class AliasFieldPlugin {
|
---|
| 16 | /**
|
---|
| 17 | * @param {string | ResolveStepHook} source source
|
---|
| 18 | * @param {string | Array<string>} field field
|
---|
| 19 | * @param {string | ResolveStepHook} target target
|
---|
| 20 | */
|
---|
| 21 | constructor(source, field, target) {
|
---|
| 22 | this.source = source;
|
---|
| 23 | this.field = field;
|
---|
| 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 | resolver
|
---|
| 34 | .getHook(this.source)
|
---|
| 35 | .tapAsync("AliasFieldPlugin", (request, resolveContext, callback) => {
|
---|
| 36 | if (!request.descriptionFileData) return callback();
|
---|
| 37 | const innerRequest = getInnerRequest(resolver, request);
|
---|
| 38 | if (!innerRequest) return callback();
|
---|
| 39 | const fieldData = DescriptionFileUtils.getField(
|
---|
| 40 | request.descriptionFileData,
|
---|
| 41 | this.field
|
---|
| 42 | );
|
---|
| 43 | if (fieldData === null || typeof fieldData !== "object") {
|
---|
| 44 | if (resolveContext.log)
|
---|
| 45 | resolveContext.log(
|
---|
| 46 | "Field '" +
|
---|
| 47 | this.field +
|
---|
| 48 | "' doesn't contain a valid alias configuration"
|
---|
| 49 | );
|
---|
| 50 | return callback();
|
---|
| 51 | }
|
---|
| 52 | const data1 = fieldData[innerRequest];
|
---|
| 53 | const data2 = fieldData[innerRequest.replace(/^\.\//, "")];
|
---|
| 54 | const data = typeof data1 !== "undefined" ? data1 : data2;
|
---|
| 55 | if (data === innerRequest) return callback();
|
---|
| 56 | if (data === undefined) return callback();
|
---|
| 57 | if (data === false) {
|
---|
| 58 | /** @type {ResolveRequest} */
|
---|
| 59 | const ignoreObj = {
|
---|
| 60 | ...request,
|
---|
| 61 | path: false
|
---|
| 62 | };
|
---|
| 63 | return callback(null, ignoreObj);
|
---|
| 64 | }
|
---|
| 65 | const obj = {
|
---|
| 66 | ...request,
|
---|
| 67 | path: request.descriptionFileRoot,
|
---|
| 68 | request: data,
|
---|
| 69 | fullySpecified: false
|
---|
| 70 | };
|
---|
| 71 | resolver.doResolve(
|
---|
| 72 | target,
|
---|
| 73 | obj,
|
---|
| 74 | "aliased from description file " +
|
---|
| 75 | request.descriptionFilePath +
|
---|
| 76 | " with mapping '" +
|
---|
| 77 | innerRequest +
|
---|
| 78 | "' to '" +
|
---|
| 79 | data +
|
---|
| 80 | "'",
|
---|
| 81 | resolveContext,
|
---|
| 82 | (err, result) => {
|
---|
| 83 | if (err) return callback(err);
|
---|
| 84 |
|
---|
| 85 | // Don't allow other aliasing or raw request
|
---|
| 86 | if (result === undefined) return callback(null, null);
|
---|
| 87 | callback(null, result);
|
---|
| 88 | }
|
---|
| 89 | );
|
---|
| 90 | });
|
---|
| 91 | }
|
---|
| 92 | };
|
---|