[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 | /** @typedef {import("./Resolver")} Resolver */
|
---|
| 9 | /** @typedef {import("./Resolver").ResolveRequest} ResolveRequest */
|
---|
| 10 | /** @typedef {import("./Resolver").ResolveStepHook} ResolveStepHook */
|
---|
| 11 |
|
---|
| 12 | module.exports = class ConditionalPlugin {
|
---|
| 13 | /**
|
---|
| 14 | * @param {string | ResolveStepHook} source source
|
---|
| 15 | * @param {Partial<ResolveRequest>} test compare object
|
---|
| 16 | * @param {string | null} message log message
|
---|
| 17 | * @param {boolean} allowAlternatives when false, do not continue with the current step when "test" matches
|
---|
| 18 | * @param {string | ResolveStepHook} target target
|
---|
| 19 | */
|
---|
| 20 | constructor(source, test, message, allowAlternatives, target) {
|
---|
| 21 | this.source = source;
|
---|
| 22 | this.test = test;
|
---|
| 23 | this.message = message;
|
---|
| 24 | this.allowAlternatives = allowAlternatives;
|
---|
| 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 | const { test, message, allowAlternatives } = this;
|
---|
| 35 | const keys = Object.keys(test);
|
---|
| 36 | resolver
|
---|
| 37 | .getHook(this.source)
|
---|
| 38 | .tapAsync("ConditionalPlugin", (request, resolveContext, callback) => {
|
---|
| 39 | for (const prop of keys) {
|
---|
| 40 | if (request[prop] !== test[prop]) return callback();
|
---|
| 41 | }
|
---|
| 42 | resolver.doResolve(
|
---|
| 43 | target,
|
---|
| 44 | request,
|
---|
| 45 | message,
|
---|
| 46 | resolveContext,
|
---|
| 47 | allowAlternatives
|
---|
| 48 | ? callback
|
---|
| 49 | : (err, result) => {
|
---|
| 50 | if (err) return callback(err);
|
---|
| 51 |
|
---|
| 52 | // Don't allow other alternatives
|
---|
| 53 | if (result === undefined) return callback(null, null);
|
---|
| 54 | callback(null, result);
|
---|
| 55 | }
|
---|
| 56 | );
|
---|
| 57 | });
|
---|
| 58 | }
|
---|
| 59 | };
|
---|