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

@hyrious/esbuild-plugin-http

v0.1.5

Published

Bundle http(s) resources on-the-fly

Downloads

25

Readme

@hyrious/esbuild-plugin-http

The HTTP plugin as a standalone package.

Usage

import { http } from "@hyrious/esbuild-plugin-http";
import { build } from "esbuild";

build({
  entryPoints: ["main.js"],
  bundle: true,
  plugins: [http()],
}).catch(() => process.exit(1));

Or, with @hyrious/esbuild-dev:

$ esbuild-dev -p:@hyrious/esbuild-plugin-http main.ts

Options

http({
  filter: (url) => true,
  agent: null,
  schemes: {},
  cache: new Map(),
  onfetch: (url) => void 0,
  fetchOptions: {},
  onload: () => void 0,
});
  • filter {RegExp|Function} Filter in URLs to be handled by this plugin. If the regex does not match, or the function returns false, the URL will be ignored.
  • agent {Object} The agent to use for HTTP requests.
  • schemes {Object} A map of custom schemes for shorter URL. For example, if you set it as { unpkg: 'https://unpkg.com/' }, then import "unpkg:react" will be resolved to https://unpkg.com/react. More details see schemes.
  • cache {Map⟨String,String|Uint8Array⟩} A map of url -> contents which can be reused during incremental or watch builds.
  • onfetch {Function} A callback function that will be called before fetching each URL.
  • fetchOptions {Object} Options passed to make-fetch-happen.
  • onload {Function} A callback function to customize the onLoad hooks of esbuild.

Proxying

You can use hpagent to apply http_proxy in your environment:

import { HttpProxyAgent } from "hpagent";

http({
  agent: new HttpProxyAgent({ proxy: process.env.http_proxy }),
});

Or, use global-agent to configure a global proxy for all requests.

Schemes

The custom schemes works by replacing prefixes in the import path, for example:

const schemes = {
  unpkg: "https://unpkg.com/", // <- trailing '/'
};

is equivalent to:

let url = args.path;
if (args.path.startsWith("unpkg:")) {
  url = "https://unpkg.com/" + args.path.slice("unpkg:".length);
}

Note that search params will be preserved to the end of the url, which means you can write this instead:

const schemes = {
  unpkg: "https://unpkg.com/?module",
};

This package includes some common schemes but does not use them by default, you can import them when needed:

import { http, default_schemes } from "@hyrious/esbuild-plugin-http";

console.log(default_schemes);
// => {
//   unpkg: 'https://unpkg.com/?module',
//   jsdelivr: 'https://esm.run/',
//   esm: 'https://esm.sh/?bundle'
// }

http({
  schemes: default_schemes,
});

Caching

By default this plugin does not cache any requests, esbuild does cache contents for the same resource path, so it won't request the same url more than once during one build.

If you want to do some basic caching during watch builds, you can pass in a map object to the cache option:

let cacheMap = new Map();
// cacheMap stores { url => contents }, you can easily persist it in file system

http({
  cache: cacheMap,
});

// in another build...
// create a new plugin instance with the same cache
http({
  cache: cacheMap,
});

A bit more details: the keys of the cache map is the url that are not resolved, which means if you're importing "https://esm.sh/react", the key will just be "https://esm.sh/react" instead of something like "https://esm.sh/[email protected]".

The schemes transformation is done before the cache lookup, so the following URLs share the same cache:

import "esm:react";
import "https://esm.sh/react";

Custom Loader

By default this plugin uses loader: "default" in the onLoad hooks of esbuild, which means it relies on the url to have a correct file extension to determine the loader. If you want to use a custom loader, you can either use the loader option in esbuild for static extensions, or use the onload option in this plugin to handle it by yourself.

http({
  onload(url, contents) {
    if (!extname(url) && is_env_node(contents)) {
      return { contents, loader: 'js' }
    }
  }
})

function is_env_node(a) {
  if (typeof a === 'string')
    return a.startsWith('#!/usr/bin/env node')
  if (Object.prototype.toString.call(a) === '[object Uint8Array]') 
    return is_env_node(new TextDecoder().decode(a.subarray(0, 20)))
}

Develop

Run npm link at the root of this repo to create a symlink in your global node_modules. This way, you can directly import this package from a global module (like the esbuild-dev usage mentioned above).

When you're done, run npm r -g @hyrious/esbuild-plugin-http to remove it from the global node_modules.

Changelog

0.1.5

Add fetchOptions and onload options.

0.1.2

Use make-fetch-happen to make HTTP cache happy.

License

MIT @ hyrious