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

html-insert-assets

v0.14.3

Published

Insert assets such as .js, .css into an HTML file.

Downloads

7,541

Readme

html-insert-assets

Overview

Insert assets such as scripts, stylesheets and shortcut icons into HTML.

Supports stamping of asset URLs, preloading assets and more fine grained options for specific asset types.

CLI

Required

    --html <file>
        the HTML template to insert assets into

    --out <file>
        the output HTML file path

Scripts

    --scripts [--async] [--module] [--nomodule] [--attr name=value] <scripts>...

Add <script src="..."> elements to the end of the <body> for each script.

Options:

    --async
        adds the 'async' attribute

    --module
        marks the scripts as es2015 modules using the 'type="module"' attribute

    --nomodule
        marks the scripts as non-es2015 using the 'nomodule' attribute

    --attr name=value
        add custom attributes, can specify multiple name=value pairs

By default the script will only include the src attribute. If neither --module or --nomodule are present defaults are determined based on file extension or naming conventions:

  • --module is assumed on a per-file basis if the file extension is .mjs or .es2015.js
  • --nomodule is assumed on a per-file basis if a matching es2015 module file is present

See:

Stylesheets

    --stylesheets [--media value] <stylesheets>...

Add <link rel="styleshsheet" href="..."> to the <head> for each stylesheet.

Options:

    --media value
        add the media query attribute

See:

Favicon Images

    --favicons [--rel value] [--sizes value] [--type value] <images>...

Add <link rel="icon" type="..." href="..."> to the <head> for eached image.

Options:

    --rel value
        add the specified 'rel' attribute value, defaults to '"icon"'

    --sizes value
        add the specified 'size' attribute value

    --type value
        add the specified 'type' attribute value

By default rel="icon" is added and the type attribute is determined per-file based on file extension.

See:

Generic Assets

    --assets <assets>...

Attempts to automatically determine asset types based on file extension and inserts such assets with the default configurations listed above.

  • *.{js,mjs} for scripts
  • *.css for stylesheets
  • *.ico for favicons

Preloading

    --preload <assets>...

Add <link rel="preload" href="..." as="..."> for each asset, determining the as attributed based on file extension.

Allows preloading assets that your page will need very soon, which you want to start loading early in the page lifecycle.

Stamping

Adds paramaters to inserted URLs to create versioned URLs, allowing long cache times while busting the cache when resources change.

Examples

Basic

Functionality shared across asset types, using --scripts for these examples.

Basic relative paths:

    html-insert-assets
        --html ./index.tmpl.html
        --out ./index.html
        --scripts ./a.js b.js sub/c.js

will insert:

    <script src="./a.js"></script>
    <script src="./b.js"></script>
    <script src="./sub/c.js"></script>

If the --out file is in a separate directory, URLs are relative to that directory

    html-insert-assets
        --html ./index.tmpl.html
        --out ./sub/index.html
        --scripts ./a.js b.js sub/c.js

will insert:

    <script src="../a.js"></script>
    <script src="../b.js"></script>
    <script src="./c.js"></script>

Root asset directories to trim and convert to URLs relative to the the --out directory

    html-insert-assets
        --html ./index.tmpl.html
        --out ./index.html
        --roots .
        --scripts ./a.js b.js sub/c.js

will insert:

    <script src="./a.js"></script>
    <script src="./b.js"></script>
    <script src="./sub/c.js"></script>

Root directories of assets (multiple):

    html-insert-assets
        --html ./index.tmpl.html
        --out ./index.html
        --roots sub /abs/path
        --scripts /abs/path/a.js b.js sub/c.js

will insert:

    <script src="./a.js"></script>
    <script src="./b.js"></script>
    <script src="./c.js"></script>

Root directories of assets (multiple + alternate --out):

    html-insert-assets
        --html ./index.tmpl.html
        --out ./out/index.html
        --roots sub /abs/path
        --scripts /abs/path/a.js b.js sub/c.js

will insert:

    <script src="./a.js"></script>
    <script src="../b.js"></script>
    <script src="./c.js"></script>

Absolute paths outside any root directories:

    html-insert-assets
        --html ./index.tmpl.html
        --out sub/index.html
        --roots sub /abs/path
        --scripts /path/to/my.js

will insert:

    <script src="/path/to/my.js"></script>

Assets

Scripts, auto detecting type="module" vs nomodule

    html-insert-assets
        --html ... --out ...
        --scripts foo.mjs foo.js

Scripts, manually specifying type="module" vs nomodule

    html-insert-assets
        --html ... --out ...
        --scripts --module app.mjs
        --scripts --nomodule app-legacy.js

Multiple favicons:

    html-insert-assets
        --html ... --out ...
        --favicon --sizes=16x16 default.ico
        --favicon --sizes=128x128 large.png

Stylesheets:

    html-insert-assets
        --html ... --out ...
        --stylesheets main.css
        --stylesheets --media=print print-only.css

Preloading Examples

Preloading some .js and .jpg files.

    html-insert-assets
        --html ./index.tmpl.html
        --out ./index.html
        --preload load-later.js large-image.jpg

will insert:

    <link rel="preload" href="./load-later.js" as="script">
    <link rel="preload" href="./large-image.jpg" as="image">

Notes

Originally forked from rules_nodejs.