Kdu Template Compiler
raw JSON → 2.7.16 verified Fri May 01 auth: no javascript maintenance
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.
Common errors
error Cannot find module 'kdu-template-compiler' ↓
cause Package not installed or incorrectly required in ESM context.
fix
Run
npm install kdu-template-compiler and use const compiler = require('kdu-template-compiler'). error TypeError: compiler.compile is not a function ↓
cause Importing default export instead of named exports.
fix
Use
const { compile } = require('kdu-template-compiler') or const compiler = require('kdu-template-compiler') then compiler.compile(). error SyntaxError: With statement cannot be used in strict mode ↓
cause Using compiled render function code in an ES module or 'use strict' context.
fix
Pre-compile templates with Kdu SFC loader (e.g., kdu-loader) or use the runtime-only build of Kdu.
Warnings
deprecated Option `preserveWhitespace` is deprecated and will be removed in future versions. Use `whitespace` instead. ↓
fix Replace `preserveWhitespace` with `whitespace: 'preserve'` or `whitespace: 'condense'`.
gotcha Compiled render function uses `with` statement, which is forbidden in strict mode code and may be disabled in some environments (e.g. ES modules). ↓
fix Use the runtime-only build of Kdu with pre-compiled render functions, or use `kdu-loader` which handles compilation.
gotcha Do not use ESM `import`; this package only provides CommonJS exports. Using `import` will fail. ↓
fix Use `require()` or ensure your bundler handles CJS packages.
breaking 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. ↓
fix Use @kdujs/compiler-sfc for Kdu 3 template compilation.
Install
npm install kdu-template-compiler yarn add kdu-template-compiler pnpm add kdu-template-compiler Imports
- compile wrong
import { compile } from 'kdu-template-compiler'correctconst { compile } = require('kdu-template-compiler') - compileToFunctions wrong
import compileToFunctions from 'kdu-template-compiler'correctconst { compileToFunctions } = require('kdu-template-compiler') - compiler wrong
import compiler from 'kdu-template-compiler'correctconst compiler = require('kdu-template-compiler')
Quickstart
const compiler = require('kdu-template-compiler');
const template = '<div>{{ message }}</div>';
const result = compiler.compile(template);
console.log(result.render);
// 'with(this){return _c("div",[_v(_s(message))])}'
const html = '<p k-if="ok">yes</p>';
const compiled = compiler.compile(html);
console.log(compiled.errors); // []
console.log(compiled.render);
const { compileToFunctions } = require('kdu-template-compiler');
const { render, staticRenderFns } = compileToFunctions('<div>{{ msg }}</div>');
// render is a function, can be used in Kdu options
const vm = new Kdu({ render, data: { msg: 'hello' } });