esbuild-library v1.0.8
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.jsonfile with an"exports"field configured something like this. The builder will look in thepackage.jsonfile for the"exports"field and use the"."entry for the./src/library.jsfile as the entry point for the build if you don't specifyentryPointsin the options. If you do specifyentryPointsin 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
srcfolder in your root folder of your project. If you don't provideentryPointsin the options, and you don't have an entry of"."in the"exports"field in thepackage.jsonfile, thenentryPointswill 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 theentryPointsarray, this is the behavior you will get. - If you don't provide
outDirin the options, thenoutdirwill 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-libraryOptions
/**
* @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.jsonAnd 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.mapIs 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.mapimport 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.mapI 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.