Last active
September 7, 2017 13:42
-
-
Save mavame/991fe9d20a442eefaaa38994eda9d2c8 to your computer and use it in GitHub Desktop.
Lazy-loading (code splitting) ES6 using Webpack 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Flickity from 'flickity'; // flickity now appears in main bundle. | |
const slideshow = document.querySelector('.slideshow'); | |
if (slideshow) { | |
new Flickity(...); | |
} else { | |
// well, slideshow doesn't exist but we're still loading Flickity on every page :-( | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// better way of doing this is to wait until you know you need Flickity and use System.import which is a Webpack-specific thing | |
const slideshow = document.querySelector('.slideshow'); | |
if (slideshow) | |
System.import('flickity').then(Flickity => { | |
new Flickity(...); | |
}); | |
// now Flickity is in a different bundle, loaded automatically at runtime only when needed (when .slideshow exists). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// make sure you include a publicPath definition in your webpack config | |
module.exports = { | |
... | |
output: { | |
publicPath: '/assets/', | |
}, | |
}; | |
// this will be where webpack tries to resolve the individual bundle it generated when System.import() ran. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment