Socket
Book a DemoInstallSign in
Socket

html-renderer-webpack-plugin

Package Overview
Dependencies
Maintainers
1
Versions
88
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

html-renderer-webpack-plugin

A webpack plugin for rendering static html pages

6.1.1
latest
Source
npmnpm
Version published
Weekly downloads
88
8700%
Maintainers
1
Weekly downloads
 
Created
Source

HtmlRendererWebpackPlugin

GitHub Actions version code size dependencies devDependencies

A webpack plugin for emitting html files from a list of paths.

👉 See Real Example Here 👈

Installation

npm install --save-dev html-renderer-webpack-plugin
yarn add --dev html-renderer-webpack-plugin

Usage

This plugin provides a server-like environment for rendering static (React) html pages. It is useful for serverless environments as a static site generator.

Pages are rendered from the supplied paths: string[] | () => Promise<string[]> argument, that should include your supported (static) routes. It might be useful to import these from your router configuration. The paths options can either be a list of strings, or an async function. The latter is useful for dynamically generating paths during built-time, for example from a directory structure.

The plugin is supplied an async renderer function that, for example, renders your pages using react-dom's renderToString and returns a complete HTML string. The default renderer function simply returns a page with Webpack's generated bundles and an empty <div id="root> tag.

Webpack plugin

import HtmlRendererWebpackPlugin from 'html-renderer-webpack-plugin';

...

config.output.publicPath = '/';

...
config.plugins.push(new HtmlRendererWebpackPlugin({
  options: {
    isProduction: process.env.NODE_ENV === 'production'
  },
  paths: [
    '/', // --> index.html
    '/about', // --> about.html
    '/portfolio/' // --> portfolio/index.html
  ],
  renderer: './src/renderer.tsx'
}));
Optiontypedescription
optionsanyPass any values from the webpack config to the renderer function
pathsstring[] | () => Promise<string[]>The paths to be renderer, like "/" and "/about"
rendererstring | RendererA file path to the renderer function, or an inline function. See below for more options.

Renderer

export default async ({ path }) => `<html>
  <head>
    <title>Page for ${path}</title>
  </head>
  <body>
    <h1>Hello, world!</h1>
  </body>
</html>`

Optiontypedescription
assets`Record<string, string[]undefined>`
compilationAssetsimport("webpack").Compilation["assets"]the current compilation's assets
filename stringThe filename for the current html file, like "index.html" or "about.html"
optionsanyAny value passed from the webpack config
pathstringThe current path, like "/" or "/about"
publicPathstringThe webpack public path prefix, like "" or "/public"
statsanyThe current compilation's JSON stats

Babel

Because your renderer function typically imports your <App />, you probably need babel. The easiest way is to run your webpack config through babel with webpack --config webpack.config.babel.js.

Hot-reloading

The renderer option can point to a file (string) that will be required before each compilation. This plugin will automatically delete all changed files from the require.cache, meaning the emitted html files should always be up-to-date even during watch mode.

Keywords

babel

FAQs

Package last updated on 05 Jul 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.