Skip to content

Commit 7ebaed9

Browse files
migration: Add Global API Treeshaking (#144)
* migration: Add Global API Treeshaking * CR * Fix heading levels * Use "previous/current syntax" for consistency * Update src/guide/migration/treeshaking.md Co-authored-by: Natalia Tepluhina <[email protected]> * chore: mention that Vue.observable is replaced by Vue.reactive Co-authored-by: Natalia Tepluhina <[email protected]>
1 parent cc0673f commit 7ebaed9

File tree

2 files changed

+162
-5
lines changed

2 files changed

+162
-5
lines changed

src/.vuepress/theme/styles/custom-blocks.styl

-4
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,12 @@
1616
color darken(#ffe564, 70%)
1717
.custom-block-title
1818
color darken(#ffe564, 50%)
19-
a
20-
color $textColor
2119
&.danger
2220
background-color #ffe6e6
2321
border-color darken(red, 20%)
2422
color darken(red, 70%)
2523
.custom-block-title
2624
color darken(red, 40%)
27-
a
28-
color $textColor
2925
&.details
3026
display block
3127
position relative

src/guide/migration/treeshaking.md

+162-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,164 @@
11
# Global API Treeshaking
22

3-
<!--Tmp. placeholder for linking purpose-->
3+
## Previous Syntax
4+
5+
If you’ve ever had to manually manipulate DOM in Vue, you might have come across this pattern:
6+
7+
```js
8+
import Vue from 'vue'
9+
10+
Vue.nextTick(() => {
11+
// something something DOM-related
12+
})
13+
```
14+
15+
Or, if you’ve been unit-testing an application involving [async components](/guide/component-dynamic-async.html), chances are you’ve written something like this:
16+
17+
```js
18+
import { shallowMount } from '@vue/test-utils'
19+
import { MyComponent } from './MyComponent.vue'
20+
21+
test('an async feature', async () => {
22+
const wrapper = shallowMount(MyComponent)
23+
24+
// execute some DOM-related tasks
25+
26+
await wrapper.vm.$nextTick()
27+
28+
// run your assertions
29+
})
30+
```
31+
32+
`Vue.nextTick()` is a global API exposed directly on a single Vue object – in fact, the instance method `$nextTick()` is just a handy wrapper around `Vue.nextTick()` with the callback’s `this` context automatically bound to the current Vue instance for convenience.
33+
34+
But what if you’ve never had to deal with manual DOM manipulation, nor are you using or testing async components in our app? Or, what if, for whatever reason, you prefer to use the good old `window.setTimeout()` instead? In such a case, the code for `nextTick()` will become dead code – that is, code that’s written but never used. And dead code is hardly a good thing, especially in our client-side context where every kilobyte matters.
35+
36+
Module bundlers like [webpack](https://webpack.js.org/) support [tree-shaking](https://webpack.js.org/guides/tree-shaking/), which is a fancy term for “dead code elimination.” Unfortunately, due to how the code is written in previous Vue versions, global APIs like `Vue.nextTick()` are not tree-shakeable and will be included in the final bundle regardless of where they are actually used or not.
37+
38+
## Current Syntax
39+
40+
In Vue 3, the global and internal APIs have been restructured with tree-shaking support in mind. As a result, the global APIs can now only be accessed as named exports for the ES Modules build. For example, our previous snippets should now look like this:
41+
42+
```js
43+
import { nextTick } from 'vue'
44+
45+
nextTick(() => {
46+
// something something DOM-related
47+
})
48+
```
49+
50+
and
51+
52+
```js
53+
import { shallowMount } from '@vue/test-utils'
54+
import { MyComponent } from './MyComponent.vue'
55+
import { nextTick } from 'vue'
56+
57+
test('an async feature', async () => {
58+
const wrapper = shallowMount(MyComponent)
59+
60+
// execute some DOM-related tasks
61+
62+
await nextTick()
63+
64+
// run your assertions
65+
})
66+
```
67+
68+
Calling `Vue.nextTick()` directly will now result in the infamous `undefined is not a function` error.
69+
70+
With this change, provided the module bundler supports tree-shaking, global APIs that are not used in a Vue application will be eliminated from the final bundle, resulting in an optimal file size.
71+
72+
## Affected APIs
73+
74+
These global APIs in Vue 2.x are affected by this change:
75+
76+
* `Vue.nextTick`
77+
* `Vue.observable` (replaced by `Vue.reactive`)
78+
* `Vue.version`
79+
* `Vue.compile` (only in full builds)
80+
* `Vue.set` (only in compat builds)
81+
* `Vue.delete` (only in compat builds)
82+
83+
84+
## Internal Helpers
85+
86+
In addition to public APIs, many of the internal components/helpers are now exported as named exports as well. This allows the compiler to output code that only imports features when they are used. For example the following template:
87+
88+
```html
89+
<transition>
90+
<div v-show="ok">hello</div>
91+
</transition>
92+
```
93+
94+
is compiled into something similar to the following:
95+
96+
```js
97+
import { h, Transition, applyDirectives, vShow } from 'vue'
98+
99+
export function render() {
100+
return h(Transition, [
101+
applyDirectives(h('div', 'hello'), this, [vShow, this.ok])
102+
])
103+
}
104+
```
105+
106+
This essentially means the `Transition` component only gets imported when the application actually makes use of it. In other words, if the application doesn’t have any `<transition>` component, the code supporting this feature will not be present in the final bundle.
107+
108+
With global tree-shaking, the user only “pay” for the features they actually use. Even better, knowing that optional features won't increase the bundle size for applications not using them, framework size has become much less a concern for additional core features in the future, if at all.
109+
110+
::: warning Important
111+
The above only applies to the [ES Modules builds](/guide/installation.html#explanation-of-different-builds) for use with tree-shaking capable bundlers - the UMD build still includes all features and exposes everything on the Vue global variable (and the compiler will produce appropriate output to use APIs off the global instead of importing).
112+
:::
113+
114+
## Usage in Plugins
115+
116+
If your plugin relies on an affected Vue 2.x global API, for instance:
117+
118+
```js
119+
const plugin = {
120+
install: Vue => {
121+
Vue.nextTick(() => {
122+
// ...
123+
})
124+
}
125+
}
126+
```
127+
128+
In Vue 3, you’ll have to import it explicitly:
129+
130+
```js
131+
import { nextTick } from 'vue'
132+
133+
const plugin = {
134+
install: app => {
135+
nextTick(() => {
136+
// ...
137+
})
138+
}
139+
}
140+
```
141+
142+
If you use a module bundle like webpack, this may cause Vue’s source code to be bundled into the plugin, and more often than not that’s not what you'd expect. A common practice to prevent this from happening is to configure the module bundler to exclude Vue from the final bundle. In webpack's case, you can use the [`externals`](https://webpack.js.org/configuration/externals/) configuration option:
143+
144+
```js
145+
// webpack.config.js
146+
module.exports = {
147+
/*...*/
148+
externals: {
149+
vue: 'Vue'
150+
}
151+
}
152+
```
153+
154+
This will tell webpack to treat the Vue module as an external library and not bundle it.
155+
156+
If your module bundler of choice happens to be [Rollup](https://rollupjs.org/), you basically get the same effect for free, as by default Rollup will treat absolute module IDs (`'vue'` in our case) as external dependencies and not include them in the final bundle. During bundling though, it might emit a [“Treating vue as external dependency”](https://rollupjs.org/guide/en/#warning-treating-module-as-external-dependency) warning, which can be suppressed with the `external` option:
157+
158+
```js
159+
// rollup.config.js
160+
export default {
161+
/*...*/
162+
external: ['vue']
163+
}
164+
```

0 commit comments

Comments
 (0)