Build modules on demand in webpack development servers
npm install webpack-on-demand
yarn add --dev webpack-on-demand
OR
npm install --save-dev webpack-on-demand
`
In webpack.config.js:
- Create an instance of the plugin
`
const WebpackOnDemand = require('webpack-on-demand').default;
const wodPluginInstance = new WebpackOnDemand();
`
- Enable Hot Module Reloading for your dev server
- Add the plugin to the webpack resolve config
`
resolve: {
plugins: [
wodPluginInstance
],
}
`
- Pass the plugin server bootstrap to the before option of devServer
`
devServer: {
...
before: wodPluginInstance.serverBootstrap
},
`
Usage instructions
Add the ?on-demand query string to an import statement. This will wrap
the import in a callback that returns a Promise.
`
// before
import HomePage from './components/HomePage';
const HomePage = new HomePage();
// after
import getHomePage from './components/HomePage?on-demand';
getHomePage().then(HomePage => {
const HomePage = new HomePage();
});
`
The module will only be compiled once the callback function (in the
above example getHomePage()`) is called. If you're not using the