Server-side template rendering using Handlebars.
npm install handlebars-webpack-plugin --save-dev
Usage
In your webpack config register and setup the handlebars plugin
const path = ;const HandlebarsPlugin = ; const webpackConfig = plugins: // path to hbs entry file(s). Also supports nested directories if write path.join(process.cwd(), "app", "src", "**", "*.hbs"), 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 // you can also add a [path] variable, which will emit the files with their relative path, like // output: path.join(process.cwd(), "build", [path], "[name].html"), // 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 // getTargetFilepath: function (filepath, outputTemplate) {}, // getPartialId: function (filePath) {} {} {} {} {} {} {} ;
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>
Options
target filepaths
Per default, the generated filepath of the html-results is defined by the output
-property in the plugin-options. To changed the output folder and name, you can pass your custom filepath-helper to the plugin-options like
/** * Modify the default output path of each entry-template * @param * @param * @param * @return */ { const fileName = path; return outputTemplate; };
You can find the default implementation in utils/getTargetFilepath.
partial ids
Per default, partials are identified with folder/filename
in a hbs-template. e.g. a file in app/partials/page/header.hbs
will be registered under page/header
and can be included with
{{> page/header title="page title"}}
To change the partial's id you can pass a custom partial-generator to the plugin-options like
/** * Modify the hbs partial-id created for a loaded partial * @param * @return */ { return filePath; }
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" HtmlWebpackPlugin // optionally: pass in HtmlWebpackPlugin if it cannot be resolved entry: path output: path partials: path path
Utilities
Merging input-data
In case you have several json-files that need to be passed to handlebars-compilation, you can build this within your webpack-configuration file. A simple helper can be found in utils/mergeJSON.js,
which finds all json files and build a dataObject with { <filename>: <data> }
. Example:
const mergeJSON = ;const projectData = ;// ... // ... data: projectData;
For custom merge behaviour you can add your own merge-helper, following the implementation from utils/mergeJSON.js.