-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Changes from 3 commits
d0f8ce2
1940914
b54cf64
42ed0a2
224cec5
daf80ce
ba8380b
81abf09
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]; | ||
}, | ||
|
@@ -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')) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 使用template字段与本身的 script,template,style 相冲突, 另外,没有看到import字段的存在有什么意义,好像不使用这个字段也可以 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'; | ||
|
@@ -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); | ||
|
@@ -372,8 +394,6 @@ export default { | |
|
||
let repeats = util.elemToArray(node.getElementsByTagName('repeat')); | ||
|
||
|
||
|
||
let forDetail = {}; | ||
template.props = {}; | ||
repeats.forEach(repeat => { | ||
|
@@ -450,7 +470,6 @@ export default { | |
}); | ||
}); | ||
|
||
|
||
let componentElements = util.elemToArray(node.getElementsByTagName('component')); | ||
let customElements = []; | ||
Object.keys(template.components).forEach((com) => { | ||
|
@@ -500,7 +519,6 @@ export default { | |
let dist = cache.getDist(); | ||
let self = this; | ||
|
||
|
||
let compiler = loader.loadCompiler(lang); | ||
|
||
if (!compiler) { | ||
|
@@ -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); | ||
|
@@ -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())); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updateBind 方法用于更新xml节点的属性,本身就是一个递归方法,在这里再遍历childNodes不合适
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我测试下,看能否在compileXML方法内遍历......
There was a problem hiding this comment.
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语句中进行的......