Gulp Esbuild Plugin
The `gulp-esbuild` package provides a Gulp plugin that integrates the high-performance `esbuild` bundler into Gulp-based build workflows. It allows developers to efficiently bundle, minify, and transform JavaScript and TypeScript assets as part of their Gulp pipelines, leveraging `esbuild`'s speed. The current stable version is 0.14.1, and the package frequently releases updates, often in response to breaking changes in the upstream `esbuild` library. A key feature is the ability to enable `esbuild`'s incremental build mode via the `createGulpEsbuild` factory function, which significantly speeds up rebuilds during development when combined with Gulp's `watch` API. Unlike direct `esbuild` usage, `gulp-esbuild` is designed to work with Gulp's virtual file streams, but it has a specific limitation: all input files specified in `gulp.src()` must physically exist on the filesystem, even if their contents are subsequently modified by earlier Gulp pipeline steps.
Common errors
-
Error: Cannot find module 'esbuild'
cause The 'esbuild' package is a peer dependency of 'gulp-esbuild' and was not installed alongside it.fixInstall esbuild alongside gulp-esbuild: `npm install esbuild gulp-esbuild` or `yarn add esbuild gulp-esbuild`. -
Error: Unknown option: 'pipe'
cause The 'pipe' option was removed from `gulp-esbuild` in v0.13.0.fixRemove the `{ pipe: true }` option from your `createGulpEsbuild` configuration. The plugin now handles both file system and virtual files by default. -
esbuild: Could not resolve './some-virtual-file.js' (or similar file not found error)
cause The input file passed to `gulp-esbuild` via `gulp.src()` does not physically exist on the file system, which is a requirement for `esbuild`'s operation even when processing virtual file contents.fixEnsure that all files processed by `gulp-esbuild` (i.e., those passed into `gulp.src()`) have a corresponding physical file on disk. If generating content virtually, write it to a temporary file first.
Warnings
- breaking Since v0.14.0, `esbuild` is a peer dependency of `gulp-esbuild` and must be installed separately. Failing to do so will result in module not found errors during runtime.
- breaking The `pipe` flag, previously used to enable processing of virtual files, was removed in v0.13.0. The plugin now automatically handles both file system files and virtual files by default.
- gotcha When processing files with `src(...).pipe(gulpEsbuild(...))`, every file passed to `src` must physically exist on the file system, even if its content is overridden by Gulp's virtual file stream. This is a limitation stemming from `esbuild`'s architecture.
- breaking Major releases of `esbuild` frequently introduce breaking changes to its API. `gulp-esbuild` often reflects these changes, requiring updates to `esbuild` options or configuration in your `gulpfile`.
- gotcha To enable `esbuild`'s incremental build feature for faster rebuilds during development (e.g., with `gulp.watch`), you must use the `createGulpEsbuild` factory function with `{ incremental: true }`. The default `gulpEsbuild` export does not support incremental builds.
Install
-
npm install gulp-esbuild -
yarn add gulp-esbuild -
pnpm add gulp-esbuild
Imports
- gulpEsbuild
import { gulpEsbuild } from 'gulp-esbuild'; const gulpEsbuild = require('gulp-esbuild').gulpEsbuild;import gulpEsbuild from 'gulp-esbuild';
- createGulpEsbuild
import createGulpEsbuild from 'gulp-esbuild'; const createGulpEsbuild = require('gulp-esbuild');import { createGulpEsbuild } from 'gulp-esbuild'; - BuildOptions
import type { BuildOptions } from 'gulp-esbuild';import type { BuildOptions } from 'esbuild';
Quickstart
import { src, dest } from 'gulp';
import gulpEsbuild from 'gulp-esbuild';
/**
* Gulp task to build a TypeScript application using gulp-esbuild.
* It bundles 'index.tsx', configures esbuild for bundling and TSX loading,
* and outputs the result to 'dist/bundle.js'.
*/
function build() {
return src('./index.tsx')
.pipe(gulpEsbuild({
outfile: 'bundle.js',
bundle: true,
loader: {
'.tsx': 'tsx'
},
minify: true,
sourcemap: 'external'
}))
.pipe(dest('./dist'));
}
export { build };
// To run: `npx gulp build` (assuming 'gulp' is installed globally or via npx)