1.0.8 ā€¢ Published 1 month ago

esbuild-library v1.0.8

Weekly downloads
-
License
ISC
Repository
github
Last release
1 month ago

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.

1.0.8

1 month ago

1.0.7

2 months ago

1.0.6

3 months ago

1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago