From fedcc5d923b7237622b1e7adef053a2ee68f872e Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Thu, 4 May 2023 13:57:52 -0400 Subject: [PATCH] fix(@angular-devkit/build-angular): properly set base dev-server path with esbuild When using the esbuild-based browser application builder with the development server, the `baseHref` build option will now be properly propagated to the underlying Vite server. (cherry picked from commit a68ef0bbb43aea4749f9855abfe0877164f82cd0) --- .../src/builders/dev-server/vite-server.ts | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/packages/angular_devkit/build_angular/src/builders/dev-server/vite-server.ts b/packages/angular_devkit/build_angular/src/builders/dev-server/vite-server.ts index da9b3307f8b8..4cbbc7b5916b 100644 --- a/packages/angular_devkit/build_angular/src/builders/dev-server/vite-server.ts +++ b/packages/angular_devkit/build_angular/src/builders/dev-server/vite-server.ts @@ -54,6 +54,10 @@ export async function* serveWithVite( builderName, )) as json.JsonObject & BrowserBuilderOptions; + if (serverOptions.servePath === undefined && browserOptions.baseHref !== undefined) { + serverOptions.servePath = browserOptions.baseHref; + } + let server: ViteDevServer | undefined; let listeningAddress: AddressInfo | undefined; const generatedFiles = new Map(); @@ -191,6 +195,7 @@ export async function setupServer( css: { devSourcemap: true, }, + base: serverOptions.servePath, server: { port: serverOptions.port, strictPort: true, @@ -247,10 +252,17 @@ export async function setupServer( // Parse the incoming request. // The base of the URL is unused but required to parse the URL. const parsedUrl = new URL(req.url, 'http://localhost'); - const extension = path.extname(parsedUrl.pathname); + let pathname = parsedUrl.pathname; + if (serverOptions.servePath && pathname.startsWith(serverOptions.servePath)) { + pathname = pathname.slice(serverOptions.servePath.length); + if (pathname[0] !== '/') { + pathname = '/' + pathname; + } + } + const extension = path.extname(pathname); // Rewrite all build assets to a vite raw fs URL - const assetSourcePath = assets.get(parsedUrl.pathname); + const assetSourcePath = assets.get(pathname); if (assetSourcePath !== undefined) { req.url = `/@fs/${assetSourcePath}`; next(); @@ -262,7 +274,7 @@ export async function setupServer( // Global stylesheets (CSS files) are currently considered resources to workaround // dev server sourcemap issues with stylesheets. if (extension !== '.html') { - const outputFile = outputFiles.get(parsedUrl.pathname); + const outputFile = outputFiles.get(pathname); if (outputFile) { const mimeType = lookupMimeType(extension); if (mimeType) {