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.

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.
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.
npm install oc-generic-template-compiler
yarn add oc-generic-template-compiler
pnpm add oc-generic-template-compiler

Creates a generic compiler with mock view, server, and statics compilers and compiles a simple template.

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>'