From 5796b07908d99edd9224486a48969316779d379b Mon Sep 17 00:00:00 2001 From: rr- Date: Wed, 13 Apr 2016 18:58:34 +0200 Subject: [PATCH] client/build: don't keep templates in DOM --- client/build.js | 29 +++++++++++++++++------------ client/js/util/views.js | 5 ++--- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/client/build.js b/client/build.js index 80a7cf3c..81fc1893 100644 --- a/client/build.js +++ b/client/build.js @@ -58,28 +58,33 @@ function getConfig() { function bundleHtml(config) { const minify = require('html-minifier').minify; const baseHtml = fs.readFileSync('./html/index.htm', 'utf-8'); + const minifyOptions = { + removeComments: true, + collapseWhitespace: true, + conservativeCollapse: true, + }; glob('./html/**/*.hbs', {}, (er, files) => { - let templatesHtml = ''; + let templates = {}; for (const file of files) { - templatesHtml += util.format( - '', - path.basename(file, '.hbs').replace(/_/g, '-'), - fs.readFileSync(file)); + const name = path.basename(file, '.hbs').replace(/_/g, '-'); + templates[name] = minify( + fs.readFileSync(file, 'utf-8'), minifyOptions); } + const templatesHolder = util.format( + '', + JSON.stringify(templates)); + const finalHtml = baseHtml - .replace(/(<\/head>)/, templatesHtml + '$1') + .replace(/(<\/head>)/, templatesHolder + '$1') .replace( /()(.*)(<\/title>)/, util.format('$1%s$3', config.name)); fs.writeFileSync( - './public/index.htm', - minify( - finalHtml, { - removeComments: true, - collapseWhitespace: true, - conservativeCollapse: true})); + './public/index.htm', minify(finalHtml, minifyOptions)); console.info('Bundled HTML'); }); } diff --git a/client/js/util/views.js b/client/js/util/views.js index 786cb47a..c0aabc6e 100644 --- a/client/js/util/views.js +++ b/client/js/util/views.js @@ -76,12 +76,11 @@ function htmlToDom(html) { } function getTemplate(templatePath) { - const templateElement = document.getElementById(templatePath + '-template'); - if (!templateElement) { + if (!(templatePath in templates)) { console.error('Missing template: ' + templatePath); return null; } - const templateText = templateElement.innerHTML.trim(); + const templateText = templates[templatePath].trim(); const templateFactory = handlebars.compile(templateText); return (...args) => { return htmlToDom(templateFactory(...args));