vite-plugin-twig-drupal 1.2.0 → 1.3.0

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/README.md CHANGED
@@ -95,6 +95,8 @@ export default defineConfig({
95
95
  // You can add custom functions - each is a function that is passed the active Twig instance and should call
96
96
  // e.g. extendFunction to register a function
97
97
  reverse: (twigInstance) => twigInstance.extendFunction("reverse", () => (text) => text.split(' ').reverse().join(' ')),
98
+ // e.g. extendFilter to register a filter
99
+ clean_unique_id: (twigInstance) => twigInstance.extendFilter("clean_unique_id", () => (text) => text.split(' ').reverse().join(' ')),
98
100
  },
99
101
  globalContext: {
100
102
  // Global variables that should be present in all templates.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vite-plugin-twig-drupal",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
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
@@ -25,8 +25,16 @@ const includeTokenTypes = [
25
25
  ]
26
26
 
27
27
  const resolveFile = (directory, file) => {
28
- if (existsSync(resolve(file))) {
29
- return resolve(file)
28
+ const filesToTry = [file, `${file}.twig`, `${file}.html.twig`]
29
+ for (const ix in filesToTry) {
30
+ const path = resolve(filesToTry[ix])
31
+ if (existsSync(path)) {
32
+ return path
33
+ }
34
+ const withDir = resolve(directory, filesToTry[ix])
35
+ if (existsSync(withDir)) {
36
+ return withDir
37
+ }
30
38
  }
31
39
  return resolve(directory, file)
32
40
  }
@@ -51,6 +59,16 @@ const pluckIncludes = (tokens) => {
51
59
  })
52
60
  }
53
61
 
62
+ const resolveNamespaceOrComponent = (namespaces, template) => {
63
+ let resolveTemplate = template
64
+ // Support for SDC.
65
+ if (template.includes(":")) {
66
+ const [namespace, component] = template.split(":")
67
+ resolveTemplate = `@${namespace}/${component}/${component}`
68
+ }
69
+ return Twig.path.expandNamespace(namespaces, resolveTemplate)
70
+ }
71
+
54
72
  const compileTemplate = (id, file, { namespaces }) => {
55
73
  return new Promise((resolve, reject) => {
56
74
  const options = { namespaces, rethrow: true, allowInlineIncludes: true }
@@ -133,7 +151,7 @@ const plugin = (options = {}) => {
133
151
  const processIncludes = (template) => {
134
152
  const file = resolveFile(
135
153
  dirname(id),
136
- Twig.path.expandNamespace(options.namespaces, template)
154
+ resolveNamespaceOrComponent(options.namespaces, template)
137
155
  )
138
156
  if (!seen.includes(template)) {
139
157
  includePromises.push(
@@ -159,7 +177,7 @@ const plugin = (options = {}) => {
159
177
  (template) =>
160
178
  `import '${resolveFile(
161
179
  dirname(id),
162
- Twig.path.expandNamespace(options.namespaces, template)
180
+ resolveNamespaceOrComponent(options.namespaces, template)
163
181
  )}';`
164
182
  )
165
183
  .join("\n")
@@ -194,6 +212,8 @@ const plugin = (options = {}) => {
194
212
 
195
213
  ${functions}
196
214
 
215
+ addDrupalExtensions(Twig);
216
+
197
217
  // Disable caching.
198
218
  Twig.cache(false);
199
219
 
@@ -1,5 +1,10 @@
1
1
  // Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2
2
 
3
+ exports[`Basic smoke test > Should recognise default Drupal functions 1`] = `
4
+ "
5
+ <p>Functions work</p>"
6
+ `;
7
+
3
8
  exports[`Basic smoke test > Should support global context and functions 1`] = `
4
9
  "<section>
5
10
  <h1>Include</h1>
@@ -27,6 +32,9 @@ exports[`Basic smoke test > Should support global context and functions 1`] = `
27
32
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
28
33
  </article>
29
34
  </section>
35
+ <h2>SDC</h2>
36
+ <p>Card</p>
37
+ <button>SDC</button>
30
38
  "
31
39
  `;
32
40
 
@@ -57,6 +65,9 @@ exports[`Basic smoke test > Should support includes 1`] = `
57
65
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
58
66
  </article>
59
67
  </section>
68
+ <h2>SDC</h2>
69
+ <p>Card</p>
70
+ <button>SDC</button>
60
71
  "
61
72
  `;
62
73
 
@@ -104,5 +115,8 @@ exports[`Basic smoke test > Should support variables 1`] = `
104
115
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
105
116
  </article>
106
117
  </section>
118
+ <h2>SDC</h2>
119
+ <p>Card</p>
120
+ <button>SDC</button>
107
121
  "
108
122
  `;
@@ -0,0 +1,2 @@
1
+ {{ attach_library('drupal/library') }}
2
+ <p>Functions work</p>
@@ -0,0 +1 @@
1
+ <button>{{title}}</button>
@@ -0,0 +1,2 @@
1
+ <h2>{{title}}</h2>
2
+ <p>{{teaser}}</p>
@@ -10,3 +10,12 @@
10
10
  {% endembed %}
11
11
  {% include "@tests/section.twig" %}
12
12
  {% include "../fixtures/section.twig" with {title: 'Relative include'} %}
13
+ {% embed 'jabba:card' with {
14
+ title: 'SDC',
15
+ teaser: 'Card',
16
+ } %}
17
+ {% endembed %}
18
+ {% embed 'jabba:button' with {
19
+ title: 'SDC',
20
+ } %}
21
+ {% endembed %}
@@ -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 drupalFunctions from "../dist/drupalFunctions.js"
4
5
  import Menu from "../dist/menu.js"
5
6
  import { describe, expect, it } from "vitest"
6
7
 
@@ -35,4 +36,9 @@ describe("Basic smoke test", () => {
35
36
  expect(markup).toContain("Nested include")
36
37
  expect(markup).toContain("IT WORKS!")
37
38
  })
39
+ it("Should recognise default Drupal functions", () => {
40
+ const markup = drupalFunctions()
41
+ expect(markup).toMatchSnapshot()
42
+ expect(markup).toContain("Functions work")
43
+ })
38
44
  })
package/vite.config.js CHANGED
@@ -11,6 +11,10 @@ export default defineConfig({
11
11
  error: resolve(__dirname, "tests/fixtures/error.twig"),
12
12
  menu: resolve(__dirname, "tests/fixtures/menu.twig"),
13
13
  errorInclude: resolve(__dirname, "tests/fixtures/error-include.twig"),
14
+ drupalFunctions: resolve(
15
+ __dirname,
16
+ "tests/fixtures/drupal-functions.twig"
17
+ ),
14
18
  },
15
19
  name: "vite-plugin-twig-drupal",
16
20
  fileName: (_, entry) => `${entry}.js`,
@@ -27,6 +31,7 @@ export default defineConfig({
27
31
  },
28
32
  namespaces: {
29
33
  tests: join(__dirname, "/tests/fixtures"),
34
+ jabba: join(__dirname, "/tests/fixtures/jabba"),
30
35
  },
31
36
  }),
32
37
  ],