Skip to content

Commit

Permalink
fix(watch): fix cache invalidation for deep dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
nfroidure committed Aug 9, 2020
1 parent 1622817 commit 86025c7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/whook-example/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
}
},
"scripts": {
"apitypes": "npm run --silent whook -- generateOpenAPISchema --authenticated=false | npm run --silent whook -- generateOpenAPITypes > src/openAPISchema.d.ts",
"apitypes": "npm run --silent whook -- generateOpenAPISchema --authenticated=true | npm run --silent whook -- generateOpenAPITypes > src/openAPISchema.d.ts",
"build": "npm run compile && NODE_ENV=${NODE_ENV:-development} node bin/build",
"cli": "env NODE_ENV=${NODE_ENV:-cli}",
"compile": "rimraf -f 'dist' && npm run compile:cjs && npm run compile:mjs",
Expand Down
22 changes: 20 additions & 2 deletions packages/whook-example/src/watch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ export async function watchDevServer() {

if (filePath.match(/package.*\.json/)) {
for (let key in require.cache) {
delete require.cache[key];
uncache(key);
}
} else {
delete require.cache[absolutePath];
uncache(absolutePath, true);
}

if (delay) {
Expand Down Expand Up @@ -116,3 +116,21 @@ export async function restartDevServer() {
log('warning', '🦄 - API types generated!');
}
}

function uncache(key: string, recursively = false) {
const module = require.cache[key];

if (!module) {
return;
}

if (!key.endsWith('.node')) {
delete require.cache[key];
}

if (!recursively) {
return;
}

uncache(module.parent.id);
}

0 comments on commit 86025c7

Please sign in to comment.