Skip to content

Commit 3ce085c

Browse files
fix(core): Support for nesting namespaces (#32)
* Support for nesting namespaces Closes #31 * Avoid collisions * Removed extra check
1 parent ce71605 commit 3ce085c

File tree

4 files changed

+43
-5
lines changed

4 files changed

+43
-5
lines changed

src/index.js

+30-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
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"
44
import { normalizePath } from "vite"
55

66
const { twig } = Twig
@@ -25,6 +25,18 @@ const includeTokenTypes = [
2525
"Twig.logic.type.import",
2626
]
2727

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+
2840
const resolveFile = (directory, file) => {
2941
const filesToTry = [file, `${file}.twig`, `${file}.html.twig`]
3042
for (const ix in filesToTry) {
@@ -63,12 +75,25 @@ const pluckIncludes = (tokens) => {
6375

6476
const resolveNamespaceOrComponent = (namespaces, template) => {
6577
let resolveTemplate = template
78+
const isNamespace = template.includes(":")
79+
6680
// Support for SDC.
67-
if (template.includes(":")) {
81+
if (isNamespace) {
6882
const [namespace, component] = template.split(":")
6983
resolveTemplate = `@${namespace}/${component}/${component}`
7084
}
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
7297
}
7398

7499
const compileTemplate = (id, file, { namespaces }) => {
@@ -224,7 +249,7 @@ const plugin = (options = {}) => {
224249
${functions}
225250
226251
addDrupalExtensions(Twig);
227-
252+
228253
// Disable caching.
229254
Twig.cache(false);
230255

tests/__snapshots__/smoke.test.js.snap

+8
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,13 @@ exports[`Basic smoke test > Should support global context and functions 1`] = `
3434
</section>
3535
<h2>SDC</h2>
3636
<p>Card</p>
37+
<div>atom badge from nested dir 🙌</div>
3738
<button>nested button 🙌</button>
3839
<button>SDC</button>
3940
<button>included button 👈️</button>
4041
<h2>Include card</h2>
4142
<p>🏆️ winning</p>
43+
<div>atom badge from nested dir 🙌</div>
4244
<button>nested button 🙌</button>
4345
"
4446
`;
@@ -72,11 +74,13 @@ exports[`Basic smoke test > Should support includes 1`] = `
7274
</section>
7375
<h2>SDC</h2>
7476
<p>Card</p>
77+
<div>atom badge from nested dir 🙌</div>
7578
<button>nested button 🙌</button>
7679
<button>SDC</button>
7780
<button>included button 👈️</button>
7881
<h2>Include card</h2>
7982
<p>🏆️ winning</p>
83+
<div>atom badge from nested dir 🙌</div>
8084
<button>nested button 🙌</button>
8185
"
8286
`;
@@ -127,11 +131,13 @@ exports[`Basic smoke test > Should support nested SDC 1`] = `
127131
</section>
128132
<h2>SDC</h2>
129133
<p>Card</p>
134+
<div>atom badge from nested dir 🙌</div>
130135
<button>nested button 🙌</button>
131136
<button>SDC</button>
132137
<button>included button 👈️</button>
133138
<h2>Include card</h2>
134139
<p>🏆️ winning</p>
140+
<div>atom badge from nested dir 🙌</div>
135141
<button>nested button 🙌</button>
136142
"
137143
`;
@@ -165,11 +171,13 @@ exports[`Basic smoke test > Should support variables 1`] = `
165171
</section>
166172
<h2>SDC</h2>
167173
<p>Card</p>
174+
<div>atom badge from nested dir 🙌</div>
168175
<button>nested button 🙌</button>
169176
<button>SDC</button>
170177
<button>included button 👈️</button>
171178
<h2>Include card</h2>
172179
<p>🏆️ winning</p>
180+
<div>atom badge from nested dir 🙌</div>
173181
<button>nested button 🙌</button>
174182
"
175183
`;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div>{{ label }}</div>
+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
<h2>{{title}}</h2>
22
<p>{{teaser}}</p>
3+
{% include 'jabba:badge' with {
4+
label: 'atom badge from nested dir 🙌',
5+
} %}
6+
37
{% include 'jabba:button' with {
48
title: 'nested button 🙌',
59
} %}

0 commit comments

Comments
 (0)