6.1.1 • Published 4 years ago

html-renderer-webpack-plugin v6.1.1

Weekly downloads
34
License
MIT
Repository
github
Last release
4 years ago

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
assetsRecord<string, string[] | undefined>List of emitted asset filenames grouped by extensions
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.

6.1.0

4 years ago

6.1.1

4 years ago

6.0.0

4 years ago

5.3.0

5 years ago

5.2.0

5 years ago

5.1.3

5 years ago

5.1.2

5 years ago

5.1.1

6 years ago

5.1.0

6 years ago

5.0.3

6 years ago

5.0.2

6 years ago

5.0.1

6 years ago

5.0.0

6 years ago

4.0.24

6 years ago

4.0.23

6 years ago

4.0.22

6 years ago

4.0.21

6 years ago

4.0.20

6 years ago

4.0.19

6 years ago

4.0.18

6 years ago

4.0.17

6 years ago

4.0.16

6 years ago

4.0.15

6 years ago

4.0.14

6 years ago

4.0.13

6 years ago

4.0.12

6 years ago

4.0.11

6 years ago

4.0.10

6 years ago

4.0.9

6 years ago

4.0.8

6 years ago

4.0.7

6 years ago

4.0.6

6 years ago

4.0.5

6 years ago

4.0.4

6 years ago

4.0.3

6 years ago

4.0.2

6 years ago

4.0.1

6 years ago

4.0.0

6 years ago

3.1.12

6 years ago

3.1.11

6 years ago

3.1.10

6 years ago

3.1.9

6 years ago

3.1.8

6 years ago

3.1.7

6 years ago

3.1.6

6 years ago

3.1.5

6 years ago

3.1.4

6 years ago

3.1.3

6 years ago

3.1.2

6 years ago

3.1.1

6 years ago

3.1.0

6 years ago

3.0.11

7 years ago

3.0.10

7 years ago

3.0.9

7 years ago

3.0.8

7 years ago

3.0.7

7 years ago

3.0.6

7 years ago

3.0.5

7 years ago

3.0.4

7 years ago

3.0.3

7 years ago

3.0.2

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.0.0

7 years ago

1.3.3

7 years ago

1.3.2

7 years ago

1.3.1

7 years ago

1.3.0

7 years ago

1.2.0

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.1.2

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago