Serverless architecture, particularly with services like AWS Lambda, has revolutionized how we build and deploy applications. It offers incredible scalability and cost-efficiency by abstracting away the underlying infrastructure. However, this new paradigm introduces unique challenges, especially when it comes to traditional development workflows like building, bundling, and transpiling JavaScript.
How do you run a build process in an environment designed to be ephemeral and lightweight? Do you bundle everything in a complex CI/CD pipeline before deployment? Or do you try to shoehorn heavy build tools into your Lambda package, inflating its size and cold-start times?
There's a better way. Enter esbuild.do, a powerful API that exposes the blazing-fast capabilities of the esbuild bundler without the setup. By externalizing your build process into a simple API call, you can create lean, powerful, and dynamic serverless functions.
In this post, we'll walk through how to integrate esbuild.do with AWS Lambda to create a function that can transpile and bundle TypeScript on the fly.
Before we dive in, let's clarify the "why." The esbuild tool itself is renowned for its incredible speed, being written in Go. So why not just run it directly?
In a serverless context, using a build tool as an API via esbuild.do offers several distinct advantages:
Let's build a practical example: an AWS Lambda function that accepts a snippet of TypeScript code, uses esbuild.do to bundle and minify it into modern JavaScript, and returns the result.
On your local machine, create a new project folder, initialize a Node.js project, and install the @do-inc/sdk. This SDK is the only dependency you'll need to communicate with the esbuild.do API.
mkdir lambda-transpiler
cd lambda-transpiler
npm init -y
npm install @do-inc/sdk
Now for the core logic. Create a file named index.js in your project folder. This code will define your Lambda handler, which receives the incoming request, calls the esbuild.do agent, and returns the bundled code.
// index.js
import { doing } from '@do-inc/sdk';
export const handler = async (event) => {
try {
// 1. Extract the TypeScript code from the incoming request body.
// We expect a JSON object like: { "sourceCode": "const msg: string = '...';" }
const { sourceCode } = JSON.parse(event.body);
if (!sourceCode) {
return {
statusCode: 400,
body: JSON.stringify({ error: 'sourceCode is required in the request body.' }),
};
}
// 2. Call the esbuild.do agent via the SDK.
// This harnesses the power of esbuild through a simple agentic workflow.
const { bundledCode, error } = await doing.esbuild({
entryPoints: ['index.ts'],
sourceFiles: {
'index.ts': sourceCode // Pass the dynamic user code here.
},
bundle: true, // Bundle all imports into one file.
minify: true, // Minify the output for production.
platform: 'node', // Target the Node.js environment.
format: 'esm' // Output as an ES Module.
});
if (error) {
// If esbuild encounters a syntax error, it will be returned here.
throw new Error(error);
}
// 3. Return the successfully bundled and minified code.
return {
statusCode: 200,
headers: {
'Content-Type': 'application/javascript',
},
body: bundledCode,
};
} catch (err) {
console.error('Error processing request:', err);
return {
statusCode: 500,
body: JSON.stringify({ error: err.message }),
};
}
};
This handler is lightweight and focused. Its only job is to orchestrate the API call. All the heavy lifting of parsing, transpiling, and minifying is handled securely and remotely by esbuild.do.
Package Your Code: Create a ZIP file containing index.js, package.json, and the node_modules directory.
zip -r deployment.zip .
Upload to Lambda: In the AWS Lambda console for your function, find the Code source section and click Upload from. Select .zip file and upload your deployment.zip.
Configure a Test Event:
{
"body": "{\"sourceCode\": \"const message: string = 'Hello from a dynamically bundled Lambda!'; console.log(message);\"}"
}
Run the Test: Click the Test button.
If everything is configured correctly, you should see a successful execution. The response body will contain the clean, minified JavaScript output from esbuild.do:
// Expected Response Body
"console.log(\"Hello from a dynamically bundled Lambda!\");"
You've just created a serverless function that performs a complex build task without any local build tools. By decoupling the build process from the execution environment, esbuild.do opens up a world of possibilities:
Ready to supercharge your serverless builds? Visit esbuild.do to explore the documentation and start building faster, lighter, and more dynamic applications today.
Q: What is esbuild.do?
A: esbuild.do is an agentic workflow that provides the capabilities of the esbuild JavaScript bundler and minifier through a simple, on-demand REST API, eliminating the need for local installation or complex build configurations.
Q: Why use esbuild.do instead of the esbuild CLI in my CI/CD pipeline?
A: Use esbuild.do in serverless functions, CI/CD pipelines, or any environment where you want to avoid managing local Node.js dependencies. It's perfect for dynamic code transpilation, playground environments, or simplifying build steps where a full Node setup is overkill.
Q: Is it secure to send my source code to the API?
A: Absolutely. Each API call runs in a secure, isolated sandbox. Your source code is used only for the duration of the bundling process and is not stored or logged.