Skip to content

Commit

Permalink
Merge 42ed0a2 into 2e40c85
Browse files Browse the repository at this point in the history
  • Loading branch information
aben1188 committed Jan 3, 2018
2 parents 2e40c85 + 42ed0a2 commit 4db0bb5
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 34 deletions.
6 changes: 6 additions & 0 deletions packages/wepy-cli/src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ let _cacheChanged = false;
let _filelistCache = {};

export default {
setFileNotWritten: function setFileNotWritten(v) {
this._filesNotWritten = (this._filesNotWritten || []).concat(v);
},
getFileNotWritten: function getFileNotWritten(v) {
return this._filesNotWritten ? this._filesNotWritten.indexOf(v) : -1;
},
setParams (v) {
this._params = v;
},
Expand Down
85 changes: 51 additions & 34 deletions packages/wepy-cli/src/compile-template.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/**
* Tencent is pleased to support the open source community by making WePY available.
* Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You
* may obtain a copy of the License at http://opensource.org/licenses/MIT Unless required by applicable law or agreed
* to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/


import {DOMParser, DOMImplementation} from 'xmldom';
import path from 'path';
import util from './util';
Expand All @@ -24,13 +25,13 @@ const BOOLEAN_ATTRS = ['a:else', 'wx:else', 'show-info', 'active', 'controls', '
export default {
comPrefix: {},
comCount: 0,
getPrefix (prefix) {
getPrefix(prefix) {
if (!this.comPrefix[prefix]) {
this.comPrefix[prefix] = util.camelize(prefix || '');;
this.comPrefix[prefix] = util.camelize(prefix || '');
}
return this.comPrefix[prefix];
},
getTemplate (content) {
getTemplate(content) {
content = `<template>${content}</template>`;
content = util.attrReplace(content);
let doc = new DOMImplementation().createDocument();
Expand All @@ -43,7 +44,7 @@ export default {
return doc;
},

isInQuote (str, n) {
isInQuote(str, n) {
let firstIndex = str.search(/"|'/);
if (firstIndex === -1 || firstIndex > n) return false;
let char = '';
Expand All @@ -62,7 +63,7 @@ export default {
return char !== '';
},

getFunctionInfo (str) {
getFunctionInfo(str) {
let rst = {name: '', params: []}, char = '', tmp = '', stack = [];
for (let i = 0, len = str.length; i < len; i++) {
char = str[i];
Expand Down Expand Up @@ -103,7 +104,7 @@ export default {
return code;
},

parseExp (content, prefix, ignores, mapping) {
parseExp(content, prefix, ignores, mapping) {
if (!prefix)
return content;
// replace {{ param ? 'abc' : 'efg' }} => {{ $prefix_param ? 'abc' : 'efg' }}
Expand Down Expand Up @@ -135,7 +136,7 @@ export default {
* Get :class expression
* e.g. getClassExp('{"abc": num < 1}');
*/
parseClassExp (exp) {
parseClassExp(exp) {
exp = exp.replace(/^\s/ig, '').replace(/\s$/ig, '');
if (exp[0] === '{' && exp[exp.length - 1] === '}') {
exp = exp.substring(1, exp.length - 1);
Expand Down Expand Up @@ -174,20 +175,21 @@ export default {
continue;
}
if (exp[i] === ',' && flag === 'expression' && flagStack.length === 0) {
result[classNames[classNames.length - 1]] = str.replace(/^\s/ig, '').replace(/\s$/ig, '');;
result[classNames[classNames.length - 1]] = str.replace(/^\s/ig, '').replace(/\s$/ig, '');
;
str = '';
flag = 'start';
continue;
}
// get rid of the begining space
if (!str.length && exp[i] === ' ')
continue;

// not started with '', like {abc: num < 1}
if (flag === 'start') {
flag = 'class';
}

if (flag === 'class' || flag === 'expression') {
str += exp[i];
}
Expand All @@ -201,9 +203,8 @@ export default {
}
},


// 通过mapping一层层映射,反应到属性上
getMappingIndex (mapping, arr) {
getMappingIndex(mapping, arr) {
if (!arr)
arr = [];

Expand All @@ -218,7 +219,7 @@ export default {
/**
* 组件图片引用会被直接编译进页面,因此需要对组件中的相对路径进行路径修正
*/
fixRelativePath (node, template, parentTemplate) {
fixRelativePath(node, template, parentTemplate) {
if (node.nodeName === 'image' && parentTemplate) {
let src = node.getAttribute('src')
if (src[0] === '.') {
Expand All @@ -231,7 +232,28 @@ export default {
return node;
},

updateBind (node, template, parentTemplate, prefix, ignores = {}, mapping = {}) {
updateBind(node, template, parentTemplate, prefix, ignores = {}, mapping = {}) {
//include模板:include进来的模板实际为直接复制合并到当前文件,使用当前文件的作用域,这不同于具有独立作用域的WePY组件模板
//与小程序原生import模板。这与小程序原生include模板功能类似,但也有明显区别:小程序原生include只是“相当于”复制合并到当前文件,
//而这里的include是实际会复制合并到当前文件,这样做的目的在于使得复制合并过来的wepy模板能够参与当前文件作用域,这对于在wepy
//组件模板部分中include模板很有用。
//语法:<include src="../relative/path/to/template.wpy" />
//注意:1)与小程序原生include的区别在于所导入的文件后缀为.wpy,因此这里的文件后缀名不能省略,否则在wepy编译时将不会被复制合并;
// 2)template.wpy模板文件顶层只能含有template标签,不能含有script标签和style标签,否则可能会引发不可知的异常;
// 3)template.wpy模板文件中不能再嵌套wepy组件,否则可能会引发不可知的异常。
[].slice.call(node.childNodes || []).forEach(function (child) {
if (child.tagName === 'include' && /\.wpy$/.test(child.getAttribute('src'))) {
let childSrcResolved = path.resolve(template.src, '..' + path.sep + child.getAttribute('src'));
if (childSrcResolved) {
cache.setFileNotWritten(childSrcResolved);
let content = util.attrReplace(util.readFile(childSrcResolved).replace(/^\s*<template[^>]*>|<\/template>\s*$/ig, ''));
node.replaceChild(cWpy.createParser().parseFromString(content), child);
//console.log('成功include模板:' + childSrcResolved)
} else {
//console.log('include模板前src路径解析错误,src路径为:', child.getAttribute('src'));
}
}
});

let config = cache.getConfig();
let tagprefix = config.output === 'ant' ? 'a' : 'wx';
Expand Down Expand Up @@ -278,7 +300,7 @@ export default {
if (
(config.output !== 'ant' && (attr.name.indexOf('bind') === 0 || attr.name.indexOf('catch') === 0)) ||
(config.output === 'ant' && (attr.name.indexOf('on') === 0 || attr.name.indexOf('catch') === 0))
) {
) {
// added index for all events;
if (mapping.items && mapping.items.length > 0) {
// prefix 减少一层
Expand Down Expand Up @@ -311,7 +333,7 @@ export default {
return node;
},

updateSlot (node, childNodes) {
updateSlot(node, childNodes) {
let slots = {}, has;
if (!childNodes || childNodes.length === 0)
slots = {};
Expand Down Expand Up @@ -351,8 +373,7 @@ export default {
return node;
},

compileXML (node, template, parentTemplate, prefix, childNodes, comAppendAttribute = {}, propsMapping = {}) {

compileXML(node, template, parentTemplate, prefix, childNodes, comAppendAttribute = {}, propsMapping = {}) {
let config = cache.getConfig();
let tagprefix = config.output === 'ant' ? 'a' : 'wx';
this.updateSlot(node, childNodes);
Expand All @@ -372,16 +393,14 @@ export default {

let repeats = util.elemToArray(node.getElementsByTagName('repeat'));



let forDetail = {};
template.props = {};
repeats.forEach(repeat => {
let repeatComs = [];
// <repeat for="xxx" index="idx" item="xxx" key="xxx"></repeat>
// To
// <block wx:for="xxx" wx:for-index="xxx" wx:for-item="xxx" wx:key="xxxx"></block>
repeat.tagName = 'block';
repeat.tagName = 'block';
let val = repeat.getAttribute('for');
if (val) {
repeat.setAttribute(tagprefix + ':for', val);
Expand Down Expand Up @@ -419,7 +438,6 @@ export default {

let prop = template.props[com.tagName], tmp = {};


if (name.indexOf('v-bind') === 0) {
tmp.bind = true;
name = name.replace(/^v\-bind\:/, '');
Expand Down Expand Up @@ -450,7 +468,6 @@ export default {
});
});


let componentElements = util.elemToArray(node.getElementsByTagName('component'));
let customElements = [];
Object.keys(template.components).forEach((com) => {
Expand Down Expand Up @@ -484,14 +501,14 @@ export default {
} else {
let comWpy = cWpy.resolveWpy(src);
let newnode = this.compileXML(this.getTemplate(comWpy.template.code), comWpy.template, template, this.getPrefix(prefix ? `${prefix}$${comid}` : `${comid}`), com.childNodes, comAttributes);

node.replaceChild(newnode, com);
}
});
return node;
},

compile (template) {
compile(template) {
let lang = template.type;
let content = util.attrReplace(template.code);

Expand All @@ -500,7 +517,6 @@ export default {
let dist = cache.getDist();
let self = this;


let compiler = loader.loadCompiler(lang);

if (!compiler) {
Expand Down Expand Up @@ -539,10 +555,12 @@ export default {
type: 'wxml',
code: util.decode(node.toString()),
file: target,
output (p) {
output(p) {
util.output(p.action, p.file);
},
done (rst) {
done(rst) {
if (cache.getFileNotWritten(opath.dir + path.sep + opath.base) > -1) return;

util.output('写入', rst.file);
rst.code = self.replaceBooleanAttr(rst.code);
util.writeFile(target, rst.code);
Expand All @@ -551,7 +569,6 @@ export default {
}).catch((e) => {
console.log(e);
});

//util.log('WXML: ' + path.relative(process.cwd(), target), '写入');
//util.writeFile(target, util.decode(node.toString()));
}
Expand Down

0 comments on commit 4db0bb5

Please sign in to comment.