From 326d7866594c1c909f5def25dfad11a94ad20d9b Mon Sep 17 00:00:00 2001 From: edison1105 Date: Mon, 30 Oct 2023 20:50:29 +0800 Subject: [PATCH 1/2] fix(compiler-sfc): ensure mergeDefaults code can be treeshaken --- .../compileScript/__snapshots__/defineProps.spec.ts.snap | 8 ++++---- .../__snapshots__/definePropsDestructure.spec.ts.snap | 6 +++--- .../compileScript/definePropsDestructure.spec.ts | 7 ++++--- packages/compiler-sfc/src/script/defineProps.ts | 8 ++++---- 4 files changed, 15 insertions(+), 14 deletions(-) diff --git a/packages/compiler-sfc/__tests__/compileScript/__snapshots__/defineProps.spec.ts.snap b/packages/compiler-sfc/__tests__/compileScript/__snapshots__/defineProps.spec.ts.snap index 5b0f96c5014..f5d382e4a26 100644 --- a/packages/compiler-sfc/__tests__/compileScript/__snapshots__/defineProps.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/compileScript/__snapshots__/defineProps.spec.ts.snap @@ -287,7 +287,7 @@ exports[`defineProps > withDefaults (dynamic) 1`] = ` import { defaults } from './foo' export default /*#__PURE__*/_defineComponent({ - props: _mergeDefaults({ + props: /*#__PURE__*/_mergeDefaults({ foo: { type: String, required: false }, bar: { type: Number, required: false }, baz: { type: Boolean, required: true } @@ -308,7 +308,7 @@ exports[`defineProps > withDefaults (dynamic) w/ production mode 1`] = ` import { defaults } from './foo' export default /*#__PURE__*/_defineComponent({ - props: _mergeDefaults({ + props: /*#__PURE__*/_mergeDefaults({ foo: { type: Function }, bar: { type: Boolean }, baz: { type: [Boolean, Function] }, @@ -330,7 +330,7 @@ exports[`defineProps > withDefaults (reference) 1`] = ` import { defaults } from './foo' export default /*#__PURE__*/_defineComponent({ - props: _mergeDefaults({ + props: /*#__PURE__*/_mergeDefaults({ foo: { type: String, required: false }, bar: { type: Number, required: false }, baz: { type: Boolean, required: true } @@ -417,7 +417,7 @@ exports[`defineProps > withDefaults w/ dynamic object method 1`] = ` "import { mergeDefaults as _mergeDefaults, defineComponent as _defineComponent } from 'vue' export default /*#__PURE__*/_defineComponent({ - props: _mergeDefaults({ + props: /*#__PURE__*/_mergeDefaults({ foo: { type: Function, required: false } }, { ['fo' + 'o']() { return 'foo' } diff --git a/packages/compiler-sfc/__tests__/compileScript/__snapshots__/definePropsDestructure.spec.ts.snap b/packages/compiler-sfc/__tests__/compileScript/__snapshots__/definePropsDestructure.spec.ts.snap index c31005c40c0..5aa7d3e871f 100644 --- a/packages/compiler-sfc/__tests__/compileScript/__snapshots__/definePropsDestructure.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/compileScript/__snapshots__/definePropsDestructure.spec.ts.snap @@ -62,7 +62,7 @@ exports[`sfc reactive props destructure > default values w/ array runtime declar "import { mergeDefaults as _mergeDefaults } from 'vue' export default { - props: _mergeDefaults(['foo', 'bar', 'baz'], { + props: /*#__PURE__*/_mergeDefaults(['foo', 'bar', 'baz'], { foo: 1, bar: () => ({}), func: () => {}, __skip_func: true @@ -81,7 +81,7 @@ exports[`sfc reactive props destructure > default values w/ object runtime decla "import { mergeDefaults as _mergeDefaults } from 'vue' export default { - props: _mergeDefaults({ foo: Number, bar: Object, func: Function, ext: null }, { + props: /*#__PURE__*/_mergeDefaults({ foo: Number, bar: Object, func: Function, ext: null }, { foo: 1, bar: () => ({}), func: () => {}, __skip_func: true, @@ -101,7 +101,7 @@ exports[`sfc reactive props destructure > default values w/ runtime declaration "import { mergeDefaults as _mergeDefaults } from 'vue' export default { - props: _mergeDefaults(['foo', 'foo:bar'], { + props: /*#__PURE__*/_mergeDefaults(['foo', 'foo:bar'], { foo: 1, \\"foo:bar\\": 'foo-bar' }), diff --git a/packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts b/packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts index a41f3131bef..89cc31340af 100644 --- a/packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts @@ -78,7 +78,8 @@ describe('sfc reactive props destructure', () => { // literals can be used as-is, non-literals are always returned from a // function // functions need to be marked with a skip marker - expect(content).toMatch(`props: _mergeDefaults(['foo', 'bar', 'baz'], { + expect(content) + .toMatch(`props: /*#__PURE__*/_mergeDefaults(['foo', 'bar', 'baz'], { foo: 1, bar: () => ({}), func: () => {}, __skip_func: true @@ -98,7 +99,7 @@ describe('sfc reactive props destructure', () => { // safely infer whether runtime type is Function (e.g. if the runtime decl // is imported, or spreads another object) expect(content) - .toMatch(`props: _mergeDefaults({ foo: Number, bar: Object, func: Function, ext: null }, { + .toMatch(`props: /*#__PURE__*/_mergeDefaults({ foo: Number, bar: Object, func: Function, ext: null }, { foo: 1, bar: () => ({}), func: () => {}, __skip_func: true, @@ -122,7 +123,7 @@ describe('sfc reactive props destructure', () => { }) expect(content).toMatch(` - props: _mergeDefaults(['foo', 'foo:bar'], { + props: /*#__PURE__*/_mergeDefaults(['foo', 'foo:bar'], { foo: 1, "foo:bar": 'foo-bar' }),`) diff --git a/packages/compiler-sfc/src/script/defineProps.ts b/packages/compiler-sfc/src/script/defineProps.ts index 5004e314da1..cc3c1137042 100644 --- a/packages/compiler-sfc/src/script/defineProps.ts +++ b/packages/compiler-sfc/src/script/defineProps.ts @@ -144,7 +144,7 @@ export function genRuntimeProps(ctx: ScriptCompileContext): string | undefined { ) } if (defaults.length) { - propsDecls = `${ctx.helper( + propsDecls = `/*#__PURE__*/${ctx.helper( `mergeDefaults` )}(${propsDecls}, {\n ${defaults.join(',\n ')}\n})` } @@ -184,9 +184,9 @@ function genRuntimePropsFromTypes(ctx: ScriptCompileContext) { ${propStrings.join(',\n ')}\n }` if (ctx.propsRuntimeDefaults && !hasStaticDefaults) { - propsDecls = `${ctx.helper('mergeDefaults')}(${propsDecls}, ${ctx.getString( - ctx.propsRuntimeDefaults - )})` + propsDecls = `/*#__PURE__*/${ctx.helper( + 'mergeDefaults' + )}(${propsDecls}, ${ctx.getString(ctx.propsRuntimeDefaults)})` } return propsDecls From 0710797d2b6140c1cf37c53e7053495365ce3691 Mon Sep 17 00:00:00 2001 From: edison1105 Date: Mon, 30 Oct 2023 21:05:13 +0800 Subject: [PATCH 2/2] chore: mergeModels can be treeshaken --- .../compileScript/__snapshots__/defineModel.spec.ts.snap | 6 +++--- .../__tests__/compileScript/defineModel.spec.ts | 4 ++-- packages/compiler-sfc/src/script/defineEmits.ts | 4 +++- packages/compiler-sfc/src/script/defineProps.ts | 4 +++- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/compiler-sfc/__tests__/compileScript/__snapshots__/defineModel.spec.ts.snap b/packages/compiler-sfc/__tests__/compileScript/__snapshots__/defineModel.spec.ts.snap index 297ee62724c..27c358c5a94 100644 --- a/packages/compiler-sfc/__tests__/compileScript/__snapshots__/defineModel.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/compileScript/__snapshots__/defineModel.spec.ts.snap @@ -27,7 +27,7 @@ exports[`defineModel() > w/ array props 1`] = ` "import { useModel as _useModel, mergeModels as _mergeModels } from 'vue' export default { - props: _mergeModels(['foo', 'bar'], { + props: /*#__PURE__*/_mergeModels(['foo', 'bar'], { \\"count\\": {}, }), emits: [\\"update:count\\"], @@ -47,10 +47,10 @@ exports[`defineModel() > w/ defineProps and defineEmits 1`] = ` "import { useModel as _useModel, mergeModels as _mergeModels } from 'vue' export default { - props: _mergeModels({ foo: String }, { + props: /*#__PURE__*/_mergeModels({ foo: String }, { \\"modelValue\\": { default: 0 }, }), - emits: _mergeModels(['change'], [\\"update:modelValue\\"]), + emits: /*#__PURE__*/_mergeModels(['change'], [\\"update:modelValue\\"]), setup(__props, { expose: __expose }) { __expose(); diff --git a/packages/compiler-sfc/__tests__/compileScript/defineModel.spec.ts b/packages/compiler-sfc/__tests__/compileScript/defineModel.spec.ts index 10fab947c13..46890109cd1 100644 --- a/packages/compiler-sfc/__tests__/compileScript/defineModel.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript/defineModel.spec.ts @@ -48,7 +48,7 @@ describe('defineModel()', () => { { defineModel: true } ) assertCode(content) - expect(content).toMatch(`props: _mergeModels({ foo: String }`) + expect(content).toMatch(`props: /*#__PURE__*/_mergeModels({ foo: String }`) expect(content).toMatch(`"modelValue": { default: 0 }`) expect(content).toMatch(`const count = _useModel(__props, "modelValue")`) expect(content).not.toMatch('defineModel') @@ -70,7 +70,7 @@ describe('defineModel()', () => { { defineModel: true } ) assertCode(content) - expect(content).toMatch(`props: _mergeModels(['foo', 'bar'], { + expect(content).toMatch(`props: /*#__PURE__*/_mergeModels(['foo', 'bar'], { "count": {}, })`) expect(content).toMatch(`const count = _useModel(__props, "count")`) diff --git a/packages/compiler-sfc/src/script/defineEmits.ts b/packages/compiler-sfc/src/script/defineEmits.ts index 8bd4cdfe543..44a3f2c8df3 100644 --- a/packages/compiler-sfc/src/script/defineEmits.ts +++ b/packages/compiler-sfc/src/script/defineEmits.ts @@ -58,7 +58,9 @@ export function genRuntimeEmits(ctx: ScriptCompileContext): string | undefined { .map(n => JSON.stringify(`update:${n}`)) .join(', ')}]` emitsDecl = emitsDecl - ? `${ctx.helper('mergeModels')}(${emitsDecl}, ${modelEmitsDecl})` + ? `/*#__PURE__*/${ctx.helper( + 'mergeModels' + )}(${emitsDecl}, ${modelEmitsDecl})` : modelEmitsDecl } return emitsDecl diff --git a/packages/compiler-sfc/src/script/defineProps.ts b/packages/compiler-sfc/src/script/defineProps.ts index cc3c1137042..18cc490b287 100644 --- a/packages/compiler-sfc/src/script/defineProps.ts +++ b/packages/compiler-sfc/src/script/defineProps.ts @@ -156,7 +156,9 @@ export function genRuntimeProps(ctx: ScriptCompileContext): string | undefined { const modelsDecls = genModelProps(ctx) if (propsDecls && modelsDecls) { - return `${ctx.helper('mergeModels')}(${propsDecls}, ${modelsDecls})` + return `/*#__PURE__*/${ctx.helper( + 'mergeModels' + )}(${propsDecls}, ${modelsDecls})` } else { return modelsDecls || propsDecls }