Skip to content

Commit b4112ff

Browse files
committed
add a note on defineComponent() treeshaking with webpack
1 parent 3491a83 commit b4112ff

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/api/general.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,18 @@ A type helper for defining a Vue component with type inference.
122122
type FooInstance = InstanceType<typeof Foo>
123123
```
124124

125+
### Note on webpack Treeshaking
126+
127+
Because `defineComponent()` is a function call, it could look like that it would produce side-effects to some build tools, e.g. webpack. This will prevent the component from being tree-shaken even when the component is never used.
128+
129+
To tell webpack that this function call is safe to be tree-shaken, you can add a `/*#__PURE__*/` comment notation before the function call:
130+
131+
```js
132+
export default /*#__PURE__*/ defineComponent(/* ... */)
133+
```
134+
135+
Note this is not necessary if you are using Vite, because Rollup (the underlying production bundler used by Vite) is smart enough to determine that `defineComponent()` is in fact side-effect-free without the need for manual annotations.
136+
125137
- **See also:** [Guide - Using Vue with TypeScript](/guide/typescript/overview.html#general-usage-notes)
126138

127139
## defineAsyncComponent()

src/guide/typescript/overview.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ export default defineComponent({
129129
})
130130
```
131131

132-
See also: [type tests for `defineComponent`](https://github.com/vuejs/core/blob/main/test-dts/defineComponent.test-d.tsx)
132+
See also:
133+
134+
- [Note on webpack Treeshaking](/api/general.html#note-on-webpack-treeshaking)
135+
- [type tests for `defineComponent`](https://github.com/vuejs/core/blob/main/test-dts/defineComponent.test-d.tsx)
133136

134137
:::tip
135138
`defineComponent()` also enables type inference for components defined in plain JavaScript.

0 commit comments

Comments
 (0)