MCPcopy Create free account
hub / github.com/TanStack/cli / compileTemplate

Function compileTemplate

packages/create/scripts/generate-manifest.mjs:206–253  ·  view source on GitHub ↗
(template)

Source from the content-addressed store, hash-verified

204}
205
206function compileTemplate(template) {
207 const regex = /<%([=_#-]?|_)?([\s\S]*?)([-_]?%>)/g
208 let cursor = 0
209 let trimLeadingWhitespace = false
210 const lines = []
211
212 function appendText(value) {
213 if (!value) {
214 return
215 }
216 lines.push(` __append(${JSON.stringify(value)})`)
217 }
218
219 for (const match of template.matchAll(regex)) {
220 let text = template.slice(cursor, match.index)
221 if (trimLeadingWhitespace) {
222 text = text.replace(/^\s*\r?\n?/, '')
223 trimLeadingWhitespace = false
224 }
225 if (match[1] === '_') {
226 text = text.replace(/\s*$/, '')
227 }
228 appendText(text)
229
230 const marker = match[1] || ''
231 const code = match[2]
232 const close = match[3]
233
234 if (marker === '=') {
235 lines.push(` __append(__escapeXML(${stripSemicolon(code.trim())}))`)
236 } else if (marker === '-') {
237 lines.push(` __append(${stripSemicolon(code.trim())})`)
238 } else if (marker !== '#') {
239 lines.push(code)
240 }
241
242 trimLeadingWhitespace = close.startsWith('-') || close.startsWith('_')
243 cursor = match.index + match[0].length
244 }
245
246 let tail = template.slice(cursor)
247 if (trimLeadingWhitespace) {
248 tail = tail.replace(/^\s*\r?\n?/, '')
249 }
250 appendText(tail)
251
252 return lines.join('\n')
253}
254
255function createTemplateRendererSource(renderers = templateRenderers) {
256 const entries = Array.from(renderers.entries()).sort(([a], [b]) =>

Calls 3

appendTextFunction · 0.85
stripSemicolonFunction · 0.85
replaceMethod · 0.65

Tested by

no test coverage detected