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

@blastjs/templating-tools

v1.2.1

Published

Tools to scan HTML and compile tags when building a templating package

Downloads

1

Readme

templating-tools

Has some conveniently abstracted functions that are used together with the caching-html-compiler package to implement different template compilers:

  1. templating
  2. static-html

These functions contain some code shared between the above build plugins, and if you are building your own build plugin they can be useful too. But they aren't guaranteed to be helpful for every use case, so you should carefully decide if they are appropriate for your package.


TemplatingTools.scanHtmlForTags(options)

Scan an HTML file for top-level tags as specified by options.tagNames, and return an array of Tag objects. See more about Tag objects below.

Options

  1. sourceName the name of the input file, used when throwing errors.
  2. contents the contents of the input file, these are parsed to find the top-level tags
  3. tagNames the top-level tags to look for in the HTML.

Example

const tags = scanHtmlForTags({
  sourceName: inputPath,
  contents: contents,
  tagNames: ["body", "head", "template"]
});

TemplatingTools.compileTagsWithSpacebars(tags)

Transform an array of tags into a result object of the following form:

{
  js: String,
  body: "",
  head: String,
  bodyAttrs: {
    [attrName]: String
  }
}
  1. The contents of every <template> and <body> tag will be compiled into JavaScript with spacebars-compiler, and the code appended to the js field of the result.
  2. The contents of every <head> tag will be concatenated into the head field of the result.
  3. Any attributes found on <body> tags will be added to the bodyAtts field of the result.
  4. Every <template> tag is required to have a name attribute, and no other attributes.
  5. The <head> tag is not allowed to have any attributes.

TemplatingTools.CompileError

This error is thrown when a compilation error happens. If you catch it, look for the following fields, which are set by TemplatingTools.throwCompileError:

  1. message The error message to show to the user.
  2. file The filename where the error occured.
  3. line The line number where the error occured.

TemplatingTools.throwCompileError(tag, message, [overrideIndex])

Throw a TemplatingTools.CompileError with the right properties. Handles generating the line number of the error for you.

Arguments

  1. tag the Tag object in which this compile error occured. The fields on this object are used to populate fields on the resulting error.
  2. message the error message, will be displayed to the user.
  3. overrideIndex optional - if provided will be used to determine the line number of the error; otherwise the index of the start of the tag will be used.

Tag object

The scanHtml and compileTagsWithSpacebars functions communicate via an array of Tag objects, which have the following form:

{
  // Name of the tag - "body", "head", "template", etc
  tagName: String,
  
  // Attributes on the tag
  attribs: { [attrName]: String },
  
  // Contents of the tag
  contents: String,
  
  // Starting index of the opening tag in the source file
  // (used to throw informative errors)
  tagStartIndex: Number,
  
  // Starting index of the contents of the tag in the source file
  // (used to throw informative errors)
  contentsStartIndex: Number,
  
  // The contents of the entire source file, should be used only to
  // throw informative errors (for example, this can be used to
  // determine the line number for an error)
  fileContents: String,
  
  // The file name of the initial source file, used to throw errors
  sourceName: String
};