diff --git a/package.json b/package.json index ee254b9..092cbce 100644 --- a/package.json +++ b/package.json @@ -18,9 +18,10 @@ "changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 2" }, "dependencies": { + "@rollup/pluginutils": "^4.1.0", "debug": "^4.1.1", "hash-sum": "^2.0.0", - "rollup-pluginutils": "^2.8.2" + "source-map": "^0.6.1" }, "peerDependencies": { "@vue/compiler-sfc": "*" @@ -38,6 +39,7 @@ "lint-staged": "^10.1.7", "npm-run-all": "^4.1.5", "prettier": "^2.0.5", + "pug": "^3.0.0", "rollup": "^2.7.2", "rollup-plugin-postcss": "^3.1.8", "ts-jest": "^26.0.0", @@ -55,7 +57,6 @@ }, "prettier": { "printWidth": 80, - "trailingComma": "es5", "semi": false, "singleQuote": true } diff --git a/src/handleHotUpdate.ts b/src/handleHotUpdate.ts new file mode 100644 index 0000000..6ae29ac --- /dev/null +++ b/src/handleHotUpdate.ts @@ -0,0 +1,192 @@ +import fs from 'fs' +import _debug from 'debug' +import { parse, SFCBlock, SFCDescriptor } from '@vue/compiler-sfc' +import { + getDescriptor, + setDescriptor, + setPrevDescriptor, +} from './utils/descriptorCache' +import { getResolvedScript, setResolvedScript } from './script' + +const debug = _debug('vite:hmr') + +/** + * Vite-specific HMR handling + */ +export async function handleHotUpdate(file: string, modules: any[]) { + if (!file.endsWith('.vue')) { + return + } + + const prevDescriptor = getDescriptor(file) + if (!prevDescriptor) { + // file hasn't been requested yet (e.g. async component) + return + } + + let content = fs.readFileSync(file, 'utf-8') + if (!content) { + await untilModified(file) + content = fs.readFileSync(file, 'utf-8') + } + + const { descriptor } = parse(content, { + filename: file, + sourceMap: true, + }) + setDescriptor(file, descriptor) + setPrevDescriptor(file, prevDescriptor) + + let needRerender = false + const filteredModules = new Set() + const mainModule = modules.find( + (m) => !/type=/.test(m.id) || /type=script/.test(m.id) + ) + const templateModule = modules.find((m) => /type=template/.test(m.id)) + + if ( + !isEqualBlock(descriptor.script, prevDescriptor.script) || + !isEqualBlock(descriptor.scriptSetup, prevDescriptor.scriptSetup) + ) { + filteredModules.add(mainModule) + } + + if (!isEqualBlock(descriptor.template, prevDescriptor.template)) { + // when a