Recipe Lazy Pages - wsdo/webpackdoc GitHub Wiki

Lazy Pages

Structure

~ lib
+-- app.js
+-~ pages
  +-- home.js
  +-- some-stuff.js
  +-- other-stuff.js

Page loading code

function createPage(name, callback) {
  try {
    // use the "bundle/lazy" loader combined with a context to
    // make pages load on demand
    require("bundle/lazy!./pages/" + name)(function(Page) {
      // create a instance of the loaded Page
      return callback(new Page());
    });
  } catch(e) {
    // no Page for this name exists
    return callback(null);
  }
}

TODO: change "bundle/lazy" to "bundle?lazy" for 0.8