Skip to content

Instantly share code, notes, and snippets.

@treshugart
Last active August 11, 2017 23:33
Show Gist options
  • Save treshugart/abf7f61c6293d9d5687cf98a9d05be1c to your computer and use it in GitHub Desktop.
Save treshugart/abf7f61c6293d9d5687cf98a9d05be1c to your computer and use it in GitHub Desktop.
SkateJS code-splitting with Webpack 2

Code-splitting and deferred upgrades in SkateJS

This is an example of code splitting and deferred upgrades in using SkateJS.

The key here is the call to System.import('./deferred') which tells Webpack 2 to bundle ./deferred and all of its dependencies into a separate bundle. When the button is clicked, the loading of that bundle is executed.

In the ./deferred bundle, the definition for <x-deferred /> is defined, thus causing the element to upgrade.

I tried using just import('./deferred'), which is the preferred way to do it, but got a syntax error.

import { Component, h } from 'skatejs';
class Deferred extends Component {
renderCallback () {
return <span>Yay! Loaded!!!</span>;
}
}
customElements.define('x-deferred', Deferred);
// Don't forget the polyfills!
import { Component, h } from 'skatejs';
class App extends Component {
renderCallback () {
return [
<button onClick={() => System.import('./deferred')}>Load deferred</button>,
<x-deferred>Not loaded yet :(</x-deferred>
];
}
}
customElements.define('x-app', App);
document.body.appendChild(new App());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment