constserver=http.createServer((req,res)=>{constpathname=url.parse(req.url).pathnameif(pathname==='/__hmrProxy'){sendJS(res,hmrProxy)}elseif(pathname.endsWith('.vue')){vue(req,res)}else{serve(req,res)}})// .....
server.listen(3000,()=>{console.log('Running at http://localhost:3000')})
constfileWatcher=chokidar.watch(process.cwd(),{ignored:[/node_modules/]})fileWatcher.on('change',(file)=>{if(file.endsWith('.vue')){// check which part of the file changed
const[descriptor,prevDescriptor]=parseSFC(file)constresourcePath='/'+path.relative(process.cwd(),file)if(!prevDescriptor){// the file has never been accessed yet
return}if((descriptor.script&&descriptor.script.content)!==(prevDescriptor.script&&prevDescriptor.script.content)){console.log(`[hmr] <script> for ${resourcePath} changed. Triggering component reload.`)notify({type:'reload',path:resourcePath})return}if((descriptor.template&&descriptor.template.content)!==(prevDescriptor.template&&prevDescriptor.template.content)){console.log(`[hmr] <template> for ${resourcePath} changed. Triggering component re-render.`)notify({type:'rerender',path:resourcePath})return}// TODO styles
}else{console.log(`[hmr] script file ${resourcePath} changed. Triggering full page reload.`)notify({type:'full-reload'})}})