Skip to content

Commit

Permalink
feat: config file hotUpdate support
Browse files Browse the repository at this point in the history
  • Loading branch information
laclys committed May 27, 2023
1 parent 0849051 commit 4482745
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { defineConfig } from '../dist'

export default defineConfig({
title: "test"
title: "test12332"
})
14 changes: 10 additions & 4 deletions src/node/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@ import { build } from './build'
const cli = cac('shaco').version('0.0.1').help()

cli.command('dev [root]', 'start dev server').action(async (root: string) => {
// console.log('dev', root)
const server = await createDevServer(root)
await server.listen()
server.printUrls()
const createServer = async () => {
const { createDevServer } = await import('./dev.js')
const server = await createDevServer(root, async () => {
await server.close()
await createServer()
})
await server.listen()
server.printUrls()
}
await createServer()
})

cli.command('build [root]', 'build in prod').action(async (root: string) => {
Expand Down
7 changes: 5 additions & 2 deletions src/node/dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@ import { PACKAGE_ROOT } from './constants'
import { resolveConfig } from './config'
import { pluginConfig } from './plugin/config'

export async function createDevServer(root = process.cwd()) {
export async function createDevServer(
root = process.cwd(),
restart: () => Promise<void>
) {
const config = await resolveConfig(root, 'serve', 'development')
// console.log('config', config)
return createServer({
root,
plugins: [pluginIndexHtml(), pluginReact(), pluginConfig(config)],
plugins: [pluginIndexHtml(), pluginReact(), pluginConfig(config, restart)],
server: {
fs: {
allow: [PACKAGE_ROOT]
Expand Down
19 changes: 18 additions & 1 deletion src/node/plugin/config.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { SiteConfig } from 'shared/types/index'
import { Plugin } from 'vite'
import { relative } from 'path'

const SITE_DATA_ID = 'shaco:site-data'

export function pluginConfig(config: SiteConfig): Plugin {
export function pluginConfig(
config: SiteConfig,
restart: () => Promise<void>
): Plugin {
return {
name: 'shaco:config',
resolveId(id) {
Expand All @@ -16,6 +20,19 @@ export function pluginConfig(config: SiteConfig): Plugin {
if (id === '\0' + SITE_DATA_ID) {
return `export default ${JSON.stringify(config.siteData)}`
}
},
async handleHotUpdate(ctx) {
const customWatchedFiles = [config.configPath]
const include = (id: string) =>
customWatchedFiles.some((file) => id.includes(file))

if (include(ctx.file)) {
console.log(
`\n${relative(config.root, ctx.file)} changed, restarting server...`
)
// restart Dev Server
await restart()
}
}
}
}
2 changes: 1 addition & 1 deletion tsup.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineConfig } from 'tsup'

export default defineConfig({
entryPoints: ['src/node/cli.ts', 'src/node/index.ts'],
entryPoints: ['src/node/cli.ts', 'src/node/index.ts', 'src/node/dev.ts'],
bundle: true,
splitting: true,
outDir: 'dist',
Expand Down

0 comments on commit 4482745

Please sign in to comment.