createInnerContext.js 1.8 KB

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