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

Commit

Permalink
refactor: early return
Browse files Browse the repository at this point in the history
  • Loading branch information
danielroe committed Sep 25, 2022
1 parent 103e09d commit 0af845c
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions packages/nuxt/src/components/tree-shake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,22 @@ export const TreeShakeTemplatePlugin = createUnplugin((options: TreeShakeTemplat

const ast = parse(template[0])
await walk(ast, (node) => {
if (node.type === ELEMENT_NODE && clientOnlyComponents.includes(node.name)) {
if (!node.children?.length) { return }
if (node.type !== ELEMENT_NODE || !clientOnlyComponents.includes(node.name) || !node.children?.length) {
return
}

const fallback = node.children.find(
(n: Node) => n.name === 'template' &&
Object.entries(n.attributes as Record<string, string>)?.flat().some(attr => PLACEHOLDER_RE.test(attr))
)

const fallback = node.children.find(
(n: Node) => n.name === 'template' &&
Object.entries(n.attributes as Record<string, string>)?.flat().some(attr => PLACEHOLDER_RE.test(attr))
)
try {
// Replace node content
const text = fallback ? code.slice(fallback.loc[0].start, fallback.loc[fallback.loc.length - 1].end) : ''
s.overwrite(template.index + node.loc[0].end, template.index + node.loc[node.loc.length - 1].start, text)
} catch (err) {
// This may fail if we have a nested client-only component and are trying
// to replace some text that has already been replaced
}
try {
// Replace node content
const text = fallback ? code.slice(fallback.loc[0].start, fallback.loc[fallback.loc.length - 1].end) : ''
s.overwrite(template.index + node.loc[0].end, template.index + node.loc[node.loc.length - 1].start, text)
} catch (err) {
// This may fail if we have a nested client-only component and are trying
// to replace some text that has already been replaced
}
})

Expand Down

0 comments on commit 0af845c

Please sign in to comment.