vite-plugin-twig-drupal 1.5.0 → 1.6.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-twig-drupal",
3
- "version": "1.5.0",
3
+ "version": "1.6.1",
4
4
  "description": "Provides a ⚡️ Vite plugin to transform 🌱 Twig into HTML with a 💧 Drupal flavour",
5
5
  "keywords": [
6
6
  "Vite",
package/src/index.js CHANGED
@@ -171,7 +171,7 @@ const plugin = (options = {}) => {
171
171
  functions,
172
172
  code,
173
173
  includes,
174
- seen = []
174
+ seen = {}
175
175
 
176
176
  try {
177
177
  const result = await compileTemplate(id, id, options).catch(
@@ -183,31 +183,36 @@ const plugin = (options = {}) => {
183
183
  }
184
184
  code = result.code
185
185
  includes = result.includes
186
- const includePromises = []
187
- const processIncludes = (template) => {
188
- const file = resolveFile(
189
- dirname(id),
190
- resolveNamespaceOrComponent(options.namespaces, template)
191
- )
192
- if (!seen.includes(template)) {
193
- includePromises.push(
194
- new Promise(async (resolve, reject) => {
195
- const { includes, code } = await compileTemplate(
196
- template,
197
- file,
198
- options
199
- ).catch(errorHandler(template, false))
200
- if (includes) {
201
- includes.forEach(processIncludes)
186
+
187
+ // Process includes in a queue.
188
+ const promisifyIncludes = (includes) => {
189
+ return includes.reduce(
190
+ (queue, template) =>
191
+ queue.then(() => {
192
+ const file = resolveFile(
193
+ dirname(id),
194
+ resolveNamespaceOrComponent(options.namespaces, template)
195
+ )
196
+ if (!(template in seen)) {
197
+ return compileTemplate(template, file, options)
198
+ .catch(errorHandler(template, false))
199
+ .then(({ code, includes }) => {
200
+ seen[template] = code
201
+ if (!includes) {
202
+ return Promise.resolve()
203
+ }
204
+ return promisifyIncludes(includes)
205
+ })
202
206
  }
203
- resolve(code)
204
- })
205
- )
206
- seen.push(template)
207
- }
207
+ return Promise.resolve()
208
+ }),
209
+ Promise.resolve()
210
+ )
208
211
  }
209
- includes.forEach(processIncludes)
210
- embed = includes
212
+ const includeResult = await promisifyIncludes(includes).catch(
213
+ errorHandler(id)
214
+ )
215
+ embed = Object.keys(seen)
211
216
  .filter((template) => template !== "_self")
212
217
  .map(
213
218
  (template) =>
@@ -227,14 +232,11 @@ const plugin = (options = {}) => {
227
232
  })
228
233
  .join("\n")
229
234
 
230
- const includeResult = await Promise.all(includePromises).catch(
231
- errorHandler(id)
232
- )
233
- if (!Array.isArray(includeResult) && "map" in includeResult) {
235
+ if (includeResult !== undefined && "map" in includeResult) {
234
236
  // An error occurred.
235
237
  return includeResult
236
238
  }
237
- embeddedIncludes = includeResult.reverse().join("\n")
239
+ embeddedIncludes = Object.values(seen).reverse().join("\n")
238
240
  } catch (e) {
239
241
  return errorHandler(id)(e)
240
242
  }
@@ -260,8 +262,13 @@ const plugin = (options = {}) => {
260
262
  const component = ${code}
261
263
  ${includes ? `component.options.allowInlineIncludes = true;` : ""}
262
264
  try {
265
+ let defaultAttributes = context.defaultAttributes ? context.defaultAttributes : [];
266
+ if (!Array.isArray(defaultAttributes)) {
267
+ // We were passed a map, turn it into an array.
268
+ defaultAttributes = Object.entries(defaultAttributes);
269
+ }
263
270
  return frameworkTransform(component.render({
264
- attributes: new DrupalAttribute(),
271
+ attributes: new DrupalAttribute(defaultAttributes),
265
272
  ...${JSON.stringify(options.globalContext)},
266
273
  ...context
267
274
  }));
@@ -1,10 +1,96 @@
1
1
  // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
2
 
3
+ exports[`Basic smoke test > Should cast default attributes to attributes 1`] = `
4
+ "<section>
5
+ <h1>Include</h1>
6
+ <article>
7
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
8
+ </article>
9
+ </section>
10
+ <section>
11
+ <h1>Embed</h1>
12
+ <article>
13
+ Lorem ipsum dolor sit amet.
14
+ <button class=\\"button--primary\\">Nested include</button>
15
+ IT WORKS!
16
+ </article>
17
+ </section>
18
+ <section>
19
+ <h1></h1>
20
+ <article>
21
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
22
+ </article>
23
+ </section>
24
+ <section>
25
+ <h1>Relative include</h1>
26
+ <article>
27
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
28
+ </article>
29
+ </section>
30
+ <h2>SDC</h2>
31
+ <p>Card</p>
32
+ <div>atom badge from nested dir 🙌</div>
33
+ <button>nested button 🙌</button>
34
+ <button>SDC</button>
35
+ <button>included button 👈️</button>
36
+ <h2>Include card</h2>
37
+ <p>🏆️ winning</p>
38
+ <div>atom badge from nested dir 🙌</div>
39
+ <button>nested button 🙌</button>
40
+ <div class=\\"ponies\\">hey there</div>
41
+ All received
42
+ pony town
43
+ "
44
+ `;
45
+
3
46
  exports[`Basic smoke test > Should recognise default Drupal functions 1`] = `
4
47
  "
5
48
  <p>Functions work</p>"
6
49
  `;
7
50
 
51
+ exports[`Basic smoke test > Should support default attributes as a map 1`] = `
52
+ "<section>
53
+ <h1>Include</h1>
54
+ <article>
55
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
56
+ </article>
57
+ </section>
58
+ <section>
59
+ <h1>Embed</h1>
60
+ <article>
61
+ Lorem ipsum dolor sit amet.
62
+ <button class=\\"button--primary\\">Nested include</button>
63
+ IT WORKS!
64
+ </article>
65
+ </section>
66
+ <section>
67
+ <h1></h1>
68
+ <article>
69
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
70
+ </article>
71
+ </section>
72
+ <section>
73
+ <h1>Relative include</h1>
74
+ <article>
75
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
76
+ </article>
77
+ </section>
78
+ <h2>SDC</h2>
79
+ <p>Card</p>
80
+ <div>atom badge from nested dir 🙌</div>
81
+ <button>nested button 🙌</button>
82
+ <button>SDC</button>
83
+ <button>included button 👈️</button>
84
+ <h2>Include card</h2>
85
+ <p>🏆️ winning</p>
86
+ <div>atom badge from nested dir 🙌</div>
87
+ <button>nested button 🙌</button>
88
+ <div class=\\"ponies\\">hey there</div>
89
+ All received
90
+ pony town
91
+ "
92
+ `;
93
+
8
94
  exports[`Basic smoke test > Should support global context and functions 1`] = `
9
95
  "<section>
10
96
  <h1>Include</h1>
@@ -42,6 +128,9 @@ exports[`Basic smoke test > Should support global context and functions 1`] = `
42
128
  <p>🏆️ winning</p>
43
129
  <div>atom badge from nested dir 🙌</div>
44
130
  <button>nested button 🙌</button>
131
+ <div >hey there</div>
132
+ All received
133
+ pony town
45
134
  "
46
135
  `;
47
136
 
@@ -82,6 +171,9 @@ exports[`Basic smoke test > Should support includes 1`] = `
82
171
  <p>🏆️ winning</p>
83
172
  <div>atom badge from nested dir 🙌</div>
84
173
  <button>nested button 🙌</button>
174
+ <div >hey there</div>
175
+ All received
176
+ pony town
85
177
  "
86
178
  `;
87
179
 
@@ -139,6 +231,9 @@ exports[`Basic smoke test > Should support nested SDC 1`] = `
139
231
  <p>🏆️ winning</p>
140
232
  <div>atom badge from nested dir 🙌</div>
141
233
  <button>nested button 🙌</button>
234
+ <div >hey there</div>
235
+ All received
236
+ pony town
142
237
  "
143
238
  `;
144
239
 
@@ -179,5 +274,8 @@ exports[`Basic smoke test > Should support variables 1`] = `
179
274
  <p>🏆️ winning</p>
180
275
  <div>atom badge from nested dir 🙌</div>
181
276
  <button>nested button 🙌</button>
277
+ <div >hey there</div>
278
+ All received
279
+ pony town
182
280
  "
183
281
  `;
@@ -0,0 +1 @@
1
+ {{ title }}
@@ -0,0 +1,7 @@
1
+ {% if variant == 'roger' %}
2
+ All received
3
+ {% else %}
4
+ Not hearing you
5
+ {% endif %}
6
+ {% block content %}
7
+ {% endblock %}
@@ -0,0 +1,5 @@
1
+ {% set variant = 'roger' %}
2
+ {% extends "extend-me.twig" %}
3
+ {% block content %}
4
+ {% include "field.twig" %}
5
+ {% endblock %}
@@ -0,0 +1,2 @@
1
+ {% set title = "pony town" %}
2
+ {% extends "extend-level2.twig" %}
@@ -26,3 +26,5 @@
26
26
  title: 'Include card',
27
27
  teaser: '🏆️ winning',
28
28
  } %}
29
+ <div {{ attributes }}>hey there</div>
30
+ {% include "extender.twig" %}
@@ -49,4 +49,27 @@ describe("Basic smoke test", () => {
49
49
  expect(markup).toMatchSnapshot()
50
50
  expect(markup).toContain("Functions work")
51
51
  })
52
+ it("Should cast default attributes to attributes", () => {
53
+ const markup = Markup({
54
+ defaultAttributes: [["class", ["ponies"]]],
55
+ })
56
+ expect(markup).toMatchSnapshot()
57
+ expect(markup).toContain('class="ponies"')
58
+ expect(markup).toContain("hey there")
59
+ })
60
+ it("Should support default attributes as a map", () => {
61
+ const markup = Markup({
62
+ defaultAttributes: {
63
+ class: ["ponies"],
64
+ },
65
+ })
66
+ expect(markup).toMatchSnapshot()
67
+ expect(markup).toContain('class="ponies"')
68
+ expect(markup).toContain("hey there")
69
+ })
70
+ it("Should support extends", () => {
71
+ const markup = Markup()
72
+ expect(markup).toContain("All received")
73
+ expect(markup).toContain("pony town")
74
+ })
52
75
  })