From d4a75240acec551f63cbbfd468f749b81641efb6 Mon Sep 17 00:00:00 2001 From: Daniel Roe Date: Wed, 1 Mar 2023 15:08:23 +0000 Subject: [PATCH] feat(nuxt): add `versions` to runtime nuxtApp (#19064) --- docs/2.guide/3.going-further/1.internals.md | 2 ++ packages/nuxt/index.d.ts | 5 +++++ packages/nuxt/src/app/nuxt.ts | 5 +++++ packages/vite/src/vite.ts | 1 + packages/webpack/src/presets/base.ts | 1 + test/bundle.test.ts | 2 +- 6 files changed, 15 insertions(+), 1 deletion(-) create mode 100644 packages/nuxt/index.d.ts diff --git a/docs/2.guide/3.going-further/1.internals.md b/docs/2.guide/3.going-further/1.internals.md index 398ee8a8db..2220aae10a 100644 --- a/docs/2.guide/3.going-further/1.internals.md +++ b/docs/2.guide/3.going-further/1.internals.md @@ -42,6 +42,8 @@ Note: this is an internal interface, and some properties might change until stab const nuxtApp = { 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 // https://github.com/nuxt/nuxt/blob/main/packages/nuxt/src/app/nuxt.ts#L18 hooks, diff --git a/packages/nuxt/index.d.ts b/packages/nuxt/index.d.ts new file mode 100644 index 0000000000..6b83f9e99c --- /dev/null +++ b/packages/nuxt/index.d.ts @@ -0,0 +1,5 @@ +declare global { + const __NUXT_VERSION__: string +} + +export {} diff --git a/packages/nuxt/src/app/nuxt.ts b/packages/nuxt/src/app/nuxt.ts index 42afeecb71..cc1bcecf7a 100644 --- a/packages/nuxt/src/app/nuxt.ts +++ b/packages/nuxt/src/app/nuxt.ts @@ -60,6 +60,7 @@ export interface NuxtSSRContext extends SSRContext { interface _NuxtApp { vueApp: App globalName: string + versions: Record hooks: Hookable hook: _NuxtApp['hooks']['hook'] @@ -120,6 +121,10 @@ export function createNuxtApp (options: CreateOptions) { const nuxtApp: NuxtApp = { provide: undefined, globalName: 'nuxt', + versions: { + get nuxt () { return __NUXT_VERSION__ }, + get vue () { return nuxtApp.vueApp.version } + }, payload: reactive({ data: {}, state: {}, diff --git a/packages/vite/src/vite.ts b/packages/vite/src/vite.ts index 07f7f4b68f..4797ec4090 100644 --- a/packages/vite/src/vite.ts +++ b/packages/vite/src/vite.ts @@ -59,6 +59,7 @@ export async function bundle (nuxt: Nuxt) { exclude: ['nuxt/app'] }, css: resolveCSSOptions(nuxt), + define: { __NUXT_VERSION__: JSON.stringify(nuxt._version) }, build: { copyPublicDir: false, rollupOptions: { diff --git a/packages/webpack/src/presets/base.ts b/packages/webpack/src/presets/base.ts index acb551a507..056c08bace 100644 --- a/packages/webpack/src/presets/base.ts +++ b/packages/webpack/src/presets/base.ts @@ -230,6 +230,7 @@ function getEnv (ctx: WebpackConfigContext) { 'process.env.NODE_ENV': JSON.stringify(ctx.config.mode), 'process.mode': JSON.stringify(ctx.config.mode), 'process.dev': options.dev, + __NUXT_VERSION__: JSON.stringify(ctx.nuxt._version), 'process.env.VUE_ENV': JSON.stringify(ctx.name), 'process.browser': ctx.isClient, 'process.client': ctx.isClient, diff --git a/test/bundle.test.ts b/test/bundle.test.ts index 03f1f0f096..d771e25342 100644 --- a/test/bundle.test.ts +++ b/test/bundle.test.ts @@ -40,7 +40,7 @@ describe.skipIf(isWindows)('minimal nuxt application', () => { it('default server bundle size', async () => { 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) expect(modules.totalBytes).toBeLessThan(2722000)