@embroider/template-tag-codemod

中文文档
1.4.2发布于 2026/10/22MITES Module构建来源已验证近 30 天下载 2049

Convert to HBS to GJS with

原始使用文档
文档目录9 节
  1. Instructions
  2. Important Options
  3. --relativeLocalPaths
  4. --nativeRouteTemplates
  5. --nativeLexicalThis
  6. --defaultFormat
  7. Prerequisites
  8. merge-history
  9. ember-css-modules

@embroider/template-tag-codemod

This codemod converts all usage of non-strict handlebars in an Ember app to the newer, strict Template Tag syntax. It uses the Embroider build infrastructure to do build-time resolution of all components, helpers, and modifiers so that you don't need to figure them out yourself.

Instructions

  1. Decide what options you will want to pass to the codemod. See "Important Options" below.
  2. Ensure your app has the prerequisites to use Template Tag. See "Prerequisites" below.
  3. Start with clean source control. We're going to mutate all your files. Use git to give you control over what changed.
  4. Run the codemod via npx @embroider/template-tag-codemod YOUR_OPTIONS_HERE
  5. Use prettier to apply nice formatting to the results.
  6. Commit your results.
  7. Optional but recommended: use the merge-history command to adjust Git history so that your GJS files inherit correctly from both the JS and HBS files they were created from. See below.

Important Options

This section explains the important options you should know about before deciding when and how to run the codemod. Additional options are available via interactive --help.

--relativeLocalPaths

By default, imports within your own app will use relative paths with a file extension. This is Node's standard for valid ES modules and it is the most future-proof output, since automatically mapping file extensions can be complex.

But there are several reasons you might prefer the traditional extensionless imports from the app's module namespace instead

  • the classic build requires it
  • @embroider/webpack 3.x does not support using the true file extension of some file types like .gjs

To get that behavior you can pass --relativeLocalPaths false.

// relativeLocalPaths true (default):
import TheButton from './components/the-button.js';

// relativeLocalpaths false:
import TheButton from 'your-app/components/the-button';

--nativeRouteTemplates

Starting at Ember 6.3.0, your route templates (app/templates/**/*.hbs) are allowed to directly export components, meaning you can write them as .gjs or .gts files. The codemod will produce this output by default.

However, if you want support for earlier Ember versions, you can pass --nativeRouteTemplates false and install the ember-route-template addon.

// app/templates/example.gjs

// nativeRouteTemplates true (default)
import MessageBox from '../components/message-box.js';
<template>
  <MessageBox>Hello world</MessageBox>
</template>

// nativeRouteTemplates false
import MessageBox from '../components/message-box.js';
import RouteTemplate from 'ember-route-template'
export default RouteTemplate(
  <template>
    <MessageBox>Hello world</MessageBox>
  </template>
)

--nativeLexicalThis

This flag is a workaround for a bug in Ember < 6.4.0. These versions have a bug that prevents you from accessing lexically scoped this in template tags that are used as expressions. The typical use case for this is in rendering tests:

// Input example:
test("some test", function(assert) {
  this.set('message', 'hi');
  render(hbs`{{this.message}}`);
})

// nativeLexicalThis true (default)
test("some test", function(assert) {
  this.set('message', 'hi');
  render(<template>{{this.message}}</template>);
})

// nativeLexicalThis false
test("some test", function(assert) {
  this.set('message', 'hi');
  const self = this;
  render(<template>{{self.message}}</template>);
})

If you want your code to work on Ember < 6.4.0, pass --nativeLexicalThis false. If you'd rather not pollute your tests with these extra lines, upgrade Ember first and keep the default value of the flag.

--defaultFormat

When converting an existing .js file to template tag, the codemod always produces a .gjs output file. When converting an existing .ts file, the codemod always produces a .gts file. But there are ambiguous cases:

  • a component that has only an .hbs with no corresponding .js or .ts.
  • a route template, which is traditionally always a standalone .hbs file

In these cases, the codemod's output is controlled by --defaultFormat.

--defaultFormat gjs is the default.

Pass --defaultFormat gts instead if you prefer to produce typescript. Also see the interactive docs for --routeTemplateSignature and --templateOnlyComponentSignature if you want to customize the default type signatures emitted by the codemod.

Prerequisites

  1. Your build must support Template Tag.

    On classic builds or on @embroider/core 3.x this means installing the ember-template-imports addon.

    On @embroider/core 4.x it is natively supported.

    To confirm this step worked, you should be able to write a new component in a .gjs file and see it working in your app.

  2. Your prettier configuration should support Template Tag. This was added to the default Ember app blueprint at ember-cli 6.1.0, but you can also set it up manually on earlier blueprint versions. You need the dependency prettier-plugin-ember-template-tag and the configuration in .prettierrc.js that goes with it.

  3. Your ESLint configuration should support Template Tag. This was added to the default Ember app blueprint at ember-cli 6.1.0, but you can also set it up manually on earlier blueprint versions. If you're using ember-cli 6.1.0 as a guide, note that the whole eslint configuration was upgraded to the newer flat-config style. To use Template Tag support in the older style of config, you need a section like:

    overrides: [
      {
        files: ['**/*.{gts,gjs}'],
        parser: 'ember-eslint-parser',
      },
    
And the `ember-eslint-parser` dependency.
  1. Upgrade @ember/test-helpers to >= 5.0.1 (because you may need this feature).

  2. If you're planning to use --nativeRouteTemplates false to support Ember < 6.3.0, make sure you have installed the ember-route-template addon.

  3. Optional: if you still have non-co-located component templates (app/templates/components/**/*.hbs) this codemod will leave those alone and they won't get upgraded to Template Tag. You should consider moving them to co-located (app/components/templates/**/*.hbs) instead. There is a codemod. This is also a prerequisite for ember-source >= 6.0.0.

  4. Optional: if you still have components that don't use native class syntax (Component.extend({}) instead of class extends Component), this codemod will leave those alone and they won't get upgraded to Template Tag. There is a native class codemod that you could use before using this codemod.

merge-history

The merge-history command takes a branch where the codemod has already been applied and produces a new branch with the same contents, except that the Git history has been adjusted so that your GJS files inherit correctly from both the JS and HBS files that they replaced.

IMPORTANT: The command only carries over changes in .gjs and .gts files from your branch to the new branch, which means all other changes in the original branch are lost if you then only merge the new one.

Example:

npx @embroider/template-tag-codemod merge-history --help
npx @embroider/template-tag-codemod merge-history main your-codemodded-branch --outputBranch better-codemodded-branch
git push -u origin better-codemodded-branch

The command also produces a .git-blame-ignore-revs file, which is supported by default by GitHub to let you see past the codemod formatting.

Known Compatibility Issues

ember-css-modules

If the codemod crashes with:

BuildError: BroccoliBridge placeholder 'modules' was never fulfilled.

this is probably because you have ember-css-modules, and it does extremely cursed things in the classic build pipeline. You can work around this problem by temporarily removing it from your package.json while you run the codemod.

版本与发布记录

@embroider/template-tag-codemod 共发布 176 个版本,首次发布于 2025/01/08;过去 12 个月发布了 5 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
1.4.2latest2026/10/2222 天前
1.4.12026/06/055 个月前
1.4.02026/05/136 个月前
1.3.62026/03/247 个月前
1.3.52026/02/049 个月前
1.3.42025/11/111 年前

其他发布通道:alpha → 0.5.0-alpha.10,unstable → 1.3.2-unstable.f6c658d。

关于 @embroider/template-tag-codemod 的常见问题

@embroider/template-tag-codemod 是什么?

Convert to HBS to GJS with

如何安装 @embroider/template-tag-codemod?

在项目目录运行 npm install @embroider/template-tag-codemod,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 template-tag-codemod,可以用 npx template-tag-codemod 直接运行。

@embroider/template-tag-codemod 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/embroider__template-tag-codemod 是否存在。

@embroider/template-tag-codemod 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@embroider/template-tag-codemod 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 20.19.*。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@embroider/template-tag-codemod 可以商用吗?

@embroider/template-tag-codemod 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@embroider/template-tag-codemod 的下载量有多少?

根据 npm 官方下载统计,@embroider/template-tag-codemod 在 2026/10/14 – 2026/11/12 这 30 天内被下载 2,049 次,日均约 68 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@embroider/template-tag-codemod 还在维护吗?

最新版本 1.4.2 发布于 2026/10/22(22 天前);至今共发布 176 个版本,其中过去 12 个月发布了 5 个;首次发布于 2025/01/08。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。