Ember CLI HTMLBars is an Ember CLI addon that provides HTMLBars template compilation functionality for Ember.js applications. It serves as the core template processing engine that transforms HTMLBars templates into JavaScript code, enabling features like Handlebars template syntax, custom AST transformations, and template precompilation.
npm install ember-cli-htmlbarsFor testing and component template compilation:
import { hbs } from 'ember-cli-htmlbars';For Broccoli build integration:
const TemplateCompilerPlugin = require('ember-cli-htmlbars').TemplateCompilerPlugin;import { hbs } from 'ember-cli-htmlbars';
import { render } from '@ember/test-helpers';
// In tests - tagged template literal
await render(hbs`<MyComponent @value={{42}} />`);
// With explicit options
const template = hbs('<p>{{this.greeting}}</p>', {
moduleName: 'components/greeting.hbs'
});// In ember-cli-build.js
module.exports = function(defaults) {
let app = new EmberApp(defaults, {
'ember-cli-htmlbars': {
templateCompilerPath: 'path/to/custom-template-compiler.js'
}
});
return app.toTree();
};Ember CLI HTMLBars is built around several key components:
hbs tagged template literalsCore template compilation functionality that transforms HTMLBars template files (.hbs) into JavaScript modules. Handles both standalone template files and colocated component templates.
function hbs(template: string, options?: PrecompileOptions): TemplateFactory;
function hbs(tagged: TemplateStringsArray): TemplateFactory;
interface PrecompileOptions {
moduleName?: string;
parseOptions?: {
srcName?: string;
};
}
class TemplateFactory {
private readonly [Data]: 'template-factory';
}Broccoli plugin for build-time template processing, providing persistent caching and incremental builds. Used internally by Ember CLI and available for custom build pipelines.
class TemplateCompilerPlugin extends BroccoliFilter {
constructor(inputTree: BroccoliTree, options?: TemplateCompilerOptions): TemplateCompilerPlugin;
processString(string: string, relativePath: string): string;
getDestFilePath(relativePath: string): string | undefined;
}Extensible system for registering custom HTMLBars AST transformations. Supports both simple transforms and dependency-aware plugins with caching strategies.
interface ASTPluginWrapper {
name: string;
plugin: ASTPluginBuilder;
dependencyInvalidation?: boolean;
cacheKey?: () => string;
baseDir?: () => string;
parallelBabel?: object;
}Complete Ember CLI addon interface providing template preprocessing, Babel plugin integration, and project configuration. Handles automatic setup and integration with the Ember build pipeline.
interface EmberCLIHTMLBarsAddon {
transpileTree(inputTree: BroccoliTree, htmlbarsOptions?: object): BroccoliTree;
setupPreprocessorRegistry(type: string, registry: PreprocessorRegistry): void;
htmlbarsOptions(): HTMLBarsOptions;
templateCompilerPath(): string;
}interface HTMLBarsOptions {
isHTMLBars: boolean;
EmberENV: object;
templateCompiler: object;
templateCompilerPath: string;
pluginNames: string[];
plugins: {
ast: ASTPlugin[];
};
dependencyInvalidation: boolean;
pluginCacheKey: string[];
}
interface ASTPluginEnvironment {
moduleName: string;
meta: object;
}
interface ASTPlugin {
name: string;
visitor: object;
}
// Broccoli types
interface BroccoliTree {
// Opaque Broccoli tree type
}
interface BroccoliFilter {
// Base class for Broccoli plugins
}
// Ember CLI types
interface PreprocessorRegistry {
// Ember CLI preprocessor registry interface
}
interface TemplateCompilerOptions {
persist?: boolean;
isProduction?: boolean;
templateCompiler?: object;
plugins?: {
ast?: ASTPlugin[];
};
pluginNames?: string[];
dependencyInvalidation?: boolean;
}