Skip to content

Commit

Permalink
feat(vanilla): Support for hot updates when create files
Browse files Browse the repository at this point in the history
  • Loading branch information
Marinerer committed Jan 16, 2025
1 parent f092929 commit 598c46b
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions packages/vanilla/src/vanillaPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export function createVanillaPlugin(options: PluginOptions = {}): Plugin {
const opts = Object.assign({}, defaults, options) as Required<PluginOptions>
const pageRE = getPageRE(opts.suffix)
const pagePathRE = getPageRE(opts.suffix, false)
const basePathResolve = path.resolve(opts.base)

// 处理 transformIndexHtml 选项
const transformIndexHtmlHandler: IndexHtmlTransformHook = async (html, ctx) => {
Expand Down Expand Up @@ -140,13 +141,23 @@ export function createVanillaPlugin(options: PluginOptions = {}): Plugin {
watcher.add(filePath)
})

// 处理热更新
watcher.on('change', (_file) => {
function handleChangeFile(_file: string) {
server.ws.send({
type: 'full-reload',
path: '*',
})
})
}
function handleAddFile(file: string) {
const _path = path.resolve(file)
if (_path.includes(basePathResolve) && pageRE.test(_path)) {
server.restart()
}
}

// 处理热更新
watcher.on('change', handleChangeFile)
watcher.on('unlink', handleChangeFile)
watcher.on('add', handleAddFile)
},

transformIndexHtml:
Expand Down

0 comments on commit 598c46b

Please sign in to comment.