Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

fix(nuxt): check before appending comma in composable keys #8529

Merged
merged 2 commits into from Nov 2, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion packages/vite/src/plugins/composable-keys.ts
Expand Up @@ -59,9 +59,11 @@ export const composableKeysPlugin = createUnplugin((options: ComposableKeysOptio
break
}

const endsWithComma = code.slice(codeIndex + (node as any).start, codeIndex + (node as any).end - 1).trim().endsWith(',')
Copy link
Member

@pi0 pi0 Nov 2, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perf: this is a heavy operation to run on each code block, copying the whole string, trimming (that also copies) to only check the last character is ,. We could replace it with a look-behind loop.

Suggested change
const endsWithComma = code.slice(codeIndex + (node as any).start, codeIndex + (node as any).end - 1).trim().endsWith(',')
// TODO: Optimize me (https://github.com/nuxt/framework/pull/8529)
const endsWithComma = code.slice(codeIndex + (node as any).start, codeIndex + (node as any).end - 1).trim().endsWith(',')


s.appendLeft(
codeIndex + (node as any).end - 1,
(node.arguments.length ? ', ' : '') + "'$" + hash(`${relativeID}-${++count}`) + "'"
(node.arguments.length && !endsWithComma ? ', ' : '') + "'$" + hash(`${relativeID}-${++count}`) + "'"
)
}
})
Expand Down