Skip to content

Commit f1b8848

Browse files
fix: do not crash if hmr fails (#3117)
1 parent 4150b2b commit f1b8848

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

.changeset/ai-lazy-tiger.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"@module-federation/node": patch
3+
---
4+
5+
Improved error handling and cache clearing process in hot-reload utility.
6+
7+
- Added error handling with try-catch block to manage potential exceptions when clearing the path cache.
8+
- Replaced direct use of module with a reference to currentChunk for cache path operations.
9+
- Ensured compatibility with TypeScript by adding @ts-ignore annotations where necessary.
10+
```

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"prepare": "husky install",
5050
"changeset": "changeset",
5151
"build:packages": "npx nx affected -t build --parallel=10 --exclude='*,!tag:type:pkg'",
52-
"changegen": "./changeset-gen.js --path ./packages/enhanced --staged && ./changeset-gen.js --path ./packages/runtime --staged && ./changeset-gen.js --path ./packages/data-prefetch --staged && ./changeset-gen.js --path ./packages/nextjs-mf --staged",
52+
"changegen": "./changeset-gen.js --path ./packages/enhanced --staged &&./changeset-gen.js --path ./packages/node --staged && ./changeset-gen.js --path ./packages/runtime --staged && ./changeset-gen.js --path ./packages/data-prefetch --staged && ./changeset-gen.js --path ./packages/nextjs-mf --staged",
5353
"commitgen:staged": "./commit-gen.js --path ./packages --staged",
5454
"commitgen:main": "./commit-gen.js --path ./packages"
5555
},

packages/node/src/utils/hot-reload.ts

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,21 +63,28 @@ const decache = async function (moduleName: string) {
6363
return;
6464
}
6565

66+
const currentChunk = getRequire().cache[__filename];
67+
6668
// Run over the cache looking for the files
6769
// loaded by the specified module name
6870
searchCache(moduleName, function (mod: NodeModule) {
6971
delete getRequire().cache[mod.id];
7072
});
71-
72-
// Remove cached paths to the module.
73-
// Thanks to @bentael for pointing this out.
74-
Object.keys((module.constructor as any)._pathCache).forEach(
75-
function (cacheKey) {
76-
if (cacheKey.indexOf(moduleName) > -1) {
77-
delete (module.constructor as any)._pathCache[cacheKey];
78-
}
79-
},
80-
);
73+
try {
74+
// Remove cached paths to the module.
75+
// Thanks to @bentael for pointing this out.
76+
//@ts-ignore
77+
Object.keys((currentChunk.constructor as any)._pathCache).forEach(
78+
function (cacheKey) {
79+
if (cacheKey.indexOf(moduleName) > -1) {
80+
//@ts-ignore
81+
delete (currentChunk.constructor as any)._pathCache[cacheKey];
82+
}
83+
},
84+
);
85+
} catch (error) {
86+
//null
87+
}
8188
};
8289

8390
/**

0 commit comments

Comments
 (0)