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-html-webpack-plugin

v1.2.0

Published

Generates static html files

Downloads

4

Readme

Build Status

Static Html Webpack plugin

It's a webpack plugin that aims to make static html generation as simple and transparent as possible.

You may want to get use of it if your are care of:

  • SEO
  • Better performance
  • Decoupling Frontend from Backend by design

Configuration

It's nothing more as just provide a plugin instance and ensure that your output's libraryTarget is equal to umd API. It should be enough to enable plugin to do his work.

// webpack.config.js

var StaticHtml = require("static-html-webapck-plugin");

module.exports = {
  entry: {
    server: __dirname + '/index.js'
  },
  output: {
    path: __dirname + '/public',
    filename: '[name].js',
    libraryTarget: 'umd'
  },
  plugins: [new StaticHtml()]
}

Plugin takes following options:

- htmlFilename: name of html file, that have to be generated (default is "index.html"),
- prependDoctypeHtml: set "<!DOCTYPE html>" before your html (default is true),
- appendHash: append hash to css and script files (default is true)

To manipulate Plugin Options just provide it to Plugin instance like that:

 plugins: [new StaticHtml({appendHash: false})]

You may have noticed, that entry applies an object with a key/value pair server: __dirname + '/index.js' that is where all static generation relevant staff have to be handled. You are free to extend it with ... client: __dirname + '/yourclient.js' in order to have a library that relies on client's objects like window.document that you don't have, when you generate your static html. StaticHtmlWebpackPlugin just doesn't care about anything that, is not relevant for server-side html generation, all it looks for is a entry.server key.

To provide your actual html you have to point to it:

// index.js

module.exports = "
<html>
  <head>
    <title>Static Html</title>
  </head>
  <body>
    <h1>Hi, i'm so static</h1>
  </body>
</html>
"

Html based on React routes.

You can also generate a multiple pages at once by providing routes of react-router, for that case you have to provide an object in stead of plain html. The object have to look like this:

// index.js

import React from 'react';
import {renderToStaticMarkup} from 'react-dom/server';
import {RoutingContext, match} from 'react-router';
import routes from './routes.js';


module.exports = {

  'react-router' : {

    getPaths: function(parser){
      return parser(routes)
    },

    buildHtml: function(path, callback){
       match({ routes, location: path }, (error, redirectLocation, renderProps) => {
         callback(renderToStaticMarkup(<RoutingContext {...renderProps} />))
       })
    }
  }

}

and for a sample routes like this:

// routes.js
import React from 'react'
import { Route, Link } from 'react-router'

const App = React.createClass({
  render() {
    return (
      <div>
        <h1>App</h1>
        <ul>
          <li><Link to="/about">About</Link></li>
        </ul>
        {this.props.children}
      </div>
    )
  }
})

const About = React.createClass({
  render() {
    return <h3>About</h3>
  }
})


module.exports = (
    <Route path="/" component={App}>
      <Route path="about" component={About} />
    </Route>
)

It'a also possible to define Routes as an Array, if you don't want to use JSX.

module.exports = [
  { path: '/',
    component: App,
    childRoutes: [
      { path: 'about', component: About }
    ]
  }
]

you will get 2 html files generated:

public/index.html
public/about/index.html

Extend it.

You can use also write your own generators, just create another addon in node_modules/static-html-webapck-plugin/addons that has the following sceleton:

//youraddon.js
module.exports = {
  getPaths: function(routes){
      let paths = [...];
      ...
      return paths;
  }
}

and reference to it in your entry file:

//index.js
module.exports = {

  'youraddon' : {

    getPaths: function(parser){
      return parser(routes)
    },

    buildHtml: function(path, callback){

         // your logic to generate an html for a given path and provide it as an argument to a callback function

    }
  }

}