Skip to content

Commit

Permalink
fix: Clear correct cache when compiling with Webpack
Browse files Browse the repository at this point in the history
  • Loading branch information
Mordred committed Sep 24, 2021
1 parent 1376eec commit 73f784e
Showing 1 changed file with 9 additions and 12 deletions.
21 changes: 9 additions & 12 deletions packages/server/src/util.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Use __non_webpack_require__ to prevent Webpack from compiling it
// when the server-side code is compiled with Webpack
// eslint-disable-next-line camelcase, no-undef, global-require, import/no-dynamic-require, no-eval
const getRequire = () => typeof __non_webpack_require__ !== 'undefined' ? __non_webpack_require__ : eval('require');

export const clearModuleCache = moduleName => {
const m = require.cache[moduleName]
const { cache } = getRequire();
const m = cache[moduleName]
if (m) {
// remove self from own parents
if (m.parent && m.parent.children) {
Expand All @@ -13,7 +19,7 @@ export const clearModuleCache = moduleName => {
}
})
}
delete require.cache[moduleName]
delete cache[moduleName]
}
}

Expand All @@ -22,16 +28,7 @@ export const smartRequire = modulePath => {
clearModuleCache(modulePath)
}

// Use __non_webpack_require__ to prevent Webpack from compiling it
// when the server-side code is compiled with Webpack
// eslint-disable-next-line camelcase
if (typeof __non_webpack_require__ !== 'undefined') {
// eslint-disable-next-line no-undef
return __non_webpack_require__(modulePath)
}

// eslint-disable-next-line global-require, import/no-dynamic-require, no-eval
return eval('require')(modulePath)
return getRequire(modulePath)
}

export const joinURLPath = (publicPath, filename) => {
Expand Down

0 comments on commit 73f784e

Please sign in to comment.