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

Package detail

prettier-plugin-ember-template-tag

ember-tooling147.1kMIT2.0.5TypeScript support: included

A prettier plugin for formatting Ember template tags

ember, ember template imports, gjs, gts, prettier, template tag

readme

prettier-plugin-ember-template-tag

CI npm license

A Prettier plugin for formatting Ember template tags in both .gjs and .gts files. See also Ember Template Imports.

Compatibility

Prettier 3.0.0 and above

IMPORTANT! If you are running Prettier via eslint-plugin-ember and eslint-plugin-prettier1:

eslint-plugin-ember prettier-plugin-ember-template-tag
<12 1.1.0
>=12 2+

1 Neither I nor the Prettier project recommend running Prettier this way.

Usage

  1. Install:

    NPM:

    npm install --save-dev prettier prettier-plugin-ember-template-tag

    Yarn:

    yarn add --dev prettier prettier-plugin-ember-template-tag

    PNPM:

    pnpm add --save-dev prettier prettier-plugin-ember-template-tag
  2. Configure prettier using your preferred method. For example, with .prettierrc.js:

    // .prettierrc.js
    module.exports = {
      // ...
      plugins: ['prettier-plugin-ember-template-tag'],
      overrides: [
        {
          files: '*.{js,ts,gjs,gts}',
          options: {
            singleQuote: true,
          },
        },
        // ...
      ],
    };

    If you already have a "prettier" section in package.json, remember that takes precedence over the .prettierrc.js file!

  3. Run prettier on your codebase

    # With prettier >= 3.1
    npm prettier --write .
    
    # With prettier < 3.1
    # See <https://github.com/gitKrystan/prettier-plugin-ember-template-tag/issues/113> and
    # <https://github.com/prettier/prettier/issues/15351> for details on why using the
    # `--plugin` flag is required here.
    npm prettier --write . --plugin prettier-plugin-ember-template-tag

Opinions

This plugin works by weaving together Prettier's standard babel-ts and glimmer parsers, so it doesn't have many opinions of its own. With that said, I did have to make some opinionated decisions regarding how templates are printed within the JavaScript. In general, my strategy has been to ask "What would a function do?" since functions can be used in the same positions as the <template> tag.

Semicolons

Semicolons will be included or omitted as follows:

export default class MyComponent extends Component {
  <template>Hello</template> // omit
}

<template>Hello</template> // omit

export default <template>Hello</template> // omit

export const MyComponent = <template>Hello</template>; // include by default, omit in no-semi mode

You can read more about and comment on my semicolon decision process on this RFC.

New Lines

Template tags will always be printed on their own line for templates that are default exports or top-level class templates. Templates used as expressions will be allowed to collapse onto a single line if they fit:

import Component from '@glimmer/component';

const what = <template>Used as an expression</template>;

export const who = <template>Used as an expression</template>;

<template>
  The default export
</template>

class MyComponent extends Component {
  <template>
    Top-level class template
  </template>
}

export default

The following <template> invocations both desugar to default exports:

// template-only-component.js

<template>Hello</template>;

// or

export default <template>Hello</template>;

By default, this plugin will remove export default to print the more concise "sugared" default export template. If you would prefer to always include export default, you can enable the templateExportDefault option described below.

Configuration

These configuration options are available in addition to Prettier's standard config for JavaScript and Handlebars files.

Name Default Description
templateExportDefault false If true, default export template tags will be prepended with export default.
templateSingleQuote undefined Same as in Prettier but affecting only template tags. If undefined, will fall back to whatever is set for the global singleQuote config.

Editor integration

VSCode

  1. Install Prettier and this plugin as shown above.
  2. Install the Prettier - Code Formatter VSCode Extension.
  3. Reload your VSCode window.
  4. The format command and format-on-save should now work for .gjs and .gts files. You might need to wait a few seconds for the Prettier extension to register the plugin. If this does not work, you can manually set Prettier as the default formatter for .gjs and .gts as shown below, like so, but please file an issue if you need to do this:

    // .vscode/settings.json
    {
      "[gjs]": {
        // "editor.formatOnSave": true,
        "editor.defaultFormatter": "esbenp.prettier-vscode"
      },
    
      "[gts]": {
        // "editor.formatOnSave": true,
        "editor.defaultFormatter": "esbenp.prettier-vscode"
      }
    }
  5. If you're still having issues, check out this issue for troubleshooting ideas and comment there with what ends up working.

Ignoring code

Because gts/gjs files include both JavaScript and Glimmer template code, you'll need to use the appropriate prettier-ignore comment for the code you are ignoring:

export default class MyComponent extends Component {
  // prettier-ignore
  cells = matrix(
    1, 0, 0,
    0, 1, 0,
    0, 0, 1
  )

  <template>
    {{! prettier-ignore }}
    {{#each this.cells as |cell|}}{{cell.contents}}{{/each}}
  </template>
}

To ignore the entire template, use a JavaScript //prettier-ignore comment before the opening <template> tag:

export default class MyComponent extends Component {
  // prettier-ignore
  <template>
    This whole template is ignored
    <MyUglyComponent     "shall"     be="preserved">
      <ThisPart
        is  =  "also preserved as is"
      />
    </MyUglyComponent>
  </template>
}

Bugs

If you find a bug, please file a bug report! See CONTRIBUTING.md for more details.

changelog

Changelog

Release (2025-03-31)

prettier-plugin-ember-template-tag 2.0.5 (patch)

:bug: Bug Fix

  • prettier-plugin-ember-template-tag
    • #341 Fix implied export default with satisfies (@ef4)

Committers: 1

  • Edward Faulkner (@ef4)

Release (2024-11-06)

prettier-plugin-ember-template-tag 2.0.4 (patch)

:bug: Bug Fix

  • prettier-plugin-ember-template-tag

Committers: 1

Release (2024-11-06)

prettier-plugin-ember-template-tag 2.0.3 (patch)

:house: Internal

Committers: 2

v2.0.2 (2024-04-03)

:bug: Bug Fix

:house: Internal

Committers: 1

v2.0.1 (2024-03-15)

:bug: Bug Fix

  • #253 Fix errors when components contain multi-byte characters (@patricklx)

:robot: Dependencies

Committers: 1

v2.0.0 (2023-12-29)

This version is essentially a re-write. We are switching from using ember-template-imports to parse template tags to the more robust content-tag preprocessor. This should result in fewer unhandled cases and other bugs. For example, this plugin can now format route templates from ember-route-template!

This is a breaking change in that we are breaking compatibility with the current version of eslint-plugin-ember. If you are running Prettier via eslint-plugin-ember and eslint-plugin-prettier1, you will need to follow the following compatibility table, which also appears in the README for this plugin:

eslint-plugin-ember prettier-plugin-ember-template-tag
<12 1.1.0
>=12.0.0-alpha.1 2+

1 Neither I nor the Prettier project recommend running Prettier this way.

Additionally, there are some formatting differences in this version as discussed in the following RFCs:

:boom: Breaking Change

:boom: Breaking Change / :rocket: Enhancement / :robot: Dependencies

:rocket: Enhancement

  • #178 Disable minification for easier debugging.

:robot: Dependencies

Committers: 3

v2.0.0-2 (2023-12-29)

:bug: Bug Fix

Committers: 1

v2.0.0-1 (2023-12-28)

:boom: Breaking Change

:rocket: Enhancement

:bug: Bug Fix

:robot: Dependencies

Committers: 1

v2.0.0-0 (2023-12-04)

This version is essentially a re-write. We are switching from using ember-template-imports to parse template tags to the more robust content-tag preprocessor. This should result in fewer unhandled cases and other bugs. For example, this plugin can now format route templates from ember-route-template!

This is a breaking change in that we are breaking compatibility with the current version of eslint-plugin-ember. If you are running Prettier via eslint-plugin-ember and eslint-plugin-prettier1, you will need to follow the following compatibility table, which also appears in the README for this plugin:

eslint-plugin-ember prettier-plugin-ember-template-tag
<12 1.1.0
>=12.0.0-alpha.1 2+

1 Neither I nor the Prettier project recommend running Prettier this way.

:boom: Breaking Change

:boom: Breaking Change / :rocket: Enhancement / :robot: Dependencies

  • #162, #178, #185, and #180 Replace ember-template-imports with content-tag. As a result, we were also able to remove dependencies on ember-cli-htmlbars and @glimmer/syntax. (@patricklx, @gitKrystan)

:rocket: Enhancement

  • #178 Disable minification for easier debugging.

:robot: Dependencies

Committers: 3

v1.1.0 (2023-09-03)

:bug: Bug Fix

  • #125 Add more detail to languages array (possibly simplifying VSCode setup!) (@gitKrystan)

:house: Internal

  • #124 Add acceptance testing and document passing --plugin flag to prettier in CLI (@gitKrystan)
  • Various dependency upgrades

Committers: 1

v1.0.2 (2023-08-09)

:bug: Bug Fix

  • #103 Remove npx only-allow pnpm preinstall hook and devDep (@chrisrng)

Committers: 1

v1.0.1 (2023-08-08)

:bug: Bug Fix

  • #93 Add only-allow to devDependencies so it does not require network (@chriskrycho)

Committers: 1

v1.0.0 (2023-07-25)

:house: Internal

  • #83 Add tests for using template tag within render in a test (@gitKrystan)

Committers: 1

v1.0.0-0 (2023-07-25)

:boom: Breaking Change

:memo: Documentation

:house: Internal

Committers: 3

v0.3.2 (2023-02-01)

:bug: Bug Fix

:house: Internal

Committers: 1

v0.3.1 (2023-02-01)

:bug: Bug Fix

:house: Internal

Committers: 1

v0.3.0 (2022-11-28)

:rocket: Enhancement

:bug: Bug Fix

:house: Internal

Committers: 1

v0.2.1 (2022-11-19)

:bug: Bug Fix

  • #36 Fix bug where parsing template on newline caused syntax error (@gitKrystan)

Committers: 1

v0.2.0 (2022-11-19)

:boom: Breaking Change

  • #35 Print template tags on separate lines for "default" templates (@gitKrystan)

:rocket: Enhancement

  • #35 Print template tags on separate lines for "default" templates (@gitKrystan)

:memo: Documentation

  • #35 Print template tags on separate lines for "default" templates (@gitKrystan)

:house: Internal

Committers: 1

v0.1.1 (2022-11-16)

:bug: Bug Fix

Committers: 1

v0.1.0 (2022-11-16)

:boom: Breaking Change

  • #30 Don't convert back to <template> if text is pre-preprocessed (@gitKrystan)
  • #27 Remove export default by default. Add templateExportDefault option to add it back. (@gitKrystan)

:rocket: Enhancement

  • #30 Don't convert back to <template> if text is pre-preprocessed (@gitKrystan)
  • #27 Remove export default by default. Add templateExportDefault option to add it back. (@gitKrystan)
  • #24 Use @glimmer/syntax getTemplateLocals (@gitKrystan)

:bug: Bug Fix

  • #28 Fix bug where preprocess caused syntax errors in component classes (@gitKrystan)

:memo: Documentation

:house: Internal

Committers: 1

v0.0.3 (2022-11-04)

:rocket: Enhancement

  • #18 Add templateSingleQuote option (See #16) + Simplify test suite further (@gitKrystan)

:memo: Documentation

:house: Internal

Committers: 1

v0.0.2 (2022-11-03)

:memo: Documentation

Committers: 1