From: Evan You Date: Tue, 1 Dec 2020 16:52:29 +0000 (-0500) Subject: fix(compiler-sfc): named imports from .vue file should not be treated as constant X-Git-Tag: v3.0.4~13 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=085bbd5fe07c52056e9f7151fbaed8f6a2e442b3;p=thirdparty%2Fvuejs%2Fcore.git fix(compiler-sfc): named imports from .vue file should not be treated as constant fix #2699 --- diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index a68625d607..01e2b80981 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -195,10 +195,10 @@ return { ref } `; exports[`SFC compile `, { inlineTemplate: true } ) // no need to unref vue component import - expect(content).toMatch(`createVNode(Foo)`) + expect(content).toMatch(`createVNode(Foo,`) + // #2699 should unref named imports from .vue + expect(content).toMatch(`unref(bar)`) // should unref other imports expect(content).toMatch(`unref(other)`) // no need to unref constant literals diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 7dca8606e2..b51ae34f21 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -168,7 +168,7 @@ export function compileScript( string, { isType: boolean - imported: string | null + imported: string source: string } > = Object.create(null) @@ -246,7 +246,7 @@ export function compileScript( } userImports[local] = { isType, - imported: imported || null, + imported: imported || 'default', source } } @@ -807,10 +807,12 @@ export function compileScript( for (const key in typeDeclaredProps) { bindingMetadata[key] = BindingTypes.PROPS } - for (const [key, { isType, source }] of Object.entries(userImports)) { + for (const [key, { isType, imported, source }] of Object.entries( + userImports + )) { if (isType) continue bindingMetadata[key] = - source.endsWith('.vue') || source === 'vue' + (imported === 'default' && source.endsWith('.vue')) || source === 'vue' ? BindingTypes.SETUP_CONST : BindingTypes.SETUP_MAYBE_REF }