vite-plugin-twig-drupal 1.0.1 → 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.1",
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"
@@ -18,9 +20,17 @@ const includeTokenTypes = [
18
20
  "Twig.logic.type.embed",
19
21
  "Twig.logic.type.include",
20
22
  "Twig.logic.type.extends",
23
+ "Twig.logic.type.import",
21
24
  ]
22
25
 
23
- 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) => {
24
34
  return [
25
35
  ...tokens
26
36
  .filter((token) => includeTokenTypes.includes(token.token?.type))
@@ -32,7 +42,10 @@ const pluckIncludes = (tokens) => {
32
42
  []
33
43
  ),
34
44
  ...tokens.reduce(
35
- (carry, token) => [...carry, ...pluckIncludes(token.token?.output || [])],
45
+ (carry, token) => [
46
+ ...carry,
47
+ ...pluckIncludes(token.token?.output || [], relative),
48
+ ],
36
49
  []
37
50
  ),
38
51
  ].filter((value, index, array) => {
@@ -54,7 +67,11 @@ const compileTemplate = (id, file, { namespaces }) => {
54
67
  return
55
68
  }
56
69
  resolve({
57
- 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
+ ),
58
75
  code: template.compile(options),
59
76
  })
60
77
  },
@@ -69,7 +86,9 @@ const errorHandler =
69
86
  (e) => {
70
87
  if (isDefault) {
71
88
  return {
72
- 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
+ }';`,
73
92
  map: null,
74
93
  }
75
94
  }
@@ -116,8 +135,11 @@ const plugin = (options = {}) => {
116
135
  includes = result.includes
117
136
  const includePromises = []
118
137
  const processIncludes = (template) => {
119
- const file = Twig.path.expandNamespace(options.namespaces, template)
120
- if (!seen.includes(file)) {
138
+ const file = resolveFile(
139
+ dirname(id),
140
+ Twig.path.expandNamespace(options.namespaces, template)
141
+ )
142
+ if (!seen.includes(template)) {
121
143
  includePromises.push(
122
144
  new Promise(async (resolve, reject) => {
123
145
  const { includes, code } = await compileTemplate(
@@ -131,14 +153,16 @@ const plugin = (options = {}) => {
131
153
  resolve(code)
132
154
  })
133
155
  )
134
- seen.push(file)
156
+ seen.push(template)
135
157
  }
136
158
  }
137
159
  includes.forEach(processIncludes)
138
160
  embed = includes
161
+ .filter((template) => template !== "_self")
139
162
  .map(
140
163
  (template) =>
141
- `import '${resolve(
164
+ `import '${resolveFile(
165
+ dirname(id),
142
166
  Twig.path.expandNamespace(options.namespaces, template)
143
167
  )}';`
144
168
  )
@@ -20,6 +20,29 @@ 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>
29
+ "
30
+ `;
31
+
32
+ exports[`Basic smoke test > Should support macros 1`] = `
33
+ "<ul>
34
+ <li><a href=\\"/home\\">Home</a>
35
+ </li>
36
+
37
+ <li><a href=\\"/about\\">About</a>
38
+ <ul>
39
+ <li><a href=\\"/contact\\">Contact</a>
40
+ </li>
41
+
42
+ </ul>
43
+ </li>
44
+
45
+ </ul>
23
46
  "
24
47
  `;
25
48
 
@@ -43,5 +66,11 @@ exports[`Basic smoke test > Should support variables 1`] = `
43
66
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
44
67
  </article>
45
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>
46
75
  "
47
76
  `;
@@ -0,0 +1,12 @@
1
+ {% macro menu_link(title, href, children = []) %}
2
+ {% import _self as navigation %}
3
+ <li><a href="{{ href }}">{{ title }}</a>
4
+ {% if children|length %}
5
+ <ul>
6
+ {% for child in children %}
7
+ {{ navigation.menu_link(child.title, child.href) }}
8
+ {% endfor %}
9
+ </ul>
10
+ {% endif %}
11
+ </li>
12
+ {% endmacro %}
@@ -0,0 +1,7 @@
1
+ {% import "@tests/macro.twig" as navigation %}
2
+ <ul>
3
+ {{ navigation.menu_link('Home', '/home') }}
4
+ {{ navigation.menu_link('About', '/about', [
5
+ { title: 'Contact', href: '/contact'}
6
+ ]) }}
7
+ </ul>
@@ -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'} %}
@@ -1,6 +1,7 @@
1
1
  import Markup from "../dist/test.js"
2
2
  import Error from "../dist/error.js"
3
3
  import ErrorInclude from "../dist/errorInclude.js"
4
+ import Menu from "../dist/menu.js"
4
5
  import { describe, expect, it } from "vitest"
5
6
 
6
7
  describe("Basic smoke test", () => {
@@ -8,6 +9,7 @@ describe("Basic smoke test", () => {
8
9
  const markup = Markup()
9
10
  expect(markup).toMatchSnapshot()
10
11
  expect(markup).toContain("Nested include")
12
+ expect(markup).toContain("Relative include")
11
13
  })
12
14
  it("Should support variables", () => {
13
15
  const markup = Markup({ title: "Pickle Fixie" })
@@ -22,4 +24,9 @@ describe("Basic smoke test", () => {
22
24
  const error = ErrorInclude()
23
25
  expect(error).toContain("An error occurred")
24
26
  })
27
+ it("Should support macros", () => {
28
+ const markup = Menu()
29
+ expect(markup).toContain("Contact")
30
+ expect(markup).toMatchSnapshot()
31
+ })
25
32
  })
package/vite.config.js CHANGED
@@ -9,6 +9,7 @@ export default defineConfig({
9
9
  entry: {
10
10
  test: resolve(__dirname, "tests/fixtures/mockup.twig"),
11
11
  error: resolve(__dirname, "tests/fixtures/error.twig"),
12
+ menu: resolve(__dirname, "tests/fixtures/menu.twig"),
12
13
  errorInclude: resolve(__dirname, "tests/fixtures/error-include.twig"),
13
14
  },
14
15
  name: "vite-plugin-twig-drupal",