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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@itrocks/asset-loader

v0.0.9

Published

Dynamically loads CSS and JavaScript files from your modules and appends them to the <head>

Readme

npm version npm downloads GitHub issues discord

asset-loader

Dynamically loads CSS and JavaScript files from your modules and appends them to the <head>.

This documentation was written by an artificial intelligence and may contain errors or approximations. It has not yet been fully reviewed by a human. If anything seems unclear or incomplete, please feel free to contact the author of this package.

Installation

npm i @itrocks/asset-loader

This package is meant to be used in a browser environment. It expects document.head to be available.

Usage

@itrocks/asset-loader exposes two helpers:

  • loadCss – injects a <link rel="stylesheet"> pointing to your CSS file.
  • loadScript – injects a <script> tag pointing to your JavaScript file.

Both helpers are idempotent: calling them multiple times with the same fileName will not create duplicate <link> / <script> elements. If the asset is already present, the optional onLoad callback is called immediately with the existing element.

Minimal example

import { loadCss, loadScript } from '@itrocks/asset-loader'

// Load a CSS file once
loadCss('/assets/styles/theme.css')

// Load a script and run some code when it is available
loadScript('/assets/vendor/chart.js', (script) => {
	// At this point the script has been loaded by the browser
	console.log('Script loaded from', script.src)
})

Complete example with conditional loading

In a typical it.rocks front‑end you may want to load optional assets only on some pages, while still avoiding duplicates when navigating.

import { loadCss, loadScript } from '@itrocks/asset-loader'

function enableCharts ()
{
	// Ensure chart styles are present (only added once, even if called
	// several times during navigation)
	loadCss('/assets/vendor/chart.css')

	// Load the script and initialize the library when ready
	loadScript('/assets/vendor/chart.js', () => {
		// The global `Chart` object is now available
		const canvas = document.querySelector<HTMLCanvasElement>('#sales')
		if (!canvas) return

		// Example chart rendering, depending on how your library works
		// (implementation details are up to your app)
		// new Chart(canvas, { ... })
	})
}

// You can call this whenever a page that needs charts becomes active
// (for example after routing or view activation)

API

loadCss(fileName: string, onLoad?: (link: HTMLLinkElement) => void): void

Injects a <link rel="stylesheet"> element into document.head if it does not already exist for the given fileName.

Parameters

  • fileName – URL or path to the CSS file, as it should appear in the href attribute. It can be absolute (https://...) or relative to the current page.
  • onLoad (optional) – callback invoked when the stylesheet has finished loading. It receives the created or existing HTMLLinkElement.

Behaviour

  • If a <link> with href === fileName is already in document.head, no new element is created and onLoad (if provided) is called immediately with that element.
  • Otherwise a new <link> is created, configured, appended to document.head, and onLoad is called when the browser fires the load event.

loadScript(fileName: string, onLoad?: (script: HTMLScriptElement) => void): void

Injects a <script> element into document.head if it does not already exist for the given fileName.

Parameters

  • fileName – URL or path to the JavaScript file, as it should appear in the src attribute. It can be absolute or relative to the current page.
  • onLoad (optional) – callback invoked when the script has finished loading. It receives the created or existing HTMLScriptElement.

Behaviour

  • If a <script> with src === fileName is already in document.head, no new element is created and onLoad (if provided) is called immediately with that element.
  • Otherwise a new <script> is created with script.src = fileName, appended to document.head, and onLoad is called when the browser fires the load event.

Typical use cases

  • Lazily loading third‑party libraries (charts, widgets, editors, etc.) only on the pages that need them.
  • Injecting theme‑specific or feature‑specific stylesheets without bloating your main CSS bundle.
  • Ensuring the same CSS or JS asset is not added multiple times when navigating in a single‑page application.
  • Loading experimental or optional assets behind feature flags while keeping the loading logic simple and declarative.