vite-plugin-twig-drupal 1.4.1 → 1.4.2

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.4.1",
3
+ "version": "1.4.2",
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
@@ -1,6 +1,6 @@
1
1
  import Twig from "twig"
2
- import { resolve, dirname } from "node:path"
3
- import { existsSync } from "node:fs"
2
+ import { join, resolve, dirname } from "node:path"
3
+ import { existsSync, readdirSync } from "node:fs"
4
4
  import { normalizePath } from "vite"
5
5
 
6
6
  const { twig } = Twig
@@ -25,6 +25,18 @@ const includeTokenTypes = [
25
25
  "Twig.logic.type.import",
26
26
  ]
27
27
 
28
+ const findInChildDirectories = (directory, component) => {
29
+ const files = readdirSync(directory, { recursive: true })
30
+ for (const file of files) {
31
+ const filePath = join(directory, file)
32
+ if (file.endsWith(`/${component}.twig`)) {
33
+ return filePath
34
+ }
35
+ }
36
+
37
+ return null
38
+ }
39
+
28
40
  const resolveFile = (directory, file) => {
29
41
  const filesToTry = [file, `${file}.twig`, `${file}.html.twig`]
30
42
  for (const ix in filesToTry) {
@@ -63,12 +75,25 @@ const pluckIncludes = (tokens) => {
63
75
 
64
76
  const resolveNamespaceOrComponent = (namespaces, template) => {
65
77
  let resolveTemplate = template
78
+ const isNamespace = template.includes(":")
79
+
66
80
  // Support for SDC.
67
- if (template.includes(":")) {
81
+ if (isNamespace) {
68
82
  const [namespace, component] = template.split(":")
69
83
  resolveTemplate = `@${namespace}/${component}/${component}`
70
84
  }
71
- return Twig.path.expandNamespace(namespaces, resolveTemplate)
85
+ let expandedPath = Twig.path.expandNamespace(namespaces, resolveTemplate)
86
+
87
+ // If file not found and we are in namespace -> search deeper.
88
+ if (!existsSync(expandedPath) && isNamespace) {
89
+ const [namespace, component] = template.split(":")
90
+ let foundFile = findInChildDirectories(namespaces[namespace], component)
91
+ if (existsSync(foundFile)) {
92
+ expandedPath = foundFile
93
+ }
94
+ }
95
+
96
+ return expandedPath
72
97
  }
73
98
 
74
99
  const compileTemplate = (id, file, { namespaces }) => {
@@ -224,7 +249,7 @@ const plugin = (options = {}) => {
224
249
  ${functions}
225
250
 
226
251
  addDrupalExtensions(Twig);
227
-
252
+
228
253
  // Disable caching.
229
254
  Twig.cache(false);
230
255
 
@@ -34,7 +34,14 @@ exports[`Basic smoke test > Should support global context and functions 1`] = `
34
34
  </section>
35
35
  <h2>SDC</h2>
36
36
  <p>Card</p>
37
+ <div>atom badge from nested dir 🙌</div>
38
+ <button>nested button 🙌</button>
37
39
  <button>SDC</button>
40
+ <button>included button 👈️</button>
41
+ <h2>Include card</h2>
42
+ <p>🏆️ winning</p>
43
+ <div>atom badge from nested dir 🙌</div>
44
+ <button>nested button 🙌</button>
38
45
  "
39
46
  `;
40
47
 
@@ -67,7 +74,14 @@ exports[`Basic smoke test > Should support includes 1`] = `
67
74
  </section>
68
75
  <h2>SDC</h2>
69
76
  <p>Card</p>
77
+ <div>atom badge from nested dir 🙌</div>
78
+ <button>nested button 🙌</button>
70
79
  <button>SDC</button>
80
+ <button>included button 👈️</button>
81
+ <h2>Include card</h2>
82
+ <p>🏆️ winning</p>
83
+ <div>atom badge from nested dir 🙌</div>
84
+ <button>nested button 🙌</button>
71
85
  "
72
86
  `;
73
87
 
@@ -88,6 +102,46 @@ exports[`Basic smoke test > Should support macros 1`] = `
88
102
  "
89
103
  `;
90
104
 
105
+ exports[`Basic smoke test > Should support nested SDC 1`] = `
106
+ "<section>
107
+ <h1>Include</h1>
108
+ <article>
109
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
110
+ </article>
111
+ </section>
112
+ <section>
113
+ <h1>Embed</h1>
114
+ <article>
115
+ Lorem ipsum dolor sit amet.
116
+ <button class=\\"button--primary\\">Nested include</button>
117
+ IT WORKS!
118
+ </article>
119
+ </section>
120
+ <section>
121
+ <h1></h1>
122
+ <article>
123
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
124
+ </article>
125
+ </section>
126
+ <section>
127
+ <h1>Relative include</h1>
128
+ <article>
129
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
130
+ </article>
131
+ </section>
132
+ <h2>SDC</h2>
133
+ <p>Card</p>
134
+ <div>atom badge from nested dir 🙌</div>
135
+ <button>nested button 🙌</button>
136
+ <button>SDC</button>
137
+ <button>included button 👈️</button>
138
+ <h2>Include card</h2>
139
+ <p>🏆️ winning</p>
140
+ <div>atom badge from nested dir 🙌</div>
141
+ <button>nested button 🙌</button>
142
+ "
143
+ `;
144
+
91
145
  exports[`Basic smoke test > Should support variables 1`] = `
92
146
  "<section>
93
147
  <h1>Include</h1>
@@ -117,6 +171,13 @@ exports[`Basic smoke test > Should support variables 1`] = `
117
171
  </section>
118
172
  <h2>SDC</h2>
119
173
  <p>Card</p>
174
+ <div>atom badge from nested dir 🙌</div>
175
+ <button>nested button 🙌</button>
120
176
  <button>SDC</button>
177
+ <button>included button 👈️</button>
178
+ <h2>Include card</h2>
179
+ <p>🏆️ winning</p>
180
+ <div>atom badge from nested dir 🙌</div>
181
+ <button>nested button 🙌</button>
121
182
  "
122
183
  `;
@@ -0,0 +1 @@
1
+ <div>{{ label }}</div>
@@ -1,2 +1,9 @@
1
1
  <h2>{{title}}</h2>
2
2
  <p>{{teaser}}</p>
3
+ {% include 'jabba:badge' with {
4
+ label: 'atom badge from nested dir 🙌',
5
+ } %}
6
+
7
+ {% include 'jabba:button' with {
8
+ title: 'nested button 🙌',
9
+ } %}
@@ -19,3 +19,10 @@
19
19
  title: 'SDC',
20
20
  } %}
21
21
  {% endembed %}
22
+ {% include 'jabba:button' with {
23
+ title: 'included button 👈️',
24
+ } %}
25
+ {% include 'jabba:card' with {
26
+ title: 'Include card',
27
+ teaser: '🏆️ winning',
28
+ } %}
@@ -30,6 +30,14 @@ describe("Basic smoke test", () => {
30
30
  expect(markup).toContain("Contact")
31
31
  expect(markup).toMatchSnapshot()
32
32
  })
33
+ it("Should support nested SDC", () => {
34
+ const markup = Markup()
35
+ expect(markup).toContain("nested button 🙌")
36
+ expect(markup).toContain("included button 👈️")
37
+ expect(markup).toContain("Include card")
38
+ expect(markup).toContain("🏆️ winning")
39
+ expect(markup).toMatchSnapshot()
40
+ })
33
41
  it("Should support global context and functions", () => {
34
42
  const markup = Markup()
35
43
  expect(markup).toMatchSnapshot()