Skip to content

Commit

Permalink
fix($core): keep createTemp from desyncing webpack
Browse files Browse the repository at this point in the history
Calling emptyDirSync every time createTemp is called - i.e, every time
App.process is called; i.e., every time source files update - throws off
webpack-dev-server, causing it to lose track of the source files and
subdirectories. This leads to a variety of sporadic errors and breaks
auto-refresh of frontmatter and configuration. Fix this by avoiding
emptying temporary directories when we can detect that they were
previously initialized by the same instance of the VuePress process.

Fixes vuejs#1283.
Fixes vuejs#2233.
Fixes vuejs#2254.
Fixes vuejs#2437.
  • Loading branch information
spinda committed Aug 11, 2020
1 parent 2287920 commit 51a55cd
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions packages/@vuepress/core/lib/node/createTemp.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const cryptoRandomString = require('crypto-random-string')
const { fs, path, chalk, logger } = require('@vuepress/shared-utils')

// Generate a unique file name whose presence in a temporary directory indicates
// that the directory was initialized by this instance of VuePress.
const shibbolethFileName = `.vuepress-${cryptoRandomString({ length: 16, type: 'hex' })}`

/**
* Create a dynamic temp utility context that allow to lanuch
* multiple apps with isolated context at the same time.
Expand All @@ -17,10 +22,16 @@ module.exports = function createTemp (tempPath) {
tempPath = path.resolve(tempPath)
}

if (!fs.existsSync(tempPath)) {
fs.ensureDirSync(tempPath)
} else {
fs.emptyDirSync(tempPath)
// Ensure the temporary directory exists and was initialized by this instance
// of VuePress, by checking for the presence of the shibboleth file. Avoid
// emptying the temporary directory if it was previously initialized by this
// instance of VuePress; otherwise, webpack-dev-server can lose track of the
// paths it's watching.
const shibbolethFilePath = path.join(tempPath, shibbolethFileName)
console.log('tempPath', tempPath)
if (!fs.existsSync(shibbolethFilePath)) {
//fs.emptyDirSync(tempPath)
fs.writeFileSync(shibbolethFilePath, '')
}

logger.debug(`Temp directory: ${chalk.gray(tempPath)}`)
Expand Down

0 comments on commit 51a55cd

Please sign in to comment.