@meistrari/tela-build 1.50.2 → 1.50.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meistrari/tela-build",
3
- "version": "1.50.2",
3
+ "version": "1.50.3",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "app.config.ts",
@@ -0,0 +1,116 @@
1
+ /**
2
+ * @vitest-environment happy-dom
3
+ */
4
+ import { beforeEach, describe, expect, it, vi } from 'vitest'
5
+
6
+ type RuntimeConfig = {
7
+ app?: Record<string, unknown>
8
+ public?: Record<string, unknown>
9
+ }
10
+
11
+ type NuxtWindow = typeof window & {
12
+ __NUXT__?: {
13
+ config?: {
14
+ app?: Record<string, unknown>
15
+ public?: Record<string, unknown>
16
+ [key: string]: unknown
17
+ }
18
+ }
19
+ }
20
+
21
+ const runtimeConfig = vi.hoisted((): { current: RuntimeConfig } => ({
22
+ current: {
23
+ app: {
24
+ baseURL: '/',
25
+ buildAssetsDir: '/_nuxt/',
26
+ cdnURL: 'https://cdn.example.com/',
27
+ },
28
+ public: {
29
+ avatarAllowedOrigins: 'https://assets.example.com',
30
+ },
31
+ },
32
+ }))
33
+
34
+ vi.mock('nuxt/app', () => ({
35
+ defineNuxtPlugin: (plugin: () => void) => plugin,
36
+ useRuntimeConfig: () => runtimeConfig.current,
37
+ }))
38
+
39
+ const { default: mirrorAvatarRuntimeConfig } = await import('../avatar-runtime-config.client')
40
+
41
+ function getWindow(): NuxtWindow {
42
+ return window
43
+ }
44
+
45
+ describe('avatar runtime config plugin', () => {
46
+ beforeEach(() => {
47
+ delete getWindow().__NUXT__
48
+ runtimeConfig.current = {
49
+ app: {
50
+ baseURL: '/',
51
+ buildAssetsDir: '/_nuxt/',
52
+ cdnURL: 'https://cdn.example.com/',
53
+ },
54
+ public: {
55
+ avatarAllowedOrigins: 'https://assets.example.com',
56
+ },
57
+ }
58
+ })
59
+
60
+ it('mirrors public and app runtime config when creating the legacy global', () => {
61
+ mirrorAvatarRuntimeConfig()
62
+
63
+ expect(getWindow().__NUXT__?.config).toEqual({
64
+ app: runtimeConfig.current.app,
65
+ public: runtimeConfig.current.public,
66
+ })
67
+ })
68
+
69
+ it('adds missing app config when public config already exists', () => {
70
+ getWindow().__NUXT__ = {
71
+ config: {
72
+ public: {
73
+ avatarAllowedOrigins: 'https://existing.example.com',
74
+ },
75
+ otherConfig: 'kept',
76
+ },
77
+ }
78
+
79
+ mirrorAvatarRuntimeConfig()
80
+
81
+ expect(getWindow().__NUXT__?.config).toEqual({
82
+ app: runtimeConfig.current.app,
83
+ public: {
84
+ avatarAllowedOrigins: 'https://existing.example.com',
85
+ },
86
+ otherConfig: 'kept',
87
+ })
88
+ })
89
+
90
+ it('fills missing fields in partial app and public config', () => {
91
+ getWindow().__NUXT__ = {
92
+ config: {
93
+ app: {
94
+ baseURL: '/custom/',
95
+ },
96
+ public: {
97
+ theme: 'dark',
98
+ },
99
+ },
100
+ }
101
+
102
+ mirrorAvatarRuntimeConfig()
103
+
104
+ expect(getWindow().__NUXT__?.config).toEqual({
105
+ app: {
106
+ baseURL: '/custom/',
107
+ buildAssetsDir: '/_nuxt/',
108
+ cdnURL: 'https://cdn.example.com/',
109
+ },
110
+ public: {
111
+ avatarAllowedOrigins: 'https://assets.example.com',
112
+ theme: 'dark',
113
+ },
114
+ })
115
+ })
116
+ })
@@ -1,5 +1,32 @@
1
1
  import { defineNuxtPlugin, useRuntimeConfig } from 'nuxt/app'
2
2
 
3
+ type RuntimeConfig = {
4
+ app?: Record<string, unknown>
5
+ public?: Record<string, unknown>
6
+ }
7
+
8
+ type NuxtConfig = {
9
+ app?: Record<string, unknown>
10
+ public?: Record<string, unknown>
11
+ [key: string]: unknown
12
+ }
13
+
14
+ type NuxtWindow = typeof window & {
15
+ __NUXT__?: {
16
+ config?: NuxtConfig
17
+ }
18
+ }
19
+
20
+ function mergeConfigSection(runtimeSection?: Record<string, unknown>, existingSection?: Record<string, unknown>) {
21
+ if (!runtimeSection && !existingSection)
22
+ return
23
+
24
+ return {
25
+ ...runtimeSection,
26
+ ...existingSection,
27
+ }
28
+ }
29
+
3
30
  /**
4
31
  * tela-build's avatar sanitizer (`sanitizeAvatarImageUrl`) reads its host
5
32
  * allowlist from the legacy `window.__NUXT__.config.public` global, which Nuxt 4
@@ -7,21 +34,23 @@ import { defineNuxtPlugin, useRuntimeConfig } from 'nuxt/app'
7
34
  * hosts (Google/MS/GitHub/Gravatar), so avatars served from `avatarAllowedOrigins`
8
35
  * (e.g. vault asset hosts) fall back to initials.
9
36
  *
10
- * This is a fallback/polyfill, not a replacement: when the global is already
11
- * populated (Nuxt 3) we leave it untouched, otherwise we mirror the public
12
- * runtime config onto it so the sanitizer can resolve the configured hosts.
37
+ * This is a fallback/polyfill, not a replacement: it fills missing legacy
38
+ * config fields while preserving values already populated by Nuxt.
13
39
  */
14
40
  export default defineNuxtPlugin(() => {
15
- const globalWithNuxt = window as typeof window & {
16
- __NUXT__?: { config?: { public?: Record<string, unknown> } }
17
- }
41
+ const globalWithNuxt: NuxtWindow = window
42
+ const runtimeConfig = useRuntimeConfig() as RuntimeConfig
43
+ const existingConfig = globalWithNuxt.__NUXT__?.config
44
+ const appConfig = mergeConfigSection(runtimeConfig.app, existingConfig?.app)
45
+ const publicConfig = mergeConfigSection(runtimeConfig.public, existingConfig?.public)
18
46
 
19
- if (globalWithNuxt.__NUXT__?.config?.public)
47
+ if (!appConfig && !publicConfig)
20
48
  return
21
49
 
22
- const { public: publicConfig } = useRuntimeConfig()
23
-
24
50
  globalWithNuxt.__NUXT__ ??= {}
25
- globalWithNuxt.__NUXT__.config ??= {}
26
- globalWithNuxt.__NUXT__.config.public = publicConfig
51
+ globalWithNuxt.__NUXT__.config = {
52
+ ...existingConfig,
53
+ ...(appConfig ? { app: appConfig } : {}),
54
+ ...(publicConfig ? { public: publicConfig } : {}),
55
+ }
27
56
  })