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

@espcom/esbuild-plugin-inject-preload

v1.0.0

Published

Esbuild plugin for injecting preload links into HTML templates

Downloads

4

Readme

@espcom/esbuild-plugin-inject-preload

npm coverage size-esm size-cjs

This is an esbuild plugin designed to inject links into HTML templates. It processes output files from esbuild, and allows to insert these links into specific placeholders within an HTML file based on a given template and options.

Features

  • Supports both ESM and CJS environments
  • Works across all platforms (Linux, Windows, etc.)
  • Works in watch mode and with any configuration of entryNames, assetNames and publicPath

Installation

To install the plugin, run the following command:

npm install @espcom/esbuild-plugin-inject-preload --save-dev

Usage

To use the plugin, add it to the plugins array in your esbuild configuration.

Example

import { pluginInjectPreload } from '@espcom/esbuild-plugin-inject-preload';

esbuild.build({
  entryPoints: ['src/entry.ts'],
  bundle: true,
  outdir: 'dist', // Required by the plugin
  metafile: true, // Required by the plugin
  plugins: [
    pluginInjectPreload([
      {
        templatePath: path.resolve('dist/template.html'),
        replace: '<!-- FONT_PRELOAD --><!-- /FONT_PRELOAD -->',
        as(filePath) {
          if (filePath.endsWith('.woff2') || filePath.endsWith('.woff')) {
            return `<link as="font" crossorigin="anonymous" href="${filePath}" rel="preload">`;
          }
        },
      },
      {
        templatePath: path.resolve('dist/template.html'),
        replace: '<!-- ENTRY_SCRIPT --><!-- /ENTRY_SCRIPT -->',
        as(filePath) {
          if (/entry([^.]+)?\.js/.test(filePath)) {
            return `<script src="${filePath}" defer=""></script>`;
          }
        },
      },
    ])
  ]
});

So, the resulting HTML file will contain smth. like

<!-- FONT_PRELOAD -->
<link as="font" crossorigin="anonymous" href="dist/Roboto-Regular.woff" rel="preload">
<link as="font" crossorigin="anonymous" href="dist/Roboto-Medium.woff2" rel="preload">
<!-- /FONT_PRELOAD -->
<!-- ENTRY_SCRIPT -->
<script src="dist/entry.js" defer=""></script>
<!-- /ENTRY_SCRIPT -->

Options

The plugin accepts an array of options. Each option object must contain the following properties:

  • templatePath (string): Path to the HTML template file where the links should be injected.
  • replace (string): The placeholder in the template to be replaced by the links. It must be a closed HTML comment, e.g., <!-- preload-links --><!-- /preload-links -->.
  • as (function): A function that takes a file path as an argument and returns the corresponding HTML element as a string.

Important Notes

  • The metafile and outdir options in the esbuild configuration must be set for the plugin to function correctly.
  • The template file should be present, this plugin does not copy it. So use smth. like esbuild-copy-plugin before this plugin

Alternatives

Currently, there are no alternatives for injecting preloading links. However, there is a plugin called esbuild-plugin-html that allows for injecting entry point scripts. In most cases, using @espcom/esbuild-plugin-inject-preload is sufficient. Nevertheless, the esbuild-plugin-html plugin offers powerful options such as findRelatedCssFiles, findRelatedOutputFiles, and inline, making it a better choice for complex projects. Notably, @espcom/esbuild-plugin-inject-preload is fully compatible with esbuild-plugin-html if placed after it in the plugins list.