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

esbuild-library

v1.0.8

Published

Creates script files for both ES modules and global variables. No CommonJS here!

Downloads

20

Readme

esbuild-library

esbuild utility class to build libraries.

Purpose

I know, I know. Why create a library just to remove some boiler-plate code? Well, I'm lazy and I don't want to write the same code over and over again. I also want to be able build different projects the same way. So, I created this library to do just that.

Features

In order to remove boiler-plate code, this library favors convention over configuration.

This library will look for the following:

  • A package.json file with an "exports" field configured something like this. The builder will look in the package.json file for the "exports" field and use the "." entry for the ./src/library.js file as the entry point for the build if you don't specify entryPoints in the options. If you do specify entryPoints in the options, it will use that instead. I did this, because for simple libraries where a single script is what most users would import.
"exports": {
  ".": "./src/library.js",
  "./dist/*": "./dist/*"
},
  • A src folder in your root folder of your project. If you don't provide entryPoints in the options, and you don't have an entry of "." in the "exports" field in the package.json file, then entryPoints will default to ['./src'] and will iterate each script in the folder and create a esmodule and minified copy. Note: If you provide an directory as an entry in the entryPoints array, this is the behavior you will get.
  • If you don't provide outDir in the options, then outdir will default to ./dist.

and does the following:

  • Clean dist folder
  • Build esmodules with esbuild
  • Optionally creates iife scripts that exposes the library to the global scope using globalThis
  • Minify with swc

Install

# if using pnpm 😎
pnpm add -D esbuild-library

# if using npm
npm i -D esbuild-library

Options

/**
 * @typedef {object} ESBuildLibraryOptions
 * @property {string[]} [entryPoints=['./src']] The entry points
 * @property {string} [outFile] The output file
 * @property {string} [outDir='dist'] The output directory
 * @property {number} [ecma=2022] The ecma version for minification using swc
 * @property {boolean} [iife=false] Whether to build an iife version with a global variable
 * @property {string} [logLevel='info'] The log level
 * @see https://esbuild.github.io/api/#log-levels
 * @see https://esbuild.github.io/api/#build-api
 * @see https://esbuild.github.io/api/#build-options
 */

const esBuildLibraryOptions = {
  entryPoints: [ './src/index.js' ],
  outFile: './dist/index.js',
  outDir: './dist',
  ecma: 2022,
  iife: true,
  logLevel: 'debug'
};

Usage example

If you have the following file structure

└── src
    ├── library.js
    └── library-helper.js
├── esbuild.js
└── package.json

And your package.json looks like this

"exports": {
  ".": "./src/library.js",
  "./dist/*": "./dist/*"
}

In the esbuild.js file, running the following

import ESBuildLibrary from 'esbuild-library';

await ESBuildLibrary.cleanAndBuild();

outputs:

dist
├── library.js
├── library.min.js
└── library.min.js.map

Is roughly equivalent to this:

// instead of this - Don't forget to add rimfaf to your devDependencies to clean your /dist folder first.

import * as esbuild from 'esbuild';
import swcMinify from 'esbuild-plugin-swc-minify';

await esbuild.build({
  entryPoints: [ './src/library.js' ],
  outfile: './dist/library.js',
  bundle: true,
  format: 'esm',
  logLevel: 'info'
});

await esbuild.build({
  entryPoints: [ './dist/library.js' ],
  outdir: './dist',
  minify: true,
  sourceMap: true,
  module: true,
  logLevel: 'info'
  plugins: [ swcMinify({ ecma: 2022 }) ]
});

You can also do this is you have subfolders in /dist for additional output

import ESBuildLibrary from 'esbuild-library';

await ESBuildLibrary.clean();

await ESBuildLibrary.build({ entryPoints: [ './src/library.js' ] });

await ESBuildLibrary.build({ entryPoints: [ './locale' ], outDir: 'dist/locale' });

Output

└── dist
    ├── locale
    │   ├── en-us.js
    │   ├── en-gb.js
    │   └── ja.map
    ├── library.js
    ├── library.min.js
    └── library.min.js.map
import ESBuildLibrary from 'esbuild-library';

await ESBuildLibrary.cleanAndBuild({ iife: true });

Output

└── dist
    ├── iife
    │   ├── library.js
    │   ├── library.min.js
    │   └── library.min.js.map
    ├── library.js
    ├── library.min.js
    └── library.min.js.map

I know, this is life changing... 😂 No one is going to use this anyway, but I need the README practice because clearly I'm not good at documentation.