Esmx is a high-performance web application framework built on Rspack. It provides complete application lifecycle management, static asset handling, and server-side rendering capabilities.
Runtime environment type:
client: runs in the browser, supports DOM operations and browser APIsserver: runs in Node.js, supports file system and server-side featuresES module import map type.
Module specifier mapping type that defines import path mappings.
Scope mapping type that defines module import mappings under specific scopes.
Command type enum:
dev: development command, starts the dev server with HMRbuild: build command, produces production build outputspreview: preview command, starts a local preview serverstart: start command, runs the production serverDefines the core configuration options of the Esmx framework.
stringprocess.cwd()Project root directory path. Can be absolute or relative; relative paths are resolved against the current working directory.
booleanprocess.env.NODE_ENV === 'production'Environment flag.
true: production environmentfalse: development environmentstring | false'[[[___ESMX_DYNAMIC_BASE___]]]'Base path placeholder configuration. Used to dynamically replace the base path for assets at runtime. Set to false to disable.
ModuleConfigModule configuration options. Configure module resolution rules such as aliases and external dependencies.
PackConfigPackaging configuration options. Packages build outputs into standard npm .tgz packages.
(esmx: Esmx) => Promise<App>Development environment app creation function. Used only in development to create the dev server app instance.
(esmx: Esmx) => Promise<void>Server startup configuration function. Configures and starts an HTTP server, usable in both development and production.
(esmx: Esmx) => Promise<void>Post-build handler executed after the project build completes. Useful for:
stringtrueCurrent module name from the module configuration.
stringtrueValid JavaScript variable name derived from the module name.
stringtrueAbsolute path to the project root. If root is relative, it is resolved from the current working directory.
booleantrueIndicates whether the current environment is production. Prefers the isProd option; otherwise derives from process.env.NODE_ENV.
stringtrueNotReadyError when the framework is not initializedReturns the module base path that begins and ends with a slash. The format is /${name}/ where name comes from the module configuration.
stringtrueReturns the base path placeholder used for runtime replacement. Can be disabled via configuration.
MiddlewaretrueReturns the static asset handling middleware. Provides different implementations for each environment:
(options?: RenderContextOptions) => Promise<RenderContext>trueReturns the server-side rendering function. Environment-specific behaviors:
typeof COMMANDtrueProvides the command enum type definition.
ParsedModuleConfigtrueNotReadyError when the framework is not initializedReturns the full module configuration, including resolution rules and alias settings.
ParsedPackConfigtrueNotReadyError when the framework is not initializedReturns the packaging-related configuration, including output paths and package.json processing.
options?: EsmxOptions – framework configuration optionsEsmxCreates an Esmx framework instance.
command: COMMANDPromise<boolean>Error: when initialized repeatedlyNotReadyError: when accessing an uninitialized instanceInitializes the Esmx framework instance. Performs the following core steps:
package.json, module config, pack config)NotReadyError when accessing an uninitialized instance
:::Promise<boolean>Destroys the Esmx framework instance, performing resource cleanup and connection shutdown. Commonly used for:
Promise<boolean>Executes the application build process, including:
::: warning Note
Throws NotReadyError if called before the framework is initialized.
Promise<void>NotReadyError when the framework is not initializedStarts the HTTP server and configures the server instance. Invoked in these lifecycles:
dev): starts the dev server with HMRstart): starts the production server with production-grade performancePromise<boolean>Executes post-build logic, useful for:
Resolves project paths, converting relative paths to absolute ones.
Parameters:
projectPath: ProjectPath – project path type...args: string[] – path segmentsReturns: string – resolved absolute path
Example:
Synchronously writes file contents.
Parameters:
filepath: string – absolute file pathdata: any – data to write, string, Buffer, or objectReturns: boolean – whether the write succeeded
Example:
Synchronously reads and parses a JSON file.
Parameters:
filename: string – absolute path to the JSON fileReturns: any – parsed JSON object
Exceptions: throws when the file does not exist or JSON is invalid
Example:
Asynchronously reads and parses a JSON file.
Parameters:
filename: string – absolute path to the JSON fileReturns: Promise<any> – parsed JSON object
Exceptions: throws when the file does not exist or JSON is invalid
Example:
Retrieves the build manifest list.
Parameters:
target: BuildEnvironment – target environment
'client': client environment'server': server environmentReturns: Promise<readonly ManifestJson[]> – read-only build manifest list
Exceptions: throws NotReadyError when the framework is not initialized
Features:
Retrieves the import map object.
Parameters:
target: BuildEnvironment – target environment
'client': generates an import map for the browser environment'server': generates an import map for the server environmentReturns: Promise<Readonly<ImportMap>> – read-only import map
Exceptions: throws NotReadyError when the framework is not initialized
Features:
Retrieves client-side import map information.
Parameters:
mode: ImportmapMode – import map mode
'inline': inline mode, returns HTML script tag code'js': JS file mode, returns file path infoReturns:
Exceptions: throws NotReadyError when the framework is not initialized
Supports two modes for generating client import map code:
Core features:
handles dynamic base paths
supports runtime replacement of module paths
optimizes caching strategies
ensures module load order
Example:
Retrieves the list of static import paths for a module.
Parameters:
target: BuildEnvironment – build target
'client': client environment'server': server environmentspecifier: string – module specifierReturns: Promise<readonly string[] | null> – static import paths, or null if not found
Exceptions: throws NotReadyError when the framework is not initialized
Example: