Skip to content

Commit 8d74ca0

Browse files
authored
fix(compiler-sfc): generate more treeshaking friendly code (#9507)
close #9500
1 parent cdb2df7 commit 8d74ca0

File tree

7 files changed

+26
-21
lines changed

7 files changed

+26
-21
lines changed

packages/compiler-sfc/__tests__/compileScript/__snapshots__/defineModel.spec.ts.snap

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ exports[`defineModel() > w/ array props 1`] = `
2727
"import { useModel as _useModel, mergeModels as _mergeModels } from 'vue'
2828
2929
export default {
30-
props: _mergeModels(['foo', 'bar'], {
30+
props: /*#__PURE__*/_mergeModels(['foo', 'bar'], {
3131
\\"count\\": {},
3232
}),
3333
emits: [\\"update:count\\"],
@@ -47,10 +47,10 @@ exports[`defineModel() > w/ defineProps and defineEmits 1`] = `
4747
"import { useModel as _useModel, mergeModels as _mergeModels } from 'vue'
4848
4949
export default {
50-
props: _mergeModels({ foo: String }, {
50+
props: /*#__PURE__*/_mergeModels({ foo: String }, {
5151
\\"modelValue\\": { default: 0 },
5252
}),
53-
emits: _mergeModels(['change'], [\\"update:modelValue\\"]),
53+
emits: /*#__PURE__*/_mergeModels(['change'], [\\"update:modelValue\\"]),
5454
setup(__props, { expose: __expose }) {
5555
__expose();
5656

packages/compiler-sfc/__tests__/compileScript/__snapshots__/defineProps.spec.ts.snap

+4-4
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ exports[`defineProps > withDefaults (dynamic) 1`] = `
332332
import { defaults } from './foo'
333333
334334
export default /*#__PURE__*/_defineComponent({
335-
props: _mergeDefaults({
335+
props: /*#__PURE__*/_mergeDefaults({
336336
foo: { type: String, required: false },
337337
bar: { type: Number, required: false },
338338
baz: { type: Boolean, required: true }
@@ -353,7 +353,7 @@ exports[`defineProps > withDefaults (dynamic) w/ production mode 1`] = `
353353
import { defaults } from './foo'
354354
355355
export default /*#__PURE__*/_defineComponent({
356-
props: _mergeDefaults({
356+
props: /*#__PURE__*/_mergeDefaults({
357357
foo: { type: Function },
358358
bar: { type: Boolean },
359359
baz: { type: [Boolean, Function] },
@@ -375,7 +375,7 @@ exports[`defineProps > withDefaults (reference) 1`] = `
375375
import { defaults } from './foo'
376376
377377
export default /*#__PURE__*/_defineComponent({
378-
props: _mergeDefaults({
378+
props: /*#__PURE__*/_mergeDefaults({
379379
foo: { type: String, required: false },
380380
bar: { type: Number, required: false },
381381
baz: { type: Boolean, required: true }
@@ -462,7 +462,7 @@ exports[`defineProps > withDefaults w/ dynamic object method 1`] = `
462462
"import { mergeDefaults as _mergeDefaults, defineComponent as _defineComponent } from 'vue'
463463
464464
export default /*#__PURE__*/_defineComponent({
465-
props: _mergeDefaults({
465+
props: /*#__PURE__*/_mergeDefaults({
466466
foo: { type: Function, required: false }
467467
}, {
468468
['fo' + 'o']() { return 'foo' }

packages/compiler-sfc/__tests__/compileScript/__snapshots__/definePropsDestructure.spec.ts.snap

+3-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ exports[`sfc reactive props destructure > default values w/ array runtime declar
6262
"import { mergeDefaults as _mergeDefaults } from 'vue'
6363
6464
export default {
65-
props: _mergeDefaults(['foo', 'bar', 'baz'], {
65+
props: /*#__PURE__*/_mergeDefaults(['foo', 'bar', 'baz'], {
6666
foo: 1,
6767
bar: () => ({}),
6868
func: () => {}, __skip_func: true
@@ -81,7 +81,7 @@ exports[`sfc reactive props destructure > default values w/ object runtime decla
8181
"import { mergeDefaults as _mergeDefaults } from 'vue'
8282
8383
export default {
84-
props: _mergeDefaults({ foo: Number, bar: Object, func: Function, ext: null }, {
84+
props: /*#__PURE__*/_mergeDefaults({ foo: Number, bar: Object, func: Function, ext: null }, {
8585
foo: 1,
8686
bar: () => ({}),
8787
func: () => {}, __skip_func: true,
@@ -101,7 +101,7 @@ exports[`sfc reactive props destructure > default values w/ runtime declaration
101101
"import { mergeDefaults as _mergeDefaults } from 'vue'
102102
103103
export default {
104-
props: _mergeDefaults(['foo', 'foo:bar'], {
104+
props: /*#__PURE__*/_mergeDefaults(['foo', 'foo:bar'], {
105105
foo: 1,
106106
\\"foo:bar\\": 'foo-bar'
107107
}),

packages/compiler-sfc/__tests__/compileScript/defineModel.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ describe('defineModel()', () => {
4848
{ defineModel: true }
4949
)
5050
assertCode(content)
51-
expect(content).toMatch(`props: _mergeModels({ foo: String }`)
51+
expect(content).toMatch(`props: /*#__PURE__*/_mergeModels({ foo: String }`)
5252
expect(content).toMatch(`"modelValue": { default: 0 }`)
5353
expect(content).toMatch(`const count = _useModel(__props, "modelValue")`)
5454
expect(content).not.toMatch('defineModel')
@@ -70,7 +70,7 @@ describe('defineModel()', () => {
7070
{ defineModel: true }
7171
)
7272
assertCode(content)
73-
expect(content).toMatch(`props: _mergeModels(['foo', 'bar'], {
73+
expect(content).toMatch(`props: /*#__PURE__*/_mergeModels(['foo', 'bar'], {
7474
"count": {},
7575
})`)
7676
expect(content).toMatch(`const count = _useModel(__props, "count")`)

packages/compiler-sfc/__tests__/compileScript/definePropsDestructure.spec.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ describe('sfc reactive props destructure', () => {
7878
// literals can be used as-is, non-literals are always returned from a
7979
// function
8080
// functions need to be marked with a skip marker
81-
expect(content).toMatch(`props: _mergeDefaults(['foo', 'bar', 'baz'], {
81+
expect(content)
82+
.toMatch(`props: /*#__PURE__*/_mergeDefaults(['foo', 'bar', 'baz'], {
8283
foo: 1,
8384
bar: () => ({}),
8485
func: () => {}, __skip_func: true
@@ -98,7 +99,7 @@ describe('sfc reactive props destructure', () => {
9899
// safely infer whether runtime type is Function (e.g. if the runtime decl
99100
// is imported, or spreads another object)
100101
expect(content)
101-
.toMatch(`props: _mergeDefaults({ foo: Number, bar: Object, func: Function, ext: null }, {
102+
.toMatch(`props: /*#__PURE__*/_mergeDefaults({ foo: Number, bar: Object, func: Function, ext: null }, {
102103
foo: 1,
103104
bar: () => ({}),
104105
func: () => {}, __skip_func: true,
@@ -122,7 +123,7 @@ describe('sfc reactive props destructure', () => {
122123
})
123124

124125
expect(content).toMatch(`
125-
props: _mergeDefaults(['foo', 'foo:bar'], {
126+
props: /*#__PURE__*/_mergeDefaults(['foo', 'foo:bar'], {
126127
foo: 1,
127128
"foo:bar": 'foo-bar'
128129
}),`)

packages/compiler-sfc/src/script/defineEmits.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ export function genRuntimeEmits(ctx: ScriptCompileContext): string | undefined {
5858
.map(n => JSON.stringify(`update:${n}`))
5959
.join(', ')}]`
6060
emitsDecl = emitsDecl
61-
? `${ctx.helper('mergeModels')}(${emitsDecl}, ${modelEmitsDecl})`
61+
? `/*#__PURE__*/${ctx.helper(
62+
'mergeModels'
63+
)}(${emitsDecl}, ${modelEmitsDecl})`
6264
: modelEmitsDecl
6365
}
6466
return emitsDecl

packages/compiler-sfc/src/script/defineProps.ts

+7-5
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ export function genRuntimeProps(ctx: ScriptCompileContext): string | undefined {
144144
)
145145
}
146146
if (defaults.length) {
147-
propsDecls = `${ctx.helper(
147+
propsDecls = `/*#__PURE__*/${ctx.helper(
148148
`mergeDefaults`
149149
)}(${propsDecls}, {\n ${defaults.join(',\n ')}\n})`
150150
}
@@ -156,7 +156,9 @@ export function genRuntimeProps(ctx: ScriptCompileContext): string | undefined {
156156
const modelsDecls = genModelProps(ctx)
157157

158158
if (propsDecls && modelsDecls) {
159-
return `${ctx.helper('mergeModels')}(${propsDecls}, ${modelsDecls})`
159+
return `/*#__PURE__*/${ctx.helper(
160+
'mergeModels'
161+
)}(${propsDecls}, ${modelsDecls})`
160162
} else {
161163
return modelsDecls || propsDecls
162164
}
@@ -184,9 +186,9 @@ function genRuntimePropsFromTypes(ctx: ScriptCompileContext) {
184186
${propStrings.join(',\n ')}\n }`
185187

186188
if (ctx.propsRuntimeDefaults && !hasStaticDefaults) {
187-
propsDecls = `${ctx.helper('mergeDefaults')}(${propsDecls}, ${ctx.getString(
188-
ctx.propsRuntimeDefaults
189-
)})`
189+
propsDecls = `/*#__PURE__*/${ctx.helper(
190+
'mergeDefaults'
191+
)}(${propsDecls}, ${ctx.getString(ctx.propsRuntimeDefaults)})`
190192
}
191193

192194
return propsDecls

0 commit comments

Comments
 (0)