| [9af201e] | 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").ResolveContext} ResolveContext */
|
|---|
| 9 |
|
|---|
| 10 | /**
|
|---|
| 11 | * Build the `ResolveContext` passed into the next hook in the chain.
|
|---|
| 12 | *
|
|---|
| 13 | * The caller — `Resolver.doResolve` — runs on every resolve step, so we
|
|---|
| 14 | * want to allocate as little as possible here. Previously the caller
|
|---|
| 15 | * constructed a temporary `{ log, yield, fileDependencies, ... }` literal
|
|---|
| 16 | * and handed it to this helper, which then copied those same fields into
|
|---|
| 17 | * a second fresh object. That's two allocations per step for what is
|
|---|
| 18 | * effectively a struct copy with one mutated field (`stack`) and one
|
|---|
| 19 | * optionally-wrapped field (`log`). Taking the parent context and the
|
|---|
| 20 | * two things we actually want to change (stack, message) as separate
|
|---|
| 21 | * arguments lets us allocate exactly one inner context.
|
|---|
| 22 | * @param {ResolveContext} parent parent resolve context to inherit dependency sets / yield from
|
|---|
| 23 | * @param {ResolveContext["stack"]} stack new stack tip for the nested call
|
|---|
| 24 | * @param {null | string} message log message prefix for this step
|
|---|
| 25 | * @returns {ResolveContext} inner context
|
|---|
| 26 | */
|
|---|
| 27 | module.exports = function createInnerContext(parent, stack, message) {
|
|---|
| 28 | const parentLog = parent.log;
|
|---|
| 29 | let innerLog;
|
|---|
| 30 | if (parentLog) {
|
|---|
| 31 | if (message) {
|
|---|
| 32 | let messageReported = false;
|
|---|
| 33 | /**
|
|---|
| 34 | * @param {string} msg message
|
|---|
| 35 | */
|
|---|
| 36 | innerLog = (msg) => {
|
|---|
| 37 | if (!messageReported) {
|
|---|
| 38 | parentLog(message);
|
|---|
| 39 | messageReported = true;
|
|---|
| 40 | }
|
|---|
| 41 | parentLog(` ${msg}`);
|
|---|
| 42 | };
|
|---|
| 43 | } else {
|
|---|
| 44 | innerLog = parentLog;
|
|---|
| 45 | }
|
|---|
| 46 | }
|
|---|
| 47 |
|
|---|
| 48 | return {
|
|---|
| 49 | log: innerLog,
|
|---|
| 50 | yield: parent.yield,
|
|---|
| 51 | fileDependencies: parent.fileDependencies,
|
|---|
| 52 | contextDependencies: parent.contextDependencies,
|
|---|
| 53 | missingDependencies: parent.missingDependencies,
|
|---|
| 54 | stack,
|
|---|
| 55 | };
|
|---|
| 56 | };
|
|---|