Skip to content

Commit

Permalink
feat: poc for config HMR
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Feb 21, 2024
1 parent 34e8011 commit d0d34fc
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 17 deletions.
32 changes: 28 additions & 4 deletions app/modules/content/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { fileURLToPath } from 'node:url'
import { defineNuxtModule } from 'nuxt/kit'
import { defineNuxtModule, createResolver, useNitro } from 'nuxt/kit'
import type { ModuleOptions as ContentOptions } from '@nuxt/content'
import type { DocsConfig } from '../../../schema/config'

Expand All @@ -9,8 +8,14 @@ export default defineNuxtModule({
return
}

const resolver = createResolver(import.meta.url)

const docsConfig = (nuxt.options as any).docs as DocsConfig

nuxt.options.nitro.externals ||= {}
nuxt.options.nitro.externals.inline ||= []
nuxt.options.nitro.externals.inline.push(resolver.resolve('./runtime'))

if (docsConfig.landing === false) {
nuxt.hooks.hook('pages:extend', (pages) => {
const index = pages.findIndex((page) => page.path === '/')
Expand All @@ -24,9 +29,28 @@ export default defineNuxtModule({
contentConfig.sources = {
...contentConfig.sources,
content: {
driver: fileURLToPath(new URL('source.mjs', import.meta.url)),
docsConfig,
driver: resolver.resolve('./runtime/unstorage.mjs'),
base: docsConfig.dir,
},
}

// Inject globalThis.__undocs__ for same process + nitro runtime
globalThis.__undocs__ = { docsConfig }
nuxt.hook('nitro:init', (nitro) => {
nitro.options.plugins.push(resolver.resolve('./runtime/nitro.mjs'))
nitro.options.runtimeConfig.__undocs__ = { docsConfig }
})

// HMR
nuxt.hook('undocs:config' as any, (newConfig) => {
Object.assign(docsConfig, newConfig)
const nitro = useNitro()
nitro.updateConfig({
runtimeConfig: {
...nitro.options.runtimeConfig,
__undocs__: { docsConfig },
},
})
})
},
})
File renamed without changes.
11 changes: 11 additions & 0 deletions app/modules/content/runtime/nitro.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useRuntimeConfig } from '#imports'

Object.defineProperty(globalThis, '__undocs__', {
get() {
return useRuntimeConfig().__undocs__ || { docsConfig: {} }
},
})

export default () => {
// noop
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { defineDriver } from 'unstorage'
import fsDriver from 'unstorage/drivers/fs'
import { transform, loadConfig } from 'automd'

export default (opts) => {
const getDocsConfig = () => globalThis.__undocs__?.docsConfig || {}

export default (driverOpts) => {
const _fs = fsDriver({
base: opts.docsConfig.dir,
base: driverOpts.base,
})

let automdConfig
Expand All @@ -13,23 +15,23 @@ export default (opts) => {
..._fs,
name: 'content',
async getItem(key) {
const docsConfig = getDocsConfig()

const val = await _fs.getItem(key)

// Landing
if (opts.docsConfig.automd) {
if (!val && key === 'index.json') {
return await import('./landing.mjs').then(({ genLanding }) => genLanding(opts.docsConfig))
}
if (!val && key === 'index.json$') {
return { mtime: new Date() }
}
if (!val && key === 'index.json') {
return await import('./landing.mjs').then(({ genLanding }) => genLanding(docsConfig))
}
if (!val && key === 'index.json$') {
return { mtime: new Date() }
}

// Automd transform
if (opts.docsConfig.automd) {
if (docsConfig.automd) {
if (key.endsWith('.md') && typeof val === 'string') {
if (!automdConfig) {
automdConfig = await loadConfig(opts.docsConfig.dir, opts.docsConfig.automd)
automdConfig = await loadConfig(docsConfig.dir, docsConfig.automd)
}
const res = await transform(val, automdConfig)
if (res.hasChanged) {
Expand All @@ -44,10 +46,12 @@ export default (opts) => {
return val
},
async getKeys(prefix) {
const docsConfig = getDocsConfig()

const keys = await _fs.getKeys(prefix)

// Landing
if (opts.docsConfig.landing !== false && !keys.some((key) => /^index\.\w+$/.test(key))) {
if (docsConfig.landing !== false && !keys.some((key) => /^index\.\w+$/.test(key))) {
keys.push('index.json')
}

Expand Down
14 changes: 13 additions & 1 deletion cli/cli.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ import consola from 'consola'
import { getContext } from 'unctx'
import { setupDocs } from './setup.mjs'

const HMRKeys = new Set([
"description",
"shortDescription",
"landing",
])

export function createCLI(opts) {
const sharedArgs = {
dir: {
Expand Down Expand Up @@ -42,7 +48,13 @@ export function createCLI(opts) {
const diff = getDiff().filter((entry) => entry.key !== 'dir')
logger.info('Config updated:\n' + diff.map((i) => ' - ' + i.toJSON()).join('\n'))
Object.assign(nuxtConfig.docs, config)
tryUseNuxt()?.callHook('restart')
if (diff.some((entry) => !HMRKeys.has(entry.key))) {
logger.info('Full reloading...')
tryUseNuxt()?.callHook('restart')
} else {
logger.info('Fast reloading...')
tryUseNuxt()?.callHook('undocs:config', config)
}
},
},
})
Expand Down

0 comments on commit d0d34fc

Please sign in to comment.