@rhinostone/swig 2.3.0 → 2.4.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -41,20 +41,38 @@ exports.compile = function (compiler, args, content, parents, options) {
41
41
  );
42
42
  }
43
43
  var ctx = args.pop(),
44
- allMacros = utils.map(args, function (arg) {
44
+ macros = [],
45
+ nestedImports = [];
46
+
47
+ // The imported file may itself import macros (`{% import "x" as y %}`).
48
+ // Those entries are flagged `isImport` by parse() and must be emitted as-is,
49
+ // before — and kept out of — the macro namespace-prefixing pass below.
50
+ utils.each(args, function (arg) {
51
+ (arg.isImport ? nestedImports : macros).push(arg);
52
+ });
53
+
54
+ var allMacros = utils.map(macros, function (arg) {
45
55
  return arg.name;
46
56
  }).join('|'),
47
57
  out = '_ctx.' + ctx + ' = {};\n var _output = "";\n',
48
- replacements = utils.map(args, function (arg) {
58
+ replacements = utils.map(macros, function (arg) {
49
59
  return {
50
60
  ex: new RegExp('_ctx.' + arg.name + '(\\W)(?!' + allMacros + ')', 'g'),
51
61
  re: '_ctx.' + ctx + '.' + arg.name + '$1'
52
62
  };
53
63
  });
54
64
 
65
+ // Emit the imported file's own (nested) imports first, un-namespaced, so the
66
+ // macros defined below can reference them by their original alias at call
67
+ // time. Without this, file-level imports are invisible inside macro bodies
68
+ // and such calls silently render empty.
69
+ utils.each(nestedImports, function (arg) {
70
+ out += arg.compiled;
71
+ });
72
+
55
73
  // Replace all occurrences of all macros in this file with
56
74
  // proper namespaced definitions and calls
57
- utils.each(args, function (arg) {
75
+ utils.each(macros, function (arg) {
58
76
  var c = arg.compiled;
59
77
  utils.each(replacements, function (re) {
60
78
  c = c.replace(re.ex, re.re);
@@ -91,7 +109,21 @@ exports.parse = function (str, line, parser, types, stack, opts, swig) {
91
109
  utils.each(tokens, function (token) {
92
110
  var out = '',
93
111
  macroName;
94
- if (!token || token.name !== 'macro' || !token.compile) {
112
+ if (!token || !token.compile) {
113
+ return;
114
+ }
115
+ // Carry the imported file's own imports through so that macros defined
116
+ // here which reference them resolve at call time. `token.args` is the
117
+ // already-parsed `[{compiled, name}, …, alias]`; slice() avoids the
118
+ // pop() in compile() mutating the cached token.
119
+ if (token.name === 'import') {
120
+ self.out.push({
121
+ compiled: token.compile(compiler, token.args.slice(), token.content, [], compileOpts) + '\n',
122
+ isImport: true
123
+ });
124
+ return;
125
+ }
126
+ if (token.name !== 'macro') {
95
127
  return;
96
128
  }
97
129
  macroName = token.args[0];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rhinostone/swig",
3
- "version": "2.3.0",
3
+ "version": "2.4.1",
4
4
  "description": "A simple, powerful, and extendable templating engine for node.js and browsers, similar to Django, Jinja2, and Twig.",
5
5
  "keywords": [
6
6
  "template",
@@ -21,7 +21,7 @@
21
21
  "Rhinostone <contact@gina.io>"
22
22
  ],
23
23
  "dependencies": {
24
- "@rhinostone/swig-core": "2.3.0"
24
+ "@rhinostone/swig-core": "2.4.1"
25
25
  },
26
26
  "devDependencies": {
27
27
  "blanket": "~1.1",