{"id":25832,"library":"kdu-template-compiler","title":"Kdu Template Compiler","description":"Pre-compiles Kdu 2.0 templates into render functions to avoid runtime compilation overhead and support CSP restrictions. v2.7.16 is the latest stable release. The package is auto-generated from the main Kdu repo. Compiles templates to JavaScript code with `with` statements, not compatible with strict mode. Provides `compile` and `compileToFunctions` methods. Unlike the full build, this allows compile-time optimization and directive transformations. Deprecated `preserveWhitespace` option (use `whitespace` instead). Ships TypeScript types.","status":"maintenance","version":"2.7.16","language":"javascript","source_language":"en","source_url":"https://github.com/kdujs/kdu","tags":["javascript","kdu","compiler","typescript"],"install":[{"cmd":"npm install kdu-template-compiler","lang":"bash","label":"npm"},{"cmd":"yarn add kdu-template-compiler","lang":"bash","label":"yarn"},{"cmd":"pnpm add kdu-template-compiler","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"CJS-only package; ESM import will fail. Use require or bundler that handles CJS.","wrong":"import { compile } from 'kdu-template-compiler'","symbol":"compile","correct":"const { compile } = require('kdu-template-compiler')"},{"note":"Named export, not default. Also accessed via require.","wrong":"import compileToFunctions from 'kdu-template-compiler'","symbol":"compileToFunctions","correct":"const { compileToFunctions } = require('kdu-template-compiler')"},{"note":"Common pattern: import entire module as 'compiler' object.","wrong":"import compiler from 'kdu-template-compiler'","symbol":"compiler","correct":"const compiler = require('kdu-template-compiler')"}],"quickstart":{"code":"const compiler = require('kdu-template-compiler');\nconst template = '<div>{{ message }}</div>';\nconst result = compiler.compile(template);\nconsole.log(result.render);\n// 'with(this){return _c(\"div\",[_v(_s(message))])}'\n\nconst html = '<p k-if=\"ok\">yes</p>';\nconst compiled = compiler.compile(html);\nconsole.log(compiled.errors); // []\nconsole.log(compiled.render);\n\nconst { compileToFunctions } = require('kdu-template-compiler');\nconst { render, staticRenderFns } = compileToFunctions('<div>{{ msg }}</div>');\n// render is a function, can be used in Kdu options\nconst vm = new Kdu({ render, data: { msg: 'hello' } });","lang":"javascript","description":"Shows how to require the package, use compile() to get render function string, and compileToFunctions() to get instantiated functions."},"warnings":[{"fix":"Replace `preserveWhitespace` with `whitespace: 'preserve'` or `whitespace: 'condense'`.","message":"Option `preserveWhitespace` is deprecated and will be removed in future versions. Use `whitespace` instead.","severity":"deprecated","affected_versions":">=2.6.0"},{"fix":"Use the runtime-only build of Kdu with pre-compiled render functions, or use `kdu-loader` which handles compilation.","message":"Compiled render function uses `with` statement, which is forbidden in strict mode code and may be disabled in some environments (e.g. ES modules).","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Use `require()` or ensure your bundler handles CJS packages.","message":"Do not use ESM `import`; this package only provides CommonJS exports. Using `import` will fail.","severity":"gotcha","affected_versions":">=2.0.0"},{"fix":"Use @kdujs/compiler-sfc for Kdu 3 template compilation.","message":"In Kdu 3, the template compiler is completely different and is not compatible with kdu-template-compiler v2. This package only works with Kdu 2.","severity":"breaking","affected_versions":">=3.0.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Run `npm install kdu-template-compiler` and use `const compiler = require('kdu-template-compiler')`.","cause":"Package not installed or incorrectly required in ESM context.","error":"Cannot find module 'kdu-template-compiler'"},{"fix":"Use `const { compile } = require('kdu-template-compiler')` or `const compiler = require('kdu-template-compiler')` then `compiler.compile()`.","cause":"Importing default export instead of named exports.","error":"TypeError: compiler.compile is not a function"},{"fix":"Pre-compile templates with Kdu SFC loader (e.g., kdu-loader) or use the runtime-only build of Kdu.","cause":"Using compiled render function code in an ES module or 'use strict' context.","error":"SyntaxError: With statement cannot be used in strict mode"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}