diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index 2d3aa8a..de0f37f 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -68,11 +68,14 @@ describe('viteTimingPlugin', () => { it('should not inject scripts in production mode', () => { const html = ''; - const result = plugin.transformIndexHtml?.(html, { mode: 'production' }); + const result = plugin.transformIndexHtml?.(html, { + command: 'build', + originalUrl: '/' + }); + expect(result).toBe(html); // Should return unmodified HTML expect(result).not.toContain('window.__VITE_TIMING__'); expect(result).not.toContain('import.meta.hot'); - expect(result).toBe(html); }); it('should handle HMR updates with module count', () => { diff --git a/src/index.ts b/src/index.ts index 229f003..74612fa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,7 +1,6 @@ - import { performance } from 'perf_hooks'; import path from 'path'; -import type { Plugin, ViteDevServer } from 'vite'; +import type { Plugin, ViteDevServer, IndexHtmlTransformContext } from 'vite'; import type { TimingEntry, HMRUpdate, ClientMessage } from './types'; import { getCommonMetadata } from './utils/metadata'; import { sendMetrics } from './utils/metrics'; @@ -170,9 +169,9 @@ export default function viteTimingPlugin(): ViteTimingPlugin { }); }, - transformIndexHtml(html: string, { mode }) { - // Only inject scripts in development mode - if (mode === 'development') { + transformIndexHtml(html: string, ctx?: { [key: string]: any }) { + // Check if we're in development based on context + if (!ctx || ctx.command !== 'build') { // Insert the main timing function first html = html.replace( '',