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 f7f18d7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
16 changes: 13 additions & 3 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,15 @@ module.exports = function createTemp (tempPath) {
tempPath = path.resolve(tempPath)
}

if (!fs.existsSync(tempPath)) {
fs.ensureDirSync(tempPath)
} else {
// 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)
if (!fs.existsSync(shibbolethFilePath)) {
fs.emptyDirSync(tempPath)
fs.writeFileSync(shibbolethFilePath, '')
}

logger.debug(`Temp directory: ${chalk.gray(tempPath)}`)
Expand Down
1 change: 1 addition & 0 deletions packages/@vuepress/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"copy-webpack-plugin": "^5.0.2",
"core-js": "^3.6.4",
"cross-spawn": "^6.0.5",
"crypto-random-string": "^3.2.0",
"css-loader": "^2.1.1",
"file-loader": "^3.0.1",
"js-yaml": "^3.13.1",
Expand Down

0 comments on commit f7f18d7

Please sign in to comment.