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

Package detail

content-tag

embroider-build540.7kMIT3.1.2TypeScript support: included

A rust program that uses a fork of swc to parse and transform Javascript containing the content-tag proposal

readme

content-tag

content-tag is a preprocessor for JS files that are using the content-tag proposal. This originated with Ember.js' GJS and GTS functionality. You can read more by checking out the original RFC

This preprocessor can be used to transform files using the content-tag spec to standard JS. It is built on top of swc using Rust and is deployed as a wasm package.

Installation

npm install content-tag

Usage

Node (CommonJS)

let { Preprocessor } = require('content-tag');
let p = new Preprocessor();
let output = p.process('<template>Hi</template>');

console.log(output);

Node (ESM)

import { Preprocessor } from 'content-tag';
let p = new Preprocessor();
let output = p.process('<template>Hi</template>');

console.log(output);

Browser (ESM)

import { Preprocessor } from 'content-tag';
let p = new Preprocessor();
let output = p.process('<template>Hi</template>');

console.log(output);

API

Preprocessor

All content-tag public API lives on the Preprocessor object.

Preprocessor.process(src: string, options?: PreprocessorOptions): string;

Parses a given source code string using the content-tag spec into standard JavaScript.

import { Preprocessor } from 'content-tag';
let p = new Preprocessor();
let output = p.process('<template>Hi</template>');

Preprocessor.parse(src: string, options?: PreprocessorOptions): Parsed[];

Parses a given source code string using the content-tag spec into an array of Parsed content tag objects.

import { Preprocessor } from 'content-tag';
let p = new Preprocessor();
let output = p.parse('<template>Hi</template>');

PreprocessorOptions

interface PreprocessorOptions {

  /** Default is `false` */
  inline_source_map?: boolean;

  filename?: string;

}

Parsed

NOTE: All ranges are in bytes, not characters.

interface Parsed {
  /**
   * The type for the content tag.
   *
   * 'expression' corresponds to a tag in an expression position, e.g.
   * ```
   * const HiComponent = <template>Hi</template>;
   * ```
   *
   * 'class-member' corresponds to a tag in a class-member position, e.g.
   * ```
   * export default class HiComponent extends Component {
   *   <template>Hi</template>
   * }
   * ```
   */
  type: "expression" | "class-member";

  /**
   * Currently, only template tags are parsed.
   */
  tagName: "template";

  /** Raw template contents. */
  contents: string;

  /**
   * Byte range of the contents, inclusive of inclusive of the
   * `<template></template>` tags.
   */
  range: {
    start: number;
    end: number;
  };

  /**
   * Byte range of the template contents, not inclusive of the
   * `<template></template>` tags.
   */
  contentRange: {
    start: number;
    end: number;
  };

  /** Byte range of the opening `<template>` tag. */
  startRange: {
    end: number;
    start: number;
  };

  /** Byte range of the closing `</template>` tag. */
  endRange: {
    start: number;
    end: number;
  };
}

Contributing

See the CONTRIBUTING.md file.

changelog

content-tag Changelog

Release (2025-03-20)

content-tag 3.1.2 (patch)

:bug: Bug Fix

  • content-tag
    • #97 Support automatic export default when using TS satisfies keyword (@ef4)

Committers: 1

  • Edward Faulkner (@ef4)

    Release (2025-02-01)

content-tag 3.1.1 (patch)

:bug: Bug Fix

Committers: 1

content-tag 3.1.0 (minor)

:rocket: Enhancement

Committers: 1

content-tag 3.0.0 (major)

:boom: Breaking Change

  • content-tag

Committers: 1

content-tag 2.0.3 (patch)

:bug: Bug Fix

  • content-tag

:house: Internal

  • content-tag

Committers: 1

  • Peter Wagenet (@wagenet)

    Release (2024-09-21)

content-tag 2.0.2 (patch)

:bug: Bug Fix

  • content-tag
    • #79 Provide correct types when using cjs with moduleResolution:nodenext (@ef4)

:house: Internal

  • content-tag

Committers: 1

  • Edward Faulkner (@ef4)

    Release (2024-02-01)

content-tag 2.0.1 (patch)

:bug: Bug Fix

Committers: 1

content-tag 2.0.0 (major)

:boom: Breaking Change

:memo: Documentation

:house: Internal

Committers: 3

content-tag 1.2.2 (patch)

:bug: Bug Fix

Committers: 1

content-tag 1.2.1 (patch)

:bug: Bug Fix

:house: Internal

Committers: 1

content-tag 1.2.0 (minor)

:rocket: Enhancement

  • #44 Standalone content-tag implemented via conditional exports in package.json (@NullVoxPopuli)

:house: Internal

Committers: 1

1.1.2 (2023-10-06)

:bug: Bug Fix

Committers: 1

  • Edward Faulkner (@ef4)

1.1.1 (2023-10-02)

:bug: Bug Fix

:house: Internal

Committers: 3

1.1.0 (2023-09-22)

  • ENHANCEMENT: added a parse method for extracting location information for content tags.

1.0.1 (2023-07-21)

  • BUGFIX: inner expressions were not getting transpiled

1.0.0 (2023-07-18)

Initial release.