build-worker icon indicating copy to clipboard operation
build-worker copied to clipboard

Build your Cloudflare Workers with esbuild.

build-worker

Bundle your Cloudflare Worker with esbuild instead of webpack. (It's ridiculously faster!)

Wrangler v1 uses webpack.
Wrangler v2 is using esbuild.

Problem: Wrangler 2 is not production-ready.
Solution: build-worker.

Usage

  1. Install.
    npm install build-worker
    
  2. Add a package.json script.
    // package.json
    {
      scripts: {
        'build:worker': 'build-worker --entry worker.js --out dist/worker.js --debug'
      }
    }
    

    The --debug flag disables minification, making debugging much easier.

  3. Run your script.
    npm run build:worker
    

It can also be used programmatically:

import { buildWorker } from 'build-worker'
await buildWorker({ entry: 'worker.js' , out: 'dist/worker.js', debug: false })

IS_CLOUDFLARE_WORKER

build-worker replaces any occurrences of IS_CLOUDFLARE_WORKER with true, which your code can use to determine whether its being run in a Cloudflare Worker:

if (IS_CLOUDFLARE_WORKER !== 'undefined' && IS_CLOUDFLARE_WORKER === true) {
  // Do something that should only happen in Cloudflare Workers
} else {
  // Do something that should not happen in Cloudflare Workers
}

How it works

build-worker uses the same esbuild configuration than Wrangler 2.

See https://github.com/evanw/esbuild/issues/1189.