mirror of
https://github.com/nuxt/nuxt.git
synced 2024-11-22 13:45:18 +00:00
feat(nuxt3): auto generate runtime config type declarations (#3573)
Co-authored-by: Rodrigo Mesquita <rodrigoriome@gmail.com> Co-authored-by: Daniel Roe <daniel@roe.dev> Co-authored-by: Rodrigo Mesquita <30835404+rrmesquita@users.noreply.github.com>
This commit is contained in:
parent
6c083f3d12
commit
20f31712c1
@ -18,7 +18,7 @@
|
||||
"pathe": "^0.2.0",
|
||||
"rimraf": "^3.0.2",
|
||||
"scule": "^0.2.1",
|
||||
"untyped": "^0.3.0",
|
||||
"untyped": "^0.4.2",
|
||||
"vue-mq": "^1.0.1",
|
||||
"vue-plausible": "^1.3.1"
|
||||
},
|
||||
|
@ -30,7 +30,7 @@
|
||||
"jiti": "^1.13.0",
|
||||
"nitropack-dev": "link:../nitropack",
|
||||
"nuxt3": "workspace:./packages/nuxt3",
|
||||
"unbuild": "^0.6.9"
|
||||
"unbuild": "^0.7.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@iconify-json/carbon": "^1.1.1",
|
||||
|
@ -58,6 +58,7 @@
|
||||
"ufo": "^0.7.9",
|
||||
"unplugin": "^0.3.2",
|
||||
"unplugin-vue2-script-setup": "0.9.2",
|
||||
"untyped": "^0.4.2",
|
||||
"vite": "^2.8.2",
|
||||
"vite-plugin-vue2": "^1.9.3",
|
||||
"vue-template-compiler": "^2.6.14"
|
||||
|
@ -29,7 +29,7 @@
|
||||
"scule": "^0.2.1",
|
||||
"semver": "^7.3.5",
|
||||
"unctx": "^1.0.2",
|
||||
"untyped": "^0.3.0"
|
||||
"untyped": "^0.4.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/lodash.template": "^4",
|
||||
|
@ -59,6 +59,7 @@
|
||||
"scule": "^0.2.1",
|
||||
"ufo": "^0.7.11",
|
||||
"unplugin": "^0.4.0",
|
||||
"untyped": "^0.4.2",
|
||||
"vue": "^3.2.31",
|
||||
"vue-router": "^4.0.13"
|
||||
},
|
||||
|
@ -3,6 +3,7 @@ import type { Nuxt, NuxtApp } from '@nuxt/schema'
|
||||
import { genArrayFromRaw, genDynamicImport, genExport, genImport, genString } from 'knitwork'
|
||||
|
||||
import { isAbsolute, join, relative } from 'pathe'
|
||||
import { resolveSchema, generateTypes } from 'untyped'
|
||||
import escapeRE from 'escape-string-regexp'
|
||||
|
||||
export interface TemplateContext {
|
||||
@ -128,6 +129,22 @@ export const schemaTemplate = {
|
||||
` [${genString(meta.configKey)}]?: typeof ${genDynamicImport(meta.importName, { wrapper: false })}.default extends NuxtModule<infer O> ? Partial<O> : Record<string, any>`
|
||||
),
|
||||
' }',
|
||||
generateTypes(resolveSchema(nuxt.options.publicRuntimeConfig),
|
||||
{
|
||||
interfaceName: 'PublicRuntimeConfig',
|
||||
addExport: false,
|
||||
addDefaults: false,
|
||||
allowExtraKeys: false,
|
||||
indentation: 2
|
||||
}),
|
||||
generateTypes(resolveSchema(nuxt.options.privateRuntimeConfig), {
|
||||
interfaceName: 'PrivateRuntimeConfig',
|
||||
addExport: false,
|
||||
addDefaults: false,
|
||||
indentation: 2,
|
||||
allowExtraKeys: false,
|
||||
defaultDescrption: 'This value is only accessible from server-side.'
|
||||
}),
|
||||
'}'
|
||||
].join('\n')
|
||||
}
|
||||
|
@ -17,4 +17,6 @@ export interface PublicRuntimeConfig extends Record<string, any> { }
|
||||
export interface PrivateRuntimeConfig extends PublicRuntimeConfig { }
|
||||
|
||||
type _RuntimeConfig = PublicRuntimeConfig & Partial<PrivateRuntimeConfig>
|
||||
export interface RuntimeConfig extends _RuntimeConfig { }
|
||||
export interface RuntimeConfig extends _RuntimeConfig {
|
||||
[key: string]: any
|
||||
}
|
||||
|
5
test/fixtures/basic/nuxt.config.ts
vendored
5
test/fixtures/basic/nuxt.config.ts
vendored
@ -8,7 +8,10 @@ export default defineNuxtConfig({
|
||||
output: { dir: process.env.NITRO_OUTPUT_DIR }
|
||||
},
|
||||
publicRuntimeConfig: {
|
||||
testConfig: '123'
|
||||
testConfig: 123
|
||||
},
|
||||
privateRuntimeConfig: {
|
||||
privateConfig: 'secret_key'
|
||||
},
|
||||
modules: ['~/modules/example'],
|
||||
hooks: {
|
||||
|
9
test/fixtures/basic/types.ts
vendored
9
test/fixtures/basic/types.ts
vendored
@ -79,3 +79,12 @@ describe('modules', () => {
|
||||
defineNuxtConfig({ undeclaredKey: { other: false } })
|
||||
})
|
||||
})
|
||||
|
||||
describe('runtimeConfig', () => {
|
||||
it('generated runtimeConfig types', () => {
|
||||
const runtimeConfig = useRuntimeConfig()
|
||||
expectTypeOf(runtimeConfig.testConfig).toMatchTypeOf<number>()
|
||||
expectTypeOf(runtimeConfig.privateConfig).toMatchTypeOf<string>()
|
||||
expectTypeOf(runtimeConfig.unknown).toMatchTypeOf<any>()
|
||||
})
|
||||
})
|
||||
|
76
yarn.lock
76
yarn.lock
@ -42,7 +42,7 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"@babel/code-frame@npm:^7.0.0, @babel/code-frame@npm:^7.10.4, @babel/code-frame@npm:^7.16.0, @babel/code-frame@npm:^7.16.7":
|
||||
"@babel/code-frame@npm:^7.0.0, @babel/code-frame@npm:^7.10.4, @babel/code-frame@npm:^7.16.7":
|
||||
version: 7.16.7
|
||||
resolution: "@babel/code-frame@npm:7.16.7"
|
||||
dependencies:
|
||||
@ -2683,6 +2683,7 @@ __metadata:
|
||||
unbuild: latest
|
||||
unplugin: ^0.3.2
|
||||
unplugin-vue2-script-setup: 0.9.2
|
||||
untyped: ^0.4.2
|
||||
vite: ^2.8.2
|
||||
vite-plugin-vue2: ^1.9.3
|
||||
vue: ^2
|
||||
@ -2985,7 +2986,7 @@ __metadata:
|
||||
semver: ^7.3.5
|
||||
unbuild: latest
|
||||
unctx: ^1.0.2
|
||||
untyped: ^0.3.0
|
||||
untyped: ^0.4.2
|
||||
languageName: unknown
|
||||
linkType: soft
|
||||
|
||||
@ -14517,7 +14518,7 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"mkdist@npm:^0.3.9":
|
||||
"mkdist@npm:^0.3.10":
|
||||
version: 0.3.10
|
||||
resolution: "mkdist@npm:0.3.10"
|
||||
dependencies:
|
||||
@ -15496,6 +15497,7 @@ __metadata:
|
||||
ufo: ^0.7.11
|
||||
unbuild: latest
|
||||
unplugin: ^0.4.0
|
||||
untyped: ^0.4.2
|
||||
vue: ^3.2.31
|
||||
vue-meta: next
|
||||
vue-router: ^4.0.13
|
||||
@ -19052,19 +19054,19 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"rollup-plugin-dts@npm:^4.1.0":
|
||||
version: 4.1.0
|
||||
resolution: "rollup-plugin-dts@npm:4.1.0"
|
||||
"rollup-plugin-dts@npm:^4.2.0":
|
||||
version: 4.2.0
|
||||
resolution: "rollup-plugin-dts@npm:4.2.0"
|
||||
dependencies:
|
||||
"@babel/code-frame": ^7.16.0
|
||||
magic-string: ^0.25.7
|
||||
"@babel/code-frame": ^7.16.7
|
||||
magic-string: ^0.26.1
|
||||
peerDependencies:
|
||||
rollup: ^2.55
|
||||
typescript: ~4.1 || ~4.2 || ~4.3 || ~4.4 || ~4.5
|
||||
typescript: ^4.1
|
||||
dependenciesMeta:
|
||||
"@babel/code-frame":
|
||||
optional: true
|
||||
checksum: add2715d4906dccb8bbad63e02668303eee553b68e2ae8c7c686abc5ea7936ef316db43845eb12bf978002f461e43b00ecfe61bd2ffedd6c570e12a289de7c18
|
||||
checksum: 2b8394bf62c3903a68722266999adf3e85768bf1a7e93286841b466afaabf5cb30501f71d40075c3393cc33defa6ed13d3276bae0d8cb7a87a95d06f7a93c04d
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
@ -20957,7 +20959,7 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"typescript@npm:^4.3.4, typescript@npm:^4.5.5, typescript@npm:^4.6.2":
|
||||
"typescript@npm:^4.3.4, typescript@npm:^4.6.2":
|
||||
version: 4.6.2
|
||||
resolution: "typescript@npm:4.6.2"
|
||||
bin:
|
||||
@ -20967,7 +20969,7 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"typescript@patch:typescript@^4.3.4#~builtin<compat/typescript>, typescript@patch:typescript@^4.5.5#~builtin<compat/typescript>, typescript@patch:typescript@^4.6.2#~builtin<compat/typescript>":
|
||||
"typescript@patch:typescript@^4.3.4#~builtin<compat/typescript>, typescript@patch:typescript@^4.6.2#~builtin<compat/typescript>":
|
||||
version: 4.6.2
|
||||
resolution: "typescript@patch:typescript@npm%3A4.6.2#~builtin<compat/typescript>::version=4.6.2&hash=bda367"
|
||||
bin:
|
||||
@ -21065,40 +21067,40 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"unbuild@npm:^0.6.9":
|
||||
version: 0.6.9
|
||||
resolution: "unbuild@npm:0.6.9"
|
||||
"unbuild@npm:^0.7.0":
|
||||
version: 0.7.0
|
||||
resolution: "unbuild@npm:0.7.0"
|
||||
dependencies:
|
||||
"@rollup/plugin-alias": ^3.1.9
|
||||
"@rollup/plugin-commonjs": ^21.0.1
|
||||
"@rollup/plugin-commonjs": ^21.0.2
|
||||
"@rollup/plugin-json": ^4.1.0
|
||||
"@rollup/plugin-node-resolve": ^13.1.3
|
||||
"@rollup/plugin-replace": ^3.0.1
|
||||
"@rollup/pluginutils": ^4.1.2
|
||||
chalk: ^5.0.0
|
||||
"@rollup/plugin-replace": ^4.0.0
|
||||
"@rollup/pluginutils": ^4.2.0
|
||||
chalk: ^5.0.1
|
||||
consola: ^2.15.3
|
||||
defu: ^5.0.1
|
||||
esbuild: ^0.14.14
|
||||
esbuild: ^0.14.25
|
||||
hookable: ^5.1.1
|
||||
jiti: ^1.12.14
|
||||
magic-string: ^0.25.7
|
||||
jiti: ^1.13.0
|
||||
magic-string: ^0.26.1
|
||||
mkdirp: ^1.0.4
|
||||
mkdist: ^0.3.9
|
||||
mlly: ^0.4.1
|
||||
mkdist: ^0.3.10
|
||||
mlly: ^0.4.3
|
||||
mri: ^1.2.0
|
||||
pathe: ^0.2.0
|
||||
pkg-types: ^0.3.2
|
||||
pretty-bytes: ^5.6.0
|
||||
pretty-bytes: ^6.0.0
|
||||
rimraf: ^3.0.2
|
||||
rollup: ^2.66.1
|
||||
rollup-plugin-dts: ^4.1.0
|
||||
rollup: ^2.70.0
|
||||
rollup-plugin-dts: ^4.2.0
|
||||
rollup-plugin-esbuild: ^4.8.2
|
||||
scule: ^0.2.1
|
||||
typescript: ^4.5.5
|
||||
untyped: ^0.3.0
|
||||
typescript: ^4.6.2
|
||||
untyped: ^0.4.0
|
||||
bin:
|
||||
unbuild: dist/cli.mjs
|
||||
checksum: f3db7b040007247ffd7e1ba66e895bb6fd85a5f1e0f8253b862b6ef0f289d0d394e9cc8fee5edbdef901fcfaecafa2cea259b314919f2c2ca7855e22cdc0cdd6
|
||||
checksum: 085af2e17825859e50979909c127e38a149abcc2f75ca57d5e47d1699d042ccbac696fadf6808b24e6ee7ac361915933efbc6452f5102fbb856997ff89fe473b
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
@ -21390,6 +21392,20 @@ __metadata:
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"untyped@npm:^0.4.0":
|
||||
version: 0.4.0
|
||||
resolution: "untyped@npm:0.4.0"
|
||||
checksum: f2557963fb93c2a055e18c3b0d06bcab2e1539c024824946053ded03e275a062c8540e1f00d90c12e5fc3f78c75a46e11ac83d83ac827b2113868489855e791f
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"untyped@npm:^0.4.2":
|
||||
version: 0.4.2
|
||||
resolution: "untyped@npm:0.4.2"
|
||||
checksum: 51468cf8cb114795d08986cd37ff37f51a039a1b8c82147d3e7a12edd8515b446c54304728e5c290161249a599e72bc90851bbe6ae90c68778ad30642b6cbcfa
|
||||
languageName: node
|
||||
linkType: hard
|
||||
|
||||
"upath@npm:^1.1.1":
|
||||
version: 1.2.0
|
||||
resolution: "upath@npm:1.2.0"
|
||||
|
Loading…
Reference in New Issue
Block a user