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 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import crypto from 'node:crypto';
import fs from 'node:fs';
import fsp from 'node:fs/promises';
import path from 'node:path';
import { cwd } from 'node:process';
import { pathToFileURL } from 'node:url';
import type { ImportMap, ScopesMap, SpecifierMap } from '@esmx/import';
import serialize from 'serialize-javascript';
import { type App, createApp } from './app';
import { getManifestList, type ManifestJson } from './manifest-json';
import {
type ModuleConfig,
type ParsedModuleConfig,
parseModuleConfig
} from './module-config';
import {
type PackConfig,
type ParsedPackConfig,
parsePackConfig
} from './pack-config';
import type {
ImportmapMode,
RenderContext,
RenderContextOptions
} from './render-context';
import { type CacheHandle, createCache } from './utils/cache';
import { generateSizeReport } from './utils/file-size-stats';
import { createClientImportMap, createImportMap } from './utils/import-map';
import type { Middleware } from './utils/middleware';
import { type ProjectPath, resolvePath } from './utils/resolve-path';
import { getImportPreloadInfo as getStaticImportPaths } from './utils/static-import-lexer';
/**
* Core configuration options interface for the Esmx framework
*/
export interface EsmxOptions {
/**
* Project root directory path
* - Can be absolute or relative path
* - Defaults to current working directory (process.cwd())
*/
root?: string;
/**
* Whether it is production environment
* - true: Production environment
* - false: Development environment
* - Defaults to process.env.NODE_ENV === 'production'
*/
isProd?: boolean;
/**
* Base path placeholder configuration
* - string: Custom placeholder
* - false: Disable placeholder
* - Default value is '[[[___ESMX_DYNAMIC_BASE___]]]'
* - Used for dynamically replacing the base path of assets at runtime
*/
basePathPlaceholder?: string | false;
/**
* Module configuration options
* - Used to configure module resolution rules for the project
* - Includes module aliases, external dependencies, etc.
*/
modules?: ModuleConfig;
/**
* Package configuration options
* - Used to package build artifacts into standard npm .tgz format packages
* - Includes output path, package.json handling, packaging hooks, etc.
*/
packs?: PackConfig;
/**
* Development environment application creation function
* - Only used in development environment
* - Used to create application instance for development server
* @param esmx Esmx instance
*/
devApp?: (esmx: Esmx) => Promise<App>;
/**
* Server startup configuration function
* - Used to configure and start HTTP server
* - Can be used in both development and production environments
* @param esmx Esmx instance
*/
server?: (esmx: Esmx) => Promise<void>;
/**
* Post-build processing function
* - Executed after project build is completed
* - Can be used to perform additional resource processing, deployment, etc.
* @param esmx Esmx instance
*/
postBuild?: (esmx: Esmx) => Promise<void>;
}
/**
* Application build target types.
* - client: Client build target, used to generate code that runs in the browser
* - server: Server build target, used to generate code that runs in Node.js environment
*/
export type BuildEnvironment = 'client' | 'server';
/**
* Command enumeration for the Esmx framework.
* Used to control the runtime mode and lifecycle of the framework.
*/
export enum COMMAND {
/**
* Development mode
* Starts development server with hot reload support
*/
dev = 'dev',
/**
* Build mode
* Generates production build artifacts
*/
build = 'build',
/**
* Preview mode
* Preview build artifacts
*/
preview = 'preview',
/**
* Start mode
* Starts production environment server
*/
start = 'start'
}
export type { ImportMap, SpecifierMap, ScopesMap };
/**
* Initialization status interface for Esmx framework instance
* @internal For framework internal use only
*
* @description
* This interface defines the status data after framework instance initialization, including:
* - Application instance: Handles requests and rendering
* - Current command: Controls runtime mode
* - Module configuration: Parsed module settings
* - Package configuration: Parsed build settings
* - Cache handling: Framework internal caching mechanism
*/
interface Readied {
/** Application instance, providing middleware and rendering functionality */
app: App;
/** Currently executing framework command */
command: COMMAND;
/** Parsed module configuration information */
moduleConfig: ParsedModuleConfig;
/** Parsed package configuration information */
packConfig: ParsedPackConfig;
/** Cache handler */
cache: CacheHandle;
}
export class Esmx {
// Basic properties and constructor
private readonly _options: EsmxOptions;
private _readied: Readied | null = null;
private _importmapHash: string | null = null;
private get readied() {
if (this._readied) {
return this._readied;
}
throw new NotReadyError();
}
/**
* Get module name
* @returns {string} The name of the current module, sourced from module configuration
* @throws {NotReadyError} Throws error when framework instance is not initialized
*/
public get name(): string {
return this.moduleConfig.name;
}
/**
* Get module variable name
* @returns {string} A valid JavaScript variable name generated based on the module name
* @throws {NotReadyError} Throws error when framework instance is not initialized
*/
public get varName(): string {
return '__' + this.name.replace(/[^a-zA-Z]/g, '_') + '__';
}
/**
* Get the absolute path of the project root directory
* @returns {string} The absolute path of the project root directory
* If the configured root is a relative path, it is resolved to an absolute path based on the current working directory
*/
public get root(): string {
const { root = cwd() } = this._options;
if (path.isAbsolute(root)) {
return root;
}
return path.resolve(cwd(), root);
}
/**
* Determine if currently in production environment
* @returns {boolean} Environment flag
* Prioritizes the isProd in configuration, if not configured, judges based on process.env.NODE_ENV
*/
public get isProd(): boolean {
return this._options?.isProd ?? process.env.NODE_ENV === 'production';
}
/**
* Get the base path of the module
* @returns {string} The base path of the module starting and ending with a slash
* Used to construct the access path for module assets
*/
public get basePath(): string {
return `/${this.name}/`;
}
/**
* Get the base path placeholder
* @returns {string} Base path placeholder or empty string
* Used for dynamically replacing the base path of the module at runtime, can be disabled through configuration
*/
public get basePathPlaceholder(): string {
const varName = this._options.basePathPlaceholder;
if (varName === false) {
return '';
}
return varName ?? '[[[___ESMX_DYNAMIC_BASE___]]]';
}
/**
* Get the currently executing command
* @returns {COMMAND} The command enumeration value currently being executed
* @throws {NotReadyError} Throws error when calling this method if the framework instance is not initialized
*/
public get command(): COMMAND {
return this.readied.command;
}
/**
* Get the command enumeration type
* @returns {typeof COMMAND} Command enumeration type definition
*/
public get COMMAND(): typeof COMMAND {
return COMMAND;
}
/**
* Get module configuration information
* @returns {ParsedModuleConfig} Complete configuration information of the current module
*/
public get moduleConfig(): ParsedModuleConfig {
return this.readied.moduleConfig;
}
/**
* Get package configuration information
* @returns {ParsedPackConfig} Package-related configuration of the current module
*/
public get packConfig(): ParsedPackConfig {
return this.readied.packConfig;
}
/**
* Get the static asset processing middleware for the application.
*
* This middleware is responsible for handling static asset requests for the application,
* providing different implementations based on the runtime environment:
* - Development environment: Supports real-time compilation and hot reloading of source code, uses no-cache strategy
* - Production environment: Handles built static assets, supports long-term caching for immutable files
*
* @returns {Middleware} Returns the static asset processing middleware function
* @throws {NotReadyError} Throws error when calling this method if the framework instance is not initialized
*
* @example
* ```ts
* const server = http.createServer((req, res) => {
* // Use middleware to handle static asset requests
* esmx.middleware(req, res, async () => {
* const rc = await esmx.render({ url: req.url });
* res.end(rc.html);
* });
* });
* ```
*/
public get middleware(): Middleware {
return this.readied.app.middleware;
}
/**
* Get the server-side rendering function for the application.
*
* This function is responsible for executing server-side rendering,
* providing different implementations based on the runtime environment:
* - Development environment: Loads server entry file from source code, supports hot reloading and real-time preview
* - Production environment: Loads built server entry file, provides optimized rendering performance
*
* @returns {(options?: RenderContextOptions) => Promise<RenderContext>} Returns the server-side rendering function
* @throws {NotReadyError} Throws error when calling this method if the framework instance is not initialized
*
* @example
* ```ts
* // Basic usage
* const rc = await esmx.render({
* params: { url: req.url }
* });
* res.end(rc.html);
*
* // Advanced configuration
* const rc = await esmx.render({
* base: '', // Set base path
* importmapMode: 'inline', // Set import map mode
* entryName: 'default', // Specify render entry
* params: {
* url: req.url,
* state: { user: 'admin' }
* }
* });
* ```
*/
public get render(): (
options?: RenderContextOptions
) => Promise<RenderContext> {
return this.readied.app.render;
}
public constructor(options: EsmxOptions = {}) {
this._options = options;
}
/**
* Initialize the Esmx framework instance.
*
* This method executes the following core initialization process:
* 1. Parse project configuration (package.json, module configuration, package configuration, etc.)
* 2. Create application instance (development or production environment)
* 3. Execute corresponding lifecycle methods based on the command
*
* @param command - Framework running command
* - dev: Start development server with hot reload support
* - build: Build production artifacts
* - preview: Preview build artifacts
* - start: Start production environment server
*
* @returns Returns true for successful initialization
* @throws {Error} Throws error when initializing repeatedly
*
* @example
* ```ts
* // entry.node.ts
* import type { EsmxOptions } from '@esmx/core';
*
* export default {
* // Development environment configuration
* async devApp(esmx) {
* return import('@esmx/rspack').then((m) =>
* m.createRspackHtmlApp(esmx, {
* config(context) {
* // Custom Rspack configuration
* }
* })
* );
* },
*
* // HTTP server configuration
* async server(esmx) {
* const server = http.createServer((req, res) => {
* // Static file handling
* esmx.middleware(req, res, async () => {
* // Pass rendering parameters
* const render = await esmx.render({
* params: { url: req.url }
* });
* // Respond with HTML content
* res.end(render.html);
* });
* });
*
* // Listen to port
* server.listen(3000, () => {
* console.log('http://localhost:3000');
* });
* }
* } satisfies EsmxOptions;
* ```
*/
public async init(command: COMMAND): Promise<boolean> {
if (this._readied) {
throw new Error('Cannot be initialized repeatedly');
}
const { name } = await this.readJson(
path.resolve(this.root, 'package.json')
);
const moduleConfig = parseModuleConfig(
name,
this.root,
this._options.modules
);
const packConfig = parsePackConfig(this._options.packs);
this._readied = {
command,
app: {
middleware() {
throw new NotReadyError();
},
async render() {
throw new NotReadyError();
}
},
moduleConfig,
packConfig,
cache: createCache(this.isProd)
};
const devApp = this._options.devApp || defaultDevApp;
const app: App = [COMMAND.dev, COMMAND.build].includes(command)
? await devApp(this)
: await createApp(this, command);
this.readied.app = app;
switch (command) {
case COMMAND.dev:
case COMMAND.start:
await this.server();
break;
case COMMAND.build:
return this.build();
case COMMAND.preview:
break;
}
return true;
}
/**
* Destroy the Esmx framework instance, performing resource cleanup and connection closing operations.
*
* This method is mainly used for resource cleanup in development environment, including:
* - Closing development servers (such as Rspack Dev Server)
* - Cleaning up temporary files and cache
* - Releasing system resources
*
* Note: In general, the framework automatically handles resource release, users do not need to manually call this method.
* Only use it when custom resource cleanup logic is needed.
*
* @returns Returns a Promise that resolves to a boolean value
* - true: Cleanup successful or no cleanup needed
* - false: Cleanup failed
*
* @example
* ```ts
* // Use when custom cleanup logic is needed
* process.once('SIGTERM', async () => {
* await esmx.destroy(); // Clean up resources
* process.exit(0);
* });
* ```
*/
public async destroy(): Promise<boolean> {
const { readied } = this;
if (readied.app?.destroy) {
return readied.app.destroy();
}
return true;
}
/**
* Execute the application's build process.
*
* This method is responsible for executing the entire application build process, including:
* - Compiling source code
* - Generating production build artifacts
* - Optimizing and compressing code
* - Generating asset manifests
*
* The build process prints start and end times, as well as total duration and other information.
*
* @returns Returns a Promise that resolves to a boolean value
* - true: Build successful or build method not implemented
* - false: Build failed
*
* @throws {NotReadyError} Throws error when calling this method if the framework instance is not initialized
*
* @example
* ```ts
* // entry.node.ts
* import type { EsmxOptions } from '@esmx/core';
*
* export default {
* // Development environment configuration
* async devApp(esmx) {
* return import('@esmx/rspack').then((m) =>
* m.createRspackHtmlApp(esmx, {
* config(context) {
* // Custom Rspack configuration
* }
* })
* );
* },
*
* // Post-build processing
* async postBuild(esmx) {
* // Generate static HTML after build completion
* const render = await esmx.render({
* params: { url: '/' }
* });
* esmx.writeSync(
* esmx.resolvePath('dist/client', 'index.html'),
* render.html
* );
* }
* } satisfies EsmxOptions;
* ```
*/
public async build(): Promise<boolean> {
const startTime = Date.now();
const successful = await this.readied.app.build?.();
const endTime = Date.now();
const duration = endTime - startTime;
const status = successful
? '\x1b[32m✓\x1b[0m'.padEnd(3)
: '\x1b[31m✗\x1b[0m'.padEnd(3);
console.log(
`${status.padEnd(2)} Build ${successful ? 'completed' : 'failed'} in ${duration}ms`
);
return successful ?? true;
}
/**
* Start HTTP server and configure server instance.
*
* This method is called in the following lifecycle of the framework:
* - Development environment (dev): Start development server, providing features like hot reload
* - Production environment (start): Start production server, providing production-grade performance
*
* The specific implementation of the server is provided by the user through the server configuration function in EsmxOptions.
* This function is responsible for:
* - Creating HTTP server instance
* - Configuring middleware and routes
* - Handling requests and responses
* - Starting server listening
*
* @returns Returns a Promise that resolves when the server startup is complete
* @throws {NotReadyError} Throws error when calling this method if the framework instance is not initialized
*
* @example
* ```ts
* // entry.node.ts
* import http from 'node:http';
* import type { EsmxOptions } from '@esmx/core';
*
* export default {
* // Server configuration
* async server(esmx) {
* const server = http.createServer((req, res) => {
* // Handle static assets
* esmx.middleware(req, res, async () => {
* // Server-side rendering
* const render = await esmx.render({
* params: { url: req.url }
* });
* res.end(render.html);
* });
* });
*
* // Start server
* server.listen(3000, () => {
* console.log('Server running at http://localhost:3000');
* });
* }
* } satisfies EsmxOptions;
* ```
*/
public async server(): Promise<void> {
await this._options?.server?.(this);
}
/**
* Execute post-build processing logic.
*
* This method is called after the application build is completed, used to perform additional resource processing, such as:
* - Generating static HTML files
* - Processing build artifacts
* - Executing deployment tasks
* - Sending build notifications
*
* The method automatically captures and handles exceptions during execution, ensuring it does not affect the main build process.
*
* @returns Returns a Promise that resolves to a boolean value
* - true: Post-processing successful or no processing needed
* - false: Post-processing failed
*
* @example
* ```ts
* // entry.node.ts
* import type { EsmxOptions } from '@esmx/core';
*
* export default {
* // Post-build processing
* async postBuild(esmx) {
* // Generate static HTML for multiple pages
* const pages = ['/', '/about', '/404'];
*
* for (const url of pages) {
* const render = await esmx.render({
* params: { url }
* });
*
* // Write static HTML file
* esmx.writeSync(
* esmx.resolvePath('dist/client', url.substring(1), 'index.html'),
* render.html
* );
* }
* }
* } satisfies EsmxOptions;
* ```
*/
public async postBuild(): Promise<boolean> {
try {
await this._options.postBuild?.(this);
return true;
} catch (e) {
console.error(e);
return false;
}
}
/**
* Resolve project relative path to absolute path
*
* @param projectPath - Project path type, such as 'dist/client', 'dist/server', etc.
* @param args - Path segments to be concatenated
* @returns Resolved absolute path
*
* @example
* ```ts
* // Used in entry.node.ts
* async postBuild(esmx) {
* const outputPath = esmx.resolvePath('dist/client', 'index.html');
* // Output: /project/root/dist/client/index.html
* }
* ```
*/
public resolvePath(projectPath: ProjectPath, ...args: string[]): string {
return resolvePath(this.root, projectPath, ...args);
}
/**
* Write file content synchronously
*
* @param filepath - Absolute path of the file
* @param data - Data to be written, can be string, Buffer or object
* @returns Whether the write was successful
*
* @example
* ```ts
* // Used in entry.node.ts
* async postBuild(esmx) {
* const htmlPath = esmx.resolvePath('dist/client', 'index.html');
* const success = esmx.writeSync(htmlPath, '<html>...</html>');
* }
* ```
*/
public writeSync(filepath: string, data: any): boolean {
try {
// Ensure the target directory exists
fs.mkdirSync(path.dirname(filepath), { recursive: true });
// Write file
fs.writeFileSync(filepath, data);
return true;
} catch {
return false;
}
}
/**
* Write file content asynchronously
*
* @param filepath - Absolute path of the file
* @param data - Data to be written, can be string, Buffer or object
* @returns Promise<boolean> Whether the write was successful
*
* @example
* ```ts
* // Used in entry.node.ts
* async postBuild(esmx) {
* const htmlPath = esmx.resolvePath('dist/client', 'index.html');
* const success = await esmx.write(htmlPath, '<html>...</html>');
* }
* ```
*/
public async write(filepath: string, data: any): Promise<boolean> {
try {
// Ensure the target directory exists
await fsp.mkdir(path.dirname(filepath), { recursive: true });
// Write file
await fsp.writeFile(filepath, data);
return true;
} catch {
return false;
}
}
/**
* Read and parse JSON file synchronously
*
* @template T - Expected JSON object type to return
* @param filename - Absolute path of the JSON file
* @returns {T} Parsed JSON object
* @throws Throws exception when file does not exist or JSON format is incorrect
*
* @example
* ```ts
* // Used in entry.node.ts
* async server(esmx) {
* const manifest = esmx.readJsonSync<Manifest>(esmx.resolvePath('dist/client', 'manifest.json'));
* // Use manifest object
* }
* ```
*/
public readJsonSync<T = any>(filename: string): T {
return JSON.parse(fs.readFileSync(filename, 'utf-8'));
}
/**
* Read and parse JSON file asynchronously
*
* @template T - Expected JSON object type to return
* @param filename - Absolute path of the JSON file
* @returns {Promise<T>} Parsed JSON object
* @throws Throws exception when file does not exist or JSON format is incorrect
*
* @example
* ```ts
* // Used in entry.node.ts
* async server(esmx) {
* const manifest = await esmx.readJson<Manifest>(esmx.resolvePath('dist/client', 'manifest.json'));
* // Use manifest object
* }
* ```
*/
public async readJson<T = any>(filename: string): Promise<T> {
return JSON.parse(await fsp.readFile(filename, 'utf-8'));
}
/**
* Get build manifest list
*
* @description
* This method is used to get the build manifest list for the specified target environment, including the following features:
* 1. **Cache Management**
* - Uses internal caching mechanism to avoid repeated loading
* - Returns immutable manifest list
*
* 2. **Environment Adaptation**
* - Supports both client and server environments
* - Returns corresponding manifest information based on the target environment
*
* 3. **Module Mapping**
* - Contains module export information
* - Records resource dependency relationships
*
* @param env - Target environment type
* - 'client': Client environment
* - 'server': Server environment
* @returns Returns read-only build manifest list
* @throws {NotReadyError} Throws error when calling this method if the framework instance is not initialized
*
* @example
* ```ts
* // Used in entry.node.ts
* async server(esmx) {
* // Get client build manifest
* const manifests = await esmx.getManifestList('client');
*
* // Find build information for a specific module
* const appModule = manifests.find(m => m.name === 'my-app');
* if (appModule) {
* console.log('App exports:', appModule.exports);
* console.log('App chunks:', appModule.chunks);
* }
* }
* ```
*/
public async getManifestList(
env: BuildEnvironment
): Promise<readonly ManifestJson[]> {
return this.readied.cache(`getManifestList-${env}`, async () =>
Object.freeze(await getManifestList(env, this.moduleConfig))
);
}
/**
* Get import map object
*
* @description
* This method is used to generate ES module import maps with the following features:
* 1. **Module Resolution**
* - Generate module mappings based on build manifests
* - Support both client and server environments
* - Automatically handle module path resolution
*
* 2. **Cache Optimization**
* - Use internal caching mechanism
* - Return immutable mapping objects
*
* 3. **Path Handling**
* - Automatically handle module paths
* - Support dynamic base paths
*
* @param env - Target environment type
* - 'client': Generate import map for browser environment
* - 'server': Generate import map for server environment
* @returns Returns read-only import map object
* @throws {NotReadyError} Throws error when calling this method if the framework instance is not initialized
*
* @example
* ```ts
* // Used in entry.node.ts
* async server(esmx) {
* // Get client import map
* const importmap = await esmx.getImportMap('client');
*
* // Custom HTML template
* const html = `
* <!DOCTYPE html>
* <html>
* <head>
* <script type="importmap">
* ${JSON.stringify(importmap)}
* </script>
* </head>
* <body>
* <!-- Page content -->
* </body>
* </html>
* `;
* }
* ```
*/
public async getImportMap(
env: BuildEnvironment
): Promise<Readonly<ImportMap>> {
return this.readied.cache(`getImportMap-${env}`, async () => {
const { moduleConfig } = this.readied;
const manifests = await this.getManifestList(env);
let json: ImportMap = {};
switch (env) {
case 'client': {
json = createClientImportMap({
manifests,
getScope(name, scope) {
return `/${name}${scope}`;
},
getFile(name, file) {
return `/${name}/${file}`;
}
});
break;
}
case 'server':
json = createImportMap({
manifests,
getScope: (name: string, scope: string) => {
const linkPath = moduleConfig.links[name].server;
// Get the real physical path instead of symbolic link
// This is crucial when generating import maps on the server side.
// If we use symbolic link paths as scopes, it would cause module resolution errors at runtime
// because the actual accessed paths are real physical paths, not the symbolic links.
// Using realpathSync ensures path consistency between import map generation and runtime resolution.
const realPath = fs.realpathSync(linkPath);
return pathToFileURL(path.join(realPath, scope))
.href;
},
getFile: (name: string, file: string) => {
const linkPath = moduleConfig.links[name].server;
// Get the real physical path instead of symbolic link
// This is crucial to maintain consistency with getScope function
// and ensure proper module resolution at runtime
const realPath = fs.realpathSync(linkPath);
return pathToFileURL(path.resolve(realPath, file))
.href;
}
});
break;
}
return Object.freeze(json);
});
}
/**
* Get client import map information
*
* @description
* This method is used to generate import map code for client environment, supporting two modes:
* 1. **Inline Mode (inline)**
* - Inline import map directly into HTML
* - Reduce additional network requests
* - Suitable for scenarios with smaller import maps
*
* 2. **JS File Mode (js)**
* - Generate standalone JS file
* - Support browser caching
* - Suitable for scenarios with larger import maps
*
* Core Features:
* - Automatically handle dynamic base paths
* - Support module path runtime replacement
* - Optimize caching strategy
* - Ensure module loading order
*
* @param mode - Import map mode
* - 'inline': Inline mode, returns HTML script tag
* - 'js': JS file mode, returns information with file path
* @returns Returns import map related information
* - src: URL of the JS file (only in js mode)
* - filepath: Local path of the JS file (only in js mode)
* - code: HTML script tag content
* @throws {NotReadyError} Throws error when calling this method if the framework instance is not initialized
*
* @example
* ```ts
* // Used in entry.node.ts
* async server(esmx) {
* const server = express();
* server.use(esmx.middleware);
*
* server.get('*', async (req, res) => {
* // Use JS file mode
* const result = await esmx.render({
* importmapMode: 'js',
* params: { url: req.url }
* });
* res.send(result.html);
* });
*
* // Or use inline mode
* server.get('/inline', async (req, res) => {
* const result = await esmx.render({
* importmapMode: 'inline',
* params: { url: req.url }
* });
* res.send(result.html);
* });
* }
* ```
*/
public async getImportMapClientInfo<T extends ImportmapMode>(
mode: T
): Promise<
T extends 'js'
? {
src: string;
filepath: string;
code: string;
}
: {
src: null;
filepath: null;
code: string;
}
> {
return this.readied.cache(
`getImportMap-${mode}`,
async (): Promise<any> => {
const importmap = await this.getImportMap('client');
const { basePathPlaceholder } = this;
let filepath: string | null = null;
if (this._importmapHash === null) {
let wrote = false;
const code = `(() => {
const base = document.currentScript.getAttribute("data-base");
const importmap = ${serialize(importmap, { isJSON: true })};
const set = (data) => {
if (!data) return;
Object.entries(data).forEach(([k, v]) => {
data[k] = base + v;
});
};
set(importmap.imports);
if (importmap.scopes) {
Object.values(importmap.scopes).forEach(set);
}
const script = document.createElement("script");
script.type = "importmap";
script.innerText = JSON.stringify(importmap);
document.head.appendChild(script);
})();`;
const hash = contentHash(code);
filepath = this.resolvePath(
'dist/client/importmap',
`${hash}.final.mjs`
);
try {
const existingContent = await fsp.readFile(
filepath,
'utf-8'
);
if (existingContent === code) {
wrote = true;
} else {
wrote = await this.write(filepath, code);
}
} catch {
wrote = await this.write(filepath, code);
}
this._importmapHash = wrote ? hash : '';
}
if (mode === 'js' && this._importmapHash) {
const src = `${basePathPlaceholder}${this.basePath}importmap/${this._importmapHash}.final.mjs`;
return {
src,
filepath,
code: `<script data-base="${basePathPlaceholder}" src="${src}"></script>`
};
}
if (basePathPlaceholder) {
const set = (data?: Record<string, string>) => {
if (!data) return;
Object.entries(data).forEach(([k, v]) => {
data[k] = basePathPlaceholder + v;
});
};
set(importmap.imports);
if (importmap.scopes) {
Object.values(importmap.scopes).forEach(set);
}
}
return {
src: null,
filepath: null,
code: `<script type="importmap">${serialize(importmap, { isJSON: true, unsafe: true })}</script>`
};
}
);
}
/**
* Get the list of static import paths for a module.
*
* @param env - Build target ('client' | 'server')
* @param specifier - Module specifier
* @returns Returns the list of static import paths, returns null if not found
* @throws {NotReadyError} Throws error when calling this method if the framework instance is not initialized
*
* @example
* ```ts
* // Get static import paths for client entry module
* const paths = await esmx.getStaticImportPaths(
* 'client',
* `your-app-name/src/entry.client`
* );
* ```
*/
public async getStaticImportPaths(
env: BuildEnvironment,
specifier: string
) {
return this.readied.cache(
`getStaticImportPaths-${env}-${specifier}`,
async () => {
const result = await getStaticImportPaths(
specifier,
await this.getImportMap(env),
this.moduleConfig
);
if (!result) {
return null;
}
return Object.freeze(Object.values(result));
}
);
}
/**
* Generate bundle size analysis report for the build artifacts.
*
* @returns {{ text: string, json: object }} Report with text and JSON formats
*
* @example
* ```ts
* const report = esmx.generateSizeReport();
* console.log(report.text);
* console.log(`Total files: ${report.json.totalFiles}`);
* ```
*/
public generateSizeReport() {
return generateSizeReport(
this.resolvePath('dist'),
'{client,server,node}/**/!(.*)'
);
}
}
/**
* Default development environment application creation function
*
* @description
* This is a default placeholder function that throws an error when the development environment application creation function is not configured.
* In actual use, the actual application creation function should be configured through EsmxOptions.devApp.
*
* @throws {Error} Throws an error when devApp is not configured, prompting the user to set up the development environment application creation function
* @returns {Promise<App>} Will not actually return, always throws an error
*
* @example
* ```ts
* // Correct usage is to provide devApp in the configuration
* const options: EsmxOptions = {
* devApp: async (esmx) => {
* return import('@esmx/rspack').then(m =>
* m.createRspackHtmlApp(esmx)
* );
* }
* };
* ```
*/
async function defaultDevApp(): Promise<App> {
throw new Error("'devApp' function not set");
}
/**
* Esmx framework not initialized error
*
* @description
* This error is thrown in the following situations:
* - Accessing methods or properties that require initialization before calling init()
* - Attempting to use core functionality when the framework is not fully initialized
* - Continuing to use framework functionality after destroying the instance
*
* @extends Error
*
* @example
* ```ts
* const esmx = new Esmx();
* try {
* // This will throw NotReadyError because it's not initialized yet
* await esmx.render();
* } catch (e) {
* if (e instanceof NotReadyError) {
* console.error('Framework not initialized');
* }
* }
* ```
*/
class NotReadyError extends Error {
constructor() {
super(`The Esmx has not been initialized yet`);
}
}
/**
* Calculate SHA-256 hash value of content
*
* @description
* This function is used for:
* - Generating unique identifiers for file content
* - Cache invalidation judgment
* - Generating filenames with content hash
*
* Features:
* - Uses SHA-256 algorithm to ensure hash uniqueness
* - Truncates to first 12 characters to balance uniqueness and length
* - Suitable for cache control and file version management
*
* @param {string} text - Text content to calculate hash for
* @returns {string} Returns 12-character hexadecimal hash string
*
* @example
* ```ts
* const content = 'some content';
* const hash = contentHash(content);
* // Output similar to: 'a1b2c3d4e5f6'
* ```
*/
function contentHash(text: string) {
const hash = crypto.createHash('sha256');
hash.update(text);
return hash.digest('hex').substring(0, 12);
}
|