vite-plugin-twig-drupal 1.0.0 → 1.0.2
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/.github/workflows/node.js.yml +1 -1
- package/README.md +2 -3
- package/package.json +1 -1
- package/src/index.js +21 -7
- package/tests/__snapshots__/smoke.test.js.snap +17 -0
- package/tests/fixtures/error-include.twig +1 -0
- package/tests/fixtures/macro.twig +12 -0
- package/tests/fixtures/menu.twig +7 -0
- package/tests/smoke.test.js +11 -0
- package/vite.config.js +2 -0
package/README.md
CHANGED
|
@@ -77,7 +77,7 @@ You then need to configure your vite.config.js.
|
|
|
77
77
|
|
|
78
78
|
```javascript
|
|
79
79
|
import { defineConfig } from "vite"
|
|
80
|
-
import twig from
|
|
80
|
+
import twig from 'vite-plugin-twig-drupal';
|
|
81
81
|
import { join } from "node:path"
|
|
82
82
|
|
|
83
83
|
export default defineConfig({
|
|
@@ -104,7 +104,7 @@ With this config in place you should be able to import twig files into your stor
|
|
|
104
104
|
```javascript
|
|
105
105
|
// stories/Button.stories.js
|
|
106
106
|
|
|
107
|
-
// Button will be a Javascript function that
|
|
107
|
+
// Button will be a Javascript function that accepts variables for the twig template.
|
|
108
108
|
import Button from './button.twig';
|
|
109
109
|
|
|
110
110
|
// Import stylesheets, this could be a sass or postcss file too.
|
|
@@ -149,7 +149,6 @@ export const ButtonStrip = {
|
|
|
149
149
|
|
|
150
150
|
|
|
151
151
|
```
|
|
152
|
-
- Refer to the [Dom testing library docs](https://testing-library.com/docs/dom-testing-library/example-intro), we're really just adding the ability to render twig templates on top of that.
|
|
153
152
|
|
|
154
153
|
## Issues
|
|
155
154
|
|
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -18,6 +18,7 @@ const includeTokenTypes = [
|
|
|
18
18
|
"Twig.logic.type.embed",
|
|
19
19
|
"Twig.logic.type.include",
|
|
20
20
|
"Twig.logic.type.extends",
|
|
21
|
+
"Twig.logic.type.import",
|
|
21
22
|
]
|
|
22
23
|
|
|
23
24
|
const pluckIncludes = (tokens) => {
|
|
@@ -64,10 +65,20 @@ const compileTemplate = (id, file, { namespaces }) => {
|
|
|
64
65
|
|
|
65
66
|
Twig.cache(false)
|
|
66
67
|
|
|
67
|
-
const errorHandler =
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
68
|
+
const errorHandler =
|
|
69
|
+
(id, isDefault = true) =>
|
|
70
|
+
(e) => {
|
|
71
|
+
if (isDefault) {
|
|
72
|
+
return {
|
|
73
|
+
code: `export default () => 'An error occurred whilst rendering ${id}: ${e.toString()}';`,
|
|
74
|
+
map: null,
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return {
|
|
78
|
+
code: null,
|
|
79
|
+
map: null,
|
|
80
|
+
}
|
|
81
|
+
}
|
|
71
82
|
|
|
72
83
|
const plugin = (options = {}) => {
|
|
73
84
|
options = { ...defaultOptions, ...options }
|
|
@@ -109,13 +120,15 @@ const plugin = (options = {}) => {
|
|
|
109
120
|
const file = Twig.path.expandNamespace(options.namespaces, template)
|
|
110
121
|
if (!seen.includes(file)) {
|
|
111
122
|
includePromises.push(
|
|
112
|
-
new Promise(async (resolve) => {
|
|
123
|
+
new Promise(async (resolve, reject) => {
|
|
113
124
|
const { includes, code } = await compileTemplate(
|
|
114
125
|
template,
|
|
115
126
|
file,
|
|
116
127
|
options
|
|
117
|
-
)
|
|
118
|
-
includes
|
|
128
|
+
).catch(errorHandler(template, false))
|
|
129
|
+
if (includes) {
|
|
130
|
+
includes.forEach(processIncludes)
|
|
131
|
+
}
|
|
119
132
|
resolve(code)
|
|
120
133
|
})
|
|
121
134
|
)
|
|
@@ -124,6 +137,7 @@ const plugin = (options = {}) => {
|
|
|
124
137
|
}
|
|
125
138
|
includes.forEach(processIncludes)
|
|
126
139
|
embed = includes
|
|
140
|
+
.filter((template) => template !== "_self")
|
|
127
141
|
.map(
|
|
128
142
|
(template) =>
|
|
129
143
|
`import '${resolve(
|
|
@@ -23,6 +23,23 @@ exports[`Basic smoke test > Should support includes 1`] = `
|
|
|
23
23
|
"
|
|
24
24
|
`;
|
|
25
25
|
|
|
26
|
+
exports[`Basic smoke test > Should support macros 1`] = `
|
|
27
|
+
"<ul>
|
|
28
|
+
<li><a href=\\"/home\\">Home</a>
|
|
29
|
+
</li>
|
|
30
|
+
|
|
31
|
+
<li><a href=\\"/about\\">About</a>
|
|
32
|
+
<ul>
|
|
33
|
+
<li><a href=\\"/contact\\">Contact</a>
|
|
34
|
+
</li>
|
|
35
|
+
|
|
36
|
+
</ul>
|
|
37
|
+
</li>
|
|
38
|
+
|
|
39
|
+
</ul>
|
|
40
|
+
"
|
|
41
|
+
`;
|
|
42
|
+
|
|
26
43
|
exports[`Basic smoke test > Should support variables 1`] = `
|
|
27
44
|
"<section>
|
|
28
45
|
<h1>Include</h1>
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{% include "@tests/error.twig" %}
|
|
@@ -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 %}
|
package/tests/smoke.test.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import Markup from "../dist/test.js"
|
|
2
2
|
import Error from "../dist/error.js"
|
|
3
|
+
import ErrorInclude from "../dist/errorInclude.js"
|
|
4
|
+
import Menu from "../dist/menu.js"
|
|
3
5
|
import { describe, expect, it } from "vitest"
|
|
4
6
|
|
|
5
7
|
describe("Basic smoke test", () => {
|
|
@@ -17,4 +19,13 @@ describe("Basic smoke test", () => {
|
|
|
17
19
|
const error = Error()
|
|
18
20
|
expect(error).toContain("An error occurred")
|
|
19
21
|
})
|
|
22
|
+
it("Should recover from include errors", () => {
|
|
23
|
+
const error = ErrorInclude()
|
|
24
|
+
expect(error).toContain("An error occurred")
|
|
25
|
+
})
|
|
26
|
+
it("Should support macros", () => {
|
|
27
|
+
const markup = Menu()
|
|
28
|
+
expect(markup).toContain("Contact")
|
|
29
|
+
expect(markup).toMatchSnapshot()
|
|
30
|
+
})
|
|
20
31
|
})
|
package/vite.config.js
CHANGED
|
@@ -9,6 +9,8 @@ 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"),
|
|
13
|
+
errorInclude: resolve(__dirname, "tests/fixtures/error-include.twig"),
|
|
12
14
|
},
|
|
13
15
|
name: "vite-plugin-twig-drupal",
|
|
14
16
|
fileName: (_, entry) => `${entry}.js`,
|