Skip to content

Commit

Permalink
Merge branch 'wxs'
Browse files Browse the repository at this point in the history
  • Loading branch information
Gcaufy committed Jan 7, 2018
2 parents 8fe6d6a + 7ad8b76 commit 678c4ef
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 8 deletions.
9 changes: 8 additions & 1 deletion packages/wepy-cli/src/compile-template.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ export default {
* 组件图片引用会被直接编译进页面,因此需要对组件中的相对路径进行路径修正
*/
fixRelativePath (node, template, parentTemplate) {
if (node.nodeName === 'image' && parentTemplate) {
if ((node.nodeName === 'wxs' || node.nodeName === 'image') && parentTemplate) {
let src = node.getAttribute('src')
if (src[0] === '.') {
let realpath = path.join(path.parse(template.src).dir, node.getAttribute('src'));
Expand All @@ -238,6 +238,13 @@ export default {

node = this.fixRelativePath(node, template, parentTemplate);

// If it's a wxs module, then do not parse it
if (template.wxs) {
for (let k in template.wxs) {
ignores[k] = true;
}
}

if (node.nodeName === '#text' && prefix) {
if (node.data && node.data.indexOf('{{') > -1) {
node.replaceData(0, node.data.length, this.parseExp(node.data, prefix, ignores, mapping));
Expand Down
47 changes: 40 additions & 7 deletions packages/wepy-cli/src/compile-wpy.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ export default {
filepath = path.resolve(filepath); // to fixed windows path bug
let content = util.readFile(filepath);


const moduleId = util.genId(filepath);

let rst = {
Expand Down Expand Up @@ -253,14 +252,19 @@ export default {
// get imports
(() => {
let coms = {};
rst.script.code.replace(/import\s*([\w\-\_]*)\s*from\s*['"]([\w\-\_\.\/\@]*)['"]/ig, (match, com, path) => {
coms[com] = path;
rst.script.code.replace(/import\s*([\w\-\_]*)\s*from\s*['"]([\w\-\_\.\/\@]*)['"];*/ig, (match, com, lib, pos) => {
coms[com] = {
pos: pos,
lib: lib,
code: match
};
});

let match = rst.script.code.match(/[\s\r\n]components\s*=[\s\r\n]*/);
match = match ? match[0] : undefined;
let components = match ? this.grabConfigFromScript(rst.script.code, rst.script.code.indexOf(match) + match.length) : false;
let vars = Object.keys(coms).map((com, i) => `var ${com} = "${coms[com]}";`).join('\r\n');
let vars = Object.keys(coms).map((com, i) => `var ${com} = "${coms[com].lib}";`).join('\r\n');

let comMatch = rst.script.code.match(/[\s\r\n]components\s*=[\s\r\n]*/);
comMatch = comMatch ? comMatch[0] : undefined;
let components = comMatch ? this.grabConfigFromScript(rst.script.code, rst.script.code.indexOf(comMatch) + comMatch.length) : false;
try {
if (components) {
rst.template.components = new Function(`${vars}\r\nreturn ${components}`)();
Expand All @@ -271,6 +275,35 @@ export default {
util.output('错误', path.join(opath.dir, opath.base));
util.error(`解析components出错,报错信息:${e}\r\n${vars}\r\nreturn ${components}`);
}

let wxsMatch = rst.script.code.match(/[\s\r\n]wxs\s*=[\s\r\n]*/);
wxsMatch = wxsMatch ? wxsMatch[0] : undefined;
let wxs = wxsMatch ? this.grabConfigFromScript(rst.script.code, rst.script.code.indexOf(wxsMatch) + wxsMatch.length) : false;

try {
if (wxs) {
rst.template.wxs = new Function(`${vars}\r\nreturn ${wxs}`)();
rst.script.code = rst.script.code.replace(wxs, '/* ' + wxs + ' */');
} else {
rst.template.wxs = false;
}
} catch (e) {
util.output('错误', path.join(opath.dir, opath.base));
util.error(`解析wxs出错,报错信息:${e}\r\n${vars}\r\nreturn ${wxs}`);
}
wxs = rst.template.wxs;

// if wxs is used, then get rid of the import and wxs
if (wxs) {
let wxsCode = '';
for (let k in wxs) {
rst.script.code = rst.script.code.replace(coms[k].code, '/* ' + coms[k].code + ' */');
wxsCode += `<wxs src="${wxs[k]}" module="${k}"/>\r\n`;
}
rst.script.code = rst.script.code.replace(wxsMatch, '/* ' + wxsMatch + ' */');
rst.template.code = wxsCode + rst.template.code;
}

})();


Expand Down

0 comments on commit 678c4ef

Please sign in to comment.