修复因端口占用导致的文件监听重复的bug

pull/1/head 0.6.2
yutent 2023-04-24 09:33:33 +08:00
parent e5a76f21ab
commit 5b3a976fd3
2 changed files with 50 additions and 48 deletions

View File

@ -295,60 +295,62 @@ export default async function createServer(root = '', conf = {}) {
PORT, PORT,
DEPLOY_PATH DEPLOY_PATH
) )
}) chokidar
.watch([SOURCE_DIR, PUBLIC_DIR, join(root, './index.html')])
.on('all', (act, filePath) => {
if (ready) {
let file = filePath.slice(SOURCE_DIR.length)
chokidar if (act === 'add' || act === 'change') {
.watch([SOURCE_DIR, PUBLIC_DIR, join(root, './index.html')]) let ext = file.slice(file.lastIndexOf('.') + 1)
.on('all', (act, filePath) => {
if (ready) {
let file = filePath.slice(SOURCE_DIR.length)
if (act === 'add' || act === 'change') { switch (ext) {
let ext = file.slice(file.lastIndexOf('.') + 1) case 'css':
case 'scss':
{
let content = fs.cat(filePath).toString()
ws.send({
action: 'render',
data: { path: file.replace(/\\/g, '/'), content }
})
}
break
switch (ext) { case 'vue':
case 'css': {
case 'scss': let content = compileVue(filePath, conf.imports, {
{ IS_MPA,
let content = fs.cat(filePath).toString() currentPage,
ws.send({ SOURCE_DIR,
action: 'render', CACHE,
data: { path: file.replace(/\\/g, '/'), content } DEPLOY_PATH
}) })
} let tmp = CACHE[filePath]
break if (tmp.changed) {
ws.send({ action: 'reload' })
} else {
ws.send({
action: 'render',
data: {
path: file.replace(/\\/g, '/'),
content: tmp.css
}
})
}
}
break
case 'vue': default:
{
let content = compileVue(filePath, conf.imports, {
IS_MPA,
currentPage,
SOURCE_DIR,
CACHE,
DEPLOY_PATH
})
let tmp = CACHE[filePath]
if (tmp.changed) {
ws.send({ action: 'reload' }) ws.send({ action: 'reload' })
} else { break
ws.send({
action: 'render',
data: { path: file.replace(/\\/g, '/'), content: tmp.css }
})
}
} }
break } else if (act === 'unlink' || act === 'unlinkDir') {
default:
ws.send({ action: 'reload' }) ws.send({ action: 'reload' })
break }
} }
} else if (act === 'unlink' || act === 'unlinkDir') { })
ws.send({ action: 'reload' }) .on('ready', () => {
} ready = true
} })
})
.on('ready', () => {
ready = true
}) })
} }

View File

@ -1,7 +1,7 @@
{ {
"name": "fite", "name": "fite",
"type": "module", "type": "module",
"version": "0.6.1", "version": "0.6.2",
"bin": { "bin": {
"fite": "index.js" "fite": "index.js"
}, },