Skip to content

Commit 246c197

Browse files
committed
fix: clear require cache after upgrade, before migrate
1 parent ef15316 commit 246c197

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

packages/@vue/cli/lib/Upgrader.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ const PackageManager = require('./util/ProjectPackageManager')
2121

2222
const { runMigrator } = require('./migrate')
2323

24+
function clearRequireCache () {
25+
Object.keys(require.cache).forEach(key => delete require.cache[key])
26+
}
27+
2428
module.exports = class Upgrader {
2529
constructor (context = process.cwd()) {
2630
this.context = context
@@ -104,12 +108,16 @@ module.exports = class Upgrader {
104108

105109
log(`Upgrading ${packageName} from ${installed} to ${targetVersion}`)
106110
await this.pm.upgrade(`${packageName}@~${targetVersion}`)
111+
// as the dependencies have now changed, the require cache must be invalidated
112+
// otherwise it may affect the behavior of the migrator
113+
clearRequireCache()
107114

108115
// The cached `pkg` field won't automatically update after running `this.pm.upgrade`.
109116
// Also, `npm install pkg@~version` won't replace the original `"pkg": "^version"` field.
110117
// So we have to manually update `this.pkg` and write to the file system in `runMigrator`
111118
this.pkg[depEntry][packageName] = `~${targetVersion}`
112119
const noop = () => {}
120+
113121
const pluginMigrator =
114122
loadModule(`${packageName}/migrator`, this.context) || noop
115123

0 commit comments

Comments
 (0)