[79a0317] | 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").JsonPrimitive} JsonPrimitive */
|
---|
| 13 | /** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
|
---|
| 14 | /** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
|
---|
| 15 |
|
---|
| 16 | module.exports = class AliasFieldPlugin {
|
---|
| 17 | /**
|
---|
| 18 | * @param {string | ResolveStepHook} source source
|
---|
| 19 | * @param {string | Array<string>} field field
|
---|
| 20 | * @param {string | ResolveStepHook} target target
|
---|
| 21 | */
|
---|
| 22 | constructor(source, field, target) {
|
---|
| 23 | this.source = source;
|
---|
| 24 | this.field = field;
|
---|
| 25 | this.target = target;
|
---|
| 26 | }
|
---|
| 27 |
|
---|
| 28 | /**
|
---|
| 29 | * @param {Resolver} resolver the resolver
|
---|
| 30 | * @returns {void}
|
---|
| 31 | */
|
---|
| 32 | apply(resolver) {
|
---|
| 33 | const target = resolver.ensureHook(this.target);
|
---|
| 34 | resolver
|
---|
| 35 | .getHook(this.source)
|
---|
| 36 | .tapAsync("AliasFieldPlugin", (request, resolveContext, callback) => {
|
---|
| 37 | if (!request.descriptionFileData) return callback();
|
---|
| 38 | const innerRequest = getInnerRequest(resolver, request);
|
---|
| 39 | if (!innerRequest) return callback();
|
---|
| 40 | const fieldData = DescriptionFileUtils.getField(
|
---|
| 41 | request.descriptionFileData,
|
---|
| 42 | this.field
|
---|
| 43 | );
|
---|
| 44 | if (fieldData === null || typeof fieldData !== "object") {
|
---|
| 45 | if (resolveContext.log)
|
---|
| 46 | resolveContext.log(
|
---|
| 47 | "Field '" +
|
---|
| 48 | this.field +
|
---|
| 49 | "' doesn't contain a valid alias configuration"
|
---|
| 50 | );
|
---|
| 51 | return callback();
|
---|
| 52 | }
|
---|
| 53 | /** @type {JsonPrimitive | undefined} */
|
---|
| 54 | const data = Object.prototype.hasOwnProperty.call(
|
---|
| 55 | fieldData,
|
---|
| 56 | innerRequest
|
---|
| 57 | )
|
---|
| 58 | ? /** @type {{[Key in string]: JsonPrimitive}} */ (fieldData)[
|
---|
| 59 | innerRequest
|
---|
| 60 | ]
|
---|
| 61 | : innerRequest.startsWith("./")
|
---|
| 62 | ? /** @type {{[Key in string]: JsonPrimitive}} */ (fieldData)[
|
---|
| 63 | innerRequest.slice(2)
|
---|
| 64 | ]
|
---|
| 65 | : undefined;
|
---|
| 66 | if (data === innerRequest) return callback();
|
---|
| 67 | if (data === undefined) return callback();
|
---|
| 68 | if (data === false) {
|
---|
| 69 | /** @type {ResolveRequest} */
|
---|
| 70 | const ignoreObj = {
|
---|
| 71 | ...request,
|
---|
| 72 | path: false
|
---|
| 73 | };
|
---|
| 74 | if (typeof resolveContext.yield === "function") {
|
---|
| 75 | resolveContext.yield(ignoreObj);
|
---|
| 76 | return callback(null, null);
|
---|
| 77 | }
|
---|
| 78 | return callback(null, ignoreObj);
|
---|
| 79 | }
|
---|
| 80 | /** @type {ResolveRequest} */
|
---|
| 81 | const obj = {
|
---|
| 82 | ...request,
|
---|
| 83 | path: /** @type {string} */ (request.descriptionFileRoot),
|
---|
| 84 | request: /** @type {string} */ (data),
|
---|
| 85 | fullySpecified: false
|
---|
| 86 | };
|
---|
| 87 | resolver.doResolve(
|
---|
| 88 | target,
|
---|
| 89 | obj,
|
---|
| 90 | "aliased from description file " +
|
---|
| 91 | request.descriptionFilePath +
|
---|
| 92 | " with mapping '" +
|
---|
| 93 | innerRequest +
|
---|
| 94 | "' to '" +
|
---|
| 95 | /** @type {string} */ (data) +
|
---|
| 96 | "'",
|
---|
| 97 | resolveContext,
|
---|
| 98 | (err, result) => {
|
---|
| 99 | if (err) return callback(err);
|
---|
| 100 |
|
---|
| 101 | // Don't allow other aliasing or raw request
|
---|
| 102 | if (result === undefined) return callback(null, null);
|
---|
| 103 | callback(null, result);
|
---|
| 104 | }
|
---|
| 105 | );
|
---|
| 106 | });
|
---|
| 107 | }
|
---|
| 108 | };
|
---|