Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit419d2d2

Browse files
committed
feat(better-define): improve production mode
1 parent722a807 commit419d2d2

File tree

12 files changed

+339
-54
lines changed

12 files changed

+339
-54
lines changed

‎.changeset/slimy-sloths-thank.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@vue-macros/api':minor
3+
'@vue-macros/better-define':minor
4+
'@vue-macros/common':patch
5+
'unplugin-vue-macros':patch
6+
---
7+
8+
improve production mode

‎.prettierignore‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ node_modules
22
dist
33
pnpm-lock.yaml
44
coverage
5-
fixtures

‎packages/api/src/vue/props.ts‎

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import{
22
babelParse,
3-
isStaticExpression,
3+
isStaticObjectKey,
44
resolveObjectExpression,
55
}from'@vue-macros/common'
66
import{
@@ -302,12 +302,7 @@ export async function handleTSPropsDefinition({
302302
if(!defaultsAst)return{}
303303

304304
constisStatic=
305-
defaultsAst.type==='ObjectExpression'&&
306-
isStaticExpression(defaultsAst,{
307-
array:true,
308-
object:true,
309-
objectMethod:true,
310-
})
305+
defaultsAst.type==='ObjectExpression'&&isStaticObjectKey(defaultsAst)
311306
if(!isStatic)return{defaultsAst:defaultsAstasExpression}
312307

313308
constdefaults=resolveObjectExpression(defaultsAst)

‎packages/api/src/vue/utils.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,21 @@ export async function inferRuntimeType(
1414
return['Boolean']
1515
case'TSObjectKeyword':
1616
return['Object']
17-
case'TSTypeLiteral':
17+
case'TSTypeLiteral':{
1818
// TODO (nice to have) generate runtime property validation
19-
return['Object']
19+
consttypes=newSet<string>()
20+
for(constmofnode.type.members){
21+
switch(m.type){
22+
case'TSCallSignatureDeclaration':
23+
case'TSConstructSignatureDeclaration':
24+
types.add('Function')
25+
break
26+
default:
27+
types.add('Object')
28+
}
29+
}
30+
returnArray.from(types)
31+
}
2032
case'TSFunctionType':
2133
return['Function']
2234
case'TSArrayType':

‎packages/better-define/src/core/index.ts‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@ import { analyzeSFC } from '@vue-macros/api'
33
importtype{TSEmits,TSProps}from'@vue-macros/api'
44
importtype{}from'@babel/types'
55

6-
exportconsttransformBetterDefine=async(code:string,id:string)=>{
6+
exportconsttransformBetterDefine=async(
7+
code:string,
8+
id:string,
9+
isProduction?:boolean
10+
)=>{
711
consts=newMagicString(code)
812
constsfc=parseSFC(code,id)
913
if(sfc.script||!sfc.scriptSetup)return
@@ -22,16 +26,24 @@ export const transformBetterDefine = async (code: string, id: string) => {
2226
asyncfunctionprocessProps(props:TSProps){
2327
construntimeDefs=awaitprops.getRuntimeDefinitions()
2428

25-
// TODO prod mode
2629
construntimeDecls=`{\n${Object.entries(runtimeDefs)
2730
.map(([key,{ type, required,default:defaultDecl}])=>{
2831
letdefaultString=''
2932
if(defaultDecl){
30-
defaultString=`,${defaultDecl('default')}`
33+
defaultString=defaultDecl('default')
34+
}
35+
if(!isProduction){
36+
return`${key}: { type:${toRuntimeTypeString(
37+
type
38+
)}, required:${required},${defaultString} }`
39+
}elseif(type.some((el)=>el==='Boolean'||el==='Function')){
40+
return`${key}: { type:${toRuntimeTypeString(
41+
type
42+
)},${defaultString} }`
43+
}else{
44+
// production: checks are useless
45+
return`${key}:${defaultString ?`{${defaultString} }` :'null'}`
3146
}
32-
return`${key}: { type:${toRuntimeTypeString(
33-
type
34-
)}, required:${required}${defaultString} }`
3547
})
3648
.join(',\n ')}\n}`
3749

‎packages/better-define/src/index.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,25 @@ import type { FilterPattern } from '@rollup/pluginutils'
1111
exportinterfaceOptions{
1212
include?:FilterPattern
1313
exclude?:FilterPattern
14+
isProduction?:boolean
1415
}
1516

1617
exporttypeOptionsResolved=Omit<Required<Options>,'exclude'>&{
1718
exclude?:FilterPattern
1819
}
1920

20-
functionresolveOption(options:Options):OptionsResolved{
21+
functionresolveOptions(options:Options):OptionsResolved{
2122
return{
2223
include:[REGEX_VUE_SFC,REGEX_SETUP_SFC],
24+
isProduction:process.env.NODE_ENV==='production',
2325
...options,
2426
}
2527
}
2628

2729
constname='unplugin-vue-better-define'
2830

2931
exportdefaultcreateUnplugin<Options|undefined>((userOptions={},meta)=>{
30-
constoptions=resolveOption(userOptions)
32+
constoptions=resolveOptions(userOptions)
3133
constfilter=createFilter(options.include,options.exclude)
3234

3335
return{
@@ -55,11 +57,17 @@ export default createUnplugin<Options | undefined>((userOptions = {}, meta) => {
5557

5658
asynctransform(code,id){
5759
try{
58-
returnawaittransformBetterDefine(code,id)
60+
returnawaittransformBetterDefine(code,id,options.isProduction)
5961
}catch(err:unknown){
6062
this.warn(`${name}${err}`)
6163
console.warn(err)
6264
}
6365
},
66+
67+
vite:{
68+
configResolved(config){
69+
options.isProduction=config.isProduction
70+
},
71+
},
6472
}
6573
})

‎packages/better-define/tests/__snapshots__/fixtures.test.ts.snap‎

Lines changed: 206 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Vitest Snapshot v1
22

3-
exports[`fixtures > tests/fixtures/basic.vue 1`]=`
3+
exports[`fixtures > tests/fixtures/basic.vue> isProduction is false1`]=`
44
"import{defineComponent,renderSlot} from 'vue';
55
66
var _sfc_main = /* @__PURE__ */ defineComponent({
@@ -35,7 +35,42 @@ export { basic as default };
3535
"
3636
`;
3737

38-
exports[`fixtures > tests/fixtures/defaults-dynamic.vue 1`]=`
38+
exports[`fixtures > tests/fixtures/basic.vue > isProduction is true 1`]=`
39+
"import{defineComponent,renderSlot} from 'vue';
40+
41+
var _sfc_main = /* @__PURE__ */ defineComponent({
42+
__name: \\"basic\\",
43+
props: {
44+
base:null,
45+
str:null,
46+
num:null,
47+
map:null,
48+
arr:null,
49+
union:null
50+
},
51+
emits: [\\"click\\", \\"change\\"],
52+
setup(__props) {
53+
return (_ctx,_cache) => {
54+
returnrenderSlot(_ctx.$slots, \\"default\\");
55+
};
56+
}
57+
});
58+
59+
var _export_sfc = (sfc, props) =>{
60+
consttarget=sfc.__vccOpts||sfc;
61+
for (const [key,val]ofprops) {
62+
target[key] = val;
63+
}
64+
returntarget;
65+
};
66+
67+
var basic = /* @__PURE__ */ _export_sfc(_sfc_main, [__FILE__]);
68+
69+
export{basicasdefault};
70+
"
71+
`;
72+
73+
exports[`fixtures > tests/fixtures/defaults-dynamic.vue > isProduction is false 1`]=`
3974
"import{defineComponent,mergeDefaults,openBlock,createElementBlock} from 'vue';
4075
4176
var _sfc_main = /* @__PURE__ */ defineComponent({
@@ -66,7 +101,38 @@ export { defaultsDynamic as default };
66101
"
67102
`;
68103

69-
exports[`fixtures > tests/fixtures/defaults-static.vue 1`]=`
104+
exports[`fixtures > tests/fixtures/defaults-dynamic.vue > isProduction is true 1`]=`
105+
"import{defineComponent,mergeDefaults,openBlock,createElementBlock} from 'vue';
106+
107+
var _sfc_main = /* @__PURE__ */ defineComponent({
108+
__name: \\"defaults-dynamic\\",
109+
props:mergeDefaults({
110+
foo:null
111+
}, {
112+
[\\"foo\\"]: \\"foo\\"
113+
}),
114+
setup(__props) {
115+
return (_ctx,_cache) => {
116+
returnopenBlock(),createElementBlock(\\"div\\");
117+
};
118+
}
119+
});
120+
121+
var _export_sfc = (sfc, props) =>{
122+
consttarget=sfc.__vccOpts||sfc;
123+
for (const [key,val]ofprops) {
124+
target[key] = val;
125+
}
126+
returntarget;
127+
};
128+
129+
var defaultsDynamic = /* @__PURE__ */ _export_sfc(_sfc_main, [__FILE__]);
130+
131+
export{defaultsDynamicasdefault};
132+
"
133+
`;
134+
135+
exports[`fixtures > tests/fixtures/defaults-static.vue > isProduction is false 1`]=`
70136
"import{defineComponent,openBlock,createElementBlock} from 'vue';
71137
72138
var _sfc_main = /* @__PURE__ */ defineComponent({
@@ -78,7 +144,10 @@ var _sfc_main = /* @__PURE__ */ defineComponent({
78144
} },
79145
baz: { type:Promise, required:false,async default() {
80146
return10;
81-
} }
147+
} },
148+
qux: { type:Function, required:false,default: ()=> {
149+
} },
150+
quux: { type:null, required:false, default:abc }
82151
},
83152
setup(__props) {
84153
return (_ctx,_cache) => {
@@ -101,7 +170,105 @@ export { defaultsStatic as default };
101170
"
102171
`;
103172

104-
exports[`fixtures > tests/fixtures/intersection.vue 1`]=`
173+
exports[`fixtures > tests/fixtures/defaults-static.vue > isProduction is true 1`]=`
174+
"import{defineComponent,openBlock,createElementBlock} from 'vue';
175+
176+
var _sfc_main = /* @__PURE__ */ defineComponent({
177+
__name: \\"defaults-static\\",
178+
props: {
179+
foo: { default: \\"foo\\" },
180+
bar: {get default() {
181+
return10;
182+
} },
183+
baz: {async default() {
184+
return10;
185+
} },
186+
qux: { type:Function,default: ()=> {
187+
} },
188+
quux: { default:abc }
189+
},
190+
setup(__props) {
191+
return (_ctx,_cache) => {
192+
returnopenBlock(),createElementBlock(\\"div\\");
193+
};
194+
}
195+
});
196+
197+
var _export_sfc = (sfc, props) =>{
198+
consttarget=sfc.__vccOpts||sfc;
199+
for (const [key,val]ofprops) {
200+
target[key] = val;
201+
}
202+
returntarget;
203+
};
204+
205+
var defaultsStatic = /* @__PURE__ */ _export_sfc(_sfc_main, [__FILE__]);
206+
207+
export{defaultsStaticasdefault};
208+
"
209+
`;
210+
211+
exports[`fixtures > tests/fixtures/fn-default.vue > isProduction is false 1`]=`
212+
"import{defineComponent} from 'vue';
213+
214+
var _sfc_main = /* @__PURE__ */ defineComponent({
215+
__name: \\"fn-default\\",
216+
props: {
217+
fn: { type:Function, required:true,default: ()=> {
218+
} }
219+
},
220+
setup(__props) {
221+
const props = __props;
222+
return () => {
223+
};
224+
}
225+
});
226+
227+
var _export_sfc = (sfc, props) =>{
228+
consttarget=sfc.__vccOpts||sfc;
229+
for (const [key,val]ofprops) {
230+
target[key] = val;
231+
}
232+
returntarget;
233+
};
234+
235+
var fnDefault = /* @__PURE__ */ _export_sfc(_sfc_main, [__FILE__]);
236+
237+
export{fnDefaultasdefault};
238+
"
239+
`;
240+
241+
exports[`fixtures > tests/fixtures/fn-default.vue > isProduction is true 1`]=`
242+
"import{defineComponent} from 'vue';
243+
244+
var _sfc_main = /* @__PURE__ */ defineComponent({
245+
__name: \\"fn-default\\",
246+
props: {
247+
fn: { type:Function,default: ()=> {
248+
} }
249+
},
250+
setup(__props) {
251+
const props = __props;
252+
return () => {
253+
};
254+
}
255+
});
256+
257+
var _export_sfc = (sfc, props) =>{
258+
consttarget=sfc.__vccOpts||sfc;
259+
for (const [key,val]ofprops) {
260+
target[key] = val;
261+
}
262+
returntarget;
263+
};
264+
265+
var fnDefault = /* @__PURE__ */ _export_sfc(_sfc_main, [__FILE__]);
266+
267+
export{fnDefaultasdefault};
268+
"
269+
`;
270+
271+
exports[`fixtures > tests/fixtures/intersection.vue > isProduction is false 1`]=`
105272
"import{defineComponent,renderSlot} from 'vue';
106273
107274
var _sfc_main = /* @__PURE__ */ defineComponent({
@@ -134,3 +301,37 @@ var intersection = /* @__PURE__ */ _export_sfc(_sfc_main, [__FILE__]);
134301
export{intersectionasdefault};
135302
"
136303
`;
304+
305+
exports[`fixtures > tests/fixtures/intersection.vue > isProduction is true 1`]=`
306+
"import{defineComponent,renderSlot} from 'vue';
307+
308+
var _sfc_main = /* @__PURE__ */ defineComponent({
309+
__name: \\"intersection\\",
310+
props: {
311+
base:null,
312+
str:null,
313+
num:null,
314+
map:null,
315+
arr:null,
316+
union:null
317+
},
318+
setup(__props) {
319+
return (_ctx,_cache) => {
320+
returnrenderSlot(_ctx.$slots, \\"default\\");
321+
};
322+
}
323+
});
324+
325+
var _export_sfc = (sfc, props) =>{
326+
consttarget=sfc.__vccOpts||sfc;
327+
for (const [key,val]ofprops) {
328+
target[key] = val;
329+
}
330+
returntarget;
331+
};
332+
333+
var intersection = /* @__PURE__ */ _export_sfc(_sfc_main, [__FILE__]);
334+
335+
export{intersectionasdefault};
336+
"
337+
`;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp