feat(nuxt): add versions to runtime nuxtApp (#19064)

This commit is contained in:
Daniel Roe 2023-03-01 15:08:23 +00:00 committed by GitHub
parent c35104c769
commit d4a75240ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 15 additions and 1 deletions

View File

@ -42,6 +42,8 @@ Note: this is an internal interface, and some properties might change until stab
const nuxtApp = { const nuxtApp = {
vueApp, // the global Vue application: https://vuejs.org/api/application.html#application-api vueApp, // the global Vue application: https://vuejs.org/api/application.html#application-api
versions, // an object containing Nuxt and Vue versions
// These let you call and add runtime NuxtApp hooks // These let you call and add runtime NuxtApp hooks
// https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/nuxt.ts#L18 // https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/nuxt.ts#L18
hooks, hooks,

5
packages/nuxt/index.d.ts vendored Normal file
View File

@ -0,0 +1,5 @@
declare global {
const __NUXT_VERSION__: string
}
export {}

View File

@ -60,6 +60,7 @@ export interface NuxtSSRContext extends SSRContext {
interface _NuxtApp { interface _NuxtApp {
vueApp: App<Element> vueApp: App<Element>
globalName: string globalName: string
versions: Record<string, string>
hooks: Hookable<RuntimeNuxtHooks> hooks: Hookable<RuntimeNuxtHooks>
hook: _NuxtApp['hooks']['hook'] hook: _NuxtApp['hooks']['hook']
@ -120,6 +121,10 @@ export function createNuxtApp (options: CreateOptions) {
const nuxtApp: NuxtApp = { const nuxtApp: NuxtApp = {
provide: undefined, provide: undefined,
globalName: 'nuxt', globalName: 'nuxt',
versions: {
get nuxt () { return __NUXT_VERSION__ },
get vue () { return nuxtApp.vueApp.version }
},
payload: reactive({ payload: reactive({
data: {}, data: {},
state: {}, state: {},

View File

@ -59,6 +59,7 @@ export async function bundle (nuxt: Nuxt) {
exclude: ['nuxt/app'] exclude: ['nuxt/app']
}, },
css: resolveCSSOptions(nuxt), css: resolveCSSOptions(nuxt),
define: { __NUXT_VERSION__: JSON.stringify(nuxt._version) },
build: { build: {
copyPublicDir: false, copyPublicDir: false,
rollupOptions: { rollupOptions: {

View File

@ -230,6 +230,7 @@ function getEnv (ctx: WebpackConfigContext) {
'process.env.NODE_ENV': JSON.stringify(ctx.config.mode), 'process.env.NODE_ENV': JSON.stringify(ctx.config.mode),
'process.mode': JSON.stringify(ctx.config.mode), 'process.mode': JSON.stringify(ctx.config.mode),
'process.dev': options.dev, 'process.dev': options.dev,
__NUXT_VERSION__: JSON.stringify(ctx.nuxt._version),
'process.env.VUE_ENV': JSON.stringify(ctx.name), 'process.env.VUE_ENV': JSON.stringify(ctx.name),
'process.browser': ctx.isClient, 'process.browser': ctx.isClient,
'process.client': ctx.isClient, 'process.client': ctx.isClient,

View File

@ -40,7 +40,7 @@ describe.skipIf(isWindows)('minimal nuxt application', () => {
it('default server bundle size', async () => { it('default server bundle size', async () => {
stats.server = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir) stats.server = await analyzeSizes(['**/*.mjs', '!node_modules'], serverDir)
expect(stats.server.totalBytes).toBeLessThan(92800) expect(stats.server.totalBytes).toBeLessThan(92900)
const modules = await analyzeSizes('node_modules/**/*', serverDir) const modules = await analyzeSizes('node_modules/**/*', serverDir)
expect(modules.totalBytes).toBeLessThan(2722000) expect(modules.totalBytes).toBeLessThan(2722000)