Skip to content

Commit

Permalink
Support for nesting namespaces
Browse files Browse the repository at this point in the history
Closes #31
  • Loading branch information
iberdinsky-skilld committed Oct 4, 2024
1 parent ce71605 commit f882575
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
37 changes: 32 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Twig from "twig"
import { resolve, dirname } from "node:path"
import { existsSync } from "node:fs"
import { join, resolve, dirname } from "node:path"
import { existsSync, readdirSync } from "node:fs"
import { normalizePath } from "vite"

const { twig } = Twig
Expand All @@ -25,6 +25,20 @@ const includeTokenTypes = [
"Twig.logic.type.import",
]

const findInChildDirectories = (directory, component) => {
const files = readdirSync(directory, { recursive: true })
for (const file of files) {
const filePath = join(directory, file)
if (file.endsWith(`${component}.twig`)) {
if (existsSync(filePath)) {
return filePath
}
}
}

return null
}

const resolveFile = (directory, file) => {
const filesToTry = [file, `${file}.twig`, `${file}.html.twig`]
for (const ix in filesToTry) {
Expand Down Expand Up @@ -63,12 +77,25 @@ const pluckIncludes = (tokens) => {

const resolveNamespaceOrComponent = (namespaces, template) => {
let resolveTemplate = template
const isNamespace = template.includes(":")

// Support for SDC.
if (template.includes(":")) {
if (isNamespace) {
const [namespace, component] = template.split(":")
resolveTemplate = `@${namespace}/${component}/${component}`
}
return Twig.path.expandNamespace(namespaces, resolveTemplate)
let expandedPath = Twig.path.expandNamespace(namespaces, resolveTemplate)

// If file not found and we are in namespace -> search deeper.
if (!existsSync(expandedPath) && isNamespace) {
const [namespace, component] = template.split(":")
let foundFile = findInChildDirectories(namespaces[namespace], component)
if (existsSync(foundFile)) {
expandedPath = foundFile
}
}

return expandedPath
}

const compileTemplate = (id, file, { namespaces }) => {
Expand Down Expand Up @@ -224,7 +251,7 @@ const plugin = (options = {}) => {
${functions}
addDrupalExtensions(Twig);
// Disable caching.
Twig.cache(false);
Expand Down
8 changes: 8 additions & 0 deletions tests/__snapshots__/smoke.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ exports[`Basic smoke test > Should support global context and functions 1`] = `
</section>
<h2>SDC</h2>
<p>Card</p>
<div>atom badge from nested dir 🙌</div>
<button>nested button 🙌</button>
<button>SDC</button>
<button>included button 👈️</button>
<h2>Include card</h2>
<p>🏆️ winning</p>
<div>atom badge from nested dir 🙌</div>
<button>nested button 🙌</button>
"
`;
Expand Down Expand Up @@ -72,11 +74,13 @@ exports[`Basic smoke test > Should support includes 1`] = `
</section>
<h2>SDC</h2>
<p>Card</p>
<div>atom badge from nested dir 🙌</div>
<button>nested button 🙌</button>
<button>SDC</button>
<button>included button 👈️</button>
<h2>Include card</h2>
<p>🏆️ winning</p>
<div>atom badge from nested dir 🙌</div>
<button>nested button 🙌</button>
"
`;
Expand Down Expand Up @@ -127,11 +131,13 @@ exports[`Basic smoke test > Should support nested SDC 1`] = `
</section>
<h2>SDC</h2>
<p>Card</p>
<div>atom badge from nested dir 🙌</div>
<button>nested button 🙌</button>
<button>SDC</button>
<button>included button 👈️</button>
<h2>Include card</h2>
<p>🏆️ winning</p>
<div>atom badge from nested dir 🙌</div>
<button>nested button 🙌</button>
"
`;
Expand Down Expand Up @@ -165,11 +171,13 @@ exports[`Basic smoke test > Should support variables 1`] = `
</section>
<h2>SDC</h2>
<p>Card</p>
<div>atom badge from nested dir 🙌</div>
<button>nested button 🙌</button>
<button>SDC</button>
<button>included button 👈️</button>
<h2>Include card</h2>
<p>🏆️ winning</p>
<div>atom badge from nested dir 🙌</div>
<button>nested button 🙌</button>
"
`;
1 change: 1 addition & 0 deletions tests/fixtures/jabba/atoms/badge/badge.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div>{{ label }}</div>
4 changes: 4 additions & 0 deletions tests/fixtures/jabba/card/card.html.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
<h2>{{title}}</h2>
<p>{{teaser}}</p>
{% include 'jabba:badge' with {
label: 'atom badge from nested dir 🙌',
} %}

{% include 'jabba:button' with {
title: 'nested button 🙌',
} %}

0 comments on commit f882575

Please sign in to comment.