client/build: don't keep templates in DOM

This commit is contained in:
rr- 2016-04-13 18:58:34 +02:00
parent 4df05fe3ec
commit 5796b07908
2 changed files with 19 additions and 15 deletions

View file

@ -58,28 +58,33 @@ function getConfig() {
function bundleHtml(config) { function bundleHtml(config) {
const minify = require('html-minifier').minify; const minify = require('html-minifier').minify;
const baseHtml = fs.readFileSync('./html/index.htm', 'utf-8'); const baseHtml = fs.readFileSync('./html/index.htm', 'utf-8');
const minifyOptions = {
removeComments: true,
collapseWhitespace: true,
conservativeCollapse: true,
};
glob('./html/**/*.hbs', {}, (er, files) => { glob('./html/**/*.hbs', {}, (er, files) => {
let templatesHtml = ''; let templates = {};
for (const file of files) { for (const file of files) {
templatesHtml += util.format( const name = path.basename(file, '.hbs').replace(/_/g, '-');
'<template id=\'%s-template\'>%s</template>', templates[name] = minify(
path.basename(file, '.hbs').replace(/_/g, '-'), fs.readFileSync(file, 'utf-8'), minifyOptions);
fs.readFileSync(file));
} }
const templatesHolder = util.format(
'<script type=\'text/javascript\'>' +
'const templates = %s;' +
'</script>',
JSON.stringify(templates));
const finalHtml = baseHtml const finalHtml = baseHtml
.replace(/(<\/head>)/, templatesHtml + '$1') .replace(/(<\/head>)/, templatesHolder + '$1')
.replace( .replace(
/(<title>)(.*)(<\/title>)/, /(<title>)(.*)(<\/title>)/,
util.format('$1%s$3', config.name)); util.format('$1%s$3', config.name));
fs.writeFileSync( fs.writeFileSync(
'./public/index.htm', './public/index.htm', minify(finalHtml, minifyOptions));
minify(
finalHtml, {
removeComments: true,
collapseWhitespace: true,
conservativeCollapse: true}));
console.info('Bundled HTML'); console.info('Bundled HTML');
}); });
} }

View file

@ -76,12 +76,11 @@ function htmlToDom(html) {
} }
function getTemplate(templatePath) { function getTemplate(templatePath) {
const templateElement = document.getElementById(templatePath + '-template'); if (!(templatePath in templates)) {
if (!templateElement) {
console.error('Missing template: ' + templatePath); console.error('Missing template: ' + templatePath);
return null; return null;
} }
const templateText = templateElement.innerHTML.trim(); const templateText = templates[templatePath].trim();
const templateFactory = handlebars.compile(templateText); const templateFactory = handlebars.compile(templateText);
return (...args) => { return (...args) => {
return htmlToDom(templateFactory(...args)); return htmlToDom(templateFactory(...args));