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

@sagrimson/esinstall

v1.1.9

Published

Convert packages to ESM.

Downloads

5

Readme

esinstall

  • Convert a set of imports from your node_modules/ directory into a fresh, new 100% ESM install directory.
  • JavaScript packages are converted to ESM regardless of how they were originally authored.
  • Import your new ESM dependencies in any ESM-only environment (websites, Deno, Node.js v14).
npm install esinstall
import {install} from 'esinstall';

await install(['preact', 'preact/hooks'], {
  /*options*/
});
// Result: Creates `preact.js` and `preact/hooks.js` inside a `web_modules/` directory in your current directory.

⚡️ Powering Snowpack and the next generation of JavaScript tooling.

Status

The core install logic of this library is considered well-tested and production-ready (1+ years of active use & development!). The JS interface is new, however, and may contain smaller bugs. We'll be working to stabilize the API over the next month, with a 1.0.0 release planned for October.

Background

Before Snowpack was a frontend build tool, it was a CJS->ESM package converter. snowpack install would read your package.json "dependencies" and re-install every frontend package from your "node_modules/" directory to a new "web_modules/" directory. "web_modules/" was guarenteed to be 100% ESM regardless of how each package was originally written. This dramatically simplified the minimum tooling required to build a website by removing a whole class of problem for the frontend basically summarized as "oh no this package was written for Node.js, it will never run in the browser, what do we do???"

Snowpack is now a fully-featured frontend build tool, but it's still built entirely on that original foundation. That foundation is now esinstall, a general-purpose JavaScript interface for creating ESM single-file versions of locally installed npm packages.

Our hope is that now others can build on top of this too. And while today we're focused on the web use-case, we're seeing a growing need for a CJS->ESM story for Node.js as well.

How it Works

To simplify things a ton, here's what esinstall does internally when you run install():

  1. Resolves the given package specifiers to exact file paths in your "node_modules/" directory.
  2. Runs Rollup with those file paths as entrypoints, essentially "bundling" your dependency tree into as few JS files as possible.
  3. Writes the result to a new, fully ESM output directory.

If you check out the code, you'll see it's not as easy as it sounds. But at a high level, that's what esinstall is all about.

Features

import {install, printStats} from 'esinstall';

// Feature: Handle CJS packages with ease, converting everything to ESM!
await install(['react', 'react-dom', 'react-redux', 'react-router']);

// Feature: Handle CSS!
await install(['bootstrap/dist/css/bootstrap.min.css']);

// Feature: Handle Non-standard packages!
await install(['some-svelte-component'], {rollup: {plugins: [require('rollup-plugin-svelte')()]}});

// Feature: Print detailed install stats to the console, including installed file sizes.
const {success, stats} = install([...]);
if (success) {
  printStats(stats);
}

// Feature: Tree-shaking! Get a smaller final build by providing more detailed install targets.
await install(
  [{specifier: 'preact/hooks', all: false, default: false, namespace: false, named: ['useState', 'useEffect']}],
  {treeshake: true}
);

API

Still TODO: Adding more detailed descriptions about each InstallOptions option.

import {Plugin as RollupPlugin} from 'rollup';
import {
  DependencyStatsOutput,
  EnvVarReplacements,
  ImportMap,
  InstallTarget,
  LoggerLevel,
} from './types';

interface InstallOptions {
  cwd: string;
  alias: Record<string, string>;
  lockfile?: ImportMap;
  logger: AbstractLogger;
  verbose?: boolean;
  dest: string;
  env: EnvVarReplacements;
  treeshake?: boolean;
  polyfillNode: boolean;
  sourceMap?: boolean | 'inline';
  externalPackage: string[];
  externalPackageEsm: string[];
  packageLookupFields: string[];
  packageExportLookupFields: string[];
  namedExports: string[];
  rollup: {
    context?: string;
    plugins?: RollupPlugin[];
    dedupe?: string[];
  };
}
declare type InstallResult =
  | {
      success: false;
      importMap: null;
      stats: null;
    }
  | {
      success: true;
      importMap: ImportMap;
      stats: DependencyStatsOutput;
    };

export declare function printStats(dependencyStats: DependencyStatsOutput): string;
export declare function install(
  _installTargets: (InstallTarget | string)[],
  _options?: Partial<InstallOptions>,
): Promise<InstallResult>;

Special Thanks

A huge thanks to all the contributors of Snowpack (and now esinstall) over the years. This wouldn't have been possible without you!

Also, it can't be stressed enough: this tool would never have existed without Rollup. If you can, consider donating to their team: https://opencollective.com/rollup