From e80476844239aafc8bb07884b072393d48caa118 Mon Sep 17 00:00:00 2001 From: Alex Van Liew Date: Sat, 21 May 2022 18:21:37 -0700 Subject: [PATCH 1/5] add failing test --- packages/vue-compat/__tests__/global.spec.ts | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/packages/vue-compat/__tests__/global.spec.ts b/packages/vue-compat/__tests__/global.spec.ts index e4cd30074da..df6890bc273 100644 --- a/packages/vue-compat/__tests__/global.spec.ts +++ b/packages/vue-compat/__tests__/global.spec.ts @@ -448,3 +448,15 @@ test('global asset registration should affect apps created via createApp', () => expect(vm.$el.textContent).toBe('foo') delete singletonApp._context.components.foo }) + +test('local asset registration should not affect other local apps', () => { + const app1 = createApp({}); + const app2 = createApp({}); + + app1.component('foo', {}); + app2.component('foo', {}); + + expect( + `Component "foo" has already been registered in target app` + ).not.toHaveBeenWarned() +}) \ No newline at end of file From 84d9d36d3351792b08658308f1a59449117f0346 Mon Sep 17 00:00:00 2001 From: Alex Van Liew Date: Sat, 21 May 2022 18:21:43 -0700 Subject: [PATCH 2/5] fix failing test --- packages/runtime-core/src/compat/global.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/runtime-core/src/compat/global.ts b/packages/runtime-core/src/compat/global.ts index e0fc699fa5f..ec89b81971f 100644 --- a/packages/runtime-core/src/compat/global.ts +++ b/packages/runtime-core/src/compat/global.ts @@ -383,7 +383,7 @@ function applySingletonAppMutations(app: App) { // copy over asset registries and deopt flag ;['mixins', 'components', 'directives', 'filters', 'deopt'].forEach(key => { // @ts-ignore - app._context[key] = singletonApp._context[key] + app._context[key] = { ...singletonApp._context[key] } }) // copy over global config mutations From a0ed3bd4bc6b1ba293f0eb6cf37c99688218e5ba Mon Sep 17 00:00:00 2001 From: Alex Van Liew Date: Sat, 21 May 2022 18:25:51 -0700 Subject: [PATCH 3/5] add another test --- packages/vue-compat/__tests__/global.spec.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/vue-compat/__tests__/global.spec.ts b/packages/vue-compat/__tests__/global.spec.ts index df6890bc273..0bc18a5d588 100644 --- a/packages/vue-compat/__tests__/global.spec.ts +++ b/packages/vue-compat/__tests__/global.spec.ts @@ -449,6 +449,16 @@ test('global asset registration should affect apps created via createApp', () => delete singletonApp._context.components.foo }) +test('post-facto global asset registration should affect apps created via createApp', () => { + const app = createApp({ + template: '' + }) + Vue.component('foo', { template: 'foo' }) + const vm = app.mount(document.createElement('div')) as any; + expect(vm.$el.textContent).toBe('foo') + delete singletonApp._context.components.foo +}) + test('local asset registration should not affect other local apps', () => { const app1 = createApp({}); const app2 = createApp({}); From 30fb445f5c75769d3df37d8726502b1bd9d0860e Mon Sep 17 00:00:00 2001 From: Alex Van Liew Date: Sat, 21 May 2022 18:48:36 -0700 Subject: [PATCH 4/5] replace shallow copy with proxy --- packages/runtime-core/src/compat/global.ts | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/packages/runtime-core/src/compat/global.ts b/packages/runtime-core/src/compat/global.ts index ec89b81971f..67a98ba89d1 100644 --- a/packages/runtime-core/src/compat/global.ts +++ b/packages/runtime-core/src/compat/global.ts @@ -380,11 +380,21 @@ function installLegacyAPIs(app: App) { } function applySingletonAppMutations(app: App) { - // copy over asset registries and deopt flag - ;['mixins', 'components', 'directives', 'filters', 'deopt'].forEach(key => { + // create asset registry proxies which lookup from singleton and fallback to local registry + (['mixins', 'components', 'directives', 'filters'] as const).forEach(key => { // @ts-ignore - app._context[key] = { ...singletonApp._context[key] } + app._context[key] = new Proxy(app._context[key]!, { + get(target, prop) { + if (prop in singletonApp._context[key]!) { + // @ts-ignore + return singletonApp._context[key][prop]; + } + return Reflect.get(target, prop); + } + }) }) + // @ts-ignore + app._context.deopt = singletonApp._context.deopt // copy over global config mutations isCopyingConfig = true From 269c821d696ee9d7b5f2088fc21b6a5c1fcd1d47 Mon Sep 17 00:00:00 2001 From: Evan You Date: Sun, 22 May 2022 21:35:17 -0400 Subject: [PATCH 5/5] Update global.ts --- packages/runtime-core/src/compat/global.ts | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/packages/runtime-core/src/compat/global.ts b/packages/runtime-core/src/compat/global.ts index 67a98ba89d1..1cde73bd48a 100644 --- a/packages/runtime-core/src/compat/global.ts +++ b/packages/runtime-core/src/compat/global.ts @@ -380,21 +380,12 @@ function installLegacyAPIs(app: App) { } function applySingletonAppMutations(app: App) { - // create asset registry proxies which lookup from singleton and fallback to local registry - (['mixins', 'components', 'directives', 'filters'] as const).forEach(key => { + // copy over asset registries and deopt flag + app._context.mixins = [...singletonApp._context.mixins] + ;['components', 'directives', 'filters'].forEach(key => { // @ts-ignore - app._context[key] = new Proxy(app._context[key]!, { - get(target, prop) { - if (prop in singletonApp._context[key]!) { - // @ts-ignore - return singletonApp._context[key][prop]; - } - return Reflect.get(target, prop); - } - }) + app._context[key] = Object.create(singletonApp._context[key]) }) - // @ts-ignore - app._context.deopt = singletonApp._context.deopt // copy over global config mutations isCopyingConfig = true