-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
244 lines (212 loc) · 7.97 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import path from "path";
import Twig from "twig";
import fs from "fs";
/**
* @params {PluginOptions} options Twig.twig() parameters.
* @example
* // vite.config.js
* import twig from 'vite-plugin-twig-loader';
* plugins: [
* twig({
* namespaces: { '@project': rootPath }, // this allows imports like this '{% from "@project/src/helper.html.twig" import some_helper %}'
* strict_variables: true
* })
* ]
*/
export default function twigPlugin(options = {}) {
let viteConfig;
// INFO: This works around duplicate template registering: 'There is already a template with the ID'. There is probably a better way to do this.
Twig.cache(false);
return {
name: "vite-plugin-storybook-twig",
enforce: "pre",
configResolved(resolvedConfig) {
viteConfig = resolvedConfig;
},
async transform(fileContent, id) {
if (!id.endsWith(".twig")) return null;
const projectRoot = viteConfig.root;
const twigOptions = {
allowInlineIncludes: true,
...options,
rethrow: true,
id: id,
path: id,
data: fileContent,
namespaces: Object.entries(options.namespaces || {}).reduce(
(acc, [key, value]) => {
acc[key] = path.resolve(projectRoot, value);
return acc;
},
{},
),
};
Twig.extend(function (eTwig) {
eTwig.compiler.module["vite"] = async function (id, tokensStr) {
const tokens = JSON.parse(tokensStr);
const dependencies = new Set();
const processDependency = async (token) => {
try {
const { content, resolvePath } = await resolveDependency(
token.value,
viteConfig.root,
twigOptions.namespaces,
);
if (!content) {
console.warn(`Empty dependency file at ${token.value}`);
return null;
}
let template;
// Only add valid dependencies to the set
if (resolvePath && !dependencies.has(resolvePath)) {
dependencies.add(resolvePath);
template = Twig.twig({
...twigOptions,
id: resolvePath,
path: resolvePath,
data: content,
});
// Process nested dependencies
for (const depToken of template.tokens) {
await processToken(depToken);
}
}
return template?.tokens;
} catch (error) {
console.warn(
`Failed to process dependency ${token.value}:`,
error,
);
return null;
}
};
const processToken = async (token) => {
if (token.type === "logic" && token.token.type) {
switch (token.token.type) {
case "Twig.logic.type.block":
case "Twig.logic.type.if":
case "Twig.logic.type.elseif":
case "Twig.logic.type.else":
case "Twig.logic.type.for":
case "Twig.logic.type.spaceless":
case "Twig.logic.type.setcapture":
case "Twig.logic.type.macro":
case "Twig.logic.type.apply":
for (const outputToken of token.token.output) {
await processToken(outputToken);
}
break;
case "Twig.logic.type.extends":
case "Twig.logic.type.include":
if (
token.token.stack.every(
(token) => token.type === "Twig.expression.type.string",
)
) {
for (const stackToken of token.token.stack) {
await processDependency(stackToken);
}
}
break;
case "Twig.logic.type.embed":
for (const outputToken of token.token.output) {
await processToken(outputToken);
}
for (const stackToken of token.token.stack) {
await processDependency(stackToken);
}
break;
case "Twig.logic.type.import":
case "Twig.logic.type.from":
if (token.token.expression !== "_self") {
for (const stackToken of token.token.stack) {
await processDependency(stackToken);
}
}
break;
}
}
};
// Process all tokens and collect dependencies
for (const token of tokens) {
await processToken(token);
}
const importStatements = Array.from(dependencies)
.map(
(dep, index) =>
`import { registerTemplate as registerTemplate_${index}} from "${dep}";`,
)
.join("\n");
// INFO: the id currently contains the actual path to the twig template
// we have to turn that path back into the same path that is used in the twig template for imports.
// Otherwise twig won't find the included template by its id.
let twigImportPath = id;
for (const [namespace, namespacePath] of Object.entries(
twigOptions.namespaces,
)) {
if (id.startsWith(namespacePath)) {
// Rereplace the namespaced path to get the original twig template path back
twigImportPath = id.replace(namespacePath, namespace);
break;
}
}
// Create the template render function
return `
import Twig from 'twig';
Twig.cache(false);
${importStatements}
const registerTemplate = () => {
${Array.from(dependencies)
.map((_dep, index) => `registerTemplate_${index}();`)
.join("\n")}
return Twig.twig(${JSON.stringify({
...twigOptions,
path: undefined, // remove the path from the original twigOptions, otherwise twig will try to import from that path instead.
id: twigImportPath, // we have to make sure this importPath is equal to the actual include paths, as twig will try to load included templates by this id.
data: tokens,
})});
};
export { registerTemplate };
export default function render(context = {}) {
try {
const template = registerTemplate();
return template.render(context);
} catch (error) {
console.error('Error rendering Twig template in ${id}:', error);
return '<div style="color: red">Error rendering Twig template in ${id}: ' + error.toString() + '</div>';
}
}
`;
};
});
// Compile the template using the custom vite compiler module above.
const code = await Twig.twig(twigOptions).compile({
module: "vite",
twig: "twig",
});
return { code };
},
};
}
async function resolveDependency(tokenValue, basePath, namespaces) {
let resolvePath = tokenValue;
// Handle namespaced paths
for (const [namespace, namespacePath] of Object.entries(namespaces)) {
if (tokenValue.startsWith(namespace)) {
resolvePath = tokenValue.replace(namespace, namespacePath);
break;
}
}
// Handle relative paths
if (resolvePath.startsWith("./") || resolvePath.startsWith("../")) {
resolvePath = path.resolve(basePath, resolvePath);
}
try {
const content = await fs.promises.readFile(resolvePath, "utf-8");
return { content, resolvePath };
} catch (error) {
throw new Error(
`Failed to load template: ${resolvePath}\nOriginal path: ${tokenValue}\nError: ${error.message}`,
);
}
}