static site generator webpack plugin
Minimal, unopinionated static site generator powered by webpack.
Provide a series of paths to be rendered, and a matching set of index.html
files will be rendered in your output directory by executing your own custom, webpack-compiled render function.
This plugin works particularly well with universal libraries like React and React Router since it allows you to prerender your routes at build time, rather than requiring a Node server in production.
Install
$ npm install --save-dev static-site-generator-webpack-plugin
Usage
webpack.config.js
var StaticSiteGeneratorPlugin = ; var paths = '/hello/' '/world/'; moduleexports = entry: 'main': './index.js' output: filename: 'index.js' path: 'dist' /* IMPORTANT! * You must compile to UMD or CommonJS * so it can be required in a Node context: */ libraryTarget: 'umd' plugins: 'main' paths locals... ;
index.js
// Client render (optional):if typeof document !== 'undefined' // Client render code goes here... // Exported static site renderer:module { ;};
Default locals
// The path currently being rendered:localspath; // An object containing all assets:localsassets; // Advanced: Webpack's stats object:localswebpackStats;
Any additional locals provided in your config are also available.
React Router example
The following example uses React Router v1.0.0-rc1 with history.
;;;;; ;; // Client render (optional):if typeof document !== 'undefined' const history = ; const outlet = document; ReactDOM; // Exported static site renderer: { const history = ; const location = history; ;};