Skip to content

Commit fedcc5d

Browse files
committed
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 a68ef0b)
1 parent 54e5000 commit fedcc5d

File tree

1 file changed

+15
-3
lines changed
  • packages/angular_devkit/build_angular/src/builders/dev-server

1 file changed

+15
-3
lines changed

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ export async function* serveWithVite(
5454
builderName,
5555
)) as json.JsonObject & BrowserBuilderOptions;
5656

57+
if (serverOptions.servePath === undefined && browserOptions.baseHref !== undefined) {
58+
serverOptions.servePath = browserOptions.baseHref;
59+
}
60+
5761
let server: ViteDevServer | undefined;
5862
let listeningAddress: AddressInfo | undefined;
5963
const generatedFiles = new Map<string, OutputFileRecord>();
@@ -191,6 +195,7 @@ export async function setupServer(
191195
css: {
192196
devSourcemap: true,
193197
},
198+
base: serverOptions.servePath,
194199
server: {
195200
port: serverOptions.port,
196201
strictPort: true,
@@ -247,10 +252,17 @@ export async function setupServer(
247252
// Parse the incoming request.
248253
// The base of the URL is unused but required to parse the URL.
249254
const parsedUrl = new URL(req.url, 'http://localhost');
250-
const extension = path.extname(parsedUrl.pathname);
255+
let pathname = parsedUrl.pathname;
256+
if (serverOptions.servePath && pathname.startsWith(serverOptions.servePath)) {
257+
pathname = pathname.slice(serverOptions.servePath.length);
258+
if (pathname[0] !== '/') {
259+
pathname = '/' + pathname;
260+
}
261+
}
262+
const extension = path.extname(pathname);
251263

252264
// Rewrite all build assets to a vite raw fs URL
253-
const assetSourcePath = assets.get(parsedUrl.pathname);
265+
const assetSourcePath = assets.get(pathname);
254266
if (assetSourcePath !== undefined) {
255267
req.url = `/@fs/${assetSourcePath}`;
256268
next();
@@ -262,7 +274,7 @@ export async function setupServer(
262274
// Global stylesheets (CSS files) are currently considered resources to workaround
263275
// dev server sourcemap issues with stylesheets.
264276
if (extension !== '.html') {
265-
const outputFile = outputFiles.get(parsedUrl.pathname);
277+
const outputFile = outputFiles.get(pathname);
266278
if (outputFile) {
267279
const mimeType = lookupMimeType(extension);
268280
if (mimeType) {

0 commit comments

Comments
 (0)