Skip to content

Commit

Permalink
增加导入模板(import template)功能
Browse files Browse the repository at this point in the history
  • Loading branch information
aben1188 committed Jan 3, 2018
1 parent 42ed0a2 commit 224cec5
Showing 1 changed file with 22 additions and 22 deletions.
44 changes: 22 additions & 22 deletions packages/wepy-cli/src/compile-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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 = `<template>${content}</template>`;
content = util.attrReplace(content);
let doc = new DOMImplementation().createDocument();
Expand All @@ -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 = '';
Expand All @@ -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];
Expand Down Expand Up @@ -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' }}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand All @@ -203,8 +201,9 @@ export default {
}
},


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

Expand All @@ -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] === '.') {
Expand All @@ -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
Expand Down Expand Up @@ -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 减少一层
Expand Down Expand Up @@ -333,7 +332,7 @@ export default {
return node;
},

updateSlot(node, childNodes) {
updateSlot (node, childNodes) {
let slots = {}, has;
if (!childNodes || childNodes.length === 0)
slots = {};
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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\:/, '');
Expand Down Expand Up @@ -508,7 +508,7 @@ export default {
return node;
},

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

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 224cec5

Please sign in to comment.