Browse Source

fix(custom-element): prevent defineCustomElement from mutating the options object (#13791)

Folee 7 months ago
parent
commit
e322436887

+ 12 - 0
packages/runtime-dom/__tests__/customElement.spec.ts

@@ -1774,4 +1774,16 @@ describe('defineCustomElement', () => {
       `<el-hyphenated-attr-removal></el-hyphenated-attr-removal>`,
     )
   })
+
+  test('no unexpected mutation of the 1st argument', () => {
+    const Foo = {
+      name: 'Foo',
+    }
+
+    defineCustomElement(Foo, { shadowRoot: false })
+
+    expect(Foo).toEqual({
+      name: 'Foo',
+    })
+  })
 })

+ 2 - 2
packages/runtime-dom/src/apiCustomElement.ts

@@ -172,8 +172,8 @@ export function defineCustomElement(
    */
   _createApp?: CreateAppFunction<Element>,
 ): VueElementConstructor {
-  const Comp = defineComponent(options, extraOptions) as any
-  if (isPlainObject(Comp)) extend(Comp, extraOptions)
+  let Comp = defineComponent(options, extraOptions) as any
+  if (isPlainObject(Comp)) Comp = extend({}, Comp, extraOptions)
   class VueCustomElement extends VueElement {
     static def = Comp
     constructor(initialProps?: Record<string, any>) {