@@ -6,6 +6,11 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau
6
6
7
7
var deindent = _interopDefault ( require ( 'de-indent' ) ) ;
8
8
var he = _interopDefault ( require ( 'he' ) ) ;
9
+ var babel = _interopDefault ( require ( 'babel-core' ) ) ;
10
+ var prettier = _interopDefault ( require ( 'prettier' ) ) ;
11
+ var t = require ( 'babel-types' ) ;
12
+ var generate = _interopDefault ( require ( 'babel-generator' ) ) ;
13
+ var template = _interopDefault ( require ( 'babel-template' ) ) ;
9
14
10
15
/* */
11
16
@@ -1870,7 +1875,7 @@ var platformGetTagNamespace;
1870
1875
* Convert HTML string to AST.
1871
1876
*/
1872
1877
function parse (
1873
- template ,
1878
+ template$$1 ,
1874
1879
options
1875
1880
) {
1876
1881
warn$1 = options . warn || baseWarn ;
@@ -1910,7 +1915,7 @@ function parse (
1910
1915
}
1911
1916
}
1912
1917
1913
- parseHTML ( template , {
1918
+ parseHTML ( template$$1 , {
1914
1919
warn : warn$1 ,
1915
1920
expectHTML : options . expectHTML ,
1916
1921
isUnaryTag : options . isUnaryTag ,
@@ -2060,7 +2065,7 @@ function parse (
2060
2065
chars : function chars ( text ) {
2061
2066
if ( ! currentParent ) {
2062
2067
if ( process . env . NODE_ENV !== 'production' ) {
2063
- if ( text === template ) {
2068
+ if ( text === template$$1 ) {
2064
2069
warnOnce (
2065
2070
'Component template requires a root element, rather than just text.'
2066
2071
) ;
@@ -3259,7 +3264,7 @@ var CodegenState = function CodegenState (options) {
3259
3264
3260
3265
3261
3266
3262
- function generate (
3267
+ function generate$1 (
3263
3268
ast ,
3264
3269
options
3265
3270
) {
@@ -3516,7 +3521,7 @@ function genInlineTemplate (el, state) {
3516
3521
state . warn ( 'Inline-template components must have exactly one child element.' ) ;
3517
3522
}
3518
3523
if ( ast . type === 1 ) {
3519
- var inlineRenderFns = generate ( ast , state . options ) ;
3524
+ var inlineRenderFns = generate$1 ( ast , state . options ) ;
3520
3525
return ( "inlineTemplate:{render:function(){" + ( inlineRenderFns . render ) + "},staticRenderFns:[" + ( inlineRenderFns . staticRenderFns . map ( function ( code ) { return ( "function(){" + code + "}" ) ; } ) . join ( ',' ) ) + "]}" )
3521
3526
}
3522
3527
}
@@ -3793,7 +3798,7 @@ function createCompileToFunctionFn (compile) {
3793
3798
var cache = Object . create ( null ) ;
3794
3799
3795
3800
return function compileToFunctions (
3796
- template ,
3801
+ template$$1 ,
3797
3802
options ,
3798
3803
vm
3799
3804
) {
@@ -3819,20 +3824,20 @@ function createCompileToFunctionFn (compile) {
3819
3824
3820
3825
// check cache
3821
3826
var key = options . delimiters
3822
- ? String ( options . delimiters ) + template
3823
- : template ;
3827
+ ? String ( options . delimiters ) + template$$1
3828
+ : template$$1 ;
3824
3829
if ( cache [ key ] ) {
3825
3830
return cache [ key ]
3826
3831
}
3827
3832
3828
3833
// compile
3829
- var compiled = compile ( template , options ) ;
3834
+ var compiled = compile ( template$$1 , options ) ;
3830
3835
3831
3836
// check compilation errors/tips
3832
3837
if ( process . env . NODE_ENV !== 'production' ) {
3833
3838
if ( compiled . errors && compiled . errors . length ) {
3834
3839
warn$2 (
3835
- "Error compiling template:\n\n" + template + "\n\n" +
3840
+ "Error compiling template:\n\n" + template$$1 + "\n\n" +
3836
3841
compiled . errors . map ( function ( e ) { return ( "- " + e ) ; } ) . join ( '\n' ) + '\n' ,
3837
3842
vm
3838
3843
) ;
@@ -3878,7 +3883,7 @@ function createCompileToFunctionFn (compile) {
3878
3883
function createCompilerCreator ( baseCompile ) {
3879
3884
return function createCompiler ( baseOptions ) {
3880
3885
function compile (
3881
- template ,
3886
+ template$$1 ,
3882
3887
options
3883
3888
) {
3884
3889
var finalOptions = Object . create ( baseOptions ) ;
@@ -3909,7 +3914,7 @@ function createCompilerCreator (baseCompile) {
3909
3914
}
3910
3915
}
3911
3916
3912
- var compiled = baseCompile ( template , finalOptions ) ;
3917
+ var compiled = baseCompile ( template$$1 , finalOptions ) ;
3913
3918
if ( process . env . NODE_ENV !== 'production' ) {
3914
3919
errors . push . apply ( errors , detectErrors ( compiled . ast ) ) ;
3915
3920
}
@@ -4201,13 +4206,13 @@ function markComponent (ast) {
4201
4206
// parser/optimizer/codegen, e.g the SSR optimizing compiler.
4202
4207
// Here we just export a default compiler using the default parts.
4203
4208
var createCompiler = createCompilerCreator ( function baseCompile (
4204
- template ,
4209
+ template$$1 ,
4205
4210
options
4206
4211
) {
4207
- var originAst = parse ( template . trim ( ) , options ) ;
4212
+ var originAst = parse ( template$$1 . trim ( ) , options ) ;
4208
4213
var ast = markComponent ( originAst ) ;
4209
4214
optimize ( ast , options ) ;
4210
- var code = generate ( ast , options ) ;
4215
+ var code = generate$1 ( ast , options ) ;
4211
4216
return {
4212
4217
ast : ast ,
4213
4218
render : code . render ,
@@ -4294,32 +4299,71 @@ var wxmlDirectiveMap = {
4294
4299
}
4295
4300
} ;
4296
4301
4297
- var utils = {
4298
- toLowerCase : function toLowerCase ( str ) {
4299
- return str . replace ( / ( [ A - Z ] ) / g, '-$1' ) . toLowerCase ( ) . trim ( )
4300
- } ,
4302
+ var tagConfig = {
4303
+ virtualTag : [ 'slot' , 'template' , 'block' ]
4304
+ } ;
4301
4305
4302
- getChar : function getChar ( index ) {
4303
- return String . fromCharCode ( 0x61 + index )
4304
- } ,
4306
+ // babel-plugin-transform-object-to-ternary-operator.js
4305
4307
4306
- log : function log ( compiled ) {
4307
- compiled . mpErrors = [ ] ;
4308
- compiled . mpTips = [ ] ;
4308
+ function getStrByNode ( node , onlyStr ) {
4309
+ if ( onlyStr === void 0 ) onlyStr = false ;
4309
4310
4310
- return function ( str , type ) {
4311
- if ( type === 'waring' ) {
4312
- compiled . mpTips . push ( str ) ;
4313
- } else {
4314
- compiled . mpErrors . push ( str ) ;
4315
- }
4316
- }
4311
+ if ( onlyStr ) {
4312
+ return node . value || node . name || ''
4313
+ }
4314
+ return node . type === 'StringLiteral' ? node : t . stringLiteral ( node . name || '' )
4315
+ }
4316
+
4317
+ // 把 { key: value } 转换成 [ value ? 'key' : '' ]
4318
+ var objectVisitor = {
4319
+ ObjectExpression : function ( path ) {
4320
+ var elements = path . node . properties . map ( function ( propertyItem ) {
4321
+ return t . conditionalExpression ( propertyItem . value , getStrByNode ( propertyItem . key ) , t . stringLiteral ( '' ) )
4322
+ } ) ;
4323
+ path . replaceWith ( t . arrayExpression ( elements ) ) ;
4317
4324
}
4318
4325
} ;
4319
4326
4320
- var tagConfig = {
4321
- virtualTag : [ 'slot' , 'template' , 'block' ]
4327
+ function transformObjectToTernaryOperator ( babel$$1 ) {
4328
+ return { visitor : objectVisitor }
4329
+ }
4330
+
4331
+ // 把 { key: value } 转换成 'key:' + value + ';'
4332
+ var objectToStringVisitor = {
4333
+ ObjectExpression : function ( path ) {
4334
+ var expression = path . node . properties . map ( function ( propertyItem ) {
4335
+ var keyStr = getStrByNode ( propertyItem . key , true ) ;
4336
+ var key = keyStr ? hyphenate ( keyStr ) : keyStr ;
4337
+ var ref = generate ( t . ExpressionStatement ( propertyItem . value ) ) ;
4338
+ var val = ref . code ;
4339
+ return ( "'" + key + ":' + " + ( val . slice ( 0 , - 1 ) ) + " + ';'" )
4340
+ } ) . join ( '+' ) ;
4341
+
4342
+ var p = template ( expression ) ( { } ) ;
4343
+ path . replaceWith ( p . expression ) ;
4344
+ }
4322
4345
} ;
4346
+ function transformObjectToString ( babel$$1 ) {
4347
+ return { visitor : objectToStringVisitor }
4348
+ }
4349
+
4350
+ function transformDynamicClass ( staticClass , clsBinding ) {
4351
+ if ( staticClass === void 0 ) staticClass = '' ;
4352
+
4353
+ var result = babel . transform ( ( "!" + clsBinding ) , { plugins : [ transformObjectToTernaryOperator ] } ) ;
4354
+ // 先实现功能,再优化代码
4355
+ // https://github.com/babel/babel/issues/7138
4356
+ var cls = prettier . format ( result . code , { semi : false , singleQuote : true } ) . slice ( 1 ) . slice ( 0 , - 1 ) ;
4357
+ return ( staticClass + " {{" + cls + "}}" )
4358
+ }
4359
+
4360
+ function transformDynamicStyle ( staticStyle , styleBinding ) {
4361
+ if ( staticStyle === void 0 ) staticStyle = '' ;
4362
+
4363
+ var result = babel . transform ( ( "!" + styleBinding ) , { plugins : [ transformObjectToString ] } ) ;
4364
+ var cls = prettier . format ( result . code , { semi : false , singleQuote : true } ) . slice ( 2 ) . slice ( 0 , - 2 ) ;
4365
+ return ( staticStyle + " {{" + cls + "}}" )
4366
+ }
4323
4367
4324
4368
var attrs = {
4325
4369
format : function format ( attrs ) {
@@ -4342,7 +4386,7 @@ var attrs = {
4342
4386
var tag = ast . tag ;
4343
4387
var staticClass = ast . staticClass ;
4344
4388
var attrs = { } ;
4345
- var wxClass = this . styleObj ( attrsMap [ 'v-bind:class' ] , staticClass ) ;
4389
+ var wxClass = this . classObj ( attrsMap [ 'v-bind:class' ] , staticClass ) ;
4346
4390
wxClass . length ? attrsMap [ 'class' ] = wxClass : '' ;
4347
4391
var wxStyle = this . styleObj ( attrsMap [ 'v-bind:style' ] , attrsMap [ 'style' ] ) ;
4348
4392
wxStyle . length ? attrsMap [ 'style' ] = wxStyle : '' ;
@@ -4393,7 +4437,7 @@ var attrs = {
4393
4437
} else if ( / ^ v \- / . test ( key ) ) {
4394
4438
log ( ( "不支持此属性-> " + key + "=\"" + val + "\"" ) , 'waring' ) ;
4395
4439
} else {
4396
- if ( ( tagConfig . virtualTag . indexOf ( tag ) > - 1 ) && ( key === 'class' || key === 'data-mpcomid' ) ) {
4440
+ if ( ( tagConfig . virtualTag . indexOf ( tag ) > - 1 ) && ( key === 'class' || key === 'style' || key === ' data-mpcomid') ) {
4397
4441
if ( key !== 'data-mpcomid' ) {
4398
4442
log ( ( "template 不支持此属性-> " + key + "=\"" + val + "\"" ) , 'waring' ) ;
4399
4443
}
@@ -4456,20 +4500,30 @@ var attrs = {
4456
4500
return attrs
4457
4501
} ,
4458
4502
4459
- styleObj : function styleObj ( styleBinding , style ) {
4503
+ classObj : function classObj ( clsBinding , staticCls ) {
4504
+ if ( clsBinding === void 0 ) clsBinding = '' ;
4505
+
4506
+ if ( ! clsBinding && ! staticCls ) {
4507
+ return ''
4508
+ }
4509
+ if ( ! clsBinding && staticCls ) {
4510
+ return staticCls
4511
+ }
4512
+
4513
+ return transformDynamicClass ( staticCls , clsBinding )
4514
+ } ,
4515
+
4516
+ styleObj : function styleObj ( styleBinding , staticStyle ) {
4460
4517
if ( styleBinding === void 0 ) styleBinding = '' ;
4461
4518
4462
- var _styleBinding = styleBinding . replace ( / [ \{ \} ] / g, '' ) . replace ( ' ' , '' ) . replace ( / \n / g, '' ) ;
4463
- _styleBinding = _styleBinding . split ( ',' ) . map ( function ( v ) {
4464
- return v . replace ( / ( [ ^ , ^ : ] + ) \: ( [ ^ , ] + ) / g, function ( v , $1 , $2 ) {
4465
- var _$1 = utils . toLowerCase ( $1 ) . replace ( / ^ ' | ' $ / g, '' ) ;
4466
- return ( "{{(" + $2 + ")? '" + _$1 + "' : ' '}}" )
4467
- } )
4468
- } ) . join ( ' ' ) ;
4469
- if ( _styleBinding . indexOf ( '{' ) === - 1 && _styleBinding ) {
4470
- _styleBinding = "{{" + _styleBinding + "}}" ;
4519
+ if ( ! styleBinding && ! staticStyle ) {
4520
+ return ''
4471
4521
}
4472
- return ( ( style || '' ) + " " + _styleBinding ) . trim ( )
4522
+ if ( ! styleBinding && staticStyle ) {
4523
+ return staticStyle
4524
+ }
4525
+
4526
+ return transformDynamicStyle ( staticStyle , styleBinding )
4473
4527
} ,
4474
4528
4475
4529
model : function model ( key , val , attrs , tag ) {
@@ -4727,7 +4781,7 @@ function wxmlAst (compiled, options, log) {
4727
4781
}
4728
4782
}
4729
4783
4730
- function generate$1 ( obj , options ) {
4784
+ function generate$2 ( obj , options ) {
4731
4785
if ( options === void 0 ) options = { } ;
4732
4786
4733
4787
var tag = obj . tag ;
@@ -4739,15 +4793,15 @@ function generate$1 (obj, options) {
4739
4793
var child = '' ;
4740
4794
if ( children && children . length ) {
4741
4795
// 递归子节点
4742
- child = children . map ( function ( v ) { return generate$1 ( v , options ) ; } ) . join ( '' ) ;
4796
+ child = children . map ( function ( v ) { return generate$2 ( v , options ) ; } ) . join ( '' ) ;
4743
4797
}
4744
4798
4745
4799
// v-if 指令
4746
4800
var ifConditionsArr = [ ] ;
4747
4801
if ( ifConditions ) {
4748
4802
var length = ifConditions . length ;
4749
4803
for ( var i = 1 ; i < length ; i ++ ) {
4750
- ifConditionsArr . push ( generate$1 ( ifConditions [ i ] . block , options ) ) ;
4804
+ ifConditionsArr . push ( generate$2 ( ifConditions [ i ] . block , options ) ) ;
4751
4805
}
4752
4806
}
4753
4807
@@ -4764,6 +4818,29 @@ function convertAttr (key, val) {
4764
4818
return ( val === '' || typeof val === 'undefined' ) ? key : ( key + "=\"" + val + "\"" )
4765
4819
}
4766
4820
4821
+ var utils = {
4822
+ toLowerCase : function toLowerCase ( str ) {
4823
+ return str . replace ( / ( [ A - Z ] ) / g, '-$1' ) . toLowerCase ( ) . trim ( )
4824
+ } ,
4825
+
4826
+ getChar : function getChar ( index ) {
4827
+ return String . fromCharCode ( 0x61 + index )
4828
+ } ,
4829
+
4830
+ log : function log ( compiled ) {
4831
+ compiled . mpErrors = [ ] ;
4832
+ compiled . mpTips = [ ] ;
4833
+
4834
+ return function ( str , type ) {
4835
+ if ( type === 'waring' ) {
4836
+ compiled . mpTips . push ( str ) ;
4837
+ } else {
4838
+ compiled . mpErrors . push ( str ) ;
4839
+ }
4840
+ }
4841
+ }
4842
+ } ;
4843
+
4767
4844
function compileToWxml ( compiled , options ) {
4768
4845
if ( options === void 0 ) options = { } ;
4769
4846
@@ -4775,7 +4852,7 @@ function compileToWxml (compiled, options) {
4775
4852
var wxast = ref . wxast ;
4776
4853
var deps = ref . deps ; if ( deps === void 0 ) deps = { } ;
4777
4854
var slots = ref . slots ; if ( slots === void 0 ) slots = { } ;
4778
- var code = generate$1 ( wxast , options ) ;
4855
+ var code = generate$2 ( wxast , options ) ;
4779
4856
4780
4857
// 引用子模版
4781
4858
var importCode = Object . keys ( deps ) . map ( function ( k ) { return components [ k ] ? ( "<import src=\"" + ( components [ k ] . src ) + "\" />" ) : '' ; } ) . join ( '' ) ;
@@ -4784,7 +4861,7 @@ function compileToWxml (compiled, options) {
4784
4861
// 生成 slots code
4785
4862
Object . keys ( slots ) . forEach ( function ( k ) {
4786
4863
var slot = slots [ k ] ;
4787
- slot . code = generate$1 ( slot . node , options ) ;
4864
+ slot . code = generate$2 ( slot . node , options ) ;
4788
4865
} ) ;
4789
4866
4790
4867
return { code : code , compiled : compiled , slots : slots }
0 commit comments