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

rollup-plugin-glimmer-template-tag

v0.4.1

Published

rollup plugin for converting <template> into valid JS / TS

Downloads

2,132

Readme

rollup-plugin-glimmer-template-tag

Rollup plugin for providing <template> support for both gjs and gts file formats

Compatibility

  • Node 16, 18, following Node's LTS support.
  • Rollup 3+
  • ESM only. Rollup configs will need to be in a type=module package, or be defined as mjs

Usage

First and foremost, this repo's test-packages contain examples of

  • js ember v2 addon using gjs
  • ts ember v2 addon using gts
  • ember test app consuming both of the above addons

For specifics, the packages/** folder may provide value.

To use <template>, you need to change two files:

  • rollup.config.mjs (where this plugin is used)
  • babel.config.js / json / etc (where the <template> transform is finished)

Rollup

The first step is add the rollup plugin, which will understand the <template> tag gjs and gts files:

 // rollup.config.mjs
 import { Addon } from '@embroider/addon-dev/rollup';

+ import { glimmerTemplateTag } from 'rollup-plugin-glimmer-template-tag';

 const addon = new Addon({
   srcDir: 'src',
   destDir: 'dist'
 });

 export default {
   output: addon.output(),
   plugins: [
     addon.publicEntrypoints(['components/demo.js']),
     addon.appReexports(['components/**/*.js']),
     addon.dependencies(),
+    glimmerTemplateTag(),
     // ...
   ]
 };

Babel

 // babel.config.js / json / etc
 'use strict';
 module.exports = {
   plugins: [
+    'ember-template-imports/src/babel-plugin',
     '@embroider/addon-dev/template-colocation-plugin',
     ['@babel/plugin-proposal-decorators', { legacy: true }],
     '@babel/plugin-proposal-class-properties'
   ]
 };

Configure rollup-plugin-ts (TS Only)

For typescript, a config change is required to allow the transpilation to happen:

// rollup.plugin.mjs
   
    typescript({
      transpiler: 'babel',
      browserslist: false,
-      transpileOnly: false,
+      transpileOnly: true,
    }),

Background: rollup-plugin-ts uses your input files (the intermediate format from the step above) to typecheck them, which in our case will always error. Telling rollup-plugin-ts to only transpile won't typecheck.

Since you want only the source files to be type-checked, it's best to use a lint:types script for type checking, and pre-publish checking both locally and in your C.I. environment. This repo is an example of how to set that up if this is unfamiliar.

"lint:types": "glint"

When using this method of type checking, the line numbers in errors will continue to match up.

Without type errors blocking the addon's build? what happens with the generated type declarations?

The errors are copied in to the type declaration files so that consumers would be alerted to the type errors. For example, given this component where we forget to import the type for the RouterService:

import Component from '@glimmer/component';
import { service } from '@ember/service';

export default class TsClassDemo extends Component {
  @service declare router: RouterService;

  greeting = 'Hello World!';

  <template>
    TS Class Demo: {{this.greeting}}
  </template>
}

The generated declaration for for this component is:

// dist/components/ts-class-demo.d.ts
import Component from '@glimmer/component';
declare class TsClassDemo extends Component {
    router: RouterService;
    greeting: string;
}
export { TsClassDemo as default };

which also excludes the type for RouterService. If an addon is using a test-app for its tests and that test-app has typescript, the test-app will report a type error when trying to resolve the type of TsClassDemo.

What about transpileOnly: false?

Without setting transpileOnly: true (using the default or explicitly setting to false),

  • line number-errors will not match up as the input to rollup-plugin-ts is the output from the <template> transformation.

  • you'll receive errors like the following

    [!] (plugin Typescript) RollupError: Cannot find module '@ember/template-compilation' or its corresponding type declarations.
    src/components/ts-class-demo.ts (2:36)
    
    2 import { precompileTemplate } from "@ember/template-compilation";
                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    This exposes internal information about the <template> transformation process. Though, you could try to get around the issue if you really want transpileOnly: false by declare moduleing for @ember/template-compilation in your unpublished-development-types except that @ember/template-compilation is not presently a real package, so the part of the error saying Cannot find module is still true, even though a corresponding type declaration is defined -- both the module and the type declarations are needed.

Manually choosing the single-stage transformation

There is an option available to the rollup plugin so that folks can choose to attempt the full <template> transform in a single pass.

This would do both steps wholly within the rollup plugin:

  1. preprocess the <template> tag into a secret internal format
  2. convert that secret internal format into vanilla JS that a consuming build environment knows how to handle

However, for performance or compatibility reasons, it may not be desireable to allow both steps to be handled automatically -- babel will have to re-parse all your code again (for example, in rollup-plugin-ts).

If you want to try out a rollup-only <template> transform, you'll want to want these changes to your config files:

// rollup.config.mjs
 export default {
   output: addon.output(),
   plugins: [
     // ...
-    glimmerTemplateTag(),
+    glimmerTemplateTag({ preprocessOnly: false }),
     // ...
   ],
 };
 // babel.config.js / json / etc
 'use strict';
 module.exports = {
   plugins: [
-    'ember-template-imports/src/babel-plugin',
     '@embroider/addon-dev/template-colocation-plugin',
     ['@babel/plugin-proposal-decorators', { legacy: true }],
     '@babel/plugin-proposal-class-properties'
   ]
 };