From 49545e6bdf190065735db085588aa5c01b56ebaa Mon Sep 17 00:00:00 2001 From: tbitw2549 Date: Sat, 26 Oct 2024 18:08:59 +0300 Subject: [PATCH] perf: scope islands transform functions --- .../components/plugins/islands-transform.ts | 68 +++++++++---------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/packages/nuxt/src/components/plugins/islands-transform.ts b/packages/nuxt/src/components/plugins/islands-transform.ts index 2b854b9bbc..9016f07986 100644 --- a/packages/nuxt/src/components/plugins/islands-transform.ts +++ b/packages/nuxt/src/components/plugins/islands-transform.ts @@ -38,6 +38,40 @@ export const IslandsTransformPlugin = (options: ServerOnlyComponentTransformPlug return `
${code}
` } + /** + * extract attributes from a node + */ + function extractAttributes (attributes: Record, names: string[]) { + const extracted: Record = {} + for (const name of names) { + if (name in attributes) { + extracted[name] = attributes[name]! + delete attributes[name] + } + } + return extracted + } + + function attributeToString (attributes: Record) { + return Object.entries(attributes).map(([name, value]) => value ? ` ${name}="${value}"` : ` ${name}`).join('') + } + + function isBinding (attr: string): boolean { + return attr.startsWith(':') + } + + function getPropsToString (bindings: Record): string { + const vfor = bindings['v-for']?.split(' in ').map((v: string) => v.trim()) as [string, string] | undefined + if (Object.keys(bindings).length === 0) { return 'undefined' } + const content = Object.entries(bindings).filter(b => b[0] && (b[0] !== '_bind' && b[0] !== 'v-for')).map(([name, value]) => isBinding(name) ? `[\`${name.slice(1)}\`]: ${value}` : `[\`${name}\`]: \`${value}\``).join(',') + const data = bindings._bind ? `__mergeProps(${bindings._bind}, { ${content} })` : `{ ${content} }` + if (!vfor) { + return `[${data}]` + } else { + return `__vforToArray(${vfor[1]}).map(${vfor[0]} => (${data}))` + } + } + return { name: 'nuxt:server-only-component-transform', enforce: 'pre', @@ -145,40 +179,6 @@ export const IslandsTransformPlugin = (options: ServerOnlyComponentTransformPlug } }) -/** - * extract attributes from a node - */ -function extractAttributes (attributes: Record, names: string[]) { - const extracted: Record = {} - for (const name of names) { - if (name in attributes) { - extracted[name] = attributes[name]! - delete attributes[name] - } - } - return extracted -} - -function attributeToString (attributes: Record) { - return Object.entries(attributes).map(([name, value]) => value ? ` ${name}="${value}"` : ` ${name}`).join('') -} - -function isBinding (attr: string): boolean { - return attr.startsWith(':') -} - -function getPropsToString (bindings: Record): string { - const vfor = bindings['v-for']?.split(' in ').map((v: string) => v.trim()) as [string, string] | undefined - if (Object.keys(bindings).length === 0) { return 'undefined' } - const content = Object.entries(bindings).filter(b => b[0] && (b[0] !== '_bind' && b[0] !== 'v-for')).map(([name, value]) => isBinding(name) ? `[\`${name.slice(1)}\`]: ${value}` : `[\`${name}\`]: \`${value}\``).join(',') - const data = bindings._bind ? `__mergeProps(${bindings._bind}, { ${content} })` : `{ ${content} }` - if (!vfor) { - return `[${data}]` - } else { - return `__vforToArray(${vfor[1]}).map(${vfor[0]} => (${data}))` - } -} - export const ComponentsChunkPlugin = createUnplugin((options: ComponentChunkOptions) => { const { buildDir } = options return {