From 307118d12190ed7037a40a714b9a8ccef5acc838 Mon Sep 17 00:00:00 2001 From: Sean McArthur <sean.monstar@gmail.com> Date: Fri, 21 Sep 2012 11:43:03 -0700 Subject: [PATCH] pre-process cachify in templates --- lib/templates.js | 28 ++++++++++++++++++- .../dialog/views/test_template_cachify.ejs | 6 ++++ .../static/test/cases/common/js/renderer.js | 8 ++++++ resources/static/test/mocks/cachify.js | 14 ---------- resources/views/test.ejs | 1 - scripts/create_templates.js | 9 +++++- 6 files changed, 49 insertions(+), 17 deletions(-) create mode 100644 resources/static/dialog/views/test_template_cachify.ejs delete mode 100644 resources/static/test/mocks/cachify.js diff --git a/lib/templates.js b/lib/templates.js index e515eae00..2fc0f4214 100644 --- a/lib/templates.js +++ b/lib/templates.js @@ -7,10 +7,32 @@ const fs = require('fs'), path = require('path'), ejs = require('ejs'), +cachify = require('connect-cachify'), config = require('./configuration'); var bundles = {}; + +const commentsRE = /<!--[\s\S]*?-->/g; +function stripComments(text) { + return String(text).replace(commentsRE, ''); +} + +const cachifyRE = /cachify\(\s*["']([^"']*)["']*\s*\)/g; // :-( +function preCachify(text) { + // cachify depends on an md5 hash of the contents of the file, so we + // can't determine that once we're on the client. This replaces + // instances of `cachify('some-url')` to the complete URL. + // + // Any instances of variables being used are part of the expression + // will FAIL. + // + // Bad: cachify( base + '/my-image.png') + return String(text).replace(cachifyRE, function(m, url) { + return "'" + cachify.cachify(url) + "'"; + }); +} + exports.generate = function generate(templatesDir, namePrefix, lastGen) { if (!namePrefix) namePrefix = ""; @@ -39,10 +61,14 @@ exports.generate = function generate(templatesDir, namePrefix, lastGen) { var fileName = fileNames[index]; if(fileName.match(/\.ejs$/)) { var templateName = namePrefix + fileName.replace(/\.ejs/, ''); + console.log('starting template: ', templateName); var templateText = fs.readFileSync(path.join(templatesDir, fileName), "utf8"); // remove HTML comments - templateText = templateText.replace(/<!--[\s\S]*?-->/g, ''); + templateText = stripComments(templateText); + + // we need to take care of cachify at this point + templateText = preCachify(templateText); templates[templateName] = ejs.compile(templateText, { client: true, diff --git a/resources/static/dialog/views/test_template_cachify.ejs b/resources/static/dialog/views/test_template_cachify.ejs new file mode 100644 index 000000000..efdd25184 --- /dev/null +++ b/resources/static/dialog/views/test_template_cachify.ejs @@ -0,0 +1,6 @@ +<!-- This Source Code Form is subject to the terms of the Mozilla Public + - License, v. 2.0. If a copy of the MPL was not distributed with this + - file, You can obtain one at http://mozilla.org/MPL/2.0/. --> + +<%- cachify('/dialog/css/style.css') %> + diff --git a/resources/static/test/cases/common/js/renderer.js b/resources/static/test/cases/common/js/renderer.js index d0472216b..8e3ab8fa2 100644 --- a/resources/static/test/cases/common/js/renderer.js +++ b/resources/static/test/cases/common/js/renderer.js @@ -39,6 +39,14 @@ ok($("#focusButton").length, "template loaded with partial"); }); + test("render dialog template with cachify", function() { + renderer.render("#formWrap .contents", "test_template_cachify"); + + var expected = /\/dialog\/css\/style\.css$/; + var value = $("#formWrap .contents").text().trim(); + ok(value.match(expected), "cachify has been pre-processed"); + }); + }()); diff --git a/resources/static/test/mocks/cachify.js b/resources/static/test/mocks/cachify.js deleted file mode 100644 index 62294edcc..000000000 --- a/resources/static/test/mocks/cachify.js +++ /dev/null @@ -1,14 +0,0 @@ -/* This Source Code Form is subject to the terms of the Mozilla Public - * License, v. 2.0. If a copy of the MPL was not distributed with this - * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -window.cachify = (function() { - "use strict"; - - // cachify is a node module used for caching resources, as such it is not - // available to the client. The main site makes use of cachify in its - // templates to serve up cached resources. The front end unit tests write - // the main site templates to the DOM to run. Create a mock cachify so the - // front end unit tests can run. - return function(url) { return url; } -}()); - diff --git a/resources/views/test.ejs b/resources/views/test.ejs index c4662d1d1..e307f6bb1 100644 --- a/resources/views/test.ejs +++ b/resources/views/test.ejs @@ -87,7 +87,6 @@ <script src="mocks/provisioning.js"></script> <script src="mocks/window.js"></script> <script src="mocks/winchan.js"></script> - <script src="mocks/cachify.js"></script> <script src="/common/js/templates.js"></script> <script src="/common/js/renderer.js"></script> diff --git a/scripts/create_templates.js b/scripts/create_templates.js index 6d91d995a..947cc103b 100755 --- a/scripts/create_templates.js +++ b/scripts/create_templates.js @@ -7,7 +7,14 @@ const fs = require("fs"), path = require('path'), -templates = require('../lib/templates'); +templates = require('../lib/templates'), +cachify = require('connect-cachify'), +config = require('../lib/configuration'); + +cachify.setup({}, { + prefix: config.get('cachify_prefix'), + root: path.join(__dirname, '../resources/static') +}); var existsSync = fs.existsSync || path.existsSync; var dir = process.env.TEMPLATE_DIR || process.cwd(); -- GitLab