Skip to content

Commit f99eac5

Browse files
committed
fix(nuxt): ignore errors when treeshaking composables within other composables
resolves #33151
1 parent 9fe2541 commit f99eac5

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

packages/nuxt/src/core/plugins/tree-shake.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,12 @@ export const TreeShakeComposablesPlugin = (options: TreeShakeComposablesPluginOp
8282
return
8383
}
8484

85-
// TODO: validate function name against actual auto-imports registry
86-
s.overwrite(node.start, node.end, ` false && /*@__PURE__*/ ${functionName}${code.slice(node.callee.end, node.end)}`)
85+
try {
86+
// TODO: validate function name against actual auto-imports registry
87+
s.overwrite(node.start, node.end, ` false && /*@__PURE__*/ ${functionName}${code.slice(node.callee.end, node.end)}`)
88+
} catch {
89+
// TODO: fix when we can skip subsequent nodes even with scope tracker
90+
}
8791
},
8892
})
8993

packages/nuxt/test/tree-shake.test.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,24 @@ describe('tree-shake', () => {
3434
`)
3535
})
3636

37+
it('should not error when tree-shaking composables within other tree-shaken composables', () => {
38+
const code = `
39+
import { onMounted as _onMounted } from 'vue'
40+
_onMounted(() => {
41+
onMounted(() => {})
42+
})
43+
console.log('Hello World')
44+
`
45+
const { code: result } = transformPlugin.transform.handler(code, 'test.js')
46+
expect(clean(result)).toMatchInlineSnapshot(`
47+
"import { onMounted as _onMounted } from 'vue'
48+
false && /*@__PURE__*/ _onMounted(() => {
49+
onMounted(() => {})
50+
})
51+
console.log('Hello World')"
52+
`)
53+
})
54+
3755
it('should tree-shake explicitly-imported composables from #imports', () => {
3856
const code = `
3957
import { onMounted } from '#imports'

0 commit comments

Comments
 (0)