Skip to content

Commit

Permalink
fix: clear require cache after upgrade, before migrate
Browse files Browse the repository at this point in the history
  • Loading branch information
sodatea committed Feb 6, 2020
1 parent ef15316 commit 246c197
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/@vue/cli/lib/Upgrader.js
Expand Up @@ -21,6 +21,10 @@ const PackageManager = require('./util/ProjectPackageManager')

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

function clearRequireCache () {
Object.keys(require.cache).forEach(key => delete require.cache[key])
}

module.exports = class Upgrader {
constructor (context = process.cwd()) {
this.context = context
Expand Down Expand Up @@ -104,12 +108,16 @@ module.exports = class Upgrader {

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

// The cached `pkg` field won't automatically update after running `this.pm.upgrade`.
// Also, `npm install pkg@~version` won't replace the original `"pkg": "^version"` field.
// So we have to manually update `this.pkg` and write to the file system in `runMigrator`
this.pkg[depEntry][packageName] = `~${targetVersion}`
const noop = () => {}

const pluginMigrator =
loadModule(`${packageName}/migrator`, this.context) || noop

Expand Down

0 comments on commit 246c197

Please sign in to comment.