Skip to content

Commit

Permalink
fix: make compiler output cleaner (#144)
Browse files Browse the repository at this point in the history
- only concat when needed
- no inefficient checks for dynamic nodes, template compiler returns dynamic state
  • Loading branch information
kbrsh committed Sep 13, 2017
1 parent 6637090 commit dba4241
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 19 deletions.
39 changes: 30 additions & 9 deletions dist/moon.js
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,9 @@


/* ======= Compiler ======= */
// Concatenation Symbol
var concatenationSymbol = " + ";

// Opening delimiter
var openRE = /\{\{\s*/;

Expand Down Expand Up @@ -871,6 +874,7 @@
var compileTemplate = function(template, exclude, dependencies) {
var length = template.length;
var current = 0;
var dynamic = false;
var output = '';

while(current < length) {
Expand All @@ -879,12 +883,21 @@
var textMatch = textTail.match(openRE);

if(textMatch === null) {
output += textTail;
output += "\"" + textTail + "\"";
break;
} else {
var textIndex = textMatch.index;
output += textTail.substring(0, textIndex);
current += textIndex;
if(textIndex !== 0) {
output += "\"" + (textTail.substring(0, textIndex)) + "\"";
current += textIndex;
}

dynamic = true;
}

// Concatenate if not at the start
if(current !== 0) {
output += concatenationSymbol;
}

// Exit opening delimiter
Expand All @@ -900,12 +913,20 @@
var expressionIndex = expressionMatch.index;
var expression = expressionTail.substring(0, expressionIndex);
compileTemplateExpression(expression, exclude, dependencies);
output += "\" + (" + expression + ") + \"";
output += "(" + expression + ")";
current += expression.length + expressionMatch[0].length;

// Concatenate if not at end
if(current !== length) {
output += concatenationSymbol;
}
}
}

return output;
return {
output: output,
dynamic: dynamic
};
}

var lex = function(template) {
Expand Down Expand Up @@ -1155,12 +1176,12 @@
var value = prop$1.value;
var compiled = compileTemplate(value, state.exclude, state.dependencies);

if(value !== compiled) {
if(compiled.dynamic === true) {
dynamic = true;
}

hasAttrs = true;
propsCode += "\"" + propKey + "\": \"" + compiled + "\", ";
propsCode += "\"" + propKey + "\": " + (compiled.output) + ", ";
}
}

Expand Down Expand Up @@ -1250,15 +1271,15 @@
var meta = {};

if(state.static === false) {
if(node !== compiled) {
if(compiled.dynamic === true) {
meta.dynamic = 1;
parent.meta.dynamic = 1;
} else if(state.dynamic === true) {
meta.dynamic = 1;
}
}

return ("m(\"#text\", " + (generateMeta(meta)) + "\"" + compiled + "\")");
return ("m(\"#text\", " + (generateMeta(meta)) + (compiled.output) + ")");
} else if(node.type === "m-insert") {
if(state.static === false) {
parent.meta.dynamic = 1;
Expand Down
2 changes: 1 addition & 1 deletion dist/moon.min.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/compiler/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Concatenation Symbol
const concatenationSymbol = " + ";

// Opening delimiter
const openRE = /\{\{\s*/;

Expand Down
8 changes: 4 additions & 4 deletions src/compiler/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ const generateProps = function(node, parent, specialDirectivesAfter, state) {
const value = prop.value;
const compiled = compileTemplate(value, state.exclude, state.dependencies);

if(value !== compiled) {
if(compiled.dynamic === true) {
dynamic = true;
}

hasAttrs = true;
propsCode += `"${propKey}": "${compiled}", `;
propsCode += `"${propKey}": ${compiled.output}, `;
}
}

Expand Down Expand Up @@ -157,15 +157,15 @@ const generateNode = function(node, parent, index, state) {
let meta = {};

if(state.static === false) {
if(node !== compiled) {
if(compiled.dynamic === true) {
meta.dynamic = 1;
parent.meta.dynamic = 1;
} else if(state.dynamic === true) {
meta.dynamic = 1;
}
}

return `m("#text", ${generateMeta(meta)}"${compiled}")`;
return `m("#text", ${generateMeta(meta)}${compiled.output})`;
} else if(node.type === "m-insert") {
if(state.static === false) {
parent.meta.dynamic = 1;
Expand Down
28 changes: 23 additions & 5 deletions src/compiler/template.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const compileTemplateExpression = function(expression, exclude, dependencies) {
const compileTemplate = function(template, exclude, dependencies) {
const length = template.length;
let current = 0;
let dynamic = false;
let output = '';

while(current < length) {
Expand All @@ -33,12 +34,21 @@ const compileTemplate = function(template, exclude, dependencies) {
const textMatch = textTail.match(openRE);

if(textMatch === null) {
output += textTail;
output += `"${textTail}"`;
break;
} else {
const textIndex = textMatch.index;
output += textTail.substring(0, textIndex);
current += textIndex;
if(textIndex !== 0) {
output += `"${textTail.substring(0, textIndex)}"`;
current += textIndex;
}

dynamic = true;
}

// Concatenate if not at the start
if(current !== 0) {
output += concatenationSymbol;
}

// Exit opening delimiter
Expand All @@ -54,10 +64,18 @@ const compileTemplate = function(template, exclude, dependencies) {
const expressionIndex = expressionMatch.index;
const expression = expressionTail.substring(0, expressionIndex);
compileTemplateExpression(expression, exclude, dependencies);
output += `" + (${expression}) + "`;
output += `(${expression})`;
current += expression.length + expressionMatch[0].length;

// Concatenate if not at end
if(current !== length) {
output += concatenationSymbol;
}
}
}

return output;
return {
output: output,
dynamic: dynamic
};
}

0 comments on commit dba4241

Please sign in to comment.