Skip to content

Commit

Permalink
fix: improve virual vue hmr
Browse files Browse the repository at this point in the history
  • Loading branch information
antfu committed May 7, 2021
1 parent 4d9f6ad commit ca1f5be
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 31 deletions.
55 changes: 38 additions & 17 deletions packages/slidev/node/plugins/loaders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,15 @@ function prepareSlideInfo(data: SlideInfo): SlideInfoExtended {
}
}

export function createSlidesLoader({ data, entry, clientRoot, themeRoots, userRoot }: ResolvedSlidevOptions, pluginOptions: SlidevPluginOptions): Plugin[] {
export function createSlidesLoader({ data, entry, clientRoot, themeRoots, userRoot }: ResolvedSlidevOptions, pluginOptions: SlidevPluginOptions, VuePlugin: Plugin): Plugin[] {
const slidePrefix = '/@slidev/slides/'
const hmrNextModuleIds: string[] = []
const hmrPages = new Set<number>()

const entryId = slash(entry)

return [
{
name: 'slidev:loader',

configureServer(server) {
server.watcher.add(entry)

Expand All @@ -90,7 +89,7 @@ export function createSlidesLoader({ data, entry, clientRoot, themeRoots, userRo
const body = await getBodyJson(req)
Object.assign(data.slides[idx], body)

hmrNextModuleIds.push(`${slidePrefix}${idx + 1}.md`)
hmrPages.add(idx)

server.ws.send({
type: 'custom',
Expand All @@ -117,11 +116,7 @@ export function createSlidesLoader({ data, entry, clientRoot, themeRoots, userRo

const newData = await parser.load(entry)

const moduleIds: (string | false)[] = [
...hmrNextModuleIds,
]

hmrNextModuleIds.length = 0
const moduleIds: string[] = []

if (data.slides.length !== newData.slides.length)
moduleIds.push('/@slidev/routes')
Expand All @@ -138,28 +133,47 @@ export function createSlidesLoader({ data, entry, clientRoot, themeRoots, userRo
const length = Math.max(data.slides.length, newData.slides.length)

for (let i = 0; i < length; i++) {
if (hmrPages.has(i))
continue

const a = data.slides[i]
const b = newData.slides[i]

if (a?.content.trim() === b?.content.trim() && JSON.stringify(a.frontmatter) === JSON.stringify(b.frontmatter))
continue
hmrPages.add(i)
}

moduleIds.push(
`${slidePrefix}${i + 1}.md`,
`${slidePrefix}${i + 1}.json`,
const modules = (
await Promise.all(
Array.from(hmrPages)
.map(async(i) => {
const id = `${slidePrefix}${i + 1}.md`
const module = ctx.server.moduleGraph.getModuleById(id)

return await VuePlugin.handleHotUpdate!({
...ctx,
modules: Array.from(module?.importedModules || []),
file: id,
read() {
return newData.slides[i - 1]?.raw
},
},
)
}),
)
}
).flatMap(i => i || [])

hmrPages.clear()

const moduleEntries = moduleIds
.filter(isTruthy)
.map(id => ctx.server.moduleGraph.getModuleById(id as string))
.map(id => ctx.server.moduleGraph.getModuleById(id))
.filter(notNullish)
.concat(modules)

pluginOptions.onDataReload?.(newData, data)

Object.assign(data, newData)

moduleEntries.map(m => ctx.server.moduleGraph.invalidateModule(m))
return moduleEntries
},

Expand Down Expand Up @@ -227,6 +241,13 @@ export function createSlidesLoader({ data, entry, clientRoot, themeRoots, userRo
}
},
},
{
name: 'xxx',
handleHotUpdate(i) {
if (i.file.endsWith('.vue'))
console.dir(i.modules)
},
},
]

async function getLayouts() {
Expand Down
30 changes: 16 additions & 14 deletions packages/slidev/node/plugins/preset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,27 @@ export async function ViteSlidevPlugin(
data: { config },
} = options

return [
createWindiCSSPlugin(options, pluginOptions),

Vue({
include: [/\.vue$/, /\.md$/],
exclude: [],
template: {
compilerOptions: {
isCustomElement(tag) {
return customElements.has(tag)
},
const VuePlugin = Vue({
include: [/\.vue$/, /\.md$/],
exclude: [],
template: {
compilerOptions: {
isCustomElement(tag) {
return customElements.has(tag)
},
},
...vueOptions,
}),
},
...vueOptions,
})

return [
createWindiCSSPlugin(options, pluginOptions),
await createMarkdownPlugin(options, pluginOptions),

VuePlugin,

createSlidesLoader(options, pluginOptions, VuePlugin),

ViteComponents({
extensions: ['vue', 'md', 'ts'],

Expand Down Expand Up @@ -124,7 +127,6 @@ export async function ViteSlidevPlugin(
}),
createConfigPlugin(options),
createEntryPlugin(options),
createSlidesLoader(options, pluginOptions),
createClientSetupPlugin(options),
createMonacoTypesLoader(),
createFixPlugins(options),
Expand Down

0 comments on commit ca1f5be

Please sign in to comment.