
Security News
Crates.io Users Targeted by Phishing Emails
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
html-renderer-webpack-plugin
Advanced tools
A webpack plugin for rendering static html pages.
npm install --save-dev html-renderer-webpack-plugin
yarn add --dev html-renderer-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'
}));
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 a supplied paths: string[]
array that should include your supported (static) routes. It might be useful to import these from your router configuration.
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 javascript bundles and an empty <div id="root>
tag.
The renderer function should be of type:
type RendererArgs = {
assets?: {
[key: string]: string[];
};
compilationAssets?: {
[key: string]: import("webpack-sources").CachedSource;
};
filename?: string;
options?: Record<string, any>;
path?: string;
publicPath?: string;
stats?: ReturnType<import("webpack").Stats["toJson"]>;
};
export declare type Renderer = (
args: Partial<RendererArgs>
) => string | Promise<string>;
where
An object with all of webpack's compiled asset filenames, seperated by their file extensions into arrays.
The raw contents of webpack's compilation.assets.
A string of the current path's filename.
An object containing anything, passed from the webpack configuration to the renderer function. Useful for variables declared during build-time.
A string of the current path. This is useful for routing.
The public path prefix as set in webpack's config.options.publicPath
.
The webpack's stats.toJson()
object. This is useful for webpack-flush-chunk.
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
.
Since version v5.0.0
the HTMLRendererWebpackPlugin
accepts the renderer
option as a path to the renderer function (string
), and will then dynamically import the function before each renderer. This makes the renderer result be always up-to-date with Hot Reloading.
For older versions:
renderer
function, require your main React component instead of importing:
const App = require('src/components/App').default
require.cache
will be invalidated and using require
will result in updated code.A typical feature of a dev environment includes some hot module replacement. When using html-renderer-webpack-plugin
, you might want to ensure that when the client bundle gets hot-updated, also the HTML files are rendered with the content.
By default, when using import
to require you application code, for example import App from 'src/components/App
, the resulting module will be cached in the node process. Thus, after recompiling your html files after a webpack HMR update, the html file will still contain the old version, because it is cached in the require.cache
.
To overcome this limitation, this plugin hooks into Webpack's watchRun
hook, that runs in watch mode when files change. It will then invalidate every require used in changed files from the require.cache
. Thus, if you use const App = require('src/components/App').default
inside your renderer function, it will be freshly required the next time the HTML file is created. This will result in "hot-reloading" working properly for statically rendered content.
You can disable this behaviour by supplying the hot: false
option in the plugin constructor.
FAQs
A webpack plugin for rendering static html pages
The npm package html-renderer-webpack-plugin receives a total of 1 weekly downloads. As such, html-renderer-webpack-plugin popularity was classified as not popular.
We found that html-renderer-webpack-plugin demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
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.
Security News
The Rust Security Response WG is warning of phishing emails from rustfoundation.dev targeting crates.io users.
Product
Socket now lets you customize pull request alert headers, helping security teams share clear guidance right in PRs to speed reviews and reduce back-and-forth.
Product
Socket's Rust support is moving to Beta: all users can scan Cargo projects and generate SBOMs, including Cargo.toml-only crates, with Rust-aware supply chain checks.