npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

prettier-plugin-ember-template-tag

v2.0.4

Published

A prettier plugin for formatting Ember template tags

Downloads

116,251

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.