vite-plugin-twig-drupal 1.0.3 → 1.1.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/package.json +1 -1
- package/src/index.js +21 -13
- package/tests/__snapshots__/smoke.test.js.snap +34 -2
- package/tests/fixtures/mockup.twig +3 -0
- package/tests/smoke.test.js +6 -0
- package/vite.config.js +7 -0
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -11,6 +11,7 @@ const defaultOptions = {
|
|
|
11
11
|
namespaces: {},
|
|
12
12
|
filters: {},
|
|
13
13
|
functions: {},
|
|
14
|
+
globalContext: {},
|
|
14
15
|
framework: FRAMEWORK_HTML,
|
|
15
16
|
pattern: /\.(twig)(\?.*)?$/,
|
|
16
17
|
}
|
|
@@ -30,7 +31,7 @@ const resolveFile = (directory, file) => {
|
|
|
30
31
|
return resolve(directory, file)
|
|
31
32
|
}
|
|
32
33
|
|
|
33
|
-
const pluckIncludes = (tokens
|
|
34
|
+
const pluckIncludes = (tokens) => {
|
|
34
35
|
return [
|
|
35
36
|
...tokens
|
|
36
37
|
.filter((token) => includeTokenTypes.includes(token.token?.type))
|
|
@@ -42,10 +43,7 @@ const pluckIncludes = (tokens, relative) => {
|
|
|
42
43
|
[]
|
|
43
44
|
),
|
|
44
45
|
...tokens.reduce(
|
|
45
|
-
(carry, token) => [
|
|
46
|
-
...carry,
|
|
47
|
-
...pluckIncludes(token.token?.output || [], relative),
|
|
48
|
-
],
|
|
46
|
+
(carry, token) => [...carry, ...pluckIncludes(token.token?.output || [])],
|
|
49
47
|
[]
|
|
50
48
|
),
|
|
51
49
|
].filter((value, index, array) => {
|
|
@@ -67,11 +65,7 @@ const compileTemplate = (id, file, { namespaces }) => {
|
|
|
67
65
|
return
|
|
68
66
|
}
|
|
69
67
|
resolve({
|
|
70
|
-
|
|
71
|
-
includes: pluckIncludes(
|
|
72
|
-
template.tokens,
|
|
73
|
-
template.id.split("/").slice(0, -1).join("/")
|
|
74
|
-
),
|
|
68
|
+
includes: pluckIncludes(template.tokens),
|
|
75
69
|
code: template.compile(options),
|
|
76
70
|
})
|
|
77
71
|
},
|
|
@@ -120,9 +114,11 @@ const plugin = (options = {}) => {
|
|
|
120
114
|
}
|
|
121
115
|
let embed,
|
|
122
116
|
embeddedIncludes,
|
|
117
|
+
functions,
|
|
123
118
|
code,
|
|
124
119
|
includes,
|
|
125
120
|
seen = []
|
|
121
|
+
|
|
126
122
|
try {
|
|
127
123
|
const result = await compileTemplate(id, id, options).catch(
|
|
128
124
|
errorHandler(id)
|
|
@@ -167,6 +163,16 @@ const plugin = (options = {}) => {
|
|
|
167
163
|
)}';`
|
|
168
164
|
)
|
|
169
165
|
.join("\n")
|
|
166
|
+
|
|
167
|
+
functions = Object.entries(options.functions)
|
|
168
|
+
.map(([name, value]) => {
|
|
169
|
+
return `
|
|
170
|
+
const ${name} = ${value};
|
|
171
|
+
${name}(Twig);
|
|
172
|
+
`
|
|
173
|
+
})
|
|
174
|
+
.join("\n")
|
|
175
|
+
|
|
170
176
|
const includeResult = await Promise.all(includePromises).catch(
|
|
171
177
|
errorHandler(id)
|
|
172
178
|
)
|
|
@@ -183,10 +189,11 @@ const plugin = (options = {}) => {
|
|
|
183
189
|
import DrupalAttribute from 'drupal-attribute';
|
|
184
190
|
import { addDrupalExtensions } from 'drupal-twig-extensions/twig';
|
|
185
191
|
${frameworkInclude}
|
|
186
|
-
|
|
192
|
+
|
|
187
193
|
${embed}
|
|
188
194
|
|
|
189
|
-
|
|
195
|
+
${functions}
|
|
196
|
+
|
|
190
197
|
// Disable caching.
|
|
191
198
|
Twig.cache(false);
|
|
192
199
|
|
|
@@ -198,7 +205,8 @@ const plugin = (options = {}) => {
|
|
|
198
205
|
${includes ? `component.options.allowInlineIncludes = true;` : ""}
|
|
199
206
|
try {
|
|
200
207
|
return frameworkTransform(component.render({
|
|
201
|
-
|
|
208
|
+
attributes: new DrupalAttribute(),
|
|
209
|
+
...${JSON.stringify(options.globalContext)},
|
|
202
210
|
...context
|
|
203
211
|
}));
|
|
204
212
|
}
|
|
@@ -1,5 +1,35 @@
|
|
|
1
1
|
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
|
|
2
2
|
|
|
3
|
+
exports[`Basic smoke test > Should support global context and functions 1`] = `
|
|
4
|
+
"<section>
|
|
5
|
+
<h1>Include</h1>
|
|
6
|
+
<article>
|
|
7
|
+
Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
|
|
8
|
+
</article>
|
|
9
|
+
</section>
|
|
10
|
+
<section>
|
|
11
|
+
<h1>Embed</h1>
|
|
12
|
+
<article>
|
|
13
|
+
Lorem ipsum dolor sit amet.
|
|
14
|
+
<button class=\\"button--primary\\">Nested include</button>
|
|
15
|
+
IT WORKS!
|
|
16
|
+
</article>
|
|
17
|
+
</section>
|
|
18
|
+
<section>
|
|
19
|
+
<h1></h1>
|
|
20
|
+
<article>
|
|
21
|
+
Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
|
|
22
|
+
</article>
|
|
23
|
+
</section>
|
|
24
|
+
<section>
|
|
25
|
+
<h1>Relative include</h1>
|
|
26
|
+
<article>
|
|
27
|
+
Lorem ipsum dolor sit amet, consectetur adipisicing elit. At dignissimos fugiat inventore laborum maiores molestiae neque quia quo unde veniam?
|
|
28
|
+
</article>
|
|
29
|
+
</section>
|
|
30
|
+
"
|
|
31
|
+
`;
|
|
32
|
+
|
|
3
33
|
exports[`Basic smoke test > Should support includes 1`] = `
|
|
4
34
|
"<section>
|
|
5
35
|
<h1>Include</h1>
|
|
@@ -12,7 +42,8 @@ exports[`Basic smoke test > Should support includes 1`] = `
|
|
|
12
42
|
<article>
|
|
13
43
|
Lorem ipsum dolor sit amet.
|
|
14
44
|
<button class=\\"button--primary\\">Nested include</button>
|
|
15
|
-
|
|
45
|
+
IT WORKS!
|
|
46
|
+
</article>
|
|
16
47
|
</section>
|
|
17
48
|
<section>
|
|
18
49
|
<h1></h1>
|
|
@@ -58,7 +89,8 @@ exports[`Basic smoke test > Should support variables 1`] = `
|
|
|
58
89
|
<article>
|
|
59
90
|
Lorem ipsum dolor sit amet.
|
|
60
91
|
<button class=\\"button--primary\\">Nested include</button>
|
|
61
|
-
|
|
92
|
+
IT WORKS!
|
|
93
|
+
</article>
|
|
62
94
|
</section>
|
|
63
95
|
<section>
|
|
64
96
|
<h1>Pickle Fixie</h1>
|
|
@@ -3,6 +3,9 @@
|
|
|
3
3
|
{% block content %}
|
|
4
4
|
Lorem ipsum dolor sit amet.
|
|
5
5
|
{% include "@tests/button.twig" with {text: 'Nested include'} %}
|
|
6
|
+
{% if active_theme == 'poodles' %}
|
|
7
|
+
{{ testFunction() }}
|
|
8
|
+
{% endif %}
|
|
6
9
|
{% endblock %}
|
|
7
10
|
{% endembed %}
|
|
8
11
|
{% include "@tests/section.twig" %}
|
package/tests/smoke.test.js
CHANGED
|
@@ -29,4 +29,10 @@ describe("Basic smoke test", () => {
|
|
|
29
29
|
expect(markup).toContain("Contact")
|
|
30
30
|
expect(markup).toMatchSnapshot()
|
|
31
31
|
})
|
|
32
|
+
it("Should support global context and functions", () => {
|
|
33
|
+
const markup = Markup()
|
|
34
|
+
expect(markup).toMatchSnapshot()
|
|
35
|
+
expect(markup).toContain("Nested include")
|
|
36
|
+
expect(markup).toContain("IT WORKS!")
|
|
37
|
+
})
|
|
32
38
|
})
|
package/vite.config.js
CHANGED
|
@@ -18,6 +18,13 @@ export default defineConfig({
|
|
|
18
18
|
},
|
|
19
19
|
plugins: [
|
|
20
20
|
twig({
|
|
21
|
+
globalContext: {
|
|
22
|
+
active_theme: "poodles",
|
|
23
|
+
},
|
|
24
|
+
functions: {
|
|
25
|
+
testFunction: (instance) =>
|
|
26
|
+
instance.extendFunction("testFunction", () => "IT WORKS!"),
|
|
27
|
+
},
|
|
21
28
|
namespaces: {
|
|
22
29
|
tests: join(__dirname, "/tests/fixtures"),
|
|
23
30
|
},
|