@@ -316,6 +316,47 @@ void ClearModuleRegistry() {
316316 g_moduleFallbackByRelative.clear ();
317317}
318318
319+ static std::string ModuleRegistryBasename (const std::string& key) {
320+ auto pos = key.find_last_of (" /\\ " );
321+ std::string base = pos == std::string::npos ? key : key.substr (pos + 1 );
322+ auto query = base.find (' ?' );
323+ if (query != std::string::npos) {
324+ base = base.substr (0 , query);
325+ }
326+ return base;
327+ }
328+
329+ static bool ShouldPreserveModuleOnApplicationReload (const std::string& key) {
330+ if (key.rfind (" node:" , 0 ) == 0 || key.rfind (" optional:" , 0 ) == 0 || key.rfind (" blob:" , 0 ) == 0 ) {
331+ return true ;
332+ }
333+ if (key.find (" /node_modules/" ) != std::string::npos ||
334+ key.find (" \\ node_modules\\ " ) != std::string::npos) {
335+ return true ;
336+ }
337+ const std::string base = ModuleRegistryBasename (key);
338+ return base == " vendor.mjs" || base == " vendor.js" || base == " runtime.mjs" ||
339+ base == " runtime.js" ;
340+ }
341+
342+ static void ClearRegistryMapForApplicationReload (
343+ std::unordered_map<std::string, v8::Global<v8::Module>>& registry) {
344+ for (auto it = registry.begin (); it != registry.end ();) {
345+ if (ShouldPreserveModuleOnApplicationReload (it->first )) {
346+ ++it;
347+ continue ;
348+ }
349+ it->second .Reset ();
350+ it = registry.erase (it);
351+ }
352+ }
353+
354+ void ClearModuleRegistryForApplicationReload () {
355+ ClearRegistryMapForApplicationReload (g_moduleRegistry);
356+ ClearRegistryMapForApplicationReload (g_moduleFallbackRegistry);
357+ ClearRegistryMapForApplicationReload (g_moduleFallbackByRelative);
358+ }
359+
319360void RemoveModuleFromRegistry (const std::string& canonicalPath) {
320361 // Defensive: never operate on an anomalous/sentinel key.
321362 // This covers the bare "@" anomaly and the special invalid-at stub module used by the dev HTTP
0 commit comments