From b77b219cf0a5564ae0b2d3838e7b75f6390b2bb1 Mon Sep 17 00:00:00 2001 From: ygj6 Date: Wed, 13 Oct 2021 15:06:53 +0800 Subject: [PATCH 1/2] fix(compiler-sfc): add type for props's properties in prod mode --- .../compileScriptPropsTransform.spec.ts.snap | 7 +++--- .../compileScriptPropsTransform.spec.ts | 9 ++++---- packages/compiler-sfc/src/compileScript.ts | 23 +++++++++++-------- 3 files changed, 22 insertions(+), 17 deletions(-) diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScriptPropsTransform.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScriptPropsTransform.spec.ts.snap index a8f95c72a13..f85b2db9b9f 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScriptPropsTransform.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScriptPropsTransform.spec.ts.snap @@ -80,9 +80,10 @@ exports[`sfc props transform default values w/ type declaration, prod mode 1`] = export default /*#__PURE__*/_defineComponent({ props: { - foo: { default: 1 }, - bar: { default: () => {} }, - baz: null + foo: { type: Number, default: 1 }, + bar: { type: Object, default: () => {} }, + baz: { type: null }, + bool: { type: Boolean } }, setup(__props: any) { diff --git a/packages/compiler-sfc/__tests__/compileScriptPropsTransform.spec.ts b/packages/compiler-sfc/__tests__/compileScriptPropsTransform.spec.ts index f388e501dd9..e8fc9039f55 100644 --- a/packages/compiler-sfc/__tests__/compileScriptPropsTransform.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScriptPropsTransform.spec.ts @@ -83,7 +83,7 @@ describe('sfc props transform', () => { const { content } = compile( ` `, { isProd: true } @@ -91,9 +91,10 @@ describe('sfc props transform', () => { // literals can be used as-is, non-literals are always returned from a // function expect(content).toMatch(`props: { - foo: { default: 1 }, - bar: { default: () => {} }, - baz: null + foo: { type: Number, default: 1 }, + bar: { type: Object, default: () => {} }, + baz: { type: null }, + bool: { type: Boolean } }`) assertCode(content) }) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 0e60d0d9e31..9a4ac1979f7 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -693,8 +693,13 @@ export function compileScript( defaultString ? `, ${defaultString}` : `` } }` } else { + const { type } = props[key] // production: checks are useless - return `${key}: ${defaultString ? `{ ${defaultString} }` : 'null'}` + return `${key}: { type: ${toRuntimeTypeString( + type + )}${ + defaultString ? `, ${defaultString}` : `` + } }` } }) .join(',\n ')}\n }` @@ -1621,15 +1626,13 @@ function extractRuntimeProps( m.key.type === 'Identifier' ) { let type - if (!isProd) { - if (m.type === 'TSMethodSignature') { - type = ['Function'] - } else if (m.typeAnnotation) { - type = inferRuntimeType( - m.typeAnnotation.typeAnnotation, - declaredTypes - ) - } + if (m.type === 'TSMethodSignature') { + type = ['Function'] + } else if (m.typeAnnotation) { + type = inferRuntimeType( + m.typeAnnotation.typeAnnotation, + declaredTypes + ) } props[m.key.name] = { key: m.key.name, From 983409e6c2c1140e1b11a17c9ebb37c547e52e97 Mon Sep 17 00:00:00 2001 From: ygj6 Date: Thu, 14 Oct 2021 16:21:31 +0800 Subject: [PATCH 2/2] fix(compiler-sfc): only keep the type when Boolean exists in prod mode --- .../compileScriptPropsTransform.spec.ts.snap | 9 +++++---- .../__tests__/compileScriptPropsTransform.spec.ts | 11 ++++++----- packages/compiler-sfc/src/compileScript.ts | 10 ++++++---- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScriptPropsTransform.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScriptPropsTransform.spec.ts.snap index f85b2db9b9f..5b11ae5c1a5 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScriptPropsTransform.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScriptPropsTransform.spec.ts.snap @@ -80,10 +80,11 @@ exports[`sfc props transform default values w/ type declaration, prod mode 1`] = export default /*#__PURE__*/_defineComponent({ props: { - foo: { type: Number, default: 1 }, - bar: { type: Object, default: () => {} }, - baz: { type: null }, - bool: { type: Boolean } + foo: { default: 1 }, + bar: { default: () => {} }, + baz: null, + boola: { type: Boolean }, + boolb: { type: [Boolean, Number] } }, setup(__props: any) { diff --git a/packages/compiler-sfc/__tests__/compileScriptPropsTransform.spec.ts b/packages/compiler-sfc/__tests__/compileScriptPropsTransform.spec.ts index e8fc9039f55..5d406608288 100644 --- a/packages/compiler-sfc/__tests__/compileScriptPropsTransform.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScriptPropsTransform.spec.ts @@ -83,7 +83,7 @@ describe('sfc props transform', () => { const { content } = compile( ` `, { isProd: true } @@ -91,10 +91,11 @@ describe('sfc props transform', () => { // literals can be used as-is, non-literals are always returned from a // function expect(content).toMatch(`props: { - foo: { type: Number, default: 1 }, - bar: { type: Object, default: () => {} }, - baz: { type: null }, - bool: { type: Boolean } + foo: { default: 1 }, + bar: { default: () => {} }, + baz: null, + boola: { type: Boolean }, + boolb: { type: [Boolean, Number] } }`) assertCode(content) }) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 9a4ac1979f7..dcaf3848caa 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -685,21 +685,23 @@ export function compileScript( } } + const { type, required } = props[key] if (!isProd) { - const { type, required } = props[key] return `${key}: { type: ${toRuntimeTypeString( type )}, required: ${required}${ defaultString ? `, ${defaultString}` : `` } }` - } else { - const { type } = props[key] - // production: checks are useless + } else if (type.indexOf('Boolean') > -1) { + // production: if boolean exists, should keep the type. return `${key}: { type: ${toRuntimeTypeString( type )}${ defaultString ? `, ${defaultString}` : `` } }` + } else { + // production: checks are useless + return `${key}: ${defaultString ? `{ ${defaultString} }` : 'null'}` } }) .join(',\n ')}\n }`