Skip to content

fix(@angular/build): update vite to 7.0.6 #30781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2025
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
2 changes: 1 addition & 1 deletion packages/angular/build/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"semver": "7.7.2",
"source-map-support": "0.5.21",
"tinyglobby": "0.2.14",
"vite": "7.0.0",
"vite": "7.0.6",
"watchpack": "2.4.4"
},
"optionalDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
*/

import { lookup as lookupMimeType } from 'mrmime';
import { createHash } from 'node:crypto';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unrelated to the version bump / upstream fix for the source map issue. At least #30770 appears to pass CI without further changes. Can you add some context to the PR description?

Copy link
Collaborator Author

@alan-agius4 alan-agius4 Jul 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because it was needed to bump to vite 7.0.5.

See: #30758

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha, so effectively this is a revert. Thanks!

import { readFileSync } from 'node:fs';
import type { ServerResponse } from 'node:http';
import { extname } from 'node:path';
import type { Connect, ViteDevServer } from 'vite';
import { AngularMemoryOutputFiles, AngularOutputAssets, pathnameWithoutBasePath } from '../utils';
Expand All @@ -17,6 +20,8 @@ export interface ComponentStyleRecord {
reload?: boolean;
}

const JS_TS_REGEXP = /\.[cm]?[tj]sx?$/;

export function createAngularAssetsMiddleware(
server: ViteDevServer,
assets: AngularOutputAssets,
Expand All @@ -38,15 +43,28 @@ export function createAngularAssetsMiddleware(
// Rewrite all build assets to a vite raw fs URL
const asset = assets.get(pathname);
if (asset) {
// Workaround to disable Vite transformer middleware.
// See: https://github.com/vitejs/vite/blob/746a1daab0395f98f0afbdee8f364cb6cf2f3b3f/packages/vite/src/node/server/middlewares/transform.ts#L201 and
// https://github.com/vitejs/vite/blob/746a1daab0395f98f0afbdee8f364cb6cf2f3b3f/packages/vite/src/node/server/transformRequest.ts#L204-L206
req.headers.accept = 'text/html';

// The encoding needs to match what happens in the vite static middleware.
// ref: https://github.com/vitejs/vite/blob/d4f13bd81468961c8c926438e815ab6b1c82735e/packages/vite/src/node/server/middlewares/static.ts#L163
req.url = `${server.config.base}@fs/${encodeURI(asset.source)}`;
next();
// This is a workaround to serve JS and TS files without Vite transformations.
if (JS_TS_REGEXP.test(extension)) {
const contents = readFileSync(asset.source);
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);
} else {
// The encoding needs to match what happens in the vite static middleware.
// ref: https://github.com/vitejs/vite/blob/d4f13bd81468961c8c926438e815ab6b1c82735e/packages/vite/src/node/server/middlewares/static.ts#L163
req.url = `${server.config.base}@fs/${encodeURI(asset.source)}`;
next();
}

return;
}
Expand Down Expand Up @@ -100,12 +118,8 @@ export function createAngularAssetsMiddleware(
componentStyle.used.add(componentId);
}

// Report if there are no changes to avoid reprocessing
const etag = `W/"${outputFile.contents.byteLength}-${outputFile.hash}-${componentId}"`;
if (req.headers['if-none-match'] === etag) {
res.statusCode = 304;
res.end();

if (checkAndHandleEtag(req, res, etag)) {
return;
}

Expand Down Expand Up @@ -134,12 +148,8 @@ export function createAngularAssetsMiddleware(
}
}

// Avoid resending the content if it has not changed since last request
const etag = `W/"${outputFile.contents.byteLength}-${outputFile.hash}"`;
if (req.headers['if-none-match'] === etag) {
res.statusCode = 304;
res.end();

if (checkAndHandleEtag(req, res, etag)) {
return;
}

Expand Down Expand Up @@ -188,3 +198,18 @@ export function createAngularAssetsMiddleware(
next();
};
}

function checkAndHandleEtag(
req: Connect.IncomingMessage,
res: ServerResponse,
etag: string,
): boolean {
if (req.headers['if-none-match'] === etag) {
res.statusCode = 304;
res.end();

return true;
}

return false;
}
97 changes: 25 additions & 72 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.