Skip to content

Commit c11a0f0

Browse files
clydinalan-agius4
authored andcommittedSep 11, 2023
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.
1 parent 7176bfd commit c11a0f0

File tree

1 file changed

+16
-2
lines changed
  • packages/angular_devkit/build_angular/src/builders/dev-server

1 file changed

+16
-2
lines changed
 

‎packages/angular_devkit/build_angular/src/builders/dev-server/vite-server.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import path, { posix } from 'node:path';
1919
import type { Connect, InlineConfig, ViteDevServer } from 'vite';
2020
import { JavaScriptTransformer } from '../../tools/esbuild/javascript-transformer';
2121
import { RenderOptions, renderPage } from '../../utils/server-rendering/render-page';
22+
import { getIndexOutputFile } from '../../utils/webpack-browser-config';
2223
import { buildEsbuildBrowser } from '../browser-esbuild';
2324
import { Schema as BrowserBuilderOptions } from '../browser-esbuild/schema';
2425
import { loadProxyConfiguration } from './load-proxy-config';
@@ -74,6 +75,11 @@ export async function* serveWithVite(
7475
1,
7576
);
7677

78+
// Extract output index from options
79+
// TODO: Provide this info from the build results
80+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
81+
const htmlIndexPath = getIndexOutputFile(browserOptions.index as any);
82+
7783
// dynamically import Vite for ESM compatibility
7884
const { createServer, normalizePath } = await import('vite');
7985

@@ -88,7 +94,7 @@ export async function* serveWithVite(
8894
assert(result.outputFiles, 'Builder did not provide result files.');
8995

9096
// Analyze result files for changes
91-
analyzeResultFiles(normalizePath, result.outputFiles, generatedFiles);
97+
analyzeResultFiles(normalizePath, htmlIndexPath, result.outputFiles, generatedFiles);
9298

9399
assetFiles.clear();
94100
if (result.assetFiles) {
@@ -191,12 +197,20 @@ function handleUpdate(
191197

192198
function analyzeResultFiles(
193199
normalizePath: (id: string) => string,
200+
htmlIndexPath: string,
194201
resultFiles: OutputFile[],
195202
generatedFiles: Map<string, OutputFileRecord>,
196203
) {
197204
const seen = new Set<string>(['/index.html']);
198205
for (const file of resultFiles) {
199-
const filePath = '/' + normalizePath(file.path);
206+
let filePath;
207+
if (file.path === htmlIndexPath) {
208+
// Convert custom index output path to standard index path for dev-server usage.
209+
// This mimics the Webpack dev-server behavior.
210+
filePath = '/index.html';
211+
} else {
212+
filePath = '/' + normalizePath(file.path);
213+
}
200214
seen.add(filePath);
201215

202216
// Skip analysis of sourcemaps

0 commit comments

Comments
 (0)