vite-plugin-twig-drupal 1.0.2 → 1.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-twig-drupal",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "Provides a ⚡️ Vite plugin to transform 🌱 Twig into HTML with a 💧 Drupal flavour",
5
5
  "keywords": [
6
6
  "Vite",
package/src/index.js CHANGED
@@ -1,6 +1,8 @@
1
1
  import Twig from "twig"
2
+ import { resolve, dirname } from "node:path"
3
+ import { existsSync } from "node:fs"
4
+
2
5
  const { twig } = Twig
3
- import { resolve } from "node:path"
4
6
 
5
7
  const FRAMEWORK_REACT = "react"
6
8
  const FRAMEWORK_HTML = "html"
@@ -21,7 +23,14 @@ const includeTokenTypes = [
21
23
  "Twig.logic.type.import",
22
24
  ]
23
25
 
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) => {
25
34
  return [
26
35
  ...tokens
27
36
  .filter((token) => includeTokenTypes.includes(token.token?.type))
@@ -33,7 +42,10 @@ const pluckIncludes = (tokens) => {
33
42
  []
34
43
  ),
35
44
  ...tokens.reduce(
36
- (carry, token) => [...carry, ...pluckIncludes(token.token?.output || [])],
45
+ (carry, token) => [
46
+ ...carry,
47
+ ...pluckIncludes(token.token?.output || [], relative),
48
+ ],
37
49
  []
38
50
  ),
39
51
  ].filter((value, index, array) => {
@@ -55,7 +67,11 @@ const compileTemplate = (id, file, { namespaces }) => {
55
67
  return
56
68
  }
57
69
  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
+ ),
59
75
  code: template.compile(options),
60
76
  })
61
77
  },
@@ -70,7 +86,9 @@ const errorHandler =
70
86
  (e) => {
71
87
  if (isDefault) {
72
88
  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
+ }';`,
74
92
  map: null,
75
93
  }
76
94
  }
@@ -117,8 +135,11 @@ const plugin = (options = {}) => {
117
135
  includes = result.includes
118
136
  const includePromises = []
119
137
  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)) {
122
143
  includePromises.push(
123
144
  new Promise(async (resolve, reject) => {
124
145
  const { includes, code } = await compileTemplate(
@@ -132,7 +153,7 @@ const plugin = (options = {}) => {
132
153
  resolve(code)
133
154
  })
134
155
  )
135
- seen.push(file)
156
+ seen.push(template)
136
157
  }
137
158
  }
138
159
  includes.forEach(processIncludes)
@@ -140,7 +161,8 @@ const plugin = (options = {}) => {
140
161
  .filter((template) => template !== "_self")
141
162
  .map(
142
163
  (template) =>
143
- `import '${resolve(
164
+ `import '${resolveFile(
165
+ dirname(id),
144
166
  Twig.path.expandNamespace(options.namespaces, template)
145
167
  )}';`
146
168
  )
@@ -20,6 +20,12 @@ exports[`Basic smoke test > Should support includes 1`] = `
20
20
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
21
21
  </article>
22
22
  </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>
23
29
  "
24
30
  `;
25
31
 
@@ -60,5 +66,11 @@ exports[`Basic smoke test > Should support variables 1`] = `
60
66
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
61
67
  </article>
62
68
  </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>
63
75
  "
64
76
  `;
@@ -6,3 +6,4 @@
6
6
  {% endblock %}
7
7
  {% endembed %}
8
8
  {% include "@tests/section.twig" %}
9
+ {% include "../fixtures/section.twig" with {title: 'Relative include'} %}
@@ -9,6 +9,7 @@ describe("Basic smoke test", () => {
9
9
  const markup = Markup()
10
10
  expect(markup).toMatchSnapshot()
11
11
  expect(markup).toContain("Nested include")
12
+ expect(markup).toContain("Relative include")
12
13
  })
13
14
  it("Should support variables", () => {
14
15
  const markup = Markup({ title: "Pickle Fixie" })