Skip to content

Conversation

@patrickbrouwers
Copy link

As discussed on Twitter, here's the example on how to load the components dynamically.

@@ -0,0 +1,1703 @@
(window["webpackJsonp"] = window["webpackJsonp"] || []).push([[0],{
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the axios chunk, gets loaded on every page that needs axios. Won't get loaded if no axios is imported in the component.

const files = require.context('./', true, /\.vue$/i)
files.keys().map(key => Vue.component(key.split('/').pop().split('.')[0], files(key).default))
// Register global Vue components
Vue.component('Layout', require('./Misc/Layout').default);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you first register the components you want globally, it will keep them in the main chunk (app.js)


// Register all Async Vue components
require.context('./', true, /\.vue$/i, 'lazy').keys().forEach(file => {
Vue.component(file.split('/').pop().split('.')[0], () => import(`${file}`));
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

${ is needed, as you can't pass an expression to import and require

Vue.component('Layout', require('./Misc/Layout').default);

// Register all Async Vue components
require.context('./', true, /\.vue$/i, 'lazy').keys().forEach(file => {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The key is to lazy load the context, so it doesn't already load all the components

.extract(['vue', 'lodash', 'popper.js', 'axios'])
.webpackConfig({
output: {
chunkFilename: 'js/[name].js',
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added this, so the chunk end up in the /js instead of /public folder


mix.js('resources/js/app.js', 'public/js')
.sass('resources/sass/app.scss', 'public/css')
.extract(['vue', 'lodash', 'popper.js', 'axios'])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is no longer needed really, as chunks already take care of this

@reinink
Copy link
Owner

reinink commented Feb 13, 2019

This is awesome @patrickbrouwers! 💥💥💥

I think I'm going to create a fresh branch for this.

Thanks so much! ❤️

@reinink reinink closed this Feb 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants