vite-plugin-twig-drupal 1.2.1 → 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.1",
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")
@@ -32,6 +32,9 @@ exports[`Basic smoke test > Should support global context and functions 1`] = `
32
32
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
33
33
  </article>
34
34
  </section>
35
+ <h2>SDC</h2>
36
+ <p>Card</p>
37
+ <button>SDC</button>
35
38
  "
36
39
  `;
37
40
 
@@ -62,6 +65,9 @@ exports[`Basic smoke test > Should support includes 1`] = `
62
65
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
63
66
  </article>
64
67
  </section>
68
+ <h2>SDC</h2>
69
+ <p>Card</p>
70
+ <button>SDC</button>
65
71
  "
66
72
  `;
67
73
 
@@ -109,5 +115,8 @@ exports[`Basic smoke test > Should support variables 1`] = `
109
115
  Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
110
116
  </article>
111
117
  </section>
118
+ <h2>SDC</h2>
119
+ <p>Card</p>
120
+ <button>SDC</button>
112
121
  "
113
122
  `;
@@ -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 %}
package/vite.config.js CHANGED
@@ -31,6 +31,7 @@ export default defineConfig({
31
31
  },
32
32
  namespaces: {
33
33
  tests: join(__dirname, "/tests/fixtures"),
34
+ jabba: join(__dirname, "/tests/fixtures/jabba"),
34
35
  },
35
36
  }),
36
37
  ],