Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import assert from 'node:assert';
import { readFile } from 'node:fs/promises';
import path from 'node:path';
import type { VitestPlugin } from 'vitest/node';
import { createBuildAssetsMiddleware } from '../../../../tools/vite/middlewares/assets-middleware';
import { toPosixPath } from '../../../../utils/path';
import type { ResultFile } from '../../../application/results';
import type { NormalizedUnitTestBuilderOptions } from '../../options';
Expand Down Expand Up @@ -129,6 +130,11 @@ export function createVitestPlugins(
};
}
},
configureServer: (server) => {
server.middlewares.use(
createBuildAssetsMiddleware(server.config.base, buildResultFiles),
);
},
},
{
name: 'angular:html-index',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { readFileSync } from 'node:fs';
import type { ServerResponse } from 'node:http';
import { extname } from 'node:path';
import type { Connect, ViteDevServer } from 'vite';
import { ResultFile } from '../../../builders/application/results';
import { AngularMemoryOutputFiles, AngularOutputAssets, pathnameWithoutBasePath } from '../utils';

export interface ComponentStyleRecord {
Expand Down Expand Up @@ -214,3 +215,45 @@ function checkAndHandleEtag(

return false;
}

export function createBuildAssetsMiddleware(
basePath: string,
buildResultFiles: ReadonlyMap<string, ResultFile>,
readHandler: (path: string) => Buffer = readFileSync,
): Connect.NextHandleFunction {
return function buildAssetsMiddleware(req, res, next) {
if (req.url === undefined || res.writableEnded) {
return;
}

// Parse the incoming request.
// The base of the URL is unused but required to parse the URL.
const pathname = pathnameWithoutBasePath(req.url, basePath);
const extension = extname(pathname);
if (extension && !/\.[mc]?[jt]s(?:\.map)?$/.test(extension)) {
const outputFile = buildResultFiles.get(pathname.slice(1));
if (outputFile) {
const contents =
outputFile.origin === 'memory' ? outputFile.contents : readHandler(outputFile.inputPath);

const etag = `W/${createHash('sha256').update(contents).digest('hex')}`;
if (checkAndHandleEtag(req, res, etag)) {
return;
}

const mimeType = lookupMimeType(extension);
if (mimeType) {
res.setHeader('Content-Type', mimeType);
}

res.setHeader('ETag', etag);
res.setHeader('Cache-Control', 'no-cache');
res.end(contents);

return;
}
}

next();
};
}