Application-Level Module System
Counterparts native ESM to replace fragile relative imports in business layers. One-line host ignition boots an autonomous Mesh universe where lifecycles and business logic circulate 100% internally.
Honoring Spring's foundational philosophy in native TypeScript. Zero decorators, zero reflection metadata, microsecond cold starts, and lock-free DAG topology.
Real-World Architecture: Module A (remoteConfig) performs an async HTTP handshake on startup to fetch encrypted configuration, exposing a pure synchronous function isEnabled(feature). Module B (orderService) must synchronously invoke Module A's function during its own instantiation to enable or disable features (Module B has an unnegotiable initialization prerequisite on Module A).
// Spring: Relies on OS threads to block the startup thread until remote config is fetched
@Configuration
public class AppConfig {
@Bean
public RemoteConfig remoteConfig() {
RemoteConfig config = new RemoteConfig();
config.initFromRemote(secretKey); // Blocks the current thread until config is ready
return config;
}
@Bean
public OrderService orderService(RemoteConfig remoteConfig) {
// Module B synchronously invokes Module A during container wiring
boolean enableDiscount = remoteConfig.isEnabled("vip_discount");
return new OrderService(enableDiscount);
}
}// Pain Point: Class constructors cannot await! Using onModuleInit causes race condition silent bugs.
// The OOP @Injectable() class syntax collapses and is forced to regress into verbose useFactory dictionaries:
@Module({
providers: [
{
provide: "REMOTE_CONFIG",
useFactory: async () => {
const config = await fetchRemoteConfig(process.env.SECRET_KEY);
return { isEnabled: (feature: string) => config[feature] ?? false };
},
},
{
provide: "ORDER_SERVICE",
useFactory: (config: RemoteConfig) => {
// Module B must write glue code and manually manage string tokens in the fragile inject array
const enableDiscount = config.isEnabled("vip_discount");
return new OrderService(enableDiscount);
},
inject: ["REMOTE_CONFIG"],
},
],
})
export class AppModule {}// Module A (src/modules/remoteConfig/index.ts): Native async function initialization, returns sync methods
export const main = async () => {
const config = await fetchRemoteConfig(process.env.SECRET_KEY);
return {
isEnabled(feature: string) {
return config[feature] ?? false;
}, // Data ready in closure, pure sync!
};
};
// Module B (src/modules/orderService/index.ts): Declare short-name dependency, consume synchronously
export const dependencies = ["remoteConfig"];
export const main = (container: ModularContainer) => {
const { remoteConfig } = container;
// Topological engine guarantees: remoteConfig's async task is 100% resolved before waking up orderService!
const enableDiscount = remoteConfig.isEnabled("vip_discount");
return {
createOrder(item: string) {
return { item, price: enableDiscount ? 80 : 100 };
},
};
};