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 3 commits
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
2 changes: 1 addition & 1 deletion packages/kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@nuxt/schema": "3.0.0-rc.8",
"c12": "^0.2.9",
"consola": "^2.15.3",
"defu": "^6.0.0",
"defu": "^6.1.0",
"globby": "^13.1.2",
"hash-sum": "^2.0.0",
"ignore": "^5.2.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/nuxt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@vueuse/head": "^0.7.9",
"chokidar": "^3.5.3",
"cookie-es": "^0.5.0",
"defu": "^6.0.0",
"defu": "^6.1.0",
"destr": "^1.1.1",
"escape-string-regexp": "^5.0.0",
"fs-extra": "^10.1.0",
Expand Down
9 changes: 4 additions & 5 deletions packages/nuxt/src/core/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ export const schemaTemplate = {
"declare module '@nuxt/schema' {",
' interface NuxtConfig {',
...moduleInfo.filter(Boolean).map(meta =>
` [${genString(meta.configKey)}]?: typeof ${genDynamicImport(meta.importName, { wrapper: false })}.default extends NuxtModule<infer O> ? Partial<O> : Record<string, any>`
` [${genString(meta.configKey)}]?: typeof ${genDynamicImport(meta.importName, { wrapper: false })}.default extends NuxtModule<infer O> ? Partial<O> : Record<string, any>`
),
' }',
generateTypes(resolveSchema(Object.fromEntries(Object.entries(nuxt.options.runtimeConfig).filter(([key]) => key !== 'public'))),
Expand Down Expand Up @@ -157,7 +157,7 @@ export const layoutTemplate: NuxtTemplate = {
}))
return [
'import { defineAsyncComponent } from \'vue\'',
`export default ${layoutsObject}`
`export default ${layoutsObject}`
].join('\n')
}
}
Expand Down Expand Up @@ -188,12 +188,11 @@ export const appConfigDeclarationTemplate: NuxtTemplate = {
filename: 'types/app.config.d.ts',
getContents: ({ app, nuxt }) => {
return `
import defu from 'defu'
import type { Defu } from 'defu'
${app.configs.map((id, index) => `import ${`cfg${index}`} from ${JSON.stringify(id.replace(/(?<=\w)\.\w+$/g, ''))}`).join('\n')}

declare const inlineConfig = ${JSON.stringify(nuxt.options.appConfig, null, 2)}
declare const resolvedConfig = defu(${app.configs.map((_id, index) => `cfg${index}`).concat(['inlineConfig']).join(', ')})
type ResolvedAppConfig = typeof resolvedConfig
type ResolvedAppConfig = Defu<typeof inlineConfig, [${app.configs.map((_id, index) => `typeof cfg${index}`).join(', ')}]>

declare module '@nuxt/schema' {
interface AppConfig extends ResolvedAppConfig { }
Expand Down
2 changes: 1 addition & 1 deletion packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"dependencies": {
"c12": "^0.2.9",
"create-require": "^1.1.1",
"defu": "^6.0.0",
"defu": "^6.1.0",
"jiti": "^1.14.0",
"pathe": "^0.3.4",
"postcss-import-resolver": "^2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/test-utils/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"dependencies": {
"@nuxt/kit": "3.0.0-rc.8",
"@nuxt/schema": "3.0.0-rc.8",
"defu": "^6.0.0",
"defu": "^6.1.0",
"execa": "^6.1.0",
"get-port-please": "^2.6.1",
"jiti": "^1.14.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"autoprefixer": "^10.4.8",
"chokidar": "^3.5.3",
"cssnano": "^5.1.12",
"defu": "^6.0.0",
"defu": "^6.1.0",
"esbuild": "^0.15.1",
"escape-string-regexp": "^5.0.0",
"estree-walker": "^3.0.1",
Expand Down
8 changes: 5 additions & 3 deletions test/fixtures/basic/app.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
// TODO
// import { defineAppConfig } from '#imports'
import type { AppConfig } from '@nuxt/schema'

export default <AppConfig>({
// For now we can't use AppConfig to type this
danielroe marked this conversation as resolved.
Show resolved Hide resolved
// as it will circularly reference itself

export default {
userConfig: 123,
nested: {
val: 2
}
})
}
15 changes: 15 additions & 0 deletions test/fixtures/basic/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { expectTypeOf } from 'expect-type'
import { describe, it } from 'vitest'
import type { Ref } from 'vue'
import type { AppConfig } from '@nuxt/schema'

import { NavigationFailure, RouteLocationNormalizedLoaded, RouteLocationRaw, useRouter as vueUseRouter } from 'vue-router'
import { defineNuxtConfig } from '~~/../../../packages/nuxt/src'
Expand Down Expand Up @@ -164,3 +165,17 @@ describe('composables', () => {
.toMatchTypeOf(useLazyAsyncData(() => Promise.resolve({ foo: Math.random() }), { transform: data => data.foo }))
})
})

describe('app config', () => {
it('merges app config as expected', () => {
interface ExpectedMergedAppConfig {
fromLayer: boolean,
fromNuxtConfig: boolean,
nested: {
val: number
},
userConfig: number
}
expectTypeOf<AppConfig>().toMatchTypeOf<ExpectedMergedAppConfig>()
})
})
17 changes: 12 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1646,7 +1646,7 @@ __metadata:
"@types/semver": ^7
c12: ^0.2.9
consola: ^2.15.3
defu: ^6.0.0
defu: ^6.1.0
globby: ^13.1.2
hash-sum: ^2.0.0
ignore: ^5.2.0
Expand Down Expand Up @@ -1679,7 +1679,7 @@ __metadata:
"@types/semver": ^7
c12: ^0.2.9
create-require: ^1.1.1
defu: ^6.0.0
defu: ^6.1.0
jiti: ^1.14.0
pathe: ^0.3.4
postcss-import-resolver: ^2.0.0
Expand Down Expand Up @@ -1728,7 +1728,7 @@ __metadata:
dependencies:
"@nuxt/kit": 3.0.0-rc.8
"@nuxt/schema": 3.0.0-rc.8
defu: ^6.0.0
defu: ^6.1.0
execa: ^6.1.0
get-port-please: ^2.6.1
jiti: ^1.14.0
Expand Down Expand Up @@ -1784,7 +1784,7 @@ __metadata:
autoprefixer: ^10.4.8
chokidar: ^3.5.3
cssnano: ^5.1.12
defu: ^6.0.0
defu: ^6.1.0
esbuild: ^0.15.1
escape-string-regexp: ^5.0.0
estree-walker: ^3.0.1
Expand Down Expand Up @@ -5093,6 +5093,13 @@ __metadata:
languageName: node
linkType: hard

"defu@npm:^6.1.0":
version: 6.1.0
resolution: "defu@npm:6.1.0"
checksum: 403a9ba8ab08dca87576eb062fa488c5cc58a5c8601955f2325dcc077c23da8a68169591505654a8c70ca3567006516ef92a4c44129643f928c58c4e7281195a
languageName: node
linkType: hard

"delegates@npm:^1.0.0":
version: 1.0.0
resolution: "delegates@npm:1.0.0"
Expand Down Expand Up @@ -9919,7 +9926,7 @@ __metadata:
"@vueuse/head": ^0.7.9
chokidar: ^3.5.3
cookie-es: ^0.5.0
defu: ^6.0.0
defu: ^6.1.0
destr: ^1.1.1
escape-string-regexp: ^5.0.0
fs-extra: ^10.1.0
Expand Down