{"id":14986777,"url":"https://github.com/sagold/handlebars-webpack-plugin","last_synced_at":"2025-04-15T03:49:43.053Z","repository":{"id":1864104,"uuid":"44902419","full_name":"sagold/handlebars-webpack-plugin","owner":"sagold","description":"Renders your html-template at build time","archived":false,"fork":false,"pushed_at":"2024-12-14T21:32:18.000Z","size":1413,"stargazers_count":161,"open_issues_count":9,"forks_count":45,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-15T03:49:28.679Z","etag":null,"topics":["compile","handlebars","npm-package","server-side","webpack-plugin"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/sagold.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGES.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2015-10-25T08:39:46.000Z","updated_at":"2025-02-18T14:36:53.000Z","dependencies_parsed_at":"2025-03-10T10:44:50.951Z","dependency_job_id":"f7fdf594-3047-4e21-b79e-8e854d5893e6","html_url":"https://github.com/sagold/handlebars-webpack-plugin","commit_stats":{"total_commits":148,"total_committers":19,"mean_commits":"7.7894736842105265","dds":0.25,"last_synced_commit":"45c53cc2e624b42917bca2b699e1a50cc50405ce"},"previous_names":[],"tags_count":29,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagold%2Fhandlebars-webpack-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagold%2Fhandlebars-webpack-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagold%2Fhandlebars-webpack-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/sagold%2Fhandlebars-webpack-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/sagold","download_url":"https://codeload.github.com/sagold/handlebars-webpack-plugin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249003943,"owners_count":21196794,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["compile","handlebars","npm-package","server-side","webpack-plugin"],"created_at":"2024-09-24T14:13:31.405Z","updated_at":"2025-04-15T03:49:43.030Z","avatar_url":"https://github.com/sagold.png","language":"JavaScript","readme":"\u003ch1 align=\"center\"\u003e\u003cimg src=\"./docs/hbs-wp-plugin.png\" width=\"276\" alt=\"handlebars webpack plugin\"\u003e\u003c/h1\u003e\n\n\u003e Server-side template rendering using [Handlebars](http://handlebarsjs.com/).\n\n\n`npm install handlebars-webpack-plugin --save-dev`\n\n\n## Usage\n\nIn your webpack config register and setup the handlebars plugin\n\n```javascript\nconst path = require(\"path\");\nconst HandlebarsPlugin = require(\"handlebars-webpack-plugin\");\n\nconst webpackConfig = {\n\n  plugins: [\n\n    new HandlebarsPlugin({\n      // path to hbs entry file(s). Also supports nested directories if write path.join(process.cwd(), \"app\", \"src\", \"**\", \"*.hbs\"),\n      entry: path.join(process.cwd(), \"app\", \"src\", \"*.hbs\"),\n      // output path and filename(s). This should lie within the webpacks output-folder\n      // if ommited, the input filepath stripped of its extension will be used\n      output: path.join(process.cwd(), \"build\", \"[name].html\"),\n      // you can also add a [path] variable, which will emit the files with their relative path, like\n      // output: path.join(process.cwd(), \"build\", [path], \"[name].html\"),\n      \n      // data passed to main hbs template: `main-template(data)`\n      data: require(\"./app/data/project.json\"),\n      // or add it as filepath to rebuild data on change using webpack-dev-server\n      data: path.join(__dirname, \"app/data/project.json\"),\n\n      // globbed path to partials, where folder/filename is unique\n      partials: [\n        path.join(process.cwd(), \"app\", \"src\", \"components\", \"*\", \"*.hbs\")\n      ],\n\n      // register custom helpers. May be either a function or a glob-pattern\n      helpers: {\n        nameOfHbsHelper: Function.prototype,\n        projectHelpers: path.join(process.cwd(), \"app\", \"helpers\", \"*.helper.js\")\n      },\n\n      // hooks\n      // getTargetFilepath: function (filepath, outputTemplate) {},\n      // getPartialId: function (filePath) {}\n      onBeforeSetup: function (Handlebars) {},\n      onBeforeAddPartials: function (Handlebars, partialsMap) {},\n      onBeforeCompile: function (Handlebars, templateContent) {},\n      onBeforeRender: function (Handlebars, data, filename) {},\n      onBeforeSave: function (Handlebars, resultHtml, filename) {},\n      onDone: function (Handlebars, filename) {}\n    })\n  ]\n};\n```\n\nPartial ids are registered by `parentFolder/filename` (without file extensions)\n\nUse handlebars in your main and partials like, i.e.\n\n```hbs\n\u003cbody\u003e\n  {{\u003e partialFolder/partialName}}\n\n  {{\u003e header/header title=\"page title\"}}\n\n  {{\u003e partial/content}}\n\u003c/body\u003e\n```\n\n\n## Options\n\n### target filepaths\n\nPer 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\n\n```javascript\n{\n    /**\n     * Modify the default output path of each entry-template\n     * @param {String} filepath     - the source of the template\n     * @param {String} outputTemplate - the filepath template defined in `output`\n     * @param {String} rootFolder   - the filepaths rootFolder\n     * @return {String} final path, where the rendered html-file should be saved\n     */\n    getTargetFilepath: function getTargetFilepath(filepath, outputTemplate, rootFolder) {\n        const fileName = path.basename(filepath).replace(path.extname(filepath), \"\");\n        return outputTemplate.replace(\"[name]\", fileName);\n    };\n}\n```\n\nYou can find the default implementation in [utils/getTargetFilepath](./utils/getTargetFilepath.js).\n\n\n### partial ids\n\nPer 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\n\n```hbs\n{{\u003e page/header title=\"page title\"}}\n```\n\nTo change the partial's id you can pass a custom partial-generator to the plugin-options like\n\n```javascript\n{\n    /**\n     * Modify the hbs partial-id created for a loaded partial\n     * @param {String} filePath   - filePath to the loaded partial\n     * @return {String} hbs-partialId, per default folder/partialName is used\n     */\n    getPartialId: function (filePath) {\n        return filePath.match(/\\/([^/]+\\/[^/]+)\\.[^.]+$/).pop();\n    }\n}\n```\n\n\n\n### Html Webpack Plugin\n\n\u003e Use the [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin) to generate partials, that are\n\u003e dynamically registered to the handlebars-webpack-plugin\n\n- the `HtmlWebpackPlugin` should be placed before the HandlebarsWebpackPlugin\n- multiple HtmlWebpackPlugins may be used\n- per default, the partials get registered to `html/\u003coutputfilename\u003e`, i.e. a filename `/dist/partials/head.hbs` will be registered as `html/head` to handlebars\n\n\n```js\nplugins: [\n   new HtmlWebpackPlugin({\n    title: \"Generic Head Title\",\n    // the template you want to use\n    template: path.join(__dirname, \"src\", \"generatedpartial\", \"head.hbs\"),\n    // the output file name\n    filename: path.join(__dirname, \"dist\", \"partials\", \"head.hbs\"),\n    inject: \"head\"\n  }),\n\n  new HandlebarsWebpackPlugin({\n\n    htmlWebpackPlugin: {\n      enabled: true, // register all partials from html-webpack-plugin, defaults to `false`\n      prefix: \"html\", // where to look for htmlWebpackPlugin output. default is \"html\"\n      HtmlWebpackPlugin // optionally: pass in HtmlWebpackPlugin if it cannot be resolved\n    },\n\n    entry: path.join(process.cwd(), \"src\", \"hbs\", \"*.hbs\"),\n    output: path.join(process.cwd(), \"dist\", \"[name].html\"),\n\n    partials: [\n      path.join(process.cwd(), \"html\",/* \u003c-- this should match htmlWebpackPlugin.prefix */ \"*\", \"*.hbs\"),\n      path.join(process.cwd(), \"src\", \"hbs\", \"*\", \"*.hbs\")\n    ]\n  })\n]\n```\n\n\n## Utilities\n\n### Merging input-data\n\nIn 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](./utils/mergeJSON.js), \nwhich finds all json files and build a dataObject with `{ \u003cfilename\u003e: \u003cdata\u003e }`. Example:\n\n```js\nconst mergeJSON = require('handlebars-webpack-plugin/utils/mergeJSON');\nconst projectData = mergeJSON(path.join(__dirname, \"data/**/*.json\"));\n// ...\nnew HandlebarsPlugin({\n    // ...\n    data: projectData\n});\n```\n\nFor custom merge behaviour you can add your own merge-helper, following the implementation from [utils/mergeJSON.js](./utils/mergeJSON.js).\n\n\n## Contributors\n\n\u003ca href=\"https://github.com/TheReincarnator\"\u003e\n    \u003cimg width=\"80\" height=\"80\" style=\"max-width:100%;\"\n        title=\"TheReincarnator\" src=\"https://avatars0.githubusercontent.com/u/840370?s=460\u0026v=4\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://github.com/muuki88\"\u003e\n    \u003cimg width=\"80\" height=\"80\" style=\"max-width:100%;\"\n        title=\"muuki88\" src=\"https://avatars2.githubusercontent.com/u/647727?s=460\u0026v=4\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://github.com/emilchristensen\"\u003e\n    \u003cimg width=\"80\" height=\"80\" style=\"max-width:100%;\"\n        title=\"emilchristensen\" src=\"https://avatars3.githubusercontent.com/u/575486?s=460\u0026v=4\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://github.com/alisonailea\"\u003e\n    \u003cimg width=\"80\" height=\"80\" style=\"max-width:100%;\"\n        title=\"alisonailea\" src=\"https://avatars2.githubusercontent.com/u/3362490?s=460\u0026v=4\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://github.com/vredondoGL\"\u003e\n    \u003cimg width=\"80\" height=\"80\" style=\"max-width:100%;\"\n        title=\"vredondoGL\" src=\"https://avatars3.githubusercontent.com/u/35344609?s=460\u0026v=4\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://github.com/mkungla\"\u003e\n    \u003cimg width=\"80\" height=\"80\" style=\"max-width:100%;\"\n        title=\"mkungla\" src=\"https://avatars2.githubusercontent.com/u/15878458?s=460\u0026v=4\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://github.com/bywo\"\u003e\n    \u003cimg width=\"80\" height=\"80\" style=\"max-width:100%;\"\n        title=\"bywo\" src=\"https://avatars2.githubusercontent.com/u/1434481?s=460\u0026v=4\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://github.com/baldurh\"\u003e\n    \u003cimg width=\"80\" height=\"80\" style=\"max-width:100%;\"\n        title=\"baldurh\" src=\"https://avatars3.githubusercontent.com/u/1823617?s=460\u0026v=4\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://github.com/DannyDelott\"\u003e\n    \u003cimg width=\"80\" height=\"80\" style=\"max-width:100%;\"\n        title=\"DannyDelott\" src=\"https://avatars3.githubusercontent.com/u/4524175?s=460\u0026v=4\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://github.com/amandabouveng\"\u003e\n    \u003cimg width=\"80\" height=\"80\" style=\"max-width:100%;\"\n        title=\"amandabouveng\" src=\"https://avatars2.githubusercontent.com/u/15197360?s=460\u0026v=4\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://github.com/patrikniebur\"\u003e\n    \u003cimg width=\"80\" height=\"80\" style=\"max-width:100%;\"\n        title=\"patrikniebur\" src=\"https://avatars0.githubusercontent.com/u/6452693?s=460\u0026v=4\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://github.com/mitchheddles\"\u003e\n    \u003cimg width=\"80\" height=\"80\" style=\"max-width:100%;\"\n        title=\"mitchheddles\" src=\"https://avatars2.githubusercontent.com/u/20656128?s=460\u0026v=4\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://github.com/maratfakhreev\"\u003e\n    \u003cimg width=\"80\" height=\"80\" style=\"max-width:100%;\"\n        title=\"maratfakhreev\" src=\"https://avatars0.githubusercontent.com/u/1300497?s=400\u0026v=4\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://github.com/DKvistgaard\"\u003e\n    \u003cimg width=\"80\" height=\"80\" style=\"max-width:100%;\"\n        title=\"DKvistgaard\" src=\"https://avatars1.githubusercontent.com/u/1705203?s=460\u0026v=4\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://github.com/raypatterson\"\u003e\n    \u003cimg width=\"80\" height=\"80\" style=\"max-width:100%;\"\n        title=\"raypatterson\" src=\"https://avatars1.githubusercontent.com/u/1051626?s=460\u0026v=4\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://github.com/queenvictoria\"\u003e\n    \u003cimg width=\"80\" height=\"80\" style=\"max-width:100%;\"\n        title=\"queenvictoria\" src=\"https://avatars1.githubusercontent.com/u/694770?s=460\u0026v=4\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://github.com/abachi\"\u003e\n    \u003cimg width=\"80\" height=\"80\" style=\"max-width:100%;\"\n        title=\"abachi\" src=\"https://avatars1.githubusercontent.com/u/12300606?s=460\u0026v=4\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://github.com/rustyy\"\u003e\n    \u003cimg width=\"80\" height=\"80\" style=\"max-width:100%;\"\n        title=\"rustyy\" src=\"https://avatars2.githubusercontent.com/u/1225568?s=460\u0026v=4\"\u003e\n\u003c/a\u003e\n\n\u003ca href=\"https://github.com/JeremyTCD\"\u003e\n    \u003cimg width=\"80\" height=\"80\" style=\"max-width:100%;\"\n        title=\"JeremyTCD\" src=\"https://avatars0.githubusercontent.com/u/11733898?s=400\u0026u=3494b06385965675883fa2f1efe03fc4eb8aa5bb\u0026v=4\"\u003e\n\u003c/a\u003e\n\n\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsagold%2Fhandlebars-webpack-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsagold%2Fhandlebars-webpack-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsagold%2Fhandlebars-webpack-plugin/lists"}