Skip to content

Commit

Permalink
fix(layout-compile): improves efficiency of compiled templates, and r…
Browse files Browse the repository at this point in the history
…esolves mobile-first issue
  • Loading branch information
runspired committed Mar 16, 2016
1 parent 5d8f7dd commit a4f4f7f
Showing 1 changed file with 34 additions and 6 deletions.
40 changes: 34 additions & 6 deletions lib/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,34 +2,62 @@
'use strict';
var fs = require('fs');
var EOL = require('os').EOL;
var chalk = require('chalk');
var debug = false;

module.exports = function compile(file, layouts, breakpoints) {
var inputs = [];

var ordered = orderedLayouts(layouts, breakpoints);

if (ordered.length === 1) {
inputs.push(
'{{#with (-inject-layout) as |FlexiLayout|}}',
ordered[0].data,
'{{/with}}'
);
return build(file, inputs);
}

ordered.forEach(function(layout, index) {
if (index === 0) {
inputs.push('{{#with (-inject-layout) as |FlexiLayout|}}', makeFirstTest(layout.name), layout.data);
}
if (index === ordered.length - 1) {
} else if (index === ordered.length - 1) {
inputs.push('{{else}}', layout.data, '{{/if}}', '{{/with}}');
} else {
inputs.push(makeMiddleTest(layout.name), layout.data);
}
});

return build(file, inputs);
};

function build(file, inputs) {
inputs.forEach(function(input) {
fs.appendFileSync(file, input + EOL);
});

};
if (debug) {
console.log(
chalk.yellow(
'Compiled Layout: ' + file.substr(file.indexOf('.tmp') + 5)
));
console.log(
'\n',
chalk.cyan(fs.readFileSync(file),
'\n\n'
));
}
return true;
}

function orderedLayouts(layouts, breakpoints) {
var sizes = breakpoints.sort(function(a, b) {
return a.begin > b.begin ? a : b;
return a.begin > b.begin ? -1 : 1;
});

if (debug) {
console.log('Layout Order:\n', sizes);
}

var ordered = sizes.map(function(size) {
return layouts[size.name];
});
Expand Down

0 comments on commit a4f4f7f

Please sign in to comment.