All files / src render-context.ts

13.11% Statements 16/122
0% Branches 0/1
0% Functions 0/12
13.11% Lines 16/122

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 13271x 1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         1x 1x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         1x     1x           1x                                                                                                                   1x                                                                                                                                                                     1x                                                                                                                                             1x                                                                                                                                                                                               1x                                                                                                                                   1x                                                                                                                     1x                                                                                               1x                                                                                                   1x         1x  
import path from 'node:path';
import serialize from 'serialize-javascript';
import type { Esmx } from './core';
 
/**
 * Configuration options interface for RenderContext
 *
 * @description
 * RenderContextOptions is used to configure the behavior of RenderContext instances, including base path, entry name, parameters, and import map mode.
 *
 * @example
 * ```ts
 * // 1. Base path configuration example
 * // Supports deploying static assets to different paths
 * const rc = await esmx.render({
 *   // Set base path to /esmx, all static assets will be loaded based on this path
 *   base: '/esmx',
 *   // Other configurations...
 * });
 *
 * // 2. Multi-language site deployment example
 * // Support multi-language sites through different base paths
 * const rc = await esmx.render({
 *   base: '/cn',  // Chinese site
 *   params: { lang: 'zh-CN' }
 * });
 *
 * // 3. Import map mode configuration example
 * const rc = await esmx.render({
 *   // Use inline mode, suitable for small applications
 *   importmapMode: 'inline',
 *   // Other configurations...
 * });
 * ```
 */
export interface RenderContextOptions {
    /**
     * Base path for static assets
     * @description
     * - Defaults to empty string
     * - All static assets (JS, CSS, images, etc.) will be loaded based on this path
     * - Supports runtime dynamic configuration without rebuilding
     * - Commonly used for multi-language sites, micro-frontends, and other scenarios
     */
    base?: string;
 
    /**
     * Server-side rendering entry name
     * @description
     * - Defaults to 'default'
     * - Used to specify the entry function used during server-side rendering
     * - Used when a module exports multiple rendering functions
     */
    entryName?: string;
 
    /**
     * Rendering parameters
     * @description
     * - Can pass parameters of any type to the rendering function
     * - Commonly used to pass request information (URL, query parameters, etc.)
     * - Can be accessed through rc.params during server-side rendering
     */
    params?: Record<string, any>;
 
    /**
     * Define the generation mode for importmap
     *
     * @description
     * ImportmapMode is used to control the generation method of importmap, supporting two modes:
     * - `inline`: Inline importmap content directly into HTML (default value), suitable for the following scenarios:
     *   - Need to reduce the number of HTTP requests
     *   - Importmap content is small
     *   - High requirements for first-screen loading performance
     * - `js`: Generate importmap content as an independent JS file, suitable for the following scenarios:
     *   - Importmap content is large
     *   - Need to utilize browser caching mechanisms
     *   - Multiple pages share the same importmap
     *
     * Reasons for choosing 'inline' as the default value:
     * 1. Simple and direct
     *    - Reduce additional HTTP requests
     *    - No additional resource management required
     *    - Suitable for most application scenarios
     * 2. First-screen performance
     *    - Avoid additional network requests
     *    - Ensure import maps are immediately available
     *    - Reduce page loading time
     * 3. Easy to debug
     *    - Import maps are directly visible
     *    - Facilitate problem diagnosis
     *    - Simplify development process
     *
     * @example
     * ```ts
     * // Use inline mode (default)
     * const rc = await esmx.render({
     *   params: { url: req.url }
     * });
     *
     * // Explicitly specify inline mode
     * const rc = await esmx.render({
     *   importmapMode: 'inline',
     *   params: { url: req.url }
     * });
     *
     * // Use JS file mode
     * const rc = await esmx.render({
     *   importmapMode: 'js',
     *   params: { url: req.url }
     * });
     * ```
     */
    importmapMode?: ImportmapMode;
}
 
/**
 * Server-side rendering function
 */
export type ServerRenderHandle = (rc: RenderContext) => Promise<void>;
 
/**
 * Render resource file list interface
 * @description
 * The RenderFiles interface defines various static assets collected during the server-side rendering process:
 *
 * 1. **Resource Types**
 *    - css: List of stylesheet files
 *    - modulepreload: List of ESM modules that need to be preloaded
 *    - js: List of JavaScript files
 *    - resources: List of other resource files
 *
 * 2. **Use Cases**
 *    - Automatically collected in the commit() method
 *    - Injected through methods like preload(), css(), etc.
 *    - Supports base path configuration
 *
 * @example
 * ```ts
 * // 1. Resource collection
 * await rc.commit();
 *
 * // 2. Resource injection
 * rc.html = `
 *   <!DOCTYPE html>
 *   <html>
 *   <head>
 *     <!-- Preload resources -->
 *     ${rc.preload()}
 *     <!-- Inject stylesheets -->
 *     ${rc.css()}
 *   </head>
 *   <body>
 *     ${html}
 *     <!-- Inject import map -->
 *     ${rc.importmap()}
 *     <!-- Inject client entry -->
 *     ${rc.moduleEntry()}
 *     <!-- Preload modules -->
 *     ${rc.modulePreload()}
 *   </body>
 *   </html>
 * `;
 * ```
 */
export interface RenderFiles {
    /**
     * List of JavaScript files
     */
    js: string[];
    /**
     * List of CSS files
     */
    css: string[];
    /**
     * List of ESM modules that need to be preloaded
     */
    modulepreload: string[];
    /**
     * List of other resource files (images, fonts, etc.)
     */
    resources: string[];
}
 
/**
 * Define the generation mode for importmap
 *
 * @description
 * ImportmapMode is used to control the generation method of importmap, supporting two modes:
 * - `inline`: Inline importmap content directly into HTML (default value), suitable for the following scenarios:
 *   - Need to reduce the number of HTTP requests
 *   - Importmap content is small
 *   - High requirements for first-screen loading performance
 * - `js`: Generate importmap content as an independent JS file, suitable for the following scenarios:
 *   - Importmap content is large
 *   - Need to utilize browser caching mechanisms
 *   - Multiple pages share the same importmap
 *
 * Reasons for choosing 'inline' as the default value:
 * 1. Simple and direct
 *    - Reduce additional HTTP requests
 *    - No additional resource management required
 *    - Suitable for most application scenarios
 * 2. First-screen performance
 *    - Avoid additional network requests
 *    - Ensure import maps are immediately available
 *    - Reduce page loading time
 * 3. Easy to debug
 *    - Import maps are directly visible
 *    - Facilitate problem diagnosis
 *    - Simplify development process
 *
 * @example
 * ```ts
 * // Use inline mode (default)
 * const rc = await esmx.render({
 *   params: { url: req.url }
 * });
 *
 * // Explicitly specify inline mode
 * const rc = await esmx.render({
 *   importmapMode: 'inline',
 *   params: { url: req.url }
 * });
 *
 * // Use JS file mode
 * const rc = await esmx.render({
 *   importmapMode: 'js',
 *   params: { url: req.url }
 * });
 * ```
 */
export type ImportmapMode = 'inline' | 'js';
 
/**
 * RenderContext is the core class in the Esmx framework, responsible for resource management and HTML generation during server-side rendering (SSR)
 *
 * @description
 * RenderContext has the following core features:
 * 1. **ESM-based module system**
 *    - Adopts modern ECMAScript Modules standard
 *    - Supports native module imports and exports
 *    - Implements better code splitting and on-demand loading
 *
 * 2. **Intelligent dependency collection**
 *    - Dynamically collects dependencies based on actual rendering paths
 *    - Avoids unnecessary resource loading
 *    - Supports async components and dynamic imports
 *
 * 3. **Precise resource injection**
 *    - Strictly controls resource loading order
 *    - Optimizes first-screen loading performance
 *    - Ensures reliability of client-side hydration
 *
 * 4. **Flexible configuration mechanism**
 *    - Supports dynamic base path configuration
 *    - Provides multiple import map modes
 *    - Adapts to different deployment scenarios
 *
 * @example
 * ```ts
 * export default async (rc: RenderContext) => {
 *     // 1. Render page content and collect dependencies
 *     const app = createApp();
 *     const html = await renderToString(app, {
 *         importMetaSet: rc.importMetaSet
 *     });
 *
 *     // 2. Commit dependency collection
 *     await rc.commit();
 *
 *     // 3. Generate complete HTML
 *     rc.html = `
 *         <!DOCTYPE html>
 *         <html>
 *         <head>
 *             <!-- Preload CSS and JS resources to start loading early for performance optimization -->
 *             ${rc.preload()}
 *             <!-- Inject first-screen stylesheets to avoid page flickering -->
 *             ${rc.css()}
 *         </head>
 *         <body>
 *             ${html}
 *             <!-- Inject module import map to define path resolution rules for ESM modules -->
 *             ${rc.importmap()}
 *             <!-- Inject client entry module, must be executed after importmap -->
 *             ${rc.moduleEntry()}
 *             <!-- Preload module dependencies, optimized loading based on dependencies collected during actual rendering -->
 *             ${rc.modulePreload()}
 *         </body>
 *         </html>
 *     `;
 * };
 * ```
 */
export class RenderContext {
    public esmx: Esmx;
    /**
     * Redirect address
     * @description
     * - Defaults to null, indicating no redirect
     * - When set, the server can perform HTTP redirection based on this value
     * - Commonly used for scenarios like login verification, permission control, etc.
     *
     * @example
     * ```ts
     * // 1. Login verification example
     * export default async (rc: RenderContext) => {
     *   if (!isLoggedIn()) {
     *     rc.redirect = '/login';
     *     rc.status = 302;
     *     return;
     *   }
     *   // Continue rendering page...
     * };
     *
     * // 2. Permission control example
     * export default async (rc: RenderContext) => {
     *   if (!hasPermission()) {
     *     rc.redirect = '/403';
     *     rc.status = 403;
     *     return;
     *   }
     *   // Continue rendering page...
     * };
     *
     * // 3. Server-side processing example
     * app.use(async (req, res) => {
     *   const rc = await esmx.render({
     *     params: {
     *       url: req.url
     *     }
     *   });
     *
     *   // Handle redirect
     *   if (rc.redirect) {
     *     res.statusCode = rc.status || 302;
     *     res.setHeader('Location', rc.redirect);
     *     res.end();
     *     return;
     *   }
     *
     *   // Set status code
     *   if (rc.status) {
     *     res.statusCode = rc.status;
     *   }
     *
     *   // Respond with HTML content
     *   res.end(rc.html);
     * });
     * ```
     */
    public redirect: string | null = null;
 
    /**
     * HTTP response status code
     * @description
     * - Defaults to null, indicating use of 200 status code
     * - Can set any valid HTTP status code
     * - Commonly used for scenarios like error handling, redirection, etc.
     * - Usually used in conjunction with the redirect property
     *
     * @example
     * ```ts
     * // 1. 404 error handling example
     * export default async (rc: RenderContext) => {
     *   const page = await findPage(rc.params.url);
     *   if (!page) {
     *     rc.status = 404;
     *     // Render 404 page...
     *     return;
     *   }
     *   // Continue rendering page...
     * };
     *
     * // 2. Temporary redirect example
     * export default async (rc: RenderContext) => {
     *   if (needMaintenance()) {
     *     rc.redirect = '/maintenance';
     *     rc.status = 307; // Temporary redirect, keep request method unchanged
     *     return;
     *   }
     *   // Continue rendering page...
     * };
     *
     * // 3. Server-side processing example
     * app.use(async (req, res) => {
     *   const rc = await esmx.render({
     *     params: {
     *       url: req.url
     *     }
     *   });
     *
     *   // Handle redirect
     *   if (rc.redirect) {
     *     res.statusCode = rc.status || 302;
     *     res.setHeader('Location', rc.redirect);
     *     res.end();
     *     return;
     *   }
     *
     *   // Set status code
     *   if (rc.status) {
     *     res.statusCode = rc.status;
     *   }
     *
     *   // Respond with HTML content
     *   res.end(rc.html);
     * });
     * ```
     */
    public status: number | null = null;
    private _html = '';
    /**
     * Base path for static assets
     * @description
     * The base property is used to control the loading path of static assets and is the core of Esmx framework's dynamic base path configuration:
     *
     * 1. **Build-time Processing**
     *    - Static asset paths are marked with special placeholders: `[[[___ESMX_DYNAMIC_BASE___]]]/your-app-name/`
     *    - Placeholders are injected into all static asset reference paths
     *    - Supports various static assets like CSS, JavaScript, images, etc.
     *
     * 2. **Runtime Replacement**
     *    - Set the actual base path through the `base` parameter of `esmx.render()`
     *    - RenderContext automatically replaces placeholders in HTML with actual paths
     *
     * 3. **Technical Advantages**
     *    - Deployment flexibility: The same set of build artifacts can be deployed to any path
     *    - Performance optimization: Maintain the best caching strategy for static assets
     *    - Development-friendly: Simplify multi-environment configuration management
     *
     * @example
     * ```ts
     * // 1. Basic usage
     * const rc = await esmx.render({
     *   base: '/esmx',  // Set base path
     *   params: { url: req.url }
     * });
     *
     * // 2. Multi-language site example
     * const rc = await esmx.render({
     *   base: '/cn',  // Chinese site
     *   params: { lang: 'zh-CN' }
     * });
     *
     * // 3. Micro-frontend application example
     * const rc = await esmx.render({
     *   base: '/app1',  // Sub-application 1
     *   params: { appId: 1 }
     * });
     * ```
     */
    public readonly base: string;
    /**
     * Server-side rendering entry function name
     * @description
     * The entryName property is used to specify the entry function used during server-side rendering:
     *
     * 1. **Basic Usage**
     *    - Default value is 'default'
     *    - Used to select the rendering function to use from entry.server.ts
     *    - Supports scenarios where a module exports multiple rendering functions
     *
     * 2. **Use Cases**
     *    - Multi-template rendering: Different pages use different rendering templates
     *    - A/B testing: The same page uses different rendering logic
     *    - Special rendering: Some pages need custom rendering processes
     *
     * @example
     * ```ts
     * // 1. Default entry function
     * // entry.server.ts
     * export default async (rc: RenderContext) => {
     *   // Default rendering logic
     * };
     *
     * // 2. Multiple entry functions
     * // entry.server.ts
     * export const mobile = async (rc: RenderContext) => {
     *   // Mobile rendering logic
     * };
     *
     * export const desktop = async (rc: RenderContext) => {
     *   // Desktop rendering logic
     * };
     *
     * // 3. Select entry function based on device type
     * const rc = await esmx.render({
     *   entryName: isMobile ? 'mobile' : 'desktop',
     *   params: { url: req.url }
     * });
     * ```
     */
    public readonly entryName: string;
 
    /**
     * Rendering parameters
     * @description
     * The params property is used to pass and access parameters during the server-side rendering process:
     *
     * 1. **Parameter Types**
     *    - Supports key-value pairs of any type
     *    - Defined through Record<string, any> type
     *    - Remains unchanged throughout the entire rendering lifecycle
     *
     * 2. **Common Use Cases**
     *    - Pass request information (URL, query parameters, etc.)
     *    - Set page configuration (language, theme, etc.)
     *    - Inject environment variables (API address, version number, etc.)
     *    - Share server-side state (user information, permissions, etc.)
     *
     * 3. **Access Methods**
     *    - Accessed through rc.params in server-side rendering functions
     *    - Can destructure to get specific parameters
     *    - Supports setting default values
     *
     * @example
     * ```ts
     * // 1. Basic usage - Pass URL and language settings
     * const rc = await esmx.render({
     *   params: {
     *     url: req.url,
     *     lang: 'zh-CN'
     *   }
     * });
     *
     * // 2. Page configuration - Set theme and layout
     * const rc = await esmx.render({
     *   params: {
     *     theme: 'dark',
     *     layout: 'sidebar'
     *   }
     * });
     *
     * // 3. Environment configuration - Inject API address
     * const rc = await esmx.render({
     *   params: {
     *     apiBaseUrl: process.env.API_BASE_URL,
     *     version: '1.0.0'
     *   }
     * });
     *
     * // 4. Use in rendering function
     * export default async (rc: RenderContext) => {
     *   // Destructure to get parameters
     *   const { url, lang = 'en' } = rc.params;
     *
     *   // Execute different logic based on parameters
     *   if (lang === 'zh-CN') {
     *     // Chinese version processing...
     *   }
     *
     *   // Pass parameters to component
     *   const html = await renderToString(createApp({
     *     props: {
     *       currentUrl: url,
     *       language: lang
     *     }
     *   }));
     *
     *   // Set HTML
     *   rc.html = `
     *     <!DOCTYPE html>
     *     <html lang="${lang}">
     *       <body>${html}</body>
     *     </html>
     *   `;
     * };
     * ```
     */
    public readonly params: Record<string, any>;
    /**
     * Module dependency collection set
     * @description
     * importMetaSet is the core of Esmx framework's intelligent dependency collection mechanism, used to track and record module dependencies during the server-side rendering process:
     *
     * 1. **On-demand Collection**
     *    - Automatically tracks and records module dependencies during the actual component rendering process
     *    - Only collects resources actually used during the current page rendering
     *    - Precisely records the module dependency relationships of each component
     *
     * 2. **Performance Optimization**
     *    - Avoids loading unused modules, significantly reducing first-screen loading time
     *    - Precisely controls resource loading order, optimizing page rendering performance
     *    - Automatically generates optimal import maps
     *
     * 3. **Usage**
     *    - Passed to renderToString in the rendering function
     *    - Framework automatically collects dependencies, no manual handling required
     *    - Supports dependency collection for async components and dynamic imports
     *
     * @example
     * ```ts
     * // 1. Basic usage
     * const renderToString = (app: any, context: { importMetaSet: Set<ImportMeta> }) => {
     *   // Automatically collect module dependencies during the rendering process
     *   // Framework will automatically call context.importMetaSet.add(import.meta) during component rendering
     *   // Developers do not need to manually handle dependency collection
     *   return '<div id="app">Hello World</div>';
     * };
     *
     * // Usage example
     * const app = createApp();
     * const html = await renderToString(app, {
     *   importMetaSet: rc.importMetaSet
     * });
     *
     * // 2. Commit dependencies
     * await rc.commit();
     *
     * // 3. Generate HTML
     * rc.html = `
     *   <!DOCTYPE html>
     *   <html>
     *   <head>
     *     <!-- Automatically inject resources based on collected dependencies -->
     *     ${rc.preload()}
     *     ${rc.css()}
     *   </head>
     *   <body>
     *     ${html}
     *     ${rc.importmap()}
     *     ${rc.moduleEntry()}
     *     ${rc.modulePreload()}
     *   </body>
     *   </html>
     * `;
     * ```
     */
    public importMetaSet = new Set<ImportMeta>();
    /**
     * Resource file list
     * @description
     * The files property stores all static resource file paths collected during the server-side rendering process:
     *
     * 1. **Resource Types**
     *    - js: List of JavaScript files, containing all scripts and modules
     *    - css: List of stylesheet files
     *    - modulepreload: List of ESM modules that need to be preloaded
     *    - importmap: List of import map files
     *    - resources: List of other resource files (images, fonts, etc.)
     *
     * 2. **Use Cases**
     *    - Automatically collect and categorize resources in the commit() method
     *    - Inject resources into HTML through methods like preload(), css(), etc.
     *    - Supports base path configuration, implementing dynamic loading of resources
     *
     * @example
     * ```ts
     * // 1. Resource collection
     * await rc.commit();
     *
     * // 2. Resource injection
     * rc.html = `
     *   <!DOCTYPE html>
     *   <html>
     *   <head>
     *     <!-- Preload resources -->
     *     ${rc.preload()}
     *     <!-- Inject stylesheets -->
     *     ${rc.css()}
     *   </head>
     *   <body>
     *     ${html}
     *     ${rc.importmap()}
     *     ${rc.moduleEntry()}
     *     ${rc.modulePreload()}
     *   </body>
     *   </html>
     * `;
     * ```
     */
    public files: RenderFiles = {
        js: [],
        css: [],
        modulepreload: [],
        resources: []
    };
    private _importMap: { src: string | null; code: string } = {
        src: '',
        code: ''
    };
    /**
     * Define the generation mode for importmap
     *
     * @description
     * ImportmapMode is used to control the generation method of importmap, supporting two modes:
     * - `inline`: Inline importmap content directly into HTML (default value), suitable for the following scenarios:
     *   - Need to reduce the number of HTTP requests
     *   - Importmap content is small
     *   - High requirements for first-screen loading performance
     * - `js`: Generate importmap content as an independent JS file, suitable for the following scenarios:
     *   - Importmap content is large
     *   - Need to utilize browser caching mechanisms
     *   - Multiple pages share the same importmap
     *
     * Reasons for choosing 'inline' as the default value:
     * 1. Simple and direct
     *    - Reduce additional HTTP requests
     *    - No additional resource management required
     *    - Suitable for most application scenarios
     * 2. First-screen performance
     *    - Avoid additional network requests
     *    - Ensure import maps are immediately available
     *    - Reduce page loading time
     * 3. Easy to debug
     *    - Import maps are directly visible
     *    - Facilitate problem diagnosis
     *    - Simplify development process
     *
     * @example
     * ```ts
     * // Use inline mode (default)
     * const rc = await esmx.render({
     *   params: { url: req.url }
     * });
     *
     * // Explicitly specify inline mode
     * const rc = await esmx.render({
     *   importmapMode: 'inline',
     *   params: { url: req.url }
     * });
     *
     * // Use JS file mode
     * const rc = await esmx.render({
     *   importmapMode: 'js',
     *   params: { url: req.url }
     * });
     * ```
     */
    public importmapMode: ImportmapMode;
    /**
     * HTML content
     * @description
     * The html property is used to set and get the final generated HTML content:
     *
     * 1. **Base Path Replacement**
     *    - Automatically handles base path placeholders when setting HTML
     *    - Replaces `[[[___ESMX_DYNAMIC_BASE___]]]/your-app-name/` with the actual base path
     *    - Ensures all static asset reference paths are correct
     *
     * 2. **Use Cases**
     *    - Set HTML content generated by server-side rendering
     *    - Support dynamic base path configuration
     *    - Automatically handle static asset reference paths
     *
     * @example
     * ```ts
     * // 1. Basic usage
     * export default async (rc: RenderContext) => {
     *   // Set HTML content
     *   rc.html = `
     *     <!DOCTYPE html>
     *     <html>
     *       <head>
     *         ${rc.preload()}
     *         ${rc.css()}
     *       </head>
     *       <body>
     *         <div id="app">Hello World</div>
     *         ${rc.importmap()}
     *         ${rc.moduleEntry()}
     *         ${rc.modulePreload()}
     *       </body>
     *     </html>
     *   `;
     * };
     *
     * // 2. Dynamic base path
     * const rc = await esmx.render({
     *   base: '/app',  // Set base path
     *   params: { url: req.url }
     * });
     *
     * // Placeholders in HTML will be automatically replaced:
     * // [[[___ESMX_DYNAMIC_BASE___]]]/your-app-name/css/style.css
     * // Replaced with:
     * // /app/your-app-name/css/style.css
     * ```
     */
    public get html() {
        return this._html;
    }
    public set html(html) {
        const varName = this.esmx.basePathPlaceholder;
        this._html = varName
            ? html.replaceAll(this.esmx.basePathPlaceholder, this.base)
            : html;
    }
    public constructor(esmx: Esmx, options: RenderContextOptions = {}) {
        this.esmx = esmx;
        this.base = options.base ?? '';
        this.params = options.params ?? {};
        this.entryName = options.entryName ?? 'default';
        this.importmapMode = options.importmapMode ?? 'inline';
    }
    /**
     * Serialize JavaScript object to string
     * @description
     * The serialize method is used to serialize state data during the server-side rendering process for passing to the client:
     *
     * 1. **Main Uses**
     *    - Serialize server-side state data
     *    - Ensure data can be safely embedded in HTML
     *    - Support complex data structures (such as Date, RegExp, etc.)
     *
     * 2. **Security Handling**
     *    - Automatically escape special characters
     *    - Prevent XSS attacks
     *    - Maintain data type integrity
     *
     * @example
     * ```ts
     * // 1. Basic usage - Serialize state data
     * export default async (rc: RenderContext) => {
     *   const state = {
     *     user: { id: 1, name: 'Alice' },
     *     timestamp: new Date(),
     *     regex: /\d+/
     *   };
     *
     *   rc.html = `
     *     <!DOCTYPE html>
     *     <html>
     *     <head>
     *       <script>
     *         // Inject serialized state into global variable
     *         window.__INITIAL_STATE__ = ${rc.serialize(state)};
     *       </script>
     *     </head>
     *     <body>${html}</body>
     *     </html>
     *   `;
     * };
     *
     * // 2. Custom serialization options
     * const state = { sensitive: 'data' };
     * const serialized = rc.serialize(state, {
     *   isJSON: true,  // Use JSON compatible mode
     *   unsafe: false  // Disable unsafe serialization
     * });
     * ```
     *
     * @param {any} input - Input data to be serialized
     * @param {serialize.SerializeJSOptions} [options] - Serialization options
     * @returns {string} Serialized string
     */
    public serialize(
        input: any,
        options?: serialize.SerializeJSOptions
    ): string {
        return serialize(input, options);
    }
    /**
     * Serialize state data and inject it into HTML
     * @description
     * The state method is used to serialize state data and inject it into HTML during server-side rendering, so that the client can restore these states when activating:
     *
     * 1. **Serialization Mechanism**
     *    - Use safe serialization methods to process data
     *    - Support complex data structures (objects, arrays, etc.)
     *    - Automatically handle special characters and XSS protection
     *
     * 2. **Use Cases**
     *    - Synchronize server-side state to client
     *    - Initialize client application state
     *    - Implement seamless server-side rendering to client activation
     *
     * @param varName Global variable name, used to access injected data on the client
     * @param data Data object that needs to be serialized
     * @returns Script tag string containing serialized data
     *
     * @example
     * ```ts
     * // 1. Basic usage - Inject user information
     * export default async (rc: RenderContext) => {
     *   const userInfo = {
     *     id: 1,
     *     name: 'John',
     *     roles: ['admin']
     *   };
     *
     *   rc.html = `
     *     <!DOCTYPE html>
     *     <html>
     *     <head>
     *       ${rc.state('__USER__', userInfo)}
     *     </head>
     *     <body>
     *       <div id="app"></div>
     *     </body>
     *     </html>
     *   `;
     * };
     *
     * // 2. Client-side usage
     * // Can directly access injected data on the client
     * const userInfo = window.__USER__;
     * console.log(userInfo.name); // Output: 'John'
     *
     * // 3. Complex data structures
     * export default async (rc: RenderContext) => {
     *   const appState = {
     *     user: {
     *       id: 1,
     *       preferences: {
     *         theme: 'dark',
     *         language: 'zh-CN'
     *       }
     *     },
     *     settings: {
     *       notifications: true,
     *       timezone: 'Asia/Shanghai'
     *     }
     *   };
     *
     *   rc.html = `
     *     <!DOCTYPE html>
     *     <html>
     *     <head>
     *       ${rc.state('__APP_STATE__', appState)}
     *     </head>
     *     <body>
     *       <div id="app"></div>
     *     </body>
     *     </html>
     *   `;
     * };
     * ```
     */
    public state(varName: string, data: Record<string, any>): string {
        return `<script>window[${serialize(varName)}] = ${serialize(data, { isJSON: true })};</script>`;
    }
    /**
     * Commit dependency collection and update resource list
     * @description
     * The commit method is the core of RenderContext's dependency collection mechanism, responsible for handling all collected module dependencies and updating the file resource list:
     *
     * 1. **Dependency Processing Flow**
     *    - Collect all used modules from importMetaSet
     *    - Parse specific resources for each module based on manifest files
     *    - Handle different types of dependencies such as JS, CSS, resource files, etc.
     *    - Automatically handle module preloading and import maps
     *
     * 2. **Resource Classification**
     *    - js: JavaScript files, containing all scripts and modules
     *    - css: Stylesheet files
     *    - modulepreload: ESM modules that need to be preloaded
     *    - importmap: Import map files
     *    - resources: Other resource files (images, fonts, etc.)
     *
     * 3. **Path Processing**
     *    - Automatically add base path prefix
     *    - Ensure the correctness of resource paths
     *    - Support resource isolation for multi-application scenarios
     *
     * @example
     * ```ts
     * // 1. Basic usage
     * export default async (rc: RenderContext) => {
     *   // Render page and collect dependencies
     *   const app = createApp();
     *   const html = await renderToString(app, {
     *     importMetaSet: rc.importMetaSet
     *   });
     *
     *   // Commit dependency collection
     *   await rc.commit();
     *
     *   // Generate HTML
     *   rc.html = `
     *     <!DOCTYPE html>
     *     <html>
     *     <head>
     *       ${rc.preload()}
     *       ${rc.css()}
     *     </head>
     *     <body>
     *       ${html}
     *       ${rc.importmap()}
     *       ${rc.moduleEntry()}
     *       ${rc.modulePreload()}
     *     </body>
     *     </html>
     *   `;
     * };
     *
     * // 2. Multi-application scenario
     * const rc = await esmx.render({
     *   base: '/app1',  // Set base path
     *   params: { appId: 1 }
     * });
     *
     * // Render and commit dependencies
     * const html = await renderApp(rc);
     * await rc.commit();
     *
     * // Resource paths will automatically add base path prefix
     * // For example: /app1/your-app-name/js/main.js
     * ```
     */
    public async commit() {
        const { esmx } = this;
        const chunkSet = new Set([`${esmx.name}@src/entry.client.ts`]);
        for (const item of this.importMetaSet) {
            if ('chunkName' in item && typeof item.chunkName === 'string') {
                chunkSet.add(item.chunkName);
            }
        }
 
        const files: {
            [K in keyof RenderFiles]: Set<string>;
        } = {
            js: new Set(),
            modulepreload: new Set(),
            css: new Set(),
            resources: new Set()
        };
 
        const getUrlPath = (...paths: string[]) =>
            path.posix.join('/', this.base, ...paths);
 
        const manifests = await this.esmx.getManifestList('client');
        manifests.forEach((item) => {
            const addPath = (setName: keyof RenderFiles, filepath: string) =>
                files[setName].add(getUrlPath(item.name, filepath));
            const addPaths = (
                setName: keyof RenderFiles,
                filepaths: string[]
            ) => filepaths.forEach((filepath) => addPath(setName, filepath));
            Object.entries(item.chunks).forEach(([filepath, info]) => {
                if (chunkSet.has(filepath)) {
                    addPath('js', info.js);
                    addPaths('css', info.css);
                    addPaths('resources', info.resources);
                }
            });
        });
 
        const paths = await esmx.getStaticImportPaths(
            'client',
            `${esmx.name}/src/entry.client`
        );
        paths?.forEach((filepath) =>
            files.modulepreload.add(getUrlPath(filepath))
        );
 
        files.js = new Set([...files.js, ...files.modulepreload]);
        Object.keys(files).forEach(
            (key) => (this.files[key] = Array.from(files[key]))
        );
        this._importMap = await esmx.getImportMapClientInfo(this.importmapMode);
    }
    /**
     * Generate resource preload tags
     * @description
     * The preload() method is used to generate resource preload tags, optimizing page performance by loading critical resources in advance:
     *
     * 1. **Resource Types**
     *    - CSS files: Use `as="style"` to preload stylesheets
     *    - JS files: Use `as="script"` to preload import map scripts
     *
     * 2. **Performance Optimization**
     *    - Discover and load critical resources in advance
     *    - Load in parallel with HTML parsing
     *    - Optimize resource loading order
     *    - Reduce page rendering blocking
     *
     * 3. **Best Practices**
     *    - Use as early as possible in the head
     *    - Only preload resources necessary for the current page
     *    - Use in conjunction with other resource loading methods
     *
     * @returns Returns HTML string containing all preload tags
     *
     * @example
     * ```ts
     * // Use in HTML head
     * rc.html = `
     *   <!DOCTYPE html>
     *   <html>
     *   <head>
     *     <!-- Preload critical resources -->
     *     ${rc.preload()}
     *     <!-- Inject stylesheets -->
     *     ${rc.css()}
     *   </head>
     *   <body>
     *     ${html}
     *     ${rc.importmap()}
     *     ${rc.moduleEntry()}
     *     ${rc.modulePreload()}
     *   </body>
     *   </html>
     * `;
     * ```
     */
    public preload() {
        const { files, _importMap } = this;
        const list = files.css.map((url) => {
            return `<link rel="preload" href="${url}" as="style">`;
        });
        if (_importMap.src) {
            list.push(
                `<link rel="preload" href="${_importMap.src}" as="script">`
            );
        }
        return list.join('');
    }
    /**
     * Inject first-screen stylesheets
     * @description
     * The css() method is used to inject stylesheet resources required by the page:
     *
     * 1. **Injection Position**
     *    - Must be injected in the head tag
     *    - Avoid page flickering (FOUC) and reflow
     *    - Ensure styles are in place when content is rendered
     *
     * 2. **Performance Optimization**
     *    - Support critical CSS extraction
     *    - Automatically handle style dependency relationships
     *    - Utilize browser parallel loading capabilities
     *
     * 3. **Use Cases**
     *    - Inject styles necessary for the first screen
     *    - Handle component-level styles
     *    - Support theme switching and dynamic styles
     *
     * @example
     * ```ts
     * // 1. Basic usage
     * rc.html = `
     *   <!DOCTYPE html>
     *   <html>
     *   <head>
     *     ${rc.preload()}  <!-- Preload resources -->
     *     ${rc.css()}      <!-- Inject stylesheets -->
     *   </head>
     *   <body>
     *     <div id="app">Hello World</div>
     *   </body>
     *   </html>
     * `;
     *
     * // 2. Use in conjunction with other resources
     * rc.html = `
     *   <!DOCTYPE html>
     *   <html>
     *   <head>
     *     ${rc.preload()}  <!-- Preload resources -->
     *     ${rc.css()}      <!-- Inject stylesheets -->
     *   </head>
     *   <body>
     *     ${html}
     *     ${rc.importmap()}
     *     ${rc.moduleEntry()}
     *     ${rc.modulePreload()}
     *   </body>
     *   </html>
     * `;
     * ```
     */
    public css() {
        return this.files.css
            .map((url) => `<link rel="stylesheet" href="${url}">`)
            .join('');
    }
    /**
     * Inject module import map
     * @description
     * The importmap() method is used to inject path resolution rules for ESM modules:
     *
     * 1. **Injection Position**
     *    - Must be injected in the body
     *    - Must be executed before moduleEntry
     *    - Avoid blocking the first page render
     *
     * 2. **Import Map Modes**
     *    - Inline mode (inline):
     *      - Inline map content directly into HTML
     *      - Suitable for scenarios with smaller map content
     *      - Reduce the number of HTTP requests
     *    - JS file mode (js):
     *      - Generate independent JS files
     *      - Suitable for scenarios with larger map content
     *      - Can utilize browser caching mechanisms
     *
     * 3. **Technical Reasons**
     *    - Define path resolution rules for ESM modules
     *    - Client entry modules and their dependencies need to use these maps
     *    - Ensure the map is correctly set before executing module code
     *
     * @example
     * ```ts
     * // 1. Basic usage - Inline mode
     * const rc = await esmx.render({
     *   importmapMode: 'inline'  // Default mode
     * });
     *
     * rc.html = `
     *   <!DOCTYPE html>
     *   <html>
     *   <head>
     *     ${rc.preload()}
     *     ${rc.css()}
     *   </head>
     *   <body>
     *     ${html}
     *     ${rc.importmap()}    <!-- Inject import map -->
     *     ${rc.moduleEntry()}  <!-- Execute after import map -->
     *     ${rc.modulePreload()}
     *   </body>
     *   </html>
     * `;
     *
     * // 2. JS file mode - Suitable for large applications
     * const rc = await esmx.render({
     *   importmapMode: 'js'  // Use JS file mode
     * });
     * ```
     */
    public importmap() {
        return this._importMap.code;
    }
    /**
     * Inject client entry module
     * @description
     * The moduleEntry() method is used to inject the client's entry module:
     * 1. **Injection Position**
     *    - Must be executed after importmap
     *    - Ensure the import map is correctly set before executing module code
     *    - Control the start timing of client activation (Hydration)
     *
     * 2. **Technical Reasons**
     *    - Serve as the entry point for client code
     *    - Need to wait for infrastructure (such as import maps) to be ready
     *    - Ensure correct module path resolution
     *
     * 3. **Use Cases**
     *    - Start the client application
     *    - Execute client activation
     *    - Initialize client state
     *
     * @example
     * ```ts
     * // 1. Basic usage
     * rc.html = `
     *   <!DOCTYPE html>
     *   <html>
     *   <head>
     *     ${rc.preload()}
     *     ${rc.css()}
     *   </head>
     *   <body>
     *     ${html}
     *     ${rc.importmap()}    <!-- Inject import map first -->
     *     ${rc.moduleEntry()}  <!-- Then inject entry module -->
     *     ${rc.modulePreload()}
     *   </body>
     *   </html>
     * `;
     *
     * // 2. Multiple entry configuration
     * const rc = await esmx.render({
     *   entryName: 'mobile',  // Specify entry name
     *   params: { device: 'mobile' }
     * });
     * ```
     */
    public moduleEntry() {
        return `<script type="module">import "${this.esmx.name}/src/entry.client";</script>`;
    }
 
    /**
     * Preload module dependencies
     * @description
     * The modulePreload() method is used to preload modules that may be needed later:
     *
     * 1. **Injection Position**
     *    - Must be after importmap and moduleEntry
     *    - Ensure the correct module path mapping is used
     *    - Avoid competing with first-screen rendering for resources
     *
     * 2. **Performance Optimization**
     *    - Preload modules that may be needed later
     *    - Improve runtime performance
     *    - Optimize on-demand loading experience
     *
     * 3. **Technical Reasons**
     *    - Need correct path resolution rules
     *    - Avoid duplicate loading
     *    - Control loading priority
     *
     * @example
     * ```ts
     * // 1. Basic usage
     * rc.html = `
     *   <!DOCTYPE html>
     *   <html>
     *   <head>
     *     ${rc.preload()}
     *     ${rc.css()}
     *   </head>
     *   <body>
     *     ${html}
     *     ${rc.importmap()}
     *     ${rc.moduleEntry()}
     *     ${rc.modulePreload()}  <!-- Preload module dependencies -->
     *   </body>
     *   </html>
     * `;
     *
     * // 2. Use with async components
     * const AsyncComponent = defineAsyncComponent(() =>
     *   import('./components/AsyncComponent.vue')
     * );
     * // modulePreload will automatically collect and preload dependencies of async components
     * ```
     */
    public modulePreload() {
        return this.files.modulepreload
            .map((url) => `<link rel="modulepreload" href="${url}">`)
            .join('');
    }
}