Skip to content

Commit dc9094c

Browse files
authored
perf: enable batch for default snippet handler (#17)
1 parent 612037a commit dc9094c

File tree

1 file changed

+39
-22
lines changed

1 file changed

+39
-22
lines changed

src/utils/template.ts

+39-22
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ interface SnippetHandler {
232232
key: (node: Expression) => string | null
233233
prepare: (node: Expression, id: number) => string
234234
parse: (code: string, id: number) => string | undefined
235+
standalone: boolean
235236
}
236237

237238
const defaultSnippetHandler: SnippetHandler = {
@@ -247,6 +248,7 @@ const defaultSnippetHandler: SnippetHandler = {
247248

248249
return res
249250
},
251+
standalone: false,
250252
}
251253

252254
const vSlotSnippetHandler: SnippetHandler = {
@@ -266,6 +268,7 @@ const vSlotSnippetHandler: SnippetHandler = {
266268
}
267269
return res.trim()
268270
},
271+
standalone: true,
269272
}
270273

271274
const snippetHandlers = [vSlotSnippetHandler, defaultSnippetHandler]
@@ -298,41 +301,55 @@ async function transformJsSnippets(expressions: Expression[], transform: (code:
298301
}
299302

300303
const resultMap = new Map<Expression, string>()
301-
for (const item of transformMap.values()) {
302-
// TODO: reenable batch processing after fixing issue with esbuild renaming variables
303-
const batch = [item]
304304

305+
const orders = Array.from(transformMap.values())
306+
const batch = orders.filter(({ handler }) => !handler.standalone)
307+
const standalone = orders.filter(({ handler }) => handler.standalone)
308+
309+
try {
305310
// transform all snippets in a single file
306311
const batchInputSplitter = `\nsplitter(${Math.random()});\n`
307312
const batchInput = batch
308313
.map(({ nodes, handler }) => handler.prepare(nodes[0], id))
309314
.join(batchInputSplitter)
310315

311-
try {
312-
const batchOutput = await transform(batchInput)
313-
const lines = batchOutput.split(batchInputSplitter).map(l => l.trim()).filter(l => !!l)
316+
const batchOutput = await transform(batchInput)
317+
const lines = batchOutput.split(batchInputSplitter).map(l => l.trim()).filter(l => !!l)
314318

315-
if (lines.length !== batch.length) {
316-
throw new Error('[vue-sfc-transform] Syntax Error')
317-
}
319+
if (lines.length !== batch.length) {
320+
throw new Error('[vue-sfc-transform] Syntax Error')
321+
}
318322

319-
for (let i = 0; i < batch.length; i++) {
320-
const line = lines[i]!
321-
const { id, handler, nodes } = batch[i]!
323+
for (let i = 0; i < batch.length; i++) {
324+
const line = lines[i]!
325+
const { id, handler, nodes } = batch[i]!
322326

323-
const res = handler.parse(line, id)
324-
if (!res) {
325-
continue
326-
}
327+
const res = handler.parse(line, id)
328+
if (!res) {
329+
continue
330+
}
327331

328-
for (const node of nodes) {
329-
resultMap.set(node, res)
330-
}
332+
for (const node of nodes) {
333+
resultMap.set(node, res)
331334
}
332335
}
333-
catch (error) {
334-
throw new Error('[vue-sfc-transform] Error parsing TypeScript expression in template', { cause: error })
335-
}
336+
337+
// transform standalone snippets
338+
await Promise.all(standalone.map(async ({ id, handler, nodes }) => {
339+
const line = await transform(handler.prepare(nodes[0], id))
340+
341+
const res = handler.parse(line.trim(), id)
342+
if (!res) {
343+
return
344+
}
345+
346+
for (const node of nodes) {
347+
resultMap.set(node, res)
348+
}
349+
}))
350+
}
351+
catch (error) {
352+
throw new Error('[vue-sfc-transform] Error parsing TypeScript expression in template', { cause: error })
336353
}
337354

338355
return resultMap

0 commit comments

Comments
 (0)