vite-plugin-twig-drupal 1.0.0 → 1.0.1
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 +19 -7
- package/tests/fixtures/error-include.twig +1 -0
- package/tests/smoke.test.js +5 -0
- package/vite.config.js +1 -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
|
@@ -64,10 +64,20 @@ const compileTemplate = (id, file, { namespaces }) => {
|
|
|
64
64
|
|
|
65
65
|
Twig.cache(false)
|
|
66
66
|
|
|
67
|
-
const errorHandler =
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
67
|
+
const errorHandler =
|
|
68
|
+
(id, isDefault = true) =>
|
|
69
|
+
(e) => {
|
|
70
|
+
if (isDefault) {
|
|
71
|
+
return {
|
|
72
|
+
code: `export default () => 'An error occurred whilst rendering ${id}: ${e.toString()}';`,
|
|
73
|
+
map: null,
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return {
|
|
77
|
+
code: null,
|
|
78
|
+
map: null,
|
|
79
|
+
}
|
|
80
|
+
}
|
|
71
81
|
|
|
72
82
|
const plugin = (options = {}) => {
|
|
73
83
|
options = { ...defaultOptions, ...options }
|
|
@@ -109,13 +119,15 @@ const plugin = (options = {}) => {
|
|
|
109
119
|
const file = Twig.path.expandNamespace(options.namespaces, template)
|
|
110
120
|
if (!seen.includes(file)) {
|
|
111
121
|
includePromises.push(
|
|
112
|
-
new Promise(async (resolve) => {
|
|
122
|
+
new Promise(async (resolve, reject) => {
|
|
113
123
|
const { includes, code } = await compileTemplate(
|
|
114
124
|
template,
|
|
115
125
|
file,
|
|
116
126
|
options
|
|
117
|
-
)
|
|
118
|
-
includes
|
|
127
|
+
).catch(errorHandler(template, false))
|
|
128
|
+
if (includes) {
|
|
129
|
+
includes.forEach(processIncludes)
|
|
130
|
+
}
|
|
119
131
|
resolve(code)
|
|
120
132
|
})
|
|
121
133
|
)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{% include "@tests/error.twig" %}
|
package/tests/smoke.test.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import Markup from "../dist/test.js"
|
|
2
2
|
import Error from "../dist/error.js"
|
|
3
|
+
import ErrorInclude from "../dist/errorInclude.js"
|
|
3
4
|
import { describe, expect, it } from "vitest"
|
|
4
5
|
|
|
5
6
|
describe("Basic smoke test", () => {
|
|
@@ -17,4 +18,8 @@ describe("Basic smoke test", () => {
|
|
|
17
18
|
const error = Error()
|
|
18
19
|
expect(error).toContain("An error occurred")
|
|
19
20
|
})
|
|
21
|
+
it("Should recover from include errors", () => {
|
|
22
|
+
const error = ErrorInclude()
|
|
23
|
+
expect(error).toContain("An error occurred")
|
|
24
|
+
})
|
|
20
25
|
})
|
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
|
+
errorInclude: resolve(__dirname, "tests/fixtures/error-include.twig"),
|
|
12
13
|
},
|
|
13
14
|
name: "vite-plugin-twig-drupal",
|
|
14
15
|
fileName: (_, entry) => `${entry}.js`,
|