Skip to content

Commit

Permalink
增加导入模板(import template)功能
Browse files Browse the repository at this point in the history
  • Loading branch information
aben1188 committed Jan 2, 2018
1 parent 919de65 commit d0f8ce2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/wepy-cli/src/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ let _cacheChanged = false;
let _filelistCache = {};

export default {
setNoWriteFile: function setNoWriteFile(v) {
this._noWriteFileList = this._noWriteFileList || [];
this._noWriteFileList = this._noWriteFileList.concat(v);
},
getNoWriteFile: function getNoWriteFile(v) {
return this._noWriteFileList ? this._noWriteFileList.indexOf(v) : -1;
},
setParams (v) {
this._params = v;
},
Expand Down
27 changes: 26 additions & 1 deletion packages/wepy-cli/src/compile-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export default {
comCount: 0,
getPrefix (prefix) {
if (!this.comPrefix[prefix]) {
this.comPrefix[prefix] = util.camelize(prefix || '');;
this.comPrefix[prefix] = util.camelize(prefix || '');
}
return this.comPrefix[prefix];
},
Expand Down Expand Up @@ -232,6 +232,29 @@ export default {
},

updateBind (node, template, parentTemplate, prefix, ignores = {}, mapping = {}) {
//导入模板(import template):导入的模板实际为直接复制到当前文件,使用当前文件的作用域,这不同于具有独立作用域的
//WePY组件模板与小程序原生模板。这一点与JavaScript的import、less/sass/scss的import保持了一致。
//语法:<template import src="../relative/path/to/template.wpy" />
// 其中import属性也可写成import="true"
// 除了import属性与src属性,不支持其他属性
[].slice.call(node.childNodes || []).forEach(function (child) {
if (child.tagName === 'template' && child.hasAttribute('import') && child.getAttribute('src')) {
let childSrcResolved = path.resolve(template.src, '..' + path.sep + child.getAttribute('src'));
if (childSrcResolved) {
cache.setNoWriteFile(childSrcResolved);
let importAttribute = child.getAttribute('import');
if (importAttribute === 'import' || importAttribute === 'true') {
let content = util.attrReplace(util.readFile(childSrcResolved).replace(/^\s*<template[^>]*>|<\/template>\s*$/ig, ''));
node.replaceChild(cWpy.createParser().parseFromString(content), child);
//console.log('成功导入模板:' + childSrcResolved)
} else {
//console.log('模板import属性不为true,不导入模板:' + childSrcResolved)
}
} else {
//console.log('导入模板前src路径解析错误');
}
}
});

let config = cache.getConfig();
let tagprefix = config.output === 'ant' ? 'a' : 'wx';
Expand Down Expand Up @@ -543,6 +566,8 @@ export default {
util.output(p.action, p.file);
},
done (rst) {
if (cache.getNoWriteFile(opath.dir + path.sep + opath.base) > -1) return;

util.output('写入', rst.file);
rst.code = self.replaceBooleanAttr(rst.code);
util.writeFile(target, rst.code);
Expand Down

0 comments on commit d0f8ce2

Please sign in to comment.