Skip to content

Commit f7f18d7

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 f7f18d7

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

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

+13-3
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,15 @@ module.exports = function createTemp (tempPath) {
1722
tempPath = path.resolve(tempPath)
1823
}
1924

20-
if (!fs.existsSync(tempPath)) {
21-
fs.ensureDirSync(tempPath)
22-
} else {
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+
if (!fs.existsSync(shibbolethFilePath)) {
2332
fs.emptyDirSync(tempPath)
33+
fs.writeFileSync(shibbolethFilePath, '')
2434
}
2535

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

packages/@vuepress/core/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"copy-webpack-plugin": "^5.0.2",
3939
"core-js": "^3.6.4",
4040
"cross-spawn": "^6.0.5",
41+
"crypto-random-string": "^3.2.0",
4142
"css-loader": "^2.1.1",
4243
"file-loader": "^3.0.1",
4344
"js-yaml": "^3.13.1",

0 commit comments

Comments
 (0)