What is a Boundary?
A Boundary means a βreloadable limitβ. The HMR system operates by the following rules:- Boundary files: Files that can be reloaded on change (Model, API, Entity, etc.)
- Non-boundary files: Files that require a full server restart on change (config, environment variables, etc.)
Why are Boundaries Necessary?
Static imports execute only once when Node.js starts, so they cannot be replaced at runtime:Sonamuβs Boundary Configuration
By default, Sonamu sets all TypeScript files in the project as Boundaries:src/ are HMR-capable and automatically reload on change.
Boundary File Examples
Boundary files (HMR capable):user.model.ts- Model fileuser.api.ts- API fileuser.entity.ts- Entity definitionhelpers.ts- Utility functionsconstants.ts- Constants definition
sonamu.config.ts- Sonamu configuration.env- Environment variablespackage.json- Dependencies
Boundary Rules
1. Dynamic Import Required
Boundary files must be imported dynamically. β Wrong: Static importSonamu Handles This Automatically
Fortunately, Sonamuβs Syncer automatically handles dynamic imports for Entity-based files:2. Static Imports Between Boundaries Allowed
Static imports between Boundary files are allowed (Sonamuβs enhancement):- Both files are Boundaries
- Both are dynamically loaded by Syncer
- HMR works correctly even when they reference each other
3. Non-boundary to Boundary Import Restriction
HMR wonβt work if a non-boundary file statically imports a Boundary:Practical Scenarios
Using HMR in User-Post Relationship
Situation: Adding arole field to User Entity and adding author permission check in Post API.
Step 1: Modify Entity
- Updates
user.entity.ts - Regenerates
user.types.ts - Reloads
UserModel
- Reloads
UserModel - Reloads all APIs that use
UserModel
- Re-registers
PostApi.create - Testable immediately without server restart!
Traditional approach:
- Modify Entity
- Manually generate Types file
- Modify Model file
- Server restart (30 seconds)
- Modify API file
- Server restart (30 seconds)
- Test
- Modify Entity (auto-generated)
- Modify Model file (auto-reload)
- Modify API file (auto-reload)
- Test β
Complex Business Logic Development
Situation: Implementing inventory check, payment processing, and notification on order creation. File structure:import.meta.hot API
In Boundary files, you can use theimport.meta.hot API to finely control HMR behavior.
dispose() - Resource Cleanup
Perform cleanup before module reload:- Clear timers/intervals
- Remove event listeners
- Close WebSocket connections
- Clean up database connection pools
- Close file handles
decline() - Require Full Restart
Exclude specific modules from HMR and require full restart:- Global configuration files
- Database connection settings
- Singletons with complex initialization
- Modules with difficult state management
boundary Object - State Sharing
Share data between Boundaries to maintain state after reload:Type Definitions
To useimport.meta.hot in TypeScript, type definitions are needed:
Debugging
Check Boundary Configuration
Verify if a specific file is configured as a Boundary:Visualize Dependency Tree
Performance Optimization
Exclude Unnecessary Boundaries
Setting all files as Boundaries can slow things down due to a large dependency tree:Minimize Dependencies
Avoid circular references between Models and only import whatβs needed:Summary
Sonamuβs Boundary system: β Automatic handling: Syncer automatically handles dynamic imports for Entity-based files β Cross-boundary references allowed: Static imports between Models permitted β Fine-grained control: Resource cleanup and state maintenance viaimport.meta.hot API
β
Debugging tools: Check dependency tree with hot.dump()
In most cases, you donβt need to worry about Boundaries - Sonamu handles it automatically!