Skip to content

Commit 0855b56

Browse files
committed
fix(core): Support relative includes
1 parent 7bdd989 commit 0855b56

File tree

4 files changed

+45
-9
lines changed

4 files changed

+45
-9
lines changed

src/index.js

+31-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import Twig from "twig"
2+
import { resolve, dirname } from "node:path"
3+
import { existsSync } from "node:fs"
4+
25
const { twig } = Twig
3-
import { resolve } from "node:path"
46

57
const FRAMEWORK_REACT = "react"
68
const FRAMEWORK_HTML = "html"
@@ -21,7 +23,14 @@ const includeTokenTypes = [
2123
"Twig.logic.type.import",
2224
]
2325

24-
const pluckIncludes = (tokens) => {
26+
const resolveFile = (directory, file) => {
27+
if (existsSync(resolve(file))) {
28+
return resolve(file)
29+
}
30+
return resolve(directory, file)
31+
}
32+
33+
const pluckIncludes = (tokens, relative) => {
2534
return [
2635
...tokens
2736
.filter((token) => includeTokenTypes.includes(token.token?.type))
@@ -33,7 +42,10 @@ const pluckIncludes = (tokens) => {
3342
[]
3443
),
3544
...tokens.reduce(
36-
(carry, token) => [...carry, ...pluckIncludes(token.token?.output || [])],
45+
(carry, token) => [
46+
...carry,
47+
...pluckIncludes(token.token?.output || [], relative),
48+
],
3749
[]
3850
),
3951
].filter((value, index, array) => {
@@ -55,7 +67,11 @@ const compileTemplate = (id, file, { namespaces }) => {
5567
return
5668
}
5769
resolve({
58-
includes: pluckIncludes(template.tokens),
70+
// We don't use dirname here because this ends up in the browser.
71+
includes: pluckIncludes(
72+
template.tokens,
73+
template.id.split("/").slice(0, -1).join("/")
74+
),
5975
code: template.compile(options),
6076
})
6177
},
@@ -70,7 +86,9 @@ const errorHandler =
7086
(e) => {
7187
if (isDefault) {
7288
return {
73-
code: `export default () => 'An error occurred whilst rendering ${id}: ${e.toString()}';`,
89+
code: `export default () => 'An error occurred whilst rendering ${id}: ${e.toString()} ${
90+
e.stack
91+
}';`,
7492
map: null,
7593
}
7694
}
@@ -117,8 +135,11 @@ const plugin = (options = {}) => {
117135
includes = result.includes
118136
const includePromises = []
119137
const processIncludes = (template) => {
120-
const file = Twig.path.expandNamespace(options.namespaces, template)
121-
if (!seen.includes(file)) {
138+
const file = resolveFile(
139+
dirname(id),
140+
Twig.path.expandNamespace(options.namespaces, template)
141+
)
142+
if (!seen.includes(template)) {
122143
includePromises.push(
123144
new Promise(async (resolve, reject) => {
124145
const { includes, code } = await compileTemplate(
@@ -132,15 +153,16 @@ const plugin = (options = {}) => {
132153
resolve(code)
133154
})
134155
)
135-
seen.push(file)
156+
seen.push(template)
136157
}
137158
}
138159
includes.forEach(processIncludes)
139160
embed = includes
140161
.filter((template) => template !== "_self")
141162
.map(
142163
(template) =>
143-
`import '${resolve(
164+
`import '${resolveFile(
165+
dirname(id),
144166
Twig.path.expandNamespace(options.namespaces, template)
145167
)}';`
146168
)

tests/__snapshots__/smoke.test.js.snap

+12
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ exports[`Basic smoke test > Should support includes 1`] = `
2020
Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
2121
</article>
2222
</section>
23+
<section>
24+
<h1>Relative include</h1>
25+
<article>
26+
Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
27+
</article>
28+
</section>
2329
"
2430
`;
2531
@@ -60,5 +66,11 @@ exports[`Basic smoke test > Should support variables 1`] = `
6066
Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
6167
</article>
6268
</section>
69+
<section>
70+
<h1>Relative include</h1>
71+
<article>
72+
Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
73+
</article>
74+
</section>
6375
"
6476
`;

tests/fixtures/mockup.twig

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
{% endblock %}
77
{% endembed %}
88
{% include "@tests/section.twig" %}
9+
{% include "../fixtures/section.twig" with {title: 'Relative include'} %}

tests/smoke.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ describe("Basic smoke test", () => {
99
const markup = Markup()
1010
expect(markup).toMatchSnapshot()
1111
expect(markup).toContain("Nested include")
12+
expect(markup).toContain("Relative include")
1213
})
1314
it("Should support variables", () => {
1415
const markup = Markup({ title: "Pickle Fixie" })

0 commit comments

Comments
 (0)