Skip to content

Commit

Permalink
feat(core): SDC support (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
larowlan committed Apr 24, 2024
1 parent 053f7df commit ade7820
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,16 @@ const includeTokenTypes = [
]

const resolveFile = (directory, file) => {
if (existsSync(resolve(file))) {
return resolve(file)
const filesToTry = [file, `${file}.twig`, `${file}.html.twig`]
for (const ix in filesToTry) {
const path = resolve(filesToTry[ix])
if (existsSync(path)) {
return path
}
const withDir = resolve(directory, filesToTry[ix])
if (existsSync(withDir)) {
return withDir
}
}
return resolve(directory, file)
}
Expand All @@ -51,6 +59,16 @@ const pluckIncludes = (tokens) => {
})
}

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

const compileTemplate = (id, file, { namespaces }) => {
return new Promise((resolve, reject) => {
const options = { namespaces, rethrow: true, allowInlineIncludes: true }
Expand Down Expand Up @@ -133,7 +151,7 @@ const plugin = (options = {}) => {
const processIncludes = (template) => {
const file = resolveFile(
dirname(id),
Twig.path.expandNamespace(options.namespaces, template)
resolveNamespaceOrComponent(options.namespaces, template)
)
if (!seen.includes(template)) {
includePromises.push(
Expand All @@ -159,7 +177,7 @@ const plugin = (options = {}) => {
(template) =>
`import '${resolveFile(
dirname(id),
Twig.path.expandNamespace(options.namespaces, template)
resolveNamespaceOrComponent(options.namespaces, template)
)}';`
)
.join("\n")
Expand Down
9 changes: 9 additions & 0 deletions tests/__snapshots__/smoke.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ exports[`Basic smoke test > Should support global context and functions 1`] = `
Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
</article>
</section>
<h2>SDC</h2>
<p>Card</p>
<button>SDC</button>
"
`;
Expand Down Expand Up @@ -62,6 +65,9 @@ exports[`Basic smoke test > Should support includes 1`] = `
Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
</article>
</section>
<h2>SDC</h2>
<p>Card</p>
<button>SDC</button>
"
`;
Expand Down Expand Up @@ -109,5 +115,8 @@ exports[`Basic smoke test > Should support variables 1`] = `
Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
</article>
</section>
<h2>SDC</h2>
<p>Card</p>
<button>SDC</button>
"
`;
1 change: 1 addition & 0 deletions tests/fixtures/jabba/button/button.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<button>{{title}}</button>
2 changes: 2 additions & 0 deletions tests/fixtures/jabba/card/card.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h2>{{title}}</h2>
<p>{{teaser}}</p>
9 changes: 9 additions & 0 deletions tests/fixtures/mockup.twig
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@
{% endembed %}
{% include "@tests/section.twig" %}
{% include "../fixtures/section.twig" with {title: 'Relative include'} %}
{% embed 'jabba:card' with {
title: 'SDC',
teaser: 'Card',
} %}
{% endembed %}
{% embed 'jabba:button' with {
title: 'SDC',
} %}
{% endembed %}
1 change: 1 addition & 0 deletions vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export default defineConfig({
},
namespaces: {
tests: join(__dirname, "/tests/fixtures"),
jabba: join(__dirname, "/tests/fixtures/jabba"),
},
}),
],
Expand Down

0 comments on commit ade7820

Please sign in to comment.