OC Generic Template Compiler
raw JSON → 2.2.0 verified Fri May 01 auth: no javascript
oc-generic-template-compiler is a module for creating a generic compiler that orchestrates view, server, and static compilers in the OpenComponents ecosystem. Version 2.2.0 is current, with stable releases following OpenComponents' cadence. It enables processing of templates across multiple compilers, supporting JavaScript and HTML. Key differentiator: acts as a unified interface for composing compilers, reducing integration complexity in OC-based projects. Supports TypeScript via bundled types.
Common errors
error TypeError: Cannot destructure property 'result' of ... as it is undefined ↓
cause One of the compilers returned undefined instead of an object with result/dependencies.
fix
Ensure all compilers return { result: string, dependencies: any[] }.
error Error: Compiler is not a function ↓
cause The compiler parameter is being passed a non-function value.
fix
Check that each compiler property is an object with a compile method.
Warnings
breaking Version 2.x requires all compilers to return { result, dependencies } objects. ↓
fix Ensure custom compilers conform to this interface; previously compilers could return strings.
deprecated Using string-only compilers (without 'dependencies') is deprecated since 2.0.0. ↓
fix Update custom compilers to return objects with 'result' and 'dependencies'.
gotcha Each compiler must be an object with a 'compile' function; passing non-object will throw. ↓
fix Ensure viewCompiler, serverCompiler, and staticsCompiler are objects implementing compile.
Install
npm install oc-generic-template-compiler yarn add oc-generic-template-compiler pnpm add oc-generic-template-compiler Imports
- default (export default) wrong
const ocGenericTemplateCompiler = require('oc-generic-template-compiler')correctimport ocGenericTemplateCompiler from 'oc-generic-template-compiler' - GenericCompiler type
import type { GenericCompiler } from 'oc-generic-template-compiler' - CompilerOptions type wrong
import { CompilerOptions } from 'oc-generic-template-compiler' (value import instead of type)correctimport type { CompilerOptions } from 'oc-generic-template-compiler'
Quickstart
import ocGenericTemplateCompiler from 'oc-generic-template-compiler';
const compiler = ocGenericTemplateCompiler({
viewCompiler: { compile: (source) => ({ result: source, dependencies: [] }) },
serverCompiler: { compile: (source) => ({ result: source, dependencies: [] }) },
staticsCompiler: { compile: (source) => ({ result: source, dependencies: [] }) }
});
const compiled = compiler.compile({
view: '<div>Hello</div>',
server: 'module.exports = () => {}',
statics: []
});
console.log(compiled.view.result); // '<div>Hello</div>'