Skip to content

Commit d4040a6

Browse files
authored
[0.3.x] Support full page reload on blade / arbitrary file changes (#43)
* support full page reloads for blade files * rename option * style * link to original plugin
1 parent df5211e commit d4040a6

File tree

3 files changed

+219
-18
lines changed

3 files changed

+219
-18
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,8 @@
4343
},
4444
"engines": {
4545
"node": ">=14"
46+
},
47+
"dependencies": {
48+
"vite-plugin-full-reload": "^1.0.0"
4649
}
4750
}

src/index.ts

Lines changed: 85 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ import fs from 'fs'
22
import { AddressInfo } from 'net'
33
import path from 'path'
44
import colors from 'picocolors'
5-
import { Plugin, loadEnv, UserConfig, ConfigEnv, Manifest, ResolvedConfig, SSROptions, normalizePath } from 'vite'
5+
import { Plugin, loadEnv, UserConfig, ConfigEnv, Manifest, ResolvedConfig, SSROptions, normalizePath, PluginOption } from 'vite'
6+
import fullReload from 'vite-plugin-full-reload'
67

78
interface PluginConfig {
89
/**
@@ -35,6 +36,47 @@ interface PluginConfig {
3536
* @default 'storage/ssr'
3637
*/
3738
ssrOutputDirectory?: string
39+
40+
/**
41+
* Configuration for performing full page refresh on blade (or other) file changes.
42+
*
43+
* {@link https://github.com/ElMassimo/vite-plugin-full-reload}
44+
* @default false
45+
*/
46+
refresh?: boolean|string|string[]|FullReloadConfig|FullReloadConfig[]
47+
}
48+
49+
interface FullReloadConfig {
50+
paths: string[],
51+
config?: {
52+
/**
53+
* Whether full reload should happen regardless of the file path.
54+
*
55+
* @default true
56+
*/
57+
always?: boolean
58+
59+
/**
60+
* How many milliseconds to wait before reloading the page after a file change.
61+
*
62+
* @default 0
63+
*/
64+
delay?: number
65+
66+
/**
67+
* Whether to log when a file change triggers a full reload.
68+
*
69+
* @default true
70+
*/
71+
log?: boolean
72+
73+
/**
74+
* Files will be resolved against this path.
75+
*
76+
* @default process.cwd()
77+
*/
78+
root?: string
79+
}
3880
}
3981

4082
interface LaravelPlugin extends Plugin {
@@ -48,8 +90,16 @@ let exitHandlersBound = false
4890
*
4991
* @param config - A config object or relative path(s) of the scripts to be compiled.
5092
*/
51-
export default function laravel(config: string|string[]|PluginConfig): LaravelPlugin {
93+
export default function laravel(config: string|string[]|PluginConfig): [LaravelPlugin, ...Plugin[]] {
5294
const pluginConfig = resolvePluginConfig(config)
95+
96+
return [
97+
resolveLaravelPlugin(pluginConfig),
98+
...resolveFullReloadConfig(pluginConfig) as Plugin[],
99+
];
100+
}
101+
102+
function resolveLaravelPlugin(pluginConfig: Required<PluginConfig>): LaravelPlugin {
53103
let viteDevServerUrl: string
54104
let resolvedConfig: ResolvedConfig
55105
const cssManifest: Manifest = {}
@@ -250,12 +300,17 @@ function resolvePluginConfig(config: string|string[]|PluginConfig): Required<Plu
250300
config.ssrOutputDirectory = config.ssrOutputDirectory.trim().replace(/^\/+/, '').replace(/\/+$/, '')
251301
}
252302

303+
if (config.refresh === true) {
304+
config.refresh = [{ paths: ['resources/views/**', 'routes/**'] }]
305+
}
306+
253307
return {
254308
input: config.input,
255309
publicDirectory: config.publicDirectory ?? 'public',
256310
buildDirectory: config.buildDirectory ?? 'build',
257311
ssr: config.ssr ?? config.input,
258312
ssrOutputDirectory: config.ssrOutputDirectory ?? 'storage/ssr',
313+
refresh: config.refresh ?? false,
259314
}
260315
}
261316

@@ -308,6 +363,34 @@ function resolveManifestConfig(config: ResolvedConfig): string|false
308363
return manifestConfig
309364
}
310365

366+
function resolveFullReloadConfig({ refresh: config }: Required<PluginConfig>): PluginOption[]{
367+
if (typeof config === 'boolean') {
368+
return [];
369+
}
370+
371+
if (typeof config === 'string') {
372+
config = [{ paths: [config]}]
373+
}
374+
375+
if (! Array.isArray(config)) {
376+
config = [config]
377+
}
378+
379+
if (config.some(c => typeof c === 'string')) {
380+
config = [{ paths: config }] as FullReloadConfig[]
381+
}
382+
383+
return (config as FullReloadConfig[]).flatMap(c => {
384+
const plugin = fullReload(c.paths, c.config)
385+
386+
/* eslint-disable-next-line @typescript-eslint/ban-ts-comment */
387+
/** @ts-ignore */
388+
plugin.__laravel_plugin_config = c
389+
390+
return plugin
391+
})
392+
}
393+
311394
/**
312395
* Add the Interia helpers to the list of SSR dependencies that aren't externalized.
313396
*

0 commit comments

Comments
 (0)