| 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 | // Sentinel stored in `_fieldDataCache` when a description file does not
|
|---|
| 17 | // contain a usable alias field object. Lets us distinguish "not cached yet"
|
|---|
| 18 | // from "no valid field" without calling back into `getField`.
|
|---|
| 19 | const NO_FIELD_OBJECT = Symbol("NoFieldObject");
|
|---|
| 20 |
|
|---|
| 21 | module.exports = class AliasFieldPlugin {
|
|---|
| 22 | /**
|
|---|
| 23 | * @param {string | ResolveStepHook} source source
|
|---|
| 24 | * @param {string | string[]} field field
|
|---|
| 25 | * @param {string | ResolveStepHook} target target
|
|---|
| 26 | */
|
|---|
| 27 | constructor(source, field, target) {
|
|---|
| 28 | this.source = source;
|
|---|
| 29 | this.field = field;
|
|---|
| 30 | this.target = target;
|
|---|
| 31 | // `this.field` is fixed for the plugin's lifetime, so caching
|
|---|
| 32 | // per description-file content is safe. The cached value is either
|
|---|
| 33 | // the resolved alias-map object or the `NO_FIELD_OBJECT` sentinel
|
|---|
| 34 | // meaning "description file has no usable alias field".
|
|---|
| 35 | /** @type {WeakMap<import("./Resolver").JsonObject, { [k: string]: JsonPrimitive } | typeof NO_FIELD_OBJECT>} */
|
|---|
| 36 | this._fieldDataCache = new WeakMap();
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | /**
|
|---|
| 40 | * @param {Resolver} resolver the resolver
|
|---|
| 41 | * @returns {void}
|
|---|
| 42 | */
|
|---|
| 43 | apply(resolver) {
|
|---|
| 44 | const target = resolver.ensureHook(this.target);
|
|---|
| 45 | resolver
|
|---|
| 46 | .getHook(this.source)
|
|---|
| 47 | .tapAsync("AliasFieldPlugin", (request, resolveContext, callback) => {
|
|---|
| 48 | if (!request.descriptionFileData) return callback();
|
|---|
| 49 | const innerRequest = getInnerRequest(resolver, request);
|
|---|
| 50 | if (!innerRequest) return callback();
|
|---|
| 51 | const { descriptionFileData } = request;
|
|---|
| 52 | let fieldData = this._fieldDataCache.get(descriptionFileData);
|
|---|
| 53 | if (fieldData === undefined) {
|
|---|
| 54 | const raw = DescriptionFileUtils.getField(
|
|---|
| 55 | descriptionFileData,
|
|---|
| 56 | this.field,
|
|---|
| 57 | );
|
|---|
| 58 | fieldData =
|
|---|
| 59 | raw === null || typeof raw !== "object"
|
|---|
| 60 | ? NO_FIELD_OBJECT
|
|---|
| 61 | : /** @type {{ [k: string]: JsonPrimitive }} */ (raw);
|
|---|
| 62 | this._fieldDataCache.set(descriptionFileData, fieldData);
|
|---|
| 63 | }
|
|---|
| 64 | if (fieldData === NO_FIELD_OBJECT) {
|
|---|
| 65 | if (resolveContext.log) {
|
|---|
| 66 | resolveContext.log(
|
|---|
| 67 | `Field '${this.field}' doesn't contain a valid alias configuration`,
|
|---|
| 68 | );
|
|---|
| 69 | }
|
|---|
| 70 | return callback();
|
|---|
| 71 | }
|
|---|
| 72 | /** @type {JsonPrimitive | undefined} */
|
|---|
| 73 | const data = Object.prototype.hasOwnProperty.call(
|
|---|
| 74 | fieldData,
|
|---|
| 75 | innerRequest,
|
|---|
| 76 | )
|
|---|
| 77 | ? /** @type {{ [Key in string]: JsonPrimitive }} */ (fieldData)[
|
|---|
| 78 | innerRequest
|
|---|
| 79 | ]
|
|---|
| 80 | : innerRequest.startsWith("./")
|
|---|
| 81 | ? /** @type {{ [Key in string]: JsonPrimitive }} */ (fieldData)[
|
|---|
| 82 | innerRequest.slice(2)
|
|---|
| 83 | ]
|
|---|
| 84 | : undefined;
|
|---|
| 85 | if (data === innerRequest) return callback();
|
|---|
| 86 | if (data === undefined) return callback();
|
|---|
| 87 | if (data === false) {
|
|---|
| 88 | /** @type {ResolveRequest} */
|
|---|
| 89 | const ignoreObj = {
|
|---|
| 90 | ...request,
|
|---|
| 91 | path: false,
|
|---|
| 92 | };
|
|---|
| 93 | if (typeof resolveContext.yield === "function") {
|
|---|
| 94 | resolveContext.yield(ignoreObj);
|
|---|
| 95 | return callback(null, null);
|
|---|
| 96 | }
|
|---|
| 97 | return callback(null, ignoreObj);
|
|---|
| 98 | }
|
|---|
| 99 | /** @type {ResolveRequest} */
|
|---|
| 100 | const obj = {
|
|---|
| 101 | ...request,
|
|---|
| 102 | path: /** @type {string} */ (request.descriptionFileRoot),
|
|---|
| 103 | request: /** @type {string} */ (data),
|
|---|
| 104 | fullySpecified: false,
|
|---|
| 105 | };
|
|---|
| 106 | resolver.doResolve(
|
|---|
| 107 | target,
|
|---|
| 108 | obj,
|
|---|
| 109 | `aliased from description file ${
|
|---|
| 110 | request.descriptionFilePath
|
|---|
| 111 | } with mapping '${innerRequest}' to '${/** @type {string} */ data}'`,
|
|---|
| 112 | resolveContext,
|
|---|
| 113 | (err, result) => {
|
|---|
| 114 | if (err) return callback(err);
|
|---|
| 115 |
|
|---|
| 116 | // Don't allow other aliasing or raw request
|
|---|
| 117 | if (result === undefined) return callback(null, null);
|
|---|
| 118 | callback(null, result);
|
|---|
| 119 | },
|
|---|
| 120 | );
|
|---|
| 121 | });
|
|---|
| 122 | }
|
|---|
| 123 | };
|
|---|