# Bundle a VS Code Extension with esbuild

Yes. An agent replaces a plain tsc build that ships node_modules in the .vsix with one esbuild bundle, fixes .vscodeignore, keeps the tests running and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-esbuild-bundling/vscode-extension/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent starts by reading how the extension is compiled and packaged today, because bundling changes both. In an extension shaped like formulahendry/vscode-code-runner, `package.json` names the publisher `formulahendry`, version `0.12.2` and `engines.vscode` of `^1.56.0`; `main` points at `./out/src/extension`, `vscode:prepublish` is `tsc -p ./`, `compile` is `tsc -watch -p ./` and `tslint` runs TSLint 5 over `src/**/*.ts`. `tsconfig.json` compiles with `module: commonjs`, `target: es6`, `rootDir: "."` and `outDir: out`, which is why the entry file lands at `out/src/extension.js` and the Mocha tests at `out/test/`. The runtime dependencies are `applicationinsights` 0.19, `micromatch` 4 and `tree-kill` 1.2; the dev side is TypeScript 3.8, Mocha 6 and `@types/node` 6. `.vscodeignore` drops `.vscode/**`, `typings/**`, `out/test/**`, `test/**`, `src/**`, source maps and `tsconfig.json`, and nothing else: `node_modules/` is not listed, so every install of the extension carries the whole dependency tree, including `applicationinsights` and everything under it.

It then reads the source for anything a bundler needs to know about. `src/extension.ts` registers four commands, `code-runner.run`, `runCustomCommand`, `runByLanguage` and `stop`, and hands them to a `CodeManager`; `src/codeManager.ts` imports `fs`, `os`, `path`, `micromatch` and `vscode` at the top but calls `require("tree-kill")` inside the stop path and `require("child_process").spawn` inside the run path, both with literal strings, which esbuild follows and bundles or treats as Node built-ins. `src/appInsightsClient.ts` uses the `import appInsights = require("applicationinsights")` form and calls `getClient` with the instrumentation key. `syntaxes/code-runner-output.tmLanguage` is a contributed grammar that must stay in the package, `images/` holds the icon and README pictures, and `.gitignore` already ignores `out`, `node_modules` and `*.vsix`.

The change is a single bundle and the scripts around it. The agent adds `esbuild` as a dev dependency and an `esbuild.js` that bundles `src/extension.ts` into `dist/extension.js` with `platform: "node"`, `format: "cjs"`, `external: ["vscode"]`, source maps in development and `minify` for the package build. `main` becomes `./dist/extension.js`, `vscode:prepublish` runs the production bundle, `compile` and `watch` run esbuild, and a new `compile-tests` keeps `tsc -p ./` so Mocha can still run from `out/test/`. `.vscodeignore` gains `node_modules/**`, `out/**`, `esbuild.js` and `**/*.map`, and `dist/` is added to `.gitignore` beside `out`. If esbuild warns that `applicationinsights` 0.19 loads modules with computed `require` calls it cannot follow, the agent keeps that one package external, excludes everything in `node_modules` except it, and says so in the pull request rather than shipping a bundle that fails at runtime.

Then it proves the package is smaller and still works. It runs the bundle, `npx @vscode/vsce ls` before and after to show which files leave the package, `npx @vscode/vsce package` to produce a `.vsix`, and `npm run tslint` and `compile-tests` so the existing checks still pass. It requires `dist/extension.js` in Node with a stub `vscode` module so every top-level import resolves, and reads the bundle for the `tree-kill` code and the `child_process` require to show the dynamic paths were bundled or left as built-ins rather than dropped. The CI workflow `.github/workflows/main.yml`, which runs `npm run vscode:prepublish` and `npm run tslint` on three operating systems, is updated only where the script names changed.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Bundle this VS Code extension with esbuild. Read package.json, tsconfig.json, .vscodeignore, .gitignore, every file under src and test, and the workflow under .github/workflows first. Add esbuild as a dev dependency, write esbuild.js that bundles src/extension.ts to dist/extension.js for Node in CommonJS format with vscode external, source maps in development and minification for packaging; point main at dist/extension.js; make vscode:prepublish run the production bundle, keep a compile-tests script that runs tsc for the Mocha tests, and update .vscodeignore to exclude node_modules, out, src and the build script while keeping syntaxes and images. Watch for dynamic require calls and modules esbuild cannot bundle and keep those external with their node_modules entries included. Run the bundle, vsce ls before and after, vsce package, tslint and the test compile, require the bundle in Node with a stub vscode module, and open a pull request with the file list difference and every warning esbuild printed.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request that adds `esbuild.js`, changes `package.json` (`main`, the scripts and one new dev dependency), extends `.vscodeignore` and `.gitignore`, and touches `.github/workflows/main.yml` only where a script was renamed. The description shows the `vsce ls` output before and after, so a reviewer sees the dependency tree leave the package and `dist/extension.js` arrive, and quotes any esbuild warning together with what was done about it.

The run's record keeps the commands themselves: the bundle, `vsce package`, TSLint, the test compile, and the Node smoke load of the bundle with its stub `vscode` module. A reviewer can read the `.vsix` contents from the log without building anything locally.

Before merging, a reviewer installs the `.vsix` from the pull request on their own machine and runs the four commands, checks that telemetry still initialises if `applicationinsights` was kept external, and decides whether to use this change to move the `tslint` script to ESLint, which the pull request leaves alone.

## Run it on every extension that still ships node_modules

A maintainer hands this off from the terminal: one `POST /o/{org}/sessions` with this body and the repository in `resources`. The run reads the real `.vscodeignore` and the real `require` calls, so the same prompt fits any extension that compiles with `tsc` alone. For a publisher with several extensions, one request per repository with a `request_id` that is a UUID v5 of the repository path makes each call safe to retry, and the key's `webhook_url` reports every finished run with its pull request link.

The sandbox parks when the turn ends, so a follow-up such as `also switch the tests to @vscode/test-electron` wakes it with esbuild installed and the bundle already proven.

## Why run it on Rush?

Bundling an extension is mostly finding out what breaks: the one dynamic `require`, the dependency with computed imports, the grammar file that must survive `.vscodeignore`. The agent finds those by reading the source and by running the bundle, the packager and the extension itself in its own sandbox, and the pull request arrives with the warnings already read and answered instead of waiting to be discovered after publishing.

The before-and-after `vsce ls` listing is the proof a reviewer wants: not a promise that the package is smaller, but the list of files that stopped shipping. Every command that produced it is in the run's record.

## Frequently asked questions

### Will the Mocha tests still run?

Yes. The tests compile with `tsc` through the new `compile-tests` script and run from `out/test/` as before. Only the published entry point moves to `dist/extension.js`; `out/` stays a development and test directory and is excluded from the package.

### What about the dynamic require of tree-kill?

esbuild follows `require` calls whose argument is a string literal, so `require("tree-kill")` and `require("child_process")` are bundled or marked as Node built-ins without a change. The agent still reads the bundle for both and loads it in Node, because checking is cheaper than reasoning about it, and the reviewer runs the stop command in a real VS Code before merging.

### What if a dependency cannot be bundled?

Then it stays external and its `node_modules` entry is kept in the package, with `.vscodeignore` excluding everything else. The pull request names the package and the esbuild warning that caused it, so the reviewer can decide whether to replace the dependency later.

### Does the minimum VS Code version change?

No. The bundle is CommonJS for Node, the same shape `tsc` produced, and `engines.vscode` stays at `^1.56.0`. Bundling changes how the code is packaged, not which API it calls.

## More VS Code Extension jobs

- [Add User Settings to a VS Code Extension](https://getrush.ai/run/add-extension-settings/vscode-extension/)
- [Set Up GitHub Actions CI for a VS Code Extension](https://getrush.ai/run/add-github-actions-ci/vscode-extension/)
- [Localize a VS Code Extension](https://getrush.ai/run/add-i18n/vscode-extension/)

## More jobs like this: Hand off a long task

- [Add ESLint to a Chrome Extension](https://getrush.ai/run/add-eslint/chrome-extension/)
- [Add ESLint to a Node.js App](https://getrush.ai/run/add-eslint/nodejs/)
- [Add ESLint to a SolidJS App](https://getrush.ai/run/add-eslint/solidjs/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
