Skip to content

Commit

Permalink
feat: reload on PHP change in serve mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaskuske committed Aug 2, 2022
1 parent 982ac38 commit 563da73
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 22 deletions.
5 changes: 3 additions & 2 deletions package.json
@@ -1,7 +1,7 @@
{
"name": "kirbyup",
"version": "1.3.2",
"packageManager": "pnpm@7.6.0",
"packageManager": "pnpm@7.8.0",
"description": "Zero-config bundler for Kirby Panel plugins",
"author": {
"name": "Johann Schopplich",
Expand Down Expand Up @@ -60,6 +60,7 @@
"cac": "^6.7.12",
"chokidar": "^3.5.3",
"consola": "^2.15.3",
"detect-package-manager": "^2.0.1",
"magic-string": "^0.26.2",
"pathe": "^0.3.2",
"perfect-debounce": "^0.1.3",
Expand All @@ -71,6 +72,7 @@
"sass": "^1.54.0",
"unconfig": "^0.3.5",
"vite": "^3.0.4",
"vite-plugin-live-reload": "^3.0.0",
"vue": "^2.7.8"
},
"devDependencies": {
Expand All @@ -80,7 +82,6 @@
"@types/prompts": "^2.4.0",
"@vitejs/plugin-vue2": "^1.1.2",
"bumpp": "^8.2.1",
"detect-package-manager": "^2.0.1",
"eslint": "^8.20.0",
"fast-glob": "^3.2.11",
"fs-extra": "^10.1.0",
Expand Down
39 changes: 21 additions & 18 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 11 additions & 2 deletions src/node/index.ts
Expand Up @@ -18,6 +18,7 @@ import { debounce } from 'perfect-debounce'
import colors from 'picocolors'
import type { RollupOutput } from 'rollup'
import type { InlineConfig } from 'vite'
import liveReloadPlugin from 'vite-plugin-live-reload'
import { name, version } from '../../package.json'
import { PrettyError, handleError } from './errors'
import { printFileInfo, toArray } from './utils'
Expand All @@ -36,10 +37,18 @@ let resolvedPostCssConfig: PostCSSConfigResult

async function createServer(options: ResolvedCliOptions) {
const aliasDir = resolve(options.cwd, dirname(options.entry))
const { alias = {}, extendViteConfig = {} } = resolvedKirbyupConfig
const { alias = {}, extendViteConfig = {}, reloadOnPhpChange = true } = resolvedKirbyupConfig

const defaultWatchConf: Parameters<typeof liveReloadPlugin> = ['./**/*.php']
const watchConf = typeof reloadOnPhpChange === 'boolean' ? defaultWatchConf : reloadOnPhpChange

const plugins = [vuePlugin(), kirbyupAutoImportPlugin(), kirbyupHmrPlugin(options)]

if (reloadOnPhpChange)
plugins.push(liveReloadPlugin(...watchConf))

const defaultConfig: InlineConfig = {
plugins: [vuePlugin(), kirbyupAutoImportPlugin(), kirbyupHmrPlugin(options)],
plugins,
// Input needs to be specified so dep pre-bundling works
build: { rollupOptions: { input: resolve(options.cwd, options.entry) } },
resolve: {
Expand Down
9 changes: 9 additions & 0 deletions src/node/types.ts
@@ -1,5 +1,6 @@
import type { AliasOptions, InlineConfig } from 'vite'
import type * as Postcss from 'postcss'
import type liveReloadPlugin from 'vite-plugin-live-reload'

export type MarkRequired<T, RK extends keyof T> = Exclude<T, RK> &
Required<Pick<T, RK>>
Expand Down Expand Up @@ -28,6 +29,14 @@ export interface UserConfig {
*/
alias?: AliasOptions

/**
* Controls whether the plugin's PHP files are watched for changes.
* Pass true or false to enable or disable. Alternatively you can pass
* an array of arguments that are passed to vite-plugin-live-reload.
* The default is to watch all PHP files in the plugin directory. (./\*\*\/*.php)
*/
reloadOnPhpChange: boolean | Parameters<typeof liveReloadPlugin>

/**
* Extends Vite's configuration. Will be merged with kirbyup's
* default configuration. Be careful what to extend.
Expand Down

0 comments on commit 563da73

Please sign in to comment.