Skip to content

Commit 01ac648

Browse files
committed
feat: ✨ add restart plugin
1 parent 6459bfb commit 01ac648

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

packages/vite-plugin-drupal/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import vuePlugin from './plugins/custom-elements'
1515
import breakpoints from './plugins/breakpoints'
1616
import tailwindHMR from './plugins/tailwind-hmr'
1717
import vueCustomElement from './plugins/vue-custom-element'
18+
import restart from './plugins/restart'
1819

1920
export default (options: UserOptions = {}): Plugin[] => {
2021
const ctx = <Context>{}
@@ -37,6 +38,7 @@ export default (options: UserOptions = {}): Plugin[] => {
3738
vue(ctx.options.vue),
3839
vuePlugin(ctx),
3940
Icons(ctx.options.icons || {}),
41+
restart(ctx),
4042
UnimportPlugin.vite(ctx.options.unimport || {}),
4143
Components(ctx.options.components),
4244
].flat()

packages/vite-plugin-drupal/src/plugins/context.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ export interface Options {
3535
multipliers: string[]
3636
}
3737
features: {
38+
resetTheme: boolean
3839
twighmr: boolean
40+
clearCache: boolean | string
3941
}
4042
experimental: object
4143
baseTheme: string
@@ -137,7 +139,9 @@ const defaults: Options = {
137139
],
138140
},
139141
features: {
142+
resetTheme: true,
140143
twighmr: true,
144+
clearCache: 'drush cr',
141145
},
142146
experimental: {},
143147
baseTheme: 'stable9',
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { spawn } from 'node:child_process'
2+
import path from 'node:path'
3+
import type { Plugin } from 'vite'
4+
import { createLogger } from 'vite'
5+
import type { Context } from './context'
6+
7+
export default (ctx: Context): Plugin => {
8+
let timer: ReturnType<typeof setTimeout> | undefined
9+
const delay = 500
10+
11+
function clear() {
12+
clearTimeout(timer)
13+
}
14+
function schedule(fn: () => void) {
15+
clear()
16+
timer = setTimeout(fn, delay)
17+
}
18+
19+
const logger = createLogger()
20+
21+
return {
22+
name: 'vite-plugin-uebertool-restart',
23+
configureServer(server) {
24+
server.watcher.on('add', handleFileChange)
25+
server.watcher.on('unlink', handleFileChange)
26+
27+
function handleFileChange(file: string) {
28+
if (qualifiedPath(file)) {
29+
schedule(() => {
30+
server.restart()
31+
32+
if (typeof ctx.options.features.clearCache === 'string') {
33+
const [command, ...args] = ctx.options.features.clearCache.split(' ')
34+
logger.info(`Running: ${command} ${args.join(' ')}`, { timestamp: true })
35+
spawn(command, args, { stdio: 'inherit' })
36+
}
37+
})
38+
}
39+
}
40+
41+
function qualifiedPath(file: string) {
42+
const filename = path.basename(file)
43+
if (filename.startsWith('_'))
44+
return false
45+
if (filename.match(/\.(css|js|ts|twig)$/))
46+
return true
47+
return false
48+
}
49+
},
50+
}
51+
}

0 commit comments

Comments
 (0)