webpack/examples/hybrid-routing/router.js

25 lines
879 B
JavaScript
Raw Normal View History

var render = require("./render");
// Event when another page should be opened
// Maybe hook click on links, hashchange or popstate
window.onLinkToPage = function onLinkToPage(name) { // name is "a" or "b"
// require the page with a dynamic require
// It's important that this require only matches the pages
// elsewise there is blood in the bundle. Here this is done with a
// specific file prefix. It's also possible to use a directory,
// overwriting the RegExp with the ContextReplacementPlugin, or
// using the require.context method.
// The bundle-loader is used to create a chunk from the page
// -> Pages are only loaded on demand
// This line may throw a exception on runtime if the page wasn't found.
var pageBundle = require("bundle-loader!./" + name + "Page");
// Wait until the chunk is loaded
pageBundle(function(page) {
render(page);
});
}