Skip to content

Commit

Permalink
fix(core): Support relative includes
Browse files Browse the repository at this point in the history
  • Loading branch information
larowlan committed Dec 14, 2023
1 parent 7bdd989 commit 0855b56
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 9 deletions.
40 changes: 31 additions & 9 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import Twig from "twig"
import { resolve, dirname } from "node:path"
import { existsSync } from "node:fs"

const { twig } = Twig
import { resolve } from "node:path"

const FRAMEWORK_REACT = "react"
const FRAMEWORK_HTML = "html"
Expand All @@ -21,7 +23,14 @@ const includeTokenTypes = [
"Twig.logic.type.import",
]

const pluckIncludes = (tokens) => {
const resolveFile = (directory, file) => {
if (existsSync(resolve(file))) {
return resolve(file)
}
return resolve(directory, file)
}

const pluckIncludes = (tokens, relative) => {
return [
...tokens
.filter((token) => includeTokenTypes.includes(token.token?.type))
Expand All @@ -33,7 +42,10 @@ const pluckIncludes = (tokens) => {
[]
),
...tokens.reduce(
(carry, token) => [...carry, ...pluckIncludes(token.token?.output || [])],
(carry, token) => [
...carry,
...pluckIncludes(token.token?.output || [], relative),
],
[]
),
].filter((value, index, array) => {
Expand All @@ -55,7 +67,11 @@ const compileTemplate = (id, file, { namespaces }) => {
return
}
resolve({
includes: pluckIncludes(template.tokens),
// We don't use dirname here because this ends up in the browser.
includes: pluckIncludes(
template.tokens,
template.id.split("/").slice(0, -1).join("/")
),
code: template.compile(options),
})
},
Expand All @@ -70,7 +86,9 @@ const errorHandler =
(e) => {
if (isDefault) {
return {
code: `export default () => 'An error occurred whilst rendering ${id}: ${e.toString()}';`,
code: `export default () => 'An error occurred whilst rendering ${id}: ${e.toString()} ${
e.stack
}';`,
map: null,
}
}
Expand Down Expand Up @@ -117,8 +135,11 @@ const plugin = (options = {}) => {
includes = result.includes
const includePromises = []
const processIncludes = (template) => {
const file = Twig.path.expandNamespace(options.namespaces, template)
if (!seen.includes(file)) {
const file = resolveFile(
dirname(id),
Twig.path.expandNamespace(options.namespaces, template)
)
if (!seen.includes(template)) {
includePromises.push(
new Promise(async (resolve, reject) => {
const { includes, code } = await compileTemplate(
Expand All @@ -132,15 +153,16 @@ const plugin = (options = {}) => {
resolve(code)
})
)
seen.push(file)
seen.push(template)
}
}
includes.forEach(processIncludes)
embed = includes
.filter((template) => template !== "_self")
.map(
(template) =>
`import '${resolve(
`import '${resolveFile(
dirname(id),
Twig.path.expandNamespace(options.namespaces, template)
)}';`
)
Expand Down
12 changes: 12 additions & 0 deletions tests/__snapshots__/smoke.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ 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>
<section>
<h1>Relative include</h1>
<article>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
</article>
</section>
"
`;
Expand Down Expand Up @@ -60,5 +66,11 @@ 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>
<section>
<h1>Relative include</h1>
<article>
Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
</article>
</section>
"
`;
1 change: 1 addition & 0 deletions tests/fixtures/mockup.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
{% endblock %}
{% endembed %}
{% include "@tests/section.twig" %}
{% include "../fixtures/section.twig" with {title: 'Relative include'} %}
1 change: 1 addition & 0 deletions tests/smoke.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ describe("Basic smoke test", () => {
const markup = Markup()
expect(markup).toMatchSnapshot()
expect(markup).toContain("Nested include")
expect(markup).toContain("Relative include")
})
it("Should support variables", () => {
const markup = Markup({ title: "Pickle Fixie" })
Expand Down

0 comments on commit 0855b56

Please sign in to comment.