# All files / src/patches/nunjucks load-event.js

68.18% Statements 45/66
53.33% Branches 24/45
83.33% Functions 5/6
67.69% Lines 44/65

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149

    7x

    7x

    7x
    224x

    224x
    224x

    224x

    224x

    224x
    224x
    224x

    224x

    224x

    224x
    224x
    224x

    224x

    198x
    396x

    198x

    198x

    224x
    198x

    198x
    198x
    198x

    26x
    26x

    26x

    26x

    26x

    26x
    26x

    26x
    26x

    26x

    26x

    26x
    26x
    26x

    26x

    26x

    26x

    26x
     /**
     Patch for nunjucks to emit the 'load' event, even if the template is accessed from its internal cache.
     https://mozilla.github.io/nunjucks/api.html#load-event

     This allows page dependencies to be properly collected for live preview in {@link VariableRenderer}.

     Patch is written against nunjucks v3.2.2
     Changes are delimited with a // CHANGE HERE comment
     */

    const { Environment, Template, lib } = require('nunjucks');

    /* eslint-disable */

    var noopTmplSrc = {
      type: 'code',
      obj: {
        root: function root(env, context, frame, runtime, cb) {
          try {
            cb(null, '');
          } catch (e) {
            cb(handleError(e, null, null));
          }
        }
      }
    };

    Environment.prototype.getTemplate = function getTemplate(name, eagerCompile, parentName, ignoreMissing, cb) {
      var _this3 = this;

      var that = this;
      var tmpl = null;

      Iif (name && name.raw) {
        // this fixes autoescape for templates referenced in symbols
        name = name.raw;
      }

      Iif (lib.isFunction(parentName)) {
        cb = parentName;
        parentName = null;
        eagerCompile = eagerCompile || false;
      }

      Eif (lib.isFunction(eagerCompile)) {
        cb = eagerCompile;
        eagerCompile = false;
      }

      Iif (name instanceof Template) {
        tmpl = name;
      } else Iif (typeof name !== 'string') {
        throw new Error('template names must be a string: ' + name);
      } else {
        for (var i = 0; i < this.loaders.length; i++) {
          var loader = this.loaders[i];
          tmpl = loader.cache[this.resolveTemplate(loader, parentName, name)];

          if (tmpl) {
            // CHANGE HERE

            // pathsToNames in nunjucks.loaders.FileSystemLoader maintains a reverse mapping of fullPath: name
            Object.entries(loader.pathsToNames).forEach(([fullPath, templateName]) => {
              if (name === templateName) {
                // Emit the load event
                this.emit('load', name, {
                  src: tmpl,
                  path: fullPath, // we only need this
                  noCache: loader.noCache
                }, loader)
              }
            });

            break;
          }
        }
      }

      if (tmpl) {
        Iif (eagerCompile) {
          tmpl.compile();
        }

        if (cb) {
          cb(null, tmpl);
          return undefined;
        } else E{
          return tmpl;
        }
      }

      var syncResult;

      var createTemplate = function createTemplate(err, info) {
        Iif (!info && !err && !ignoreMissing) {
          err = new Error('template not found: ' + name);
        }

        Iif (err) {
          if (cb) {
            cb(err);
            return;
          } else {
            throw err;
          }
        }

        var newTmpl;

        Iif (!info) {
          newTmpl = new Template(noopTmplSrc, _this3, '', eagerCompile);
        } else {
          newTmpl = new Template(info.src, _this3, info.path, eagerCompile);

          Eif (!info.noCache) {
            info.loader.cache[name] = newTmpl;
          }
        }

        if (cb) {
          cb(null, newTmpl);
        } else E{
          syncResult = newTmpl;
        }
      };

      lib.asyncIter(this.loaders, function (loader, i, next, done) {
        function handle(err, src) {
          Iif (err) {
            done(err);
          } else if (src) {
            src.loader = loader;
            done(null, src);
          } else E{
            next();
          }
        } // Resolve name relative to parentName

        name = that.resolveTemplate(loader, parentName, name);

        Iif (loader.async) {
          loader.getSource(name, handle);
        } else {
          handle(null, loader.getSource(name));
        }
      }, createTemplate);
      return syncResult;
    }