Skip to content

Commit

Permalink
Adding Lazy Loading documentation with examples (#2118)
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaumebriday authored and gauravtiwari committed Jun 10, 2019
1 parent 4787903 commit 53129ef
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,72 @@ end
```
You can read more about this in the [Vue docs](https://vuejs.org/v2/guide/installation.html#CSP-environments).

#### Lazy loading integration

See [docs/es6](docs/es6.md) to know more about Webpack and Webpacker configuration.

For instance, you can lazy load Vue JS components:

Before:
```js
import Vue from 'vue'
import { VCard } from 'vuetify/lib'
Vue.component('VCard', VCard)
```

After:
```js
import Vue from 'vue'
// With destructuring assignment
Vue.component('VCard', import('vuetify/lib').then(({ VCard }) => VCard)
// Or without destructuring assignment
Vue.component('OtherComponent', () => import('./OtherComponent'))
```

You can use it in a Single File Component as well:

```html
<template>
...
</template>
<script>
export default {
components: {
OtherComponent: () => import('./OtherComponent')
}
}
</script>
```

By wrapping the import function into an arrow function, Vue will execute it only when it gets requested, loading the module in that moment.

##### Automatic registration

```js
/**
* The following block of code may be used to automatically register your
* Vue components. It will recursively scan this directory for the Vue
* components and automatically register them with their "basename".
*
* Eg. ./components/OtherComponent.vue -> <other-component></other-component>
* Eg. ./UI/ButtonComponent.vue -> <button-component></button-component>
*/
const files = require.context('./', true, /\.vue$/i)
files.keys().map(key => {
const component = key.split('/').pop().split('.')[0]
// With Lazy Loading
Vue.component(component, () => import(`${key}`))

// Or without Lazy Loading
Vue.component(component, files(key).default)
})
```

### Elm

Expand Down
20 changes: 19 additions & 1 deletion docs/es6.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# ES6


## Babel

Webpacker ships with [babel](https://babeljs.io/) - a JavaScript compiler so
Expand All @@ -26,6 +25,25 @@ import "core-js/stable";
import "regenerator-runtime/runtime";
```

## Dynamic/Lazy Chunk Loading

For this section, you need Webpack and Webpacker 4. Then enable `SplitChunks` as it is explained in [docs/webpack](docs/webpack.md).

[Dynamic code splitting](https://webpack.js.org/guides/code-splitting#dynamic-imports) enables you to conditionally request/run only the JS that you need. For example, if your site has a `searchBarComponent` on every page, you can reduce the page overhead by deferring the request for the `searchBarComponent` code until after the page has loaded, until the user has scrolled it into view, or until the user has clicked on an element.

```js
function loadSearchBarComponent() {
return import(/* webpackChunkName: "searchBarComponent" */ './pathTo/searchBarComponent')
}
```

The comment you see above (`/* webpackChunkName */`) is not arbitrary, it is one of webpacks [magic comments](https://webpack.js.org/api/module-methods/#magic-comments). They can be used to fine-tune `import()` with settings such as `defer` or `prefetch`.

**Warning**: You should not attempt to dynamically load anything from your `packs/` folder. Instead, try to make your `pack` scripts a hub from which you dynamically load `non-pack` scripts.

- [Docs for using magic comments](https://webpack.js.org/api/module-methods/#magic-comments)
- [Docs for configuring `splitChunks` in webpacker](https://github.com/rails/webpacker/blob/master/docs/webpack.md#add-splitchunks-webpack-v4).
- [Docs for using dynamic `import()`](https://webpack.js.org/guides/code-splitting#dynamic-imports).

## Module import vs require()

Expand Down

0 comments on commit 53129ef

Please sign in to comment.