diff --git a/packages/wepy-cli/src/compile-template.js b/packages/wepy-cli/src/compile-template.js index 13eb17b2a..8e79cc4b8 100644 --- a/packages/wepy-cli/src/compile-template.js +++ b/packages/wepy-cli/src/compile-template.js @@ -2,13 +2,12 @@ * 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'; @@ -25,13 +24,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 || ''); } return this.comPrefix[prefix]; }, - getTemplate(content) { + getTemplate (content) { content = ``; content = util.attrReplace(content); let doc = new DOMImplementation().createDocument(); @@ -44,7 +43,7 @@ export default { return doc; }, - isInQuote(str, n) { + isInQuote (str, n) { let firstIndex = str.search(/"|'/); if (firstIndex === -1 || firstIndex > n) return false; let char = ''; @@ -63,7 +62,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]; @@ -104,7 +103,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' }} @@ -136,7 +135,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); @@ -175,8 +174,7 @@ 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; @@ -203,8 +201,9 @@ export default { } }, + // 通过mapping一层层映射,反应到属性上 - getMappingIndex(mapping, arr) { + getMappingIndex (mapping, arr) { if (!arr) arr = []; @@ -219,7 +218,7 @@ export default { /** * 组件图片引用会被直接编译进页面,因此需要对组件中的相对路径进行路径修正 */ - fixRelativePath(node, template, parentTemplate) { + fixRelativePath (node, template, parentTemplate) { if (node.nodeName === 'image' && parentTemplate) { let src = node.getAttribute('src') if (src[0] === '.') { @@ -232,7 +231,7 @@ 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 @@ -300,7 +299,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 减少一层 @@ -333,7 +332,7 @@ export default { return node; }, - updateSlot(node, childNodes) { + updateSlot (node, childNodes) { let slots = {}, has; if (!childNodes || childNodes.length === 0) slots = {}; @@ -373,7 +372,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); @@ -438,6 +437,7 @@ export default { let prop = template.props[com.tagName], tmp = {}; + if (name.indexOf('v-bind') === 0) { tmp.bind = true; name = name.replace(/^v\-bind\:/, ''); @@ -508,7 +508,7 @@ export default { return node; }, - compile(template) { + compile (template) { let lang = template.type; let content = util.attrReplace(template.code); @@ -555,10 +555,10 @@ 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);