{"id":22049,"library":"rollup-plugin-handlebars-precompiler","title":"rollup-plugin-handlebars-precompiler","description":"Rollup plugin that precompiles Handlebars templates into ES modules. Version 1.0.2 requires Node >=18 and Handlebars as a peer dependency. Generates ESM-only modules with TypeScript support, automatic partial detection, source maps, and convenient DOM integration via exported RawTemplate and Template functions. Unlike other Handlebars rollup plugins, it outputs DocumentFragment-ready modules with type definitions.","status":"active","version":"1.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/mbland/rollup-plugin-handlebars-precompiler","tags":["javascript","rollup","handlebars","JavaScript","typescript"],"install":[{"cmd":"npm install rollup-plugin-handlebars-precompiler","lang":"bash","label":"npm"},{"cmd":"yarn add rollup-plugin-handlebars-precompiler","lang":"bash","label":"yarn"},{"cmd":"pnpm add rollup-plugin-handlebars-precompiler","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"The Handlebars runtime is bundled into generated modules; users must install it separately.","package":"handlebars","optional":false}],"imports":[{"note":"Package is ESM-only; CommonJS require will fail. Use default import only.","wrong":"const handlebarsPrecompiler = require('rollup-plugin-handlebars-precompiler')","symbol":"rollup-plugin-handlebars-precompiler","correct":"import handlebarsPrecompiler from 'rollup-plugin-handlebars-precompiler'"},{"note":"Default export is the Template function; RawTemplate is a named export.","wrong":"import { Template } from './template.hbs'","symbol":"Template","correct":"import Template, { RawTemplate } from './template.hbs'"},{"note":"RawTemplate is not the default export; must use named import syntax.","wrong":"import RawTemplate from './template.hbs'","symbol":"RawTemplate","correct":"import { RawTemplate } from './template.hbs'"}],"quickstart":{"code":"// rollup.config.js\nimport handlebarsPrecompiler from 'rollup-plugin-handlebars-precompiler'\n\nexport default {\n  input: 'src/main.js',\n  output: { dir: 'dist', format: 'esm' },\n  plugins: [\n    handlebarsPrecompiler({\n      // Include handlebars helpers from a specific file\n      helpers: 'src/helpers/**/*.js',\n      // enable source maps\n      sourceMap: true\n    })\n  ]\n}\n\n// src/main.js\nimport Template from './component.hbs'\n\nconst app = document.getElementById('app')\nconst context = { message: 'Hello' }\napp.appendChild(Template(context))","lang":"javascript","description":"Shows rollup config with plugin setup and usage of imported Handlebars template to append a DocumentFragment to the DOM."},"warnings":[{"fix":"Use Node 18+ and import via ESM syntax only.","message":"Package requires Node >=18 and ESM-only — no CommonJS support.","severity":"breaking","affected_versions":">=1.0.0"},{"fix":"Run 'npm install handlebars' or 'pnpm add handlebars' in your project.","message":"Handlebars must be installed as a direct dependency of your project, not just this plugin.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Make sure your bundler resolves the runtime; it is included automatically if rollup processes node_modules.","message":"Generated modules use the Handlebars runtime from 'node_modules/handlebars/lib/handlebars.runtime.js' — ensure it is bundled.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Validate template compilation with the plugin's specific configuration for partials and helpers.","message":"The plugin may not handle all Handlebars features; test partials and helpers thoroughly.","severity":"deprecated","affected_versions":">=1.0.0"}],"env_vars":null,"last_verified":"2026-04-27T00:00:00.000Z","next_check":"2026-07-26T00:00:00.000Z","problems":[{"fix":"Change require() to import and ensure rollup.config.js uses ESM (e.g., rename to .mjs or set type: module in package.json).","cause":"Attempting to use CommonJS require to load the ESM-only plugin.","error":"Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/node_modules/rollup-plugin-handlebars-precompiler/dist/index.js from /path/to/rollup.config.js not supported."},{"fix":"Run 'npm install handlebars' or 'pnpm add handlebars' in your project.","cause":"Handlebars is not installed as a project dependency.","error":"Error: Cannot find module 'handlebars'"},{"fix":"Add a global type declaration: declare module '*.hbs' { import type { TemplateFunction } from 'rollup-plugin-handlebars-precompiler'; const tmpl: TemplateFunction; export default tmpl; export const RawTemplate: (context?: any, options?: any) => string; }","cause":"TypeScript does not recognize .hbs imports; missing module declaration.","error":"Cannot find module './template.hbs' or its corresponding type declarations."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}