Skip to content

Commit

Permalink
feat(vite): add watch options
Browse files Browse the repository at this point in the history
  • Loading branch information
innocenzi committed Feb 16, 2022
1 parent a273e35 commit a16bde0
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 23 deletions.
18 changes: 0 additions & 18 deletions vite-plugin-laravel/src/blade.ts

This file was deleted.

9 changes: 5 additions & 4 deletions vite-plugin-laravel/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { PluginOption, UserConfig } from 'vite'
import { manifest } from './manifest'
import { config } from './config'
import { blade } from './blade'
import { reload } from './reload'
import { callArtisan, callShell, findPhpPath } from './utils'
import type { Options } from './types'

export function defineConfig(base: UserConfig = {}) {
Expand All @@ -10,18 +11,18 @@ export function defineConfig(base: UserConfig = {}) {
plugins: [
...base?.plugins as any,
config(),
blade(),
reload(),
manifest(),
],
}
}

export const laravel = (options: Options = {}): PluginOption[] => [
blade(),
reload(options),
config(options),
manifest(),
]

export { manifest, blade, config }
export { manifest, reload, config, callArtisan, callShell, findPhpPath }

export default laravel
62 changes: 62 additions & 0 deletions vite-plugin-laravel/src/reload.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { Plugin, ViteDevServer } from 'vite'
import makeDebugger from 'debug'
import { Options, WatchOptions } from './types'

const PREFIX = 'vite:laravel:reload'
const debug = makeDebugger(PREFIX)

/**
* Reload when some files are changed.
*/
export const reload = (options: Options = {}): Plugin => {
const watchOptions: Required<WatchOptions> = {
input: [],
reloadOnBladeUpdates: true,
reloadOnConfigUpdates: true,
...(Array.isArray(options.watch) ? { input: options.watch } : options.watch),
}

debug('Given options:', options)
debug('Resolved options:', watchOptions)

// When the config change, we want a full module graph
// invalidation as well as a full reload
watchOptions.input.push({
condition: (file) => file.endsWith('config/vite.php'),
handle: ({ server }) => {
debug('Configuration file changed, invalidating module graph and reloading')
server.moduleGraph.invalidateAll()
server.ws.send({ type: 'full-reload', path: '*' })
},
})

// Blade files
watchOptions.input.push({
condition: (file) => file.endsWith('.blade.php'),
handle: ({ server }) => {
debug('Blade file changed, reloading')
server.ws.send({ type: 'full-reload', path: '*' })
},
})

function handleReload(file: string, server: ViteDevServer) {
file = file.replaceAll('\\', '/')

watchOptions.input.forEach((value) => {
if (value.condition(file)) {
debug(`${file} changed, applying its handler`)
value.handle({ file, server })
}
})
}

return {
name: 'vite:laravel:reload',
configureServer(server) {
server.watcher
.on('add', (path) => handleReload(path, server))
.on('change', (path) => handleReload(path, server))
.on('unlink', (path) => handleReload(path, server))
},
}
}
23 changes: 22 additions & 1 deletion vite-plugin-laravel/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { SSROptions } from 'vite'
import type { SSROptions, ViteDevServer } from 'vite'

export interface ServerConfiguration {
default: keyof ServerConfiguration['configs']
Expand Down Expand Up @@ -69,6 +69,27 @@ export interface Options {
* @default true
*/
allowOverrides?: boolean

/**
* List of file changes to listen to.
*/
watch?: WatchInput[] | WatchOptions
}

export interface WatchOptions {
reloadOnBladeUpdates?: boolean
reloadOnConfigUpdates?: boolean
input?: WatchInput[]
}

export interface WatchInputHandlerParameters {
file: string
server: ViteDevServer
}

export interface WatchInput {
condition: (file: string) => boolean
handle: (parameters: WatchInputHandlerParameters) => void
}

export interface Certificates {
Expand Down

0 comments on commit a16bde0

Please # to comment.