almostnode 0.2.6 → 0.2.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -7,6 +7,7 @@ import { DevServer, DevServerOptions, ResponseData, HMRUpdate } from '../dev-ser
7
7
  import { VirtualFS } from '../virtual-fs';
8
8
  import { Buffer } from '../shims/stream';
9
9
  import { simpleHash } from '../utils/hash';
10
+ import { addReactRefresh as _addReactRefresh } from './code-transforms';
10
11
 
11
12
  // Check if we're in a real browser environment (not jsdom or Node.js)
12
13
  // jsdom has window but doesn't have ServiceWorker or SharedArrayBuffer
@@ -574,68 +575,8 @@ export class ViteDevServer extends DevServer {
574
575
  return result.code;
575
576
  }
576
577
 
577
- /**
578
- * Add React Refresh registration to transformed code
579
- * This enables true HMR (state-preserving) for React components
580
- */
581
578
  private addReactRefresh(code: string, filename: string): string {
582
- // Find React components (functions starting with uppercase letter)
583
- const components: string[] = [];
584
-
585
- // Match function declarations: function App() { ... }
586
- // Also handles: export function App() { ... }
587
- const funcDeclRegex = /(?:^|\n)(?:export\s+)?function\s+([A-Z][a-zA-Z0-9]*)\s*\(/g;
588
- let match;
589
- while ((match = funcDeclRegex.exec(code)) !== null) {
590
- if (!components.includes(match[1])) {
591
- components.push(match[1]);
592
- }
593
- }
594
-
595
- // Match arrow function components: const App = () => { ... }
596
- // Also handles: export const App = () => { ... }
597
- // And: const App = function() { ... }
598
- const arrowRegex = /(?:^|\n)(?:export\s+)?(?:const|let|var)\s+([A-Z][a-zA-Z0-9]*)\s*=/g;
599
- while ((match = arrowRegex.exec(code)) !== null) {
600
- if (!components.includes(match[1])) {
601
- components.push(match[1]);
602
- }
603
- }
604
-
605
- // If no components found, just add hot module setup without refresh
606
- if (components.length === 0) {
607
- return `// HMR Setup
608
- import.meta.hot = window.__vite_hot_context__("${filename}");
609
-
610
- ${code}
611
-
612
- // HMR Accept
613
- if (import.meta.hot) {
614
- import.meta.hot.accept();
615
- }
616
- `;
617
- }
618
-
619
- // Build React Refresh registration calls
620
- const registrations = components
621
- .map(name => ` $RefreshReg$(${name}, "${filename} ${name}");`)
622
- .join('\n');
623
-
624
- return `// HMR Setup
625
- import.meta.hot = window.__vite_hot_context__("${filename}");
626
-
627
- ${code}
628
-
629
- // React Refresh Registration
630
- if (import.meta.hot) {
631
- ${registrations}
632
- import.meta.hot.accept(() => {
633
- if (window.$RefreshRuntime$) {
634
- window.$RefreshRuntime$.performReactRefresh();
635
- }
636
- });
637
- }
638
- `;
579
+ return _addReactRefresh(code, filename);
639
580
  }
640
581
 
641
582
  /**