Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

ember-cli-htmlbars

ember-cli2.2mMIT6.3.0TypeScript support: included

A library for adding htmlbars to ember CLI

ember-addon, ember-cli

readme

Ember CLI HTMLBars

Build Status

Compatibility

  • Ember.js v3.8 or above
  • Ember CLI v3.8 or above
  • Node.js v12 or above

Tagged Template Usage / Migrating from htmlbars-inline-precompile

Starting with version 4.0, this addon now includes the testing helper from ember-cli-htmlbars-inline-precompile

This will require an update to the imports of the hbs helper in your tests:

Prior syntax:

import hbs from 'htmlbars-inline-precompile';

...

await render(hbs`
  <MyComponent />
`);

New syntax:

import { hbs } from 'ember-cli-htmlbars';

...

await render(hbs`
  <MyComponent />
`);

There is a codemod available to automate this change.

Additional Trees

For addons which want additional customizations, they are able to interact with this addon directly.

interface EmberCLIHTMLBars {
  /**
    Supports easier transpilation of non-standard input paths (e.g. to transpile
    a non-addon NPM dependency) while still leveraging the logic within
    ember-cli-htmlbars for transpiling (e.g. custom AST transforms, colocation, etc).
  */
  transpileTree(inputTree: BroccoliTree): BroccoliTree;
}

transpileTree usage

// find the ember-cli-htmlbars addon
let htmlbarsAddon = this.addons.find(addon => addon.name === 'ember-cli-htmlbars');

// invoke .transpileTree passing in the custom input tree
let transpiledCustomTree = htmlbarsAddon.transpileTree(someCustomTree);

Adding Custom Plugins

You can add custom plugins to be used during transpilation of the addon/ or addon-test-support/ trees of your addon (or the app/ and tests/ trees of an application) by registering a custom AST transform.

var SomeTransform = require('./some-path/transform');

module.exports = {
  name: 'my-addon-name',

  included: function() {
    // we have to wrap these in an object so the ember-cli
    // registry doesn't try to call `new` on them (new is actually
    // called within htmlbars when compiling a given template).
    this.app.registry.add('htmlbars-ast-plugin', {
      name: 'some-transform',
      plugin: SomeTransform
    });

    this._super.included.apply(this, arguments);
  }
};

Options for registering a plugin

  • name - String. The name of the AST transform for debugging purposes.
  • plugin - A function of type ASTPluginBuilder.
  • dependencyInvalidation - Boolean. A flag that indicates the AST Plugin may, on a per-template basis, depend on other files that affect its output.
  • cacheKey - function that returns any JSON-compatible value - The value returned is used to invalidate the persistent cache across restarts, usually in the case of a dependency or configuration change.
  • baseDir - () => string. A function that returns the directory on disk of the npm module for the plugin. If provided, a basic cache invalidation is performed if any of the dependencies change (e.g. due to a npm install/upgrade).

Implementing Dependency Invalidation in an AST Plugin

Plugins that set the dependencyInvalidation option to true can provide function for the plugin of type ASTDependencyPlugin as given below.

Note: the plugin function is invoked without a value for this in context.

import {ASTPluginBuilder, ASTPlugin} from "@glimmer/syntax/dist/types/lib/parser/tokenizer-event-handlers";

export type ASTDependencyPlugin = ASTPluginWithDepsBuilder | ASTPluginBuilderWithDeps;

export interface ASTPluginWithDepsBuilder {
  (env: ASTPluginEnvironment): ASTPluginWithDeps;
}

export interface ASTPluginBuilderWithDeps extends ASTPluginBuilder {
  /**
   * @see {ASTPluginWithDeps.dependencies} below.
   **/
  dependencies(relativePath): string[];
}

export interface ASTPluginWithDeps extends ASTPlugin {
  /**
   * If this method exists, it is called with the relative path to the current
   * file just before processing starts. Use this method to reset the
   * dependency tracking state associated with the file.
   */
  resetDependencies?(relativePath: string): void;
  /**
   * This method is called just as the template finishes being processed.
   *
   * @param relativePath {string} A relative path to the file that may have dependencies.
   * @return {string[]} paths to files that are a dependency for the given
   * file. Any relative paths returned by this method are taken to be relative
   * to the file that was processed.
   */
  dependencies(relativePath: string): string[];
}

Precompile HTMLBars template strings within other addons

module.exports = {
  name: 'my-addon-name',

  setupPreprocessorRegistry: function(type, registry) {
    var htmlbarsPlugin = registry.load('template').find(function(plugin) {
      return plugin.name === 'ember-cli-htmlbars';
    });

    // precompile any htmlbars template string via the precompile method on the
    // ember-cli-htmlbars plugin wrapper; `precompiled` will be a string of the
    // form:
    //
    //   Ember.HTMLBars.template(function() {...})
    //
    var precompiled = htmlbarsPlugin.precompile("{{my-component}}");
  }
};

Custom Template Compiler

You can still provide a custom path to the template compiler (e.g. to test custom template compiler tweaks in an application) by:

// ember-cli-build.js

module.exports = function(defaults) {
  let app = new EmberApp(defaults, {
    'ember-cli-htmlbars': {
      templateCompilerPath: `some_path/to/ember-template-compiler.js`,
    }
  });
};

Using as a Broccoli Plugin

var HtmlbarsCompiler = require('ember-cli-htmlbars');

var templateTree = new HtmlbarsCompiler('app/templates', {
  isHTMLBars: true,

  // provide the templateCompiler that is paired with your Ember version
  templateCompiler: require('./bower_components/ember/ember-template-compiler')
});

changelog

Changelog

v6.3.0 (2023-08-08)

:rocket: Enhancement

  • #773 Adjust error message for re-exported class when it has a co-located template (@robinborst95)

:bug: Bug Fix

Committers: 2

v6.2.0 (2023-01-17)

:rocket: Enhancement

  • #762 Upgrade to babel-plugin-ember-template-compilation v2 (@dfreeman)

Committers: 1

v6.1.1 (2022-09-08)

:bug: Bug Fix

  • #755 Fix template compilation in addons on ember-cli < 3.13. This was a regression in 6.1.0. (@mansona)

Committers: 1

v6.1.0 (2022-07-04)

:rocket: Enhancement

  • #749 Drive all template compilation from babel (@ef4)

:bug: Bug Fix

  • #747 Avoid registering babel-plugin-ember-template-compilation repeatedly (@dfreeman)
  • #741 Fix incorrect ember-source version check (@simonihmig)

:memo: Documentation

Committers: 3

v6.0.1 (2021-12-05)

:bug: Bug Fix

Committers: 1

v6.0.0 (2021-10-14)

:boom: Breaking Change

  • #724 Use simplified babel plugin on ember 3.27+ and drop unsupported node versions (@ef4)

:rocket: Enhancement

  • #733 Avoid repeated encoding in getTemplateCompiler (@rwjblue)
  • #724 Use simplified babel plugin on ember 3.27+ and drop unsupported node versions (@ef4)

:bug: Bug Fix

:house: Internal

Committers: 3

v5.7.1 (2021-03-18)

:bug: Bug Fix

  • #685 Ensure global is present for Node 10 + globalThis polyfill (@rwjblue)

Committers: 1

v5.7.0 (2021-03-18)

:rocket: Enhancement

  • #683 Disable the modules API polyfill on Ember 3.27+ (@pzuraq)

:house: Internal

  • #684 Update babel-plugin-htmlbars-inline-precompile to 4.4.6. (@rwjblue)

Committers: 2

v5.6.5 (2021-03-12)

:bug: Bug Fix

  • #680 Update inline template compilation plugin to avoid errors on rebuilds (@rwjblue)

Committers: 2

v5.6.4 (2021-03-07)

:bug: Bug Fix

  • #678 Make setTimeout/clearTimeout available to the template compiler sandbox (@rwjblue)
  • #677 Support TypeScript merging of export default declarations in template colocation (@dfreeman)

Committers: 2

v5.6.3 (2021-03-04)

:bug: Bug Fix

  • #675 Remove development only optionalDependencies (release-it and release-it-lerna-changelog). (@alexlafroscia)

Committers: 1

v5.6.2 (2021-02-27)

:bug: Bug Fix

Committers: 1

v5.6.1 (2021-02-26)

:bug: Bug Fix

  • #663 Ember Ember 3.27+ can determine global for template compilation (@rwjblue)

Committers: 1

v5.6.0 (2021-02-26)

:rocket: Enhancement

  • #661 Remove usage of registerPlugin / unregisterPlugin (@rwjblue)

:bug: Bug Fix

  • #662 Avoid building the template compiler cache key repeatedly (@rwjblue)

Committers: 1

v5.5.0 (2021-02-26)

:rocket: Enhancement

  • #660 Replace purgeModule cache busting with vm based sandboxing (@rwjblue)

Committers: 1

v5.4.0 (2021-02-24)

:house: Internal

  • #659 Enable experimentation via ember-template-imports addon (@pzuraq)

Committers: 1

v5.3.2 (2021-02-10)

:rocket: Enhancement

Committers: 2

v5.3.1 (2020-08-11)

:bug: Bug Fix

  • #599 Move ember-template-lint to devDependencies (from dependencies) (@jamescdavis)

Committers: 1

v5.3.0 (2020-08-10)

:rocket: Enhancement

  • #597 Pass isProduction to Ember template compiler. (@rwjblue)

:memo: Documentation

:house: Internal

  • #584 Replace ember-cli-template-lint with ember-template-lint (@arthirm)

Committers: 2

v5.2.0 (2020-06-25)

:rocket: Enhancement

Committers: 1

v5.1.2 (2020-05-08)

:bug: Bug Fix

  • #553 Ensure custom templateCompilerPath is an absolute path. (@rwjblue)

Committers: 1

v5.1.1 (2020-05-07)

:bug: Bug Fix

  • #551 Ensure EmberENV is available to inline template compilation (@rwjblue)
  • #550 Fix specifying custom template compiler path. (@rwjblue)

:house: Internal

Committers: 1

v5.1.0 (2020-05-06)

:rocket: Enhancement

  • #543 Update babel-plugin-htmlbars-inline-precompile to 4.0.0. (@rwjblue)

Committers: 1

v5.0.0 (2020-05-04)

:boom: Breaking Change

:rocket: Enhancement

  • #528 Use smaller cache key for ember-template-compiler (reduce overall memory overhead of caching) (@xg-wang)
  • #512 Update Broccoli dependencies to latest. (@rwjblue)

:house: Internal

Committers: 2

v4.3.1 (2020-04-09)

:bug: Bug Fix

Committers: 1

v4.3.0 (2020-04-08)

:memo: Documentation

Committers: 1

v4.2.3 (2020-02-24)

:house: Internal

Committers: 2

v4.2.2 (2020-01-15)

:bug: Bug Fix

:memo: Documentation

Committers: 3

v4.2.1 (2020-01-09)

:bug: Bug Fix

  • #423 Only check semver range if ember-source is present (@mixonic)
  • #392 Ensure inline precompilation does not error when a template contains */ (@rwjblue)

Committers: 2

v4.2.0 (2019-12-11)

:rocket: Enhancement

  • #384 Remove setEdition requirement for colocation. (@rwjblue)

Committers: 1

v4.1.1 (2019-12-11)

:bug: Bug Fix

  • #390 Ensure reexported components do not throw an error. (@rwjblue)

Committers: 1

v4.1.0 (2019-12-10)

:rocket: Enhancement

  • #380 Implement basic patching strategy for colocated components. (@rwjblue)

Committers: 1

v4.0.9 (2019-12-04)

:rocket: Enhancement

  • #373 Add co-location support to CoffeeScript component class files (@locks)

:memo: Documentation

  • #351 Update Readme with syntax for usage with tagged templates (@thec0keman)

:house: Internal

Committers: 3

v4.0.8 (2019-10-19)

:bug: Bug Fix

  • #340 Fix issues when using colocated component's with decorators. (@rwjblue)

:house: Internal

  • #341 Add test using native classes + decorators. (@rwjblue)
  • #338 Add broccoli plugin + babel plugin colocation tests (@rwjblue)

Committers: 1

v4.0.7 (2019-10-18)

:bug: Bug Fix

  • #336 Support as default exports with template colocation (@dfreeman)

:house: Internal

Committers: 2

v4.0.6 (2019-10-17)

:rocket: Enhancement

:bug: Bug Fix

  • #333 Enable colocated component classes to be TypeScript (@rwjblue)
  • #332 Ensure "pods style" templates are compiled properly. (@rwjblue)

:house: Internal

Committers: 2

v4.0.5 (2019-10-04)

:bug: Bug Fix

  • #324 More fixes for proper babel plugin deduplication. (@rwjblue)

:memo: Documentation

  • #323 Ensure deprecation message shows correct heirarchy. (@rwjblue)

Committers: 1

v4.0.4 (2019-10-02)

:bug: Bug Fix

  • #322 Fix issue with deduplcation of babel plugin when parallelized (@rwjblue)

Committers: 1

v4.0.3 (2019-10-01)

:bug: Bug Fix

  • #317 Avoid conflicts with ember-cli-htmlbars-inline-precompile (@rwjblue)

:memo: Documentation

  • #318 Ensure all debug logging is emitted with DEBUG=ember-cli-htmlbars:* (@rwjblue)

Committers: 2

v4.0.2 (2019-09-30)

:bug: Bug Fix

  • #309 Ensure inline precompile and colocated templates run template AST plugins. (@rwjblue)
  • #310 Fix issues using inline precompilation with JSON.stringify'ed options. (@rwjblue)

Committers: 1

v4.0.1 (2019-09-25)

:bug: Bug Fix

  • #304 Do nothing in ColocatedTemplateProcessor if input tree is empty. (@rwjblue)

Committers: 1

v4.0.0 (2019-09-24)

:boom: Breaking Change

:rocket: Enhancement

:house: Internal

Committers: 2