# esbuild-library

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

Latest version **1.0.9** (published 2026-03-08) · ISC license · 0 weekly downloads

## Install

```sh
npm install esbuild-library
pnpm add esbuild-library
yarn add esbuild-library
bun add esbuild-library
```

## Health

**Score 55/100 (C)** — status: stable.

Positive: esm support; no vulnerabilities; has provenance.

Warnings: low downloads; no types.

## Facts

| | |
|---|---|
| Version | 1.0.9 |
| Published | 2026-03-08 |
| First published | 2023-11-01 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | ESM |
| Node | >=22.0.0 |
| Dependencies | 5 |
| Unpacked size | 13 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 0 |
| Author | D1g1talEntr0py |
| Maintainers | d1g1tal |
| Keywords | esbuild, library |

## Links

- npm: https://www.npmjs.com/package/esbuild-library
- Repository: https://github.com/D1g1talEntr0py/esbuild-library
- Homepage: https://github.com/D1g1talEntr0py/esbuild-library#readme
- Issues: https://github.com/D1g1talEntr0py/esbuild-library/issues
- npm.io page: https://npm.io/package/esbuild-library

## Dependencies (5)

- [esbuild](https://npm.io/package/esbuild.md) ^0.27.3
- [@swc/cli](https://npm.io/package/@swc/cli.md) ^0.8.0
- [@swc/core](https://npm.io/package/@swc/core.md) ^1.15.18
- [@types/node](https://npm.io/package/@types/node.md) ^25.3.5
- [esbuild-plugin-swc-minify](https://npm.io/package/esbuild-plugin-swc-minify.md) ^2.1.2

## Alternatives

- [raw-loader](https://npm.io/package/raw-loader.md) — 4.3M weekly downloads
- [plop](https://npm.io/package/plop.md) — 1.4M weekly downloads
- [webpack-deadcode-plugin](https://npm.io/package/webpack-deadcode-plugin.md) — 80.3K weekly downloads
- [@storybook/preact-vite](https://npm.io/package/@storybook/preact-vite.md) — 54.2K weekly downloads
- [vite-plugin-transform](https://npm.io/package/vite-plugin-transform.md) — 2.4K weekly downloads

## Recent versions

- 1.0.9 (latest) — 2026-03-08
- 1.0.8 — 2024-03-27
- 1.0.7 — 2024-02-24
- 1.0.6 — 2024-02-10
- 1.0.5 — 2023-12-18
- 1.0.4 — 2023-12-03
- 1.0.3 — 2023-11-21
- 1.0.2 — 2023-11-06
- 1.0.1 — 2023-11-01
- 1.0.0 — 2023-11-01

## 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.

```json
"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
```bash
# if using pnpm 😎
pnpm add -D esbuild-library

# if using npm
npm i -D esbuild-library
```

## Options

```js
/**
 * @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
```file-tree
└── src
    ├── library.js
    └── library-helper.js
├── esbuild.js
└── package.json
```
And your package.json looks like this
```json
"exports": {
  ".": "./src/library.js",
  "./dist/*": "./dist/*"
}
```
In the esbuild.js file, running the following
```js
import ESBuildLibrary from 'esbuild-library';

await ESBuildLibrary.cleanAndBuild();
```
outputs:

```file-tree
dist
├── library.js
├── library.min.js
└── library.min.js.map
```
Is roughly equivalent to this:
```js
// 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
```js
import ESBuildLibrary from 'esbuild-library';

await ESBuildLibrary.clean();

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

await ESBuildLibrary.build({ entryPoints: [ './locale' ], outDir: 'dist/locale' });
```
Output
```file-tree
└── dist
    ├── locale
    │   ├── en-us.js
    │   ├── en-gb.js
    │   └── ja.map
    ├── library.js
    ├── library.min.js
    └── library.min.js.map
```
```js
import ESBuildLibrary from 'esbuild-library';

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

Output
```file-tree
└── 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.

---
_Source: https://npm.io/package/esbuild-library · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
