From c11a0f0d36f6cbffdf0464135510bda454efb08b Mon Sep 17 00:00:00 2001 From: Charles Lyding <19598772+clydin@users.noreply.github.com> Date: Fri, 8 Sep 2023 11:24:24 -0400 Subject: [PATCH] fix(@angular-devkit/build-angular): support custom index option paths in Vite-based dev server When using the Vite-based development server and a custom `index` build option (not `index.html`), the custom index path will now be used as the root of the development server. This mimics the behavior of the Webpack-based development server. --- .../src/builders/dev-server/vite-server.ts | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 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 d5917db82ccd..5f829f1ae232 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 @@ -19,6 +19,7 @@ import path, { posix } from 'node:path'; import type { Connect, InlineConfig, ViteDevServer } from 'vite'; import { JavaScriptTransformer } from '../../tools/esbuild/javascript-transformer'; import { RenderOptions, renderPage } from '../../utils/server-rendering/render-page'; +import { getIndexOutputFile } from '../../utils/webpack-browser-config'; import { buildEsbuildBrowser } from '../browser-esbuild'; import { Schema as BrowserBuilderOptions } from '../browser-esbuild/schema'; import { loadProxyConfiguration } from './load-proxy-config'; @@ -74,6 +75,11 @@ export async function* serveWithVite( 1, ); + // Extract output index from options + // TODO: Provide this info from the build results + // eslint-disable-next-line @typescript-eslint/no-explicit-any + const htmlIndexPath = getIndexOutputFile(browserOptions.index as any); + // dynamically import Vite for ESM compatibility const { createServer, normalizePath } = await import('vite'); @@ -88,7 +94,7 @@ export async function* serveWithVite( assert(result.outputFiles, 'Builder did not provide result files.'); // Analyze result files for changes - analyzeResultFiles(normalizePath, result.outputFiles, generatedFiles); + analyzeResultFiles(normalizePath, htmlIndexPath, result.outputFiles, generatedFiles); assetFiles.clear(); if (result.assetFiles) { @@ -191,12 +197,20 @@ function handleUpdate( function analyzeResultFiles( normalizePath: (id: string) => string, + htmlIndexPath: string, resultFiles: OutputFile[], generatedFiles: Map, ) { const seen = new Set(['/index.html']); for (const file of resultFiles) { - const filePath = '/' + normalizePath(file.path); + let filePath; + if (file.path === htmlIndexPath) { + // Convert custom index output path to standard index path for dev-server usage. + // This mimics the Webpack dev-server behavior. + filePath = '/index.html'; + } else { + filePath = '/' + normalizePath(file.path); + } seen.add(filePath); // Skip analysis of sourcemaps