Skip to content

Commit

Permalink
fix(@angular-devkit/build-angular): support custom index option paths…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
clydin authored and alan-agius4 committed Sep 11, 2023
1 parent 7176bfd commit c11a0f0
Showing 1 changed file with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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');

Expand All @@ -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) {
Expand Down Expand Up @@ -191,12 +197,20 @@ function handleUpdate(

function analyzeResultFiles(
normalizePath: (id: string) => string,
htmlIndexPath: string,
resultFiles: OutputFile[],
generatedFiles: Map<string, OutputFileRecord>,
) {
const seen = new Set<string>(['/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
Expand Down

0 comments on commit c11a0f0

Please sign in to comment.