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

typescript-react-router-static-html-webpack-plugin

v0.1.8

Published

Generates a series of static routes for a website from a React Router route configuration, supports Typescript

Downloads

25

Readme

Typescript and React Router static HTML Webpack plugin

npm

This plugin will generate static html from your React Router configuration to allow for only static HTML sites, or universal/isomorphic sites. It supports Typescript or ES2015+ javascript through the Typescript compiler.

Credit given to the static-render-webpack-plugin for the starting point for this plugin.

Getting Started

First you'll want to be sure that you've setup webpack and webpack-dev-server

$ npm install webpack webpack-dev-server --save-dev 

Then be sure you've installed this webpack plugin (sorry about the horrible name)

$ npm install typescript-react-router-static-html-webpack-plugin --save-dev

Usage

src/Routing.tsx

import ...

const Routing = (
  <Route path='/' component={Root}>
    <IndexRoute component={Home} />
    <Route path='four-oh-four' component={FourOhFour} />
    
    <Route path='about' component={About}>
        <Route path'team' component={AboutTeam} />
    </Route>
    
    <Route path='contact' component={FourOhFour} />
    
    <Route path='*' component={FourOhFour} />
  </Route>
);

export default Routing;

src/index.tsx

import * as React from 'react';
import * as ReactDOMServer from 'react-dom/server';
import { RouterContext, match, createRoutes } from 'react-router';
import Logger from './util/Logger'; //bootstrapping for Winston (optional)
import Routing from './Routing';

interface GenerateStaticHTML {
  (html: string): string;
}

module.exports = (path: string, props: {}, callback: GenerateStaticHTML) => {
  let html = '';
  const routes = createRoutes(Routing);

  try {
    match({
      routes,
      location: path,
    }, (err, redirect, renderProps) => {
      if (err) {
        throw Error(`${err.stack}`);
      } else if (redirect) {
        //Logger.info(`Redirecting: ${redirect}`);
      } else if (renderProps) {
        html = ReactDOMServer.renderToStaticMarkup(
          <RouterContext {...renderProps} />
        );
      } else {
        throw Error(`${path} Not Found`);
      }
    });

    callback(html);
  } catch (e) {
    Logger.error(`${e.stack}`);
  }
};

webpack.config.js

const StaticHTMLPlugin = require('typescript-react-router-static-html-webpack-plugin');

module.exports = {
  target: 'node',
  entry: 'src/index.tsx',
  resolve: {
    extensions: [ '', '.js', '.ts', '.tsx', '.scss', '.css' ],
  },

  output: {
    filename: 'bundle.js', //the output js file
    path: root('dist'), //build directory (uses a root function to find the abs path)
    /**
     * This is really important, the plugin expects the
     * bundle output to export a function
     */
    libraryTarget: 'umd',
  },

  module: {
    loaders: [...],
  },

  plugins: [
    ...,
    new ReactRouterStaticHTMLPlugin('bundle.js', root('src/Routing.tsx')),
  ],
};

After you've configured your webpack.config.js and passed in your Routes file, you'll then want to run webpack to generate the static HTML files. You can do this one of two ways:

$ webpack --progress --display-error-details               #outputs the HTML to your `output.path` 
$ webpack-dev-server --progress --display-error-details    #runs a 'hot' version through node 

For more information on customizing your webpack-dev-server, see the Webpack docs.

The above commands should output the following HTML files:

/index.html
/about/index.html
/about/team/index.html
/contact/index.html
/four-oh-four.html

This plugin takes the output of webpack's compilation process from the bundle.js (or whatever you name it) and expects it to export a function with 3 arguments

module.exports = function(path, props, callback) {
  // Callback with the desired HTML string
  callback(...)
}

Advanced

StaticSiteGeneratorPlugin is a constructor that takes up to 5 arguments. Only the first two are required.

new StaticSiteGeneratorPlugin(bundlePath, reactRoutesPath, ignoreExtensions, props, watchFiles)

bundlePath: string

The path to the webpack compiled javascript file, output.filename.

reactRoutesPath: string

The path to the Routes file that contains all your React Routes.

ignoreExtensions?: string[]

An array of extensions to be excluded from being processed and compiling within Node/Typescript.

['.png', '.jpg', '.jpeg', '.gif', '.css', '.scss']

props?: any

This property is passed to your javascript bundle as the 2nd parameter in the exported function. It can be anything.

If props is a function, the function is executed (with no parameters) every time a file needs to be rendered. This way, if you have static assets you want webpack to watch (markdown files, for instance), you can load them in with a function instead, and each time webpack compiles, the props will be different.

watchFiles?: string[]

You can define an array of paths to files that you want the compiler to add to its dependencies. This way, when running webpack in watch mode, or webpack-dev-server, the files which are not in the javascript dependency tree will also be watched and can cause recompilation.

Using Static Render with React Hot Loader

Yes! This is possible. Look at this boilerplate project to see how: static-render-react-hot