Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

pre-process cachify in templates

  • Loading branch information...
commit 307118d12190ed7037a40a714b9a8ccef5acc838 1 parent e4714c8
@seanmonstar seanmonstar authored
View
28 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,
View
6 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') %>
+
View
8 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");
+ });
+
}());
View
14 resources/static/test/mocks/cachify.js
@@ -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; }
-}());
-
View
1  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>
View
9 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();
Please sign in to comment.
Something went wrong with that request. Please try again.