Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加导入模板(import template)功能 #722

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
33 changes: 26 additions & 7 deletions 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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updateBind 方法用于更新xml节点的属性,本身就是一个递归方法,在这里再遍历childNodes不合适

Copy link
Contributor Author

@aben1188 aben1188 Jan 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我测试下,看能否在compileXML方法内遍历......

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

经测试,不能在compileXML方法内遍历。而且,发现还只能放在updateBind方法的现有位置进行childNodes遍历,因为虽然updateBind方法本身确实存在递归调用不假,但这个递归调用是在if语句中进行的......

if (child.tagName === 'template' && child.hasAttribute('import') && child.getAttribute('src')) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用template字段与本身的 script,template,style 相冲突, 另外,没有看到import字段的存在有什么意义,好像不使用这个字段也可以

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

template标签内要是没有import属性,则不能导入,因此必须显式地设置该属性,才能导入模板,这样就可以明显地区别于wepy模板标签和小程序原生模板标签了。

至于使用template与本身的 script,template,style 相冲突,我测试中倒是没发现(我测试了在页面中导入、在组件中导入,还测试了循环导入的情形),具体情况可能你更了解了。

let childSrcResolved = path.resolve(template.src, '..' + path.sep + child.getAttribute('src'));
if (childSrcResolved) {
cache.setFileNotWritten(childSrcResolved);
let importAttr = child.getAttribute('import');
if (importAttr === 'import' || importAttr === '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路径解析错误,src路径为:', child.getAttribute('src'));
}
}
});

let config = cache.getConfig();
let tagprefix = config.output === 'ant' ? 'a' : 'wx';
Expand Down Expand Up @@ -352,7 +375,6 @@ export default {
},

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,8 +394,6 @@ export default {

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



let forDetail = {};
template.props = {};
repeats.forEach(repeat => {
Expand Down Expand Up @@ -450,7 +470,6 @@ export default {
});
});


let componentElements = util.elemToArray(node.getElementsByTagName('component'));
let customElements = [];
Object.keys(template.components).forEach((com) => {
Expand Down Expand Up @@ -500,7 +519,6 @@ export default {
let dist = cache.getDist();
let self = this;


let compiler = loader.loadCompiler(lang);

if (!compiler) {
Expand Down Expand Up @@ -543,6 +561,8 @@ export default {
util.output(p.action, p.file);
},
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 +571,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