Server-side template rendering using Handlebars.
npm install handlebars-webpack-plugin --save-dev
Usage
In your webpack config register and setup the handlebars plugin
var path = ;var HandlebarsPlugin = ; var webpackConfig = plugins: // path to hbs entry file(s) entry: path // output path and filename(s). This should lie within the webpacks output-folder // if ommited, the input filepath stripped of its extension will be used output: path // data passed to main hbs template: `main-template(data)` data: // or add it as filepath to rebuild data on change using webpack-dev-server data: path // globbed path to partials, where folder/filename is unique partials: path // register custom helpers. May be either a function or a glob-pattern helpers: nameOfHbsHelper: Functionprototype projectHelpers: path // hooks {} {} {} {} {} {} {} ;
Partial ids are registered by parentFolder/filename
(without file extensions)
Use handlebars in your main and partials like, i.e.
<body> {{> partialFolder/partialName}} {{> header/header title="page title"}} {{> partial/content}}</body>
Html Webpack Plugin
Use the html-webpack-plugin to generate partials, that are dynamically registered to the handlebars-webpack-plugin
- the
HtmlWebpackPlugin
should be placed before the HandlebarsWebpackPlugin - multiple HtmlWebpackPlugins may be used
- per default, the partials get registered to
html/<outputfilename>
, i.e. a filename/dist/partials/head.hbs
will be registered ashtml/head
to handlebars
plugins: title: "Generic Head Title" // the template you want to use template: path // the output file name filename: path inject: "head" htmlWebpackPlugin: enabled: true // register all partials from html-webpack-plugin, defaults to `false` prefix: "html" // where to look for htmlWebpackPlugin output. default is "html" entry: path output: path partials: path path