static-react-render-webpack-plugin
Render static sites with React
and Webpack
.
Installation
npm install --save-dev static-react-render-webpack-plugin
Usage
webpack.config.js
const path = ;const StaticReactRenderWebpackPlugin = ; moduleexports = context: path entry: layout: './src/layout/index.jsx' productList: './src/pages/product-list/index.jsx' productPage: './src/pages/product-page/index.jsx' output: path: path publicPath: '/' libraryTarget: 'commonjs' /* required so the plugin can evaluate your components */ target: 'node' /* required so the plugin can evaluate your components on node */ /* ...configure loaders, resolvers, etc... */ plugins: layout: 'layout' pages: 'productList' 'productPage' ;
./src/fetchProducts.js
; ;
./src/layout/index.jsx
- wraps pages with a common header and footer
<html> <head></head> <body>children</body> </html>;
./src/pages/product-list/index.jsx
- creates a single HTML file listing all products
; Component static `products//index.html`; static ; { const products = thisprops; return <div> <h1>Products</h1> <ul> products </ul> </div> ; }
./src/pages/product-page/index.jsx
- creates multiple HTML files describing each individual product
; Component static `products//index.html`; static ; { const product = thisprops; return <div> <h1>productname</h1> <h2>productprice</h2> </div> ; }
Options
layout
The name of the layout chunk.
Required. A
string
.
pages
The names of the page chunks.
Required. An
array
ofstring
s.
getLayoutProps
A function modifying the props passed to the layout component.
Optional. A
function(props, context) : object
where:
props
is anobject
context
is anobject
containing:pageChunk
layoutChunk
compilation
getPageProps
A function modifying the props passed to the page component.
Optional. A
function(props, context) : object
where:
props
is anobject
context
is anobject
containing:pageChunk
layoutChunk
compilation
Change log
0.6.1
- fix: bug where
modifiedProps
weren't being passed through to an array of pages
0.6.0
- add: Support
__dirname
and__filename
.
0.5.0
- add: Passing the page props to the layout component (e.g. so in the layout you can render them as JSON and resume rendering
with them on the client). If you are passing a prop named
props
to the layout you won't be able to access the page props.
0.4.0
- add: automatically add the HTML doctype to HTML documents beginning with
<html
0.3.3
- fix: support non-transpiled modules
0.3.2
- fix: doco
0.3.1
- fix: remove unnecessary log
- fix: report errors
0.3.0
- break: moved
.getPath()
and.getProps()
to the component