Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

feat(nuxt): app.config with hmr and reactivity support #6333

Merged
merged 38 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
5b8a04e
feat(nuxt): `app.config` with hmr support
pi0 Aug 3, 2022
eda4b99
Merge branch 'main' into feat/app-config
pi0 Aug 4, 2022
79ff3b1
webpack hmr support
pi0 Aug 4, 2022
c949684
update AppConfig schema
pi0 Aug 4, 2022
8316b5a
handle key removals
pi0 Aug 4, 2022
171d869
support inline config using `appConfig` in nuxt.config
pi0 Aug 4, 2022
22518d8
fix template when no appConfigs added
pi0 Aug 4, 2022
75e3be9
handle app.config add/removal
pi0 Aug 4, 2022
231e0ca
Merge branch 'main' into feat/app-config
pi0 Aug 4, 2022
efd22f0
auto generate types
pi0 Aug 4, 2022
3e0534c
add tests
pi0 Aug 4, 2022
8c2a012
Merge branch 'main' into feat/app-config
pi0 Aug 4, 2022
9cb0c74
fix test side effect
pi0 Aug 4, 2022
7d6c04a
Merge branch 'main' into feat/app-config
pi0 Aug 4, 2022
d3b9163
simplify reserved namespaces
pi0 Aug 4, 2022
9178cd8
fix: reserved are optional
pi0 Aug 4, 2022
bc269f3
Merge branch 'main' into feat/app-config
pi0 Aug 12, 2022
808bf9d
feat(nuxt): include type of resolved configs in AppConfig
danielroe Aug 16, 2022
d5011cf
refactor: write a single type declaration file
danielroe Aug 16, 2022
5d9c593
chore: upgrade defu
danielroe Aug 16, 2022
43b58f3
test: add type test
danielroe Aug 16, 2022
d152678
fix: update to use `Defu` type helper
danielroe Aug 16, 2022
59ed668
fix: use `ResolvedAppConfig` to for type inference and extract `defin…
danielroe Aug 17, 2022
c63fd89
Merge branch 'main' into feat/app-config
pi0 Aug 17, 2022
69c5b50
try removing subpath from package.json
pi0 Aug 17, 2022
15ed77c
refactor: move `defineAppConfig` to `nuxt.ts`
pi0 Aug 17, 2022
f36e0d0
Update packages/nuxt/src/app/config.ts
pi0 Aug 17, 2022
df2380d
chore: fix ts issue
pi0 Aug 17, 2022
b2c7f57
remove unused import
pi0 Aug 17, 2022
a08d987
add usage to examples
pi0 Aug 17, 2022
6a571f5
add docs
pi0 Aug 17, 2022
e54db5d
fix vite hmr
pi0 Aug 17, 2022
2cace40
update docs
pi0 Aug 17, 2022
840f245
update api guide
pi0 Aug 17, 2022
ed0b584
revert useRuntimeConfig back to nuxt.ts
pi0 Aug 17, 2022
12ec374
Merge branch 'main' into feat/app-config
pi0 Aug 17, 2022
5e7d9c6
i touched it!
pi0 Aug 17, 2022
15f5e3f
strict is not funny
pi0 Aug 17, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions packages/nuxt/src/app/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { AppConfig, RuntimeConfig } from '@nuxt/schema'
import { reactive } from 'vue'
import { useNuxtApp } from './nuxt'
// @ts-ignore
import __appConfig from '#build/app.config.mjs'

export const _appConfig = __appConfig as AppConfig

export function useRuntimeConfig (): RuntimeConfig {
pi0 marked this conversation as resolved.
Show resolved Hide resolved
return useNuxtApp().$config
}

export function defineAppConfig (config: AppConfig): AppConfig {
return config
}

export function useAppConfig (): AppConfig {
const nuxtApp = useNuxtApp()
if (!nuxtApp._appConfig) {
nuxtApp._appConfig = reactive(_appConfig) as AppConfig
}
return nuxtApp._appConfig
}

if (import.meta.hot) {
import.meta.hot.accept((newModule) => {
const appConfig = useAppConfig()
const newConfig = newModule?._appConfig
if (newConfig && appConfig) {
// TODO: Deep assign
for (const key in newConfig) {
appConfig[key] = newConfig[key]
}
}
})
}
1 change: 1 addition & 0 deletions packages/nuxt/src/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
export * from './nuxt'
export * from './composables'
export * from './components'
export * from './config'

// eslint-disable-next-line import/no-restricted-paths
export type { PageMeta } from '../pages/runtime'
Expand Down
4 changes: 0 additions & 4 deletions packages/nuxt/src/app/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,6 @@ export function useNuxtApp () {
return nuxtAppInstance
}

export function useRuntimeConfig (): RuntimeConfig {
return useNuxtApp().$config
}

function defineGetter<K extends string | number | symbol, V> (obj: Record<K, V>, key: K, val: V) {
Object.defineProperty(obj, key, { get: () => val })
}
4 changes: 3 additions & 1 deletion packages/nuxt/src/auto-imports/presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ export const appPreset = defineUnimportPreset({
'isNuxtError',
'useError',
'createError',
'defineNuxtLink'
'defineNuxtLink',
'defineAppConfig',
'useAppConfig'
]
})

Expand Down
9 changes: 9 additions & 0 deletions packages/nuxt/src/core/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ export async function resolveApp (nuxt: Nuxt, app: NuxtApp) {
}
app.plugins = uniqueBy(app.plugins, 'src')

// Resolve app.config
app.configs = []
for (const config of nuxt.options._layers.map(layer => layer.config)) {
const appConfigPath = await findPath(resolve(config.srcDir, 'app.config'))
if (appConfigPath) {
app.configs.push(appConfigPath)
}
}

// Extend app
await nuxt.callHook('app:resolve', app)
}
12 changes: 12 additions & 0 deletions packages/nuxt/src/core/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ export const useRuntimeConfig = () => window?.__NUXT__?.config || {}
`
}

export const appConfigTemplate: NuxtTemplate = {
filename: 'app.config.mjs',
write: true,
getContents: ({ app }) => {
return `
import defu from 'defu'
${app.configs.map((id, index) => `import ${`cfg${index}`} from ${JSON.stringify(id)}`).join('\n')}
pi0 marked this conversation as resolved.
Show resolved Hide resolved
export default defu(${app.configs.map((_id, index) => `cfg${index}`).join(', ')})
`
}
}

export const publicPathTemplate: NuxtTemplate = {
filename: 'paths.mjs',
getContents ({ nuxt }) {
Expand Down
7 changes: 7 additions & 0 deletions packages/schema/src/types/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,19 @@ type RuntimeConfigNamespace = Record<string, any>

export interface PublicRuntimeConfig extends RuntimeConfigNamespace { }

export interface AppConfig extends RuntimeConfigNamespace {
baseURL: string
buildAssetsDir: string
cdnURL: string
}

// TODO: remove before release of 3.0.0
/** @deprecated use RuntimeConfig interface */
export interface PrivateRuntimeConfig extends RuntimeConfigNamespace { }

export interface RuntimeConfig extends PrivateRuntimeConfig, RuntimeConfigNamespace {
public: PublicRuntimeConfig
app: AppConfig
}

export interface ViteConfig extends ViteUserConfig {
Expand Down
1 change: 1 addition & 0 deletions packages/schema/src/types/nuxt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ export interface NuxtApp {
layouts: Record<string, NuxtLayout>
middleware: NuxtMiddleware[]
templates: NuxtTemplate[]
configs: string[]
}

type _TemplatePlugin = Omit<NuxtPlugin, 'src'> & NuxtTemplate
Expand Down
6 changes: 6 additions & 0 deletions playground/app.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// import { defineAppConfig } from '#imports'
pi0 marked this conversation as resolved.
Show resolved Hide resolved
// console.log(defineAppConfig)

export default ({
test: 'boo4'
})
6 changes: 5 additions & 1 deletion playground/app.vue
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
<script setup lang="ts">
const appConfig = useAppConfig()
watch(appConfig, () => {
console.log('App config updated:', appConfig)
})
</script>

<template>
<!-- Edit this file to play around with Nuxt but never commit changes! -->
<div>
Nuxt 3 Playground
Hello World {{ appConfig }}
</div>
</template>

Expand Down