Skip to content

Commit 86dc4b8

Browse files
authored
Merge pull request Meituan-Dianping#82 from Meituan-Dianping/develop
new version 1.0.3
2 parents 28850bc + f0db3d0 commit 86dc4b8

File tree

9 files changed

+306
-78
lines changed

9 files changed

+306
-78
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@
116116
"nightwatch": "^0.9.16",
117117
"nightwatch-helpers": "^1.2.0",
118118
"phantomjs-prebuilt": "^2.1.14",
119+
"prettier": "^1.11.1",
119120
"resolve": "^1.3.3",
120121
"rollup": "^0.45.1",
121122
"rollup-plugin-alias": "^1.3.1",

packages/mpvue-template-compiler/build.js

Lines changed: 129 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'defau
66

77
var deindent = _interopDefault(require('de-indent'));
88
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'));
914

1015
/* */
1116

@@ -1870,7 +1875,7 @@ var platformGetTagNamespace;
18701875
* Convert HTML string to AST.
18711876
*/
18721877
function parse (
1873-
template,
1878+
template$$1,
18741879
options
18751880
) {
18761881
warn$1 = options.warn || baseWarn;
@@ -1910,7 +1915,7 @@ function parse (
19101915
}
19111916
}
19121917

1913-
parseHTML(template, {
1918+
parseHTML(template$$1, {
19141919
warn: warn$1,
19151920
expectHTML: options.expectHTML,
19161921
isUnaryTag: options.isUnaryTag,
@@ -2060,7 +2065,7 @@ function parse (
20602065
chars: function chars (text) {
20612066
if (!currentParent) {
20622067
if (process.env.NODE_ENV !== 'production') {
2063-
if (text === template) {
2068+
if (text === template$$1) {
20642069
warnOnce(
20652070
'Component template requires a root element, rather than just text.'
20662071
);
@@ -3259,7 +3264,7 @@ var CodegenState = function CodegenState (options) {
32593264

32603265

32613266

3262-
function generate (
3267+
function generate$1 (
32633268
ast,
32643269
options
32653270
) {
@@ -3516,7 +3521,7 @@ function genInlineTemplate (el, state) {
35163521
state.warn('Inline-template components must have exactly one child element.');
35173522
}
35183523
if (ast.type === 1) {
3519-
var inlineRenderFns = generate(ast, state.options);
3524+
var inlineRenderFns = generate$1(ast, state.options);
35203525
return ("inlineTemplate:{render:function(){" + (inlineRenderFns.render) + "},staticRenderFns:[" + (inlineRenderFns.staticRenderFns.map(function (code) { return ("function(){" + code + "}"); }).join(',')) + "]}")
35213526
}
35223527
}
@@ -3793,7 +3798,7 @@ function createCompileToFunctionFn (compile) {
37933798
var cache = Object.create(null);
37943799

37953800
return function compileToFunctions (
3796-
template,
3801+
template$$1,
37973802
options,
37983803
vm
37993804
) {
@@ -3819,20 +3824,20 @@ function createCompileToFunctionFn (compile) {
38193824

38203825
// check cache
38213826
var key = options.delimiters
3822-
? String(options.delimiters) + template
3823-
: template;
3827+
? String(options.delimiters) + template$$1
3828+
: template$$1;
38243829
if (cache[key]) {
38253830
return cache[key]
38263831
}
38273832

38283833
// compile
3829-
var compiled = compile(template, options);
3834+
var compiled = compile(template$$1, options);
38303835

38313836
// check compilation errors/tips
38323837
if (process.env.NODE_ENV !== 'production') {
38333838
if (compiled.errors && compiled.errors.length) {
38343839
warn$2(
3835-
"Error compiling template:\n\n" + template + "\n\n" +
3840+
"Error compiling template:\n\n" + template$$1 + "\n\n" +
38363841
compiled.errors.map(function (e) { return ("- " + e); }).join('\n') + '\n',
38373842
vm
38383843
);
@@ -3878,7 +3883,7 @@ function createCompileToFunctionFn (compile) {
38783883
function createCompilerCreator (baseCompile) {
38793884
return function createCompiler (baseOptions) {
38803885
function compile (
3881-
template,
3886+
template$$1,
38823887
options
38833888
) {
38843889
var finalOptions = Object.create(baseOptions);
@@ -3909,7 +3914,7 @@ function createCompilerCreator (baseCompile) {
39093914
}
39103915
}
39113916

3912-
var compiled = baseCompile(template, finalOptions);
3917+
var compiled = baseCompile(template$$1, finalOptions);
39133918
if (process.env.NODE_ENV !== 'production') {
39143919
errors.push.apply(errors, detectErrors(compiled.ast));
39153920
}
@@ -4201,13 +4206,13 @@ function markComponent (ast) {
42014206
// parser/optimizer/codegen, e.g the SSR optimizing compiler.
42024207
// Here we just export a default compiler using the default parts.
42034208
var createCompiler = createCompilerCreator(function baseCompile (
4204-
template,
4209+
template$$1,
42054210
options
42064211
) {
4207-
var originAst = parse(template.trim(), options);
4212+
var originAst = parse(template$$1.trim(), options);
42084213
var ast = markComponent(originAst);
42094214
optimize(ast, options);
4210-
var code = generate(ast, options);
4215+
var code = generate$1(ast, options);
42114216
return {
42124217
ast: ast,
42134218
render: code.render,
@@ -4294,32 +4299,71 @@ var wxmlDirectiveMap = {
42944299
}
42954300
};
42964301

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+
};
43014305

4302-
getChar: function getChar (index) {
4303-
return String.fromCharCode(0x61 + index)
4304-
},
4306+
// babel-plugin-transform-object-to-ternary-operator.js
43054307

4306-
log: function log (compiled) {
4307-
compiled.mpErrors = [];
4308-
compiled.mpTips = [];
4308+
function getStrByNode (node, onlyStr) {
4309+
if ( onlyStr === void 0 ) onlyStr = false;
43094310

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));
43174324
}
43184325
};
43194326

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+
}
43224345
};
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+
}
43234367

43244368
var attrs = {
43254369
format: function format (attrs) {
@@ -4342,7 +4386,7 @@ var attrs = {
43424386
var tag = ast.tag;
43434387
var staticClass = ast.staticClass;
43444388
var attrs = {};
4345-
var wxClass = this.styleObj(attrsMap['v-bind:class'], staticClass);
4389+
var wxClass = this.classObj(attrsMap['v-bind:class'], staticClass);
43464390
wxClass.length ? attrsMap['class'] = wxClass : '';
43474391
var wxStyle = this.styleObj(attrsMap['v-bind:style'], attrsMap['style']);
43484392
wxStyle.length ? attrsMap['style'] = wxStyle : '';
@@ -4393,7 +4437,7 @@ var attrs = {
43934437
} else if (/^v\-/.test(key)) {
43944438
log(("不支持此属性-> " + key + "=\"" + val + "\""), 'waring');
43954439
} 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')) {
43974441
if (key !== 'data-mpcomid') {
43984442
log(("template 不支持此属性-> " + key + "=\"" + val + "\""), 'waring');
43994443
}
@@ -4456,20 +4500,30 @@ var attrs = {
44564500
return attrs
44574501
},
44584502

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) {
44604517
if ( styleBinding === void 0 ) styleBinding = '';
44614518

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 ''
44714521
}
4472-
return ((style || '') + " " + _styleBinding).trim()
4522+
if (!styleBinding && staticStyle) {
4523+
return staticStyle
4524+
}
4525+
4526+
return transformDynamicStyle(staticStyle, styleBinding)
44734527
},
44744528

44754529
model: function model (key, val, attrs, tag) {
@@ -4727,7 +4781,7 @@ function wxmlAst (compiled, options, log) {
47274781
}
47284782
}
47294783

4730-
function generate$1 (obj, options) {
4784+
function generate$2 (obj, options) {
47314785
if ( options === void 0 ) options = {};
47324786

47334787
var tag = obj.tag;
@@ -4739,15 +4793,15 @@ function generate$1 (obj, options) {
47394793
var child = '';
47404794
if (children && children.length) {
47414795
// 递归子节点
4742-
child = children.map(function (v) { return generate$1(v, options); }).join('');
4796+
child = children.map(function (v) { return generate$2(v, options); }).join('');
47434797
}
47444798

47454799
// v-if 指令
47464800
var ifConditionsArr = [];
47474801
if (ifConditions) {
47484802
var length = ifConditions.length;
47494803
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));
47514805
}
47524806
}
47534807

@@ -4764,6 +4818,29 @@ function convertAttr (key, val) {
47644818
return (val === '' || typeof val === 'undefined') ? key : (key + "=\"" + val + "\"")
47654819
}
47664820

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+
47674844
function compileToWxml (compiled, options) {
47684845
if ( options === void 0 ) options = {};
47694846

@@ -4775,7 +4852,7 @@ function compileToWxml (compiled, options) {
47754852
var wxast = ref.wxast;
47764853
var deps = ref.deps; if ( deps === void 0 ) deps = {};
47774854
var slots = ref.slots; if ( slots === void 0 ) slots = {};
4778-
var code = generate$1(wxast, options);
4855+
var code = generate$2(wxast, options);
47794856

47804857
// 引用子模版
47814858
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) {
47844861
// 生成 slots code
47854862
Object.keys(slots).forEach(function (k) {
47864863
var slot = slots[k];
4787-
slot.code = generate$1(slot.node, options);
4864+
slot.code = generate$2(slot.node, options);
47884865
});
47894866

47904867
return { code: code, compiled: compiled, slots: slots }

packages/mpvue-template-compiler/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mpvue-template-compiler",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "mpvue template compiler for Vue",
55
"main": "index.js",
66
"repository": {

0 commit comments

Comments
 (0)