npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

static-react-render-webpack-plugin

v0.6.1

Published

Render static sites with `React` and `Webpack`.

Downloads

18

Readme

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 = require('path');
const StaticReactRenderWebpackPlugin = require('static-react-render-webpack-plugin');

module.exports = {
  
  context: path.join(__dirname, 'src'),

  entry: {
    layout: './src/layout/index.jsx',
    productList: './src/pages/product-list/index.jsx',
    productPage: './src/pages/product-page/index.jsx'
  },
  
  output: {
    path: path.join(__dirname, 'dest'),
    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: [
    new StaticReactRenderWebpackPlugin({
      layout: 'layout',
      pages: [
        'productList',
        'productPage'
      ]
    })
  ]
  
};

./src/fetchProducts.js

import fetch from 'node-fetch';

export default () => fetch('http://example.com/products.json')
  .then(response => response.json())
;

./src/layout/index.jsx - wraps pages with a common header and footer

export default ({children}) => (
  <html>
    <head></head>
    <body>{children}</body>
  </html>
);

./src/pages/product-list/index.jsx - creates a single HTML file listing all products

import fetchProducts from '../../fetchProducts';

export default class ProductList extends React.Component {
  
  static getPath = ({product}) => `products/${product.slug}/index.html`;
  
  static getProps = () => fetchProducts()
    .then(products => ({products}))
  ;

  render() {
    const {products} = this.props;
    return (
      <div>
        <h1>Products</h1>
        <ul>
          {products.map(product => (
            <li>
              <a href={`products/${product.slug}/`}>{product.name}</a>
            </li>
          ))}
        </ul>
      </div>
    );
  }
    
}

./src/pages/product-page/index.jsx - creates multiple HTML files describing each individual product

import fetchProducts from '../../fetchProducts';

export default class ProductPage extends React.Component {
  
  static getPath = ({product}) => `products/${product.slug}/index.html`;
  
  static getProps = () => fetchProducts()
    .then(products => products.map(product => ({product})))
  ;

  render() {
    const {product} = this.props;
    return (
      <div>
        <h1>{product.name}</h1>
        <h2>{product.price}</h2>
      </div>  
    );
  }
    
}

Options

layout

The name of the layout chunk.

Required. A string.

pages

The names of the page chunks.

Required. An array of strings.

getLayoutProps

A function modifying the props passed to the layout component.

Optional. A function(props, context) : object where:

  • props is an object
  • context is an object containing:
    • pageChunk
    • layoutChunk
    • compilation
getPageProps

A function modifying the props passed to the page component.

Optional. A function(props, context) : object where:

  • props is an object
  • context is an object 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