+
Skip to content

fix(vite-plugin-angular): fix live reload and move to separate plugin #1739

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
May 22, 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 apps/analog-app/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default defineConfig(({ mode, isSsrBuild }) => {
supportAnalogFormat: true,
},
},
liveReload: false,
liveReload: true,
nitro: {
routeRules: {
'/cart/**': {
Expand Down
126 changes: 14 additions & 112 deletions packages/vite-plugin-angular/src/lib/angular-vite-plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ import { mkdirSync, writeFileSync } from 'node:fs';
import * as compilerCli from '@angular/compiler-cli';
import * as ts from 'typescript';
import { createRequire } from 'node:module';
import { ServerResponse } from 'node:http';

import {
ModuleNode,
normalizePath,
Plugin,
ViteDevServer,
preprocessCSS,
ResolvedConfig,
Connect,
} from 'vite';
import * as ngCompiler from '@angular/compiler';

Expand Down Expand Up @@ -48,7 +47,8 @@ import {
} from './authoring/markdown-transform.js';
import { routerPlugin } from './router-plugin.js';
import { pendingTasksPlugin } from './angular-pending-tasks.plugin.js';
import { analyzeFileUpdates } from './utils/hmr-candidates.js';
import { EmitFileResult } from './models.js';
import { liveReloadPlugin } from './live-reload-plugin.js';

export interface PluginOptions {
tsconfig?: string;
Expand Down Expand Up @@ -83,24 +83,12 @@ export interface PluginOptions {
disableTypeChecking?: boolean;
}

interface EmitFileResult {
content?: string;
map?: string;
dependencies: readonly string[];
hash?: Uint8Array;
errors?: (string | ts.DiagnosticMessageChain)[];
warnings?: (string | ts.DiagnosticMessageChain)[];
hmrUpdateCode?: string | null;
hmrEligible?: boolean;
}

/**
* TypeScript file extension regex
* Match .(c or m)ts, .ts extensions with an optional ? for query params
* Ignore .tsx extensions
*/
const TS_EXT_REGEX = /\.[cm]?(ts|analog|ag)[^x]?\??/;
const ANGULAR_COMPONENT_PREFIX = '/@ng/component';
const classNames = new Map();

interface DeclarationFile {
Expand Down Expand Up @@ -246,55 +234,6 @@ export function angular(options?: PluginOptions): Plugin[] {
server.watcher.on('unlink', async () => {
await performCompilation(resolvedConfig);
});

if (pluginOptions.liveReload) {
const angularComponentMiddleware: Connect.HandleFunction = async (
req: Connect.IncomingMessage,
res: ServerResponse<Connect.IncomingMessage>,
next: Connect.NextFunction,
) => {
if (req.url === undefined || res.writableEnded) {
return;
}

if (!req.url.includes(ANGULAR_COMPONENT_PREFIX)) {
next();

return;
}

const requestUrl = new URL(req.url, 'http://localhost');
const componentId = requestUrl.searchParams.get('c');

if (!componentId) {
res.statusCode = 400;
res.end();

return;
}

const [fileId] = decodeURIComponent(componentId).split('@');
const resolvedId = resolve(process.cwd(), fileId);
const invalidated =
!!server.moduleGraph.getModuleById(resolvedId)
?.lastInvalidationTimestamp && classNames.get(resolvedId);

// don't send an HMR update until the file has been invalidated
if (!invalidated) {
res.setHeader('Content-Type', 'text/javascript');
res.setHeader('Cache-Control', 'no-cache');
res.end('');
return;
}

const result = fileEmitter(resolvedId);
res.setHeader('Content-Type', 'text/javascript');
res.setHeader('Cache-Control', 'no-cache');
res.end(`${result?.hmrUpdateCode || ''}`);
};

viteServer.middlewares.use(angularComponentMiddleware);
}
},
async buildStart() {
// Defer the first compilation in test mode
Expand All @@ -319,14 +258,13 @@ export function angular(options?: PluginOptions): Plugin[] {
fileId += '.ts';
}

sourceFileCache.invalidate([fileId]);
await performCompilation(resolvedConfig, [fileId]);

const result = fileEmitter(fileId);

if (
pluginOptions.liveReload &&
!!result?.hmrEligible &&
result?.hmrEligible &&
classNames.get(fileId)
) {
const relativeFileId = `${relative(
Expand Down Expand Up @@ -440,7 +378,7 @@ export function angular(options?: PluginOptions): Plugin[] {
classNames.clear();
return ctx.modules;
},
resolveId(id, importer, options) {
resolveId(id, importer) {
if (id.startsWith('angular:jit:')) {
const path = id.split(';')[1];
return `${normalizePath(
Expand All @@ -458,21 +396,9 @@ export function angular(options?: PluginOptions): Plugin[] {
}
}

if (options?.ssr && id.includes(ANGULAR_COMPONENT_PREFIX)) {
const requestUrl = new URL(id.slice(1), 'http://localhost');
const componentId = requestUrl.searchParams.get('c');

const res = resolve(
process.cwd(),
decodeURIComponent(componentId as string).split('@')[0],
);

return res;
}

return undefined;
},
async load(id, options) {
async load(id) {
// Map angular inline styles to the source text
if (isComponentStyleSheet(id)) {
const componentStyles = inlineComponentStyles?.get(
Expand All @@ -483,28 +409,6 @@ export function angular(options?: PluginOptions): Plugin[] {
}
}

if (
pluginOptions.liveReload &&
options?.ssr &&
id.includes(ANGULAR_COMPONENT_PREFIX)
) {
const requestUrl = new URL(id.slice(1), 'http://localhost');
const componentId = requestUrl.searchParams.get('c');

if (!componentId) {
return;
}

const result = fileEmitter(
resolve(
process.cwd(),
decodeURIComponent(componentId).split('@')[0],
),
);

return result?.hmrUpdateCode || '';
}

return;
},
async transform(code, id) {
Expand Down Expand Up @@ -688,6 +592,7 @@ export function angular(options?: PluginOptions): Plugin[] {

return [
angularPlugin(),
pluginOptions.liveReload && liveReloadPlugin({ classNames, fileEmitter }),
...(isTest && !isStackBlitz ? angularVitestPlugins() : []),
(jit &&
jitPlugin({
Expand Down Expand Up @@ -949,9 +854,7 @@ export function angular(options?: PluginOptions): Plugin[] {
return;
}

const metadata = watchMode
? fileMetadata(filename, sourceFileCache.get(filename))
: {};
const metadata = watchMode ? fileMetadata(filename) : {};

outputFiles.set(filename, {
content,
Expand Down Expand Up @@ -1054,17 +957,12 @@ export function getFileMetadata(
disableTypeChecking?: boolean,
) {
const ts = require('typescript');
return (file: string, stale?: ts.SourceFile) => {
return (file: string) => {
const sourceFile = program.getSourceFile(file);
if (!sourceFile) {
return {};
}

const hmrEligible =
liveReload && stale
? !!analyzeFileUpdates(stale, sourceFile, angularCompiler!)
: false;

const diagnostics = getDiagnosticsForSourceFile(
sourceFile,
!!disableTypeChecking,
Expand All @@ -1086,11 +984,15 @@ export function getFileMetadata(

let hmrUpdateCode: string | null | undefined = undefined;

let hmrEligible = false;
if (liveReload) {
for (const node of sourceFile.statements) {
if (ts.isClassDeclaration(node) && (node as any).name != null) {
hmrUpdateCode = angularCompiler?.emitHmrUpdateModule(node as any);
!!hmrUpdateCode && classNames.set(file, (node as any).name.getText());
if (!!hmrUpdateCode) {
classNames.set(file, (node as any).name.getText());
hmrEligible = true;
}
}
}
}
Expand Down
97 changes: 97 additions & 0 deletions packages/vite-plugin-angular/src/lib/live-reload-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { resolve } from 'node:path';
import { ServerResponse } from 'node:http';
import { Connect, Plugin, ViteDevServer } from 'vite';

import { EmitFileResult } from './models.js';

const ANGULAR_COMPONENT_PREFIX = '/@ng/component';
const FILE_PREFIX = 'file:';

export function liveReloadPlugin({
classNames,
fileEmitter,
}: {
classNames: Map<string, string>;
fileEmitter: (file: string) => EmitFileResult | undefined;
}): Plugin {
return {
name: 'analogjs-live-reload-plugin',
configureServer(server: ViteDevServer) {
const angularComponentMiddleware: Connect.HandleFunction = async (
req: Connect.IncomingMessage,
res: ServerResponse<Connect.IncomingMessage>,
next: Connect.NextFunction,
) => {
if (req.url === undefined || res.writableEnded) {
return;
}

if (!req.url.includes(ANGULAR_COMPONENT_PREFIX)) {
next();

return;
}

const requestUrl = new URL(req.url, 'http://localhost');
const componentId = requestUrl.searchParams.get('c');

if (!componentId) {
res.statusCode = 400;
res.end();

return;
}

const [fileId] = decodeURIComponent(componentId).split('@');
const resolvedId = resolve(process.cwd(), fileId);
const invalidated =
!!server.moduleGraph.getModuleById(resolvedId)
?.lastInvalidationTimestamp && classNames.get(resolvedId);

// don't send an HMR update until the file has been invalidated
if (!invalidated) {
res.setHeader('Content-Type', 'text/javascript');
res.setHeader('Cache-Control', 'no-cache');
res.end('');
return;
}

const result = fileEmitter(resolvedId);
res.setHeader('Content-Type', 'text/javascript');
res.setHeader('Cache-Control', 'no-cache');
res.end(`${result?.hmrUpdateCode || ''}`);
};

server.middlewares.use(angularComponentMiddleware);
},
resolveId(id, _importer, options) {
if (
options?.ssr &&
id.startsWith(FILE_PREFIX) &&
id.includes(ANGULAR_COMPONENT_PREFIX)
) {
return `\0${id}`;
}

return undefined;
},
load(id, options) {
if (options?.ssr && id.includes(ANGULAR_COMPONENT_PREFIX)) {
const requestUrl = new URL(id.slice(1), 'http://localhost');
const componentId = requestUrl.searchParams.get('c');

if (!componentId) {
return;
}

const result = fileEmitter(
resolve(process.cwd(), decodeURIComponent(componentId).split('@')[0]),
);

return result?.hmrUpdateCode || '';
}

return;
},
};
}
12 changes: 12 additions & 0 deletions packages/vite-plugin-angular/src/lib/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import type ts from 'typescript';

export interface EmitFileResult {
content?: string;
map?: string;
dependencies: readonly string[];
hash?: Uint8Array;
errors?: (string | ts.DiagnosticMessageChain)[];
warnings?: (string | ts.DiagnosticMessageChain)[];
hmrUpdateCode?: string | null;
hmrEligible?: boolean | null;
}
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载