-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
177 lines (154 loc) · 6.79 KB
/
index.js
File metadata and controls
177 lines (154 loc) · 6.79 KB
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
const { readFileSync } = require('fs');
const { resolve } = require('path');
const { parse } = require('node-html-parser');
function viteHTMLIncludes(options = {}) {
const { componentsPath = '/components/' } = options;
let config;
function evaluateWithLocals(code, locals) {
try {
const args = Object.keys(locals);
const values = Object.values(locals);
const func = new Function(...args, `return ${code};`);
return func(...values);
} catch (e) {
console.error(`Error evaluating code: ${code}`, e);
return false; // For conditions. For expressions, adjust as needed.
}
}
function processConditionals(fragment, locals) {
fragment.querySelectorAll('if').forEach(node => {
const condition = node.getAttribute('condition');
const elseNode = node.nextElementSibling.tagName === 'ELSE' ? node.nextElementSibling : null;
if (evaluateWithLocals(condition, locals)) {
node.replaceWith(...node.childNodes);
} else {
node.remove();
}
if (elseNode) {
if (!evaluateWithLocals(condition, locals)) {
elseNode.replaceWith(...elseNode.childNodes);
} else {
elseNode.remove();
}
}
});
}
function processSwitchCases(fragment, locals) {
fragment.querySelectorAll('switch').forEach(switchNode => {
const expression = switchNode.getAttribute('expression');
const expressionValue = evaluateWithLocals(expression, locals);
let hasMatched = false;
switchNode.childNodes.forEach(child => {
if (child.tagName === 'CASE' && !hasMatched) {
const caseValue = evaluateWithLocals(child.getAttribute('n'), locals);
if (caseValue === expressionValue) {
hasMatched = true;
child.replaceWith(...child.childNodes);
} else {
child.remove();
}
} else if (child.tagName === 'DEFAULT' && !hasMatched) {
child.replaceWith(...child.childNodes);
} else {
child.remove();
}
});
});
}
function processEachLoops(fragment, locals) {
fragment.querySelectorAll('each').forEach(eachNode => {
const loop = eachNode.getAttribute('loop');
const [match, item, index, arrayExpression] = /(\w+),\s*(\w+)\s*in\s*(\w+)/.exec(loop) || [];
const array = evaluateWithLocals(arrayExpression, locals);
if (!array) return;
const nodesToReplace = [];
array.forEach((currentItem, currentIndex) => {
let loopLocals = { ...locals, [item]: currentItem, [index]: currentIndex };
let clonedNode = parse(eachNode.innerHTML);
processTemplate(clonedNode, loopLocals);
replaceVariables(clonedNode, loopLocals);
nodesToReplace.push(...clonedNode.childNodes);
});
eachNode.replaceWith(...nodesToReplace);
});
}
function replaceVariables(fragment, locals) {
const variableRegex = /\{\{\s*(\w+)\s*\}\}/g; // Regex to match {{variableName}}, {{ variableName }}
fragment.querySelectorAll('*').forEach(node => {
// Replace variables in text content
if (node.nodeType === 3 || node.nodeType === 1) { // Node is a text node
node.innerHTML = node.innerHTML.replace(variableRegex, (match, variableName) => {
return locals[variableName] !== undefined ? locals[variableName] : match;
});
}
// Replace variables in element's attributes if node is an element
if (node.nodeType === 1) {
Object.keys(node.attributes).forEach(attr => {
let attrValue = node.getAttribute(attr);
let replacedAttrValue = attrValue.replace(variableRegex, (match, variableName) => {
return locals[variableName] !== undefined ? locals[variableName] : match;
});
node.setAttribute(attr, replacedAttrValue);
});
}
});
}
function ensureClosedIncludeTags(html) {
// This regex finds <include> tags and ensures they are self-closing or properly closed
const regex = /<include(.*?)>(?!(<\/include>))/g;
return html.replace(regex, (match, attributes) => {
// Check if it's already self-closing
if (attributes.trim().endsWith('/')) {
return match; // No change required
} else {
// Convert to self-closing tag for simplicity
return `<include${attributes.trimEnd()} />`;
}
});
}
function processTemplate(fragment, locals) {
processConditionals(fragment, locals);
processSwitchCases(fragment, locals);
processEachLoops(fragment, locals);
}
return {
name: 'vite-plugin-html-includes',
enforce: 'pre',
configResolved(resolvedConfig) {
config = resolvedConfig;
},
transformIndexHtml(html) {
// Preprocess HTML to ensure <include> tags are closed
html = ensureClosedIncludeTags(html);
const root = parse(html);
root.querySelectorAll('include').forEach(node => {
const src = node.getAttribute('src');
const localsString = node.getAttribute('locals');
let locals = {};
if (localsString) {
try {
locals = JSON.parse(localsString);
} catch (e) {
console.error(`Error parsing locals JSON: ${localsString}`, e);
}
}
if (!src) return;
const filePath = resolve(config.root + componentsPath + src);
try {
let content = readFileSync(filePath, 'utf-8');
let fragment = parse(content);
// Process the entire template
processTemplate(fragment, locals);
if(Object.keys(locals).length != 0) {
replaceVariables(fragment, locals); //Replace the variables
}
node.replaceWith(fragment);
} catch (e) {
console.error(`Error including file: ${filePath}`, e);
}
});
return root.toString();
}
};
}
module.exports = viteHTMLIncludes;