Skip to content

Commit 51a55cd

Browse files
committed
fix($core): keep createTemp from desyncing webpack
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.
1 parent 2287920 commit 51a55cd

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

packages/@vuepress/core/lib/node/createTemp.js

+15-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
const cryptoRandomString = require('crypto-random-string')
12
const { fs, path, chalk, logger } = require('@vuepress/shared-utils')
23

4+
// Generate a unique file name whose presence in a temporary directory indicates
5+
// that the directory was initialized by this instance of VuePress.
6+
const shibbolethFileName = `.vuepress-${cryptoRandomString({ length: 16, type: 'hex' })}`
7+
38
/**
49
* Create a dynamic temp utility context that allow to lanuch
510
* multiple apps with isolated context at the same time.
@@ -17,10 +22,16 @@ module.exports = function createTemp (tempPath) {
1722
tempPath = path.resolve(tempPath)
1823
}
1924

20-
if (!fs.existsSync(tempPath)) {
21-
fs.ensureDirSync(tempPath)
22-
} else {
23-
fs.emptyDirSync(tempPath)
25+
// Ensure the temporary directory exists and was initialized by this instance
26+
// of VuePress, by checking for the presence of the shibboleth file. Avoid
27+
// emptying the temporary directory if it was previously initialized by this
28+
// instance of VuePress; otherwise, webpack-dev-server can lose track of the
29+
// paths it's watching.
30+
const shibbolethFilePath = path.join(tempPath, shibbolethFileName)
31+
console.log('tempPath', tempPath)
32+
if (!fs.existsSync(shibbolethFilePath)) {
33+
//fs.emptyDirSync(tempPath)
34+
fs.writeFileSync(shibbolethFilePath, '')
2435
}
2536

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

0 commit comments

Comments
 (0)