sourcegraph/client/web/dev/esbuild/server.ts
Quinn Slack de613e92b6
use esbuild for client/web builds (#57365)
Use [esbuild](https://esbuild.github.io/) instead of Webpack for builds of `client/web`, for faster builds (dev and prod) and greater dev-prod parity. This PR completely removes all use of Webpack in this repository.

`client/web` is the last build target that still uses Webpack; all others have been recently migrated to esbuild. Most devs here have been using esbuild for local dev of `client/web` for the last 6-12 months anyway. The change here is that now our production builds will be built by esbuild.

All sg commands, integration/e2e tests, etc., continue to work as-is. The bundlesize report will take a while to stabilize because the new build products use different filenames.

## Benchmarks

Running `pnpm run generate && time pnpm -C client/web run task:gulp webBuild` and taking the `time` output from the last command:

- Webpack: 62.5s
- esbuild: 6.7s

Note: This understates esbuild's victory for 2 reasons: (1) because esbuild is building both the main and embed entrypoints, whereas Webpack only builds the main entrypoint in this benchmark) and (2) because a lot of it is in the fixed startup time of `gulp`; esbuild incremental rebuilds during local dev only take ~1s.

## Notes

We no longer use Babel to produce web builds (we use esbuild), so we don't need any Babel plugins that optimize the output or improve browser compatibility. Right now, Babel is only used by Jest (for tests) and by Bazel as an intermediate step.
2023-10-23 10:59:06 -07:00

70 lines
2.6 KiB
TypeScript

import path from 'path'
import { context as esbuildContext } from 'esbuild'
import express from 'express'
import { createProxyMiddleware } from 'http-proxy-middleware'
import signale from 'signale'
import { STATIC_ASSETS_PATH } from '@sourcegraph/build-config'
import { buildMonaco } from '@sourcegraph/build-config/src/esbuild/monacoPlugin'
import { ENVIRONMENT_CONFIG, HTTPS_WEB_SERVER_URL, printSuccessBanner } from '../utils'
import { BUILD_OPTIONS } from './build'
import { assetPathPrefix } from './manifest'
export const esbuildDevelopmentServer = async (
listenAddress: { host: string; port: number },
configureProxy: (app: express.Application) => void
): Promise<void> => {
const start = performance.now()
// One-time build (these files only change when the monaco-editor npm package is changed, which
// is rare enough to ignore here).
if (!ENVIRONMENT_CONFIG.DEV_WEB_BUILDER_OMIT_SLOW_DEPS) {
const ctx = await buildMonaco(STATIC_ASSETS_PATH)
await ctx.rebuild()
await ctx.dispose()
}
const ctx = await esbuildContext(BUILD_OPTIONS)
await ctx.watch()
// Start esbuild's server on a random local port.
const { host: esbuildHost, port: esbuildPort } = await ctx.serve({
host: 'localhost',
servedir: STATIC_ASSETS_PATH,
})
// Start a proxy at :3080. Asset requests (underneath /.assets/) go to esbuild; all other
// requests go to the upstream.
const proxyApp = express()
proxyApp.use(
assetPathPrefix,
createProxyMiddleware({
target: { protocol: 'http:', host: esbuildHost, port: esbuildPort },
pathRewrite: { [`^${assetPathPrefix}`]: '' },
onProxyRes: (_proxyResponse, request, response) => {
// Cache chunks because their filename includes a hash of the content.
const isCacheableChunk = path.basename(request.url).startsWith('chunk-')
if (isCacheableChunk) {
response.setHeader('Cache-Control', 'max-age=3600')
}
},
logLevel: 'error',
})
)
configureProxy(proxyApp)
const proxyServer = proxyApp.listen(listenAddress)
// eslint-disable-next-line @typescript-eslint/return-await
return await new Promise<void>((_resolve, reject) => {
proxyServer.once('listening', () => {
signale.success(`esbuild server is ready after ${Math.round(performance.now() - start)}ms`)
printSuccessBanner(['✱ Sourcegraph is really ready now!', `Click here: ${HTTPS_WEB_SERVER_URL}`])
})
proxyServer.once('error', error => reject(error))
})
}