From 0bd5c46e40d7a665ab9bb755720189c9b1eab885 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Wed, 10 Jul 2019 23:54:21 -0700 Subject: [PATCH] zjsunit/handlebars: Support auto-registering partials. webpack with handlebars-loader already does this, so we only need to fix up the tests. Signed-off-by: Anders Kaseorg --- frontend_tests/zjsunit/handlebars.js | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/frontend_tests/zjsunit/handlebars.js b/frontend_tests/zjsunit/handlebars.js index ad06f58650..7054c6d490 100644 --- a/frontend_tests/zjsunit/handlebars.js +++ b/frontend_tests/zjsunit/handlebars.js @@ -9,15 +9,31 @@ module.exports.make_handlebars = () => { const Handlebars = require("handlebars/dist/cjs/handlebars.js"); const hb = Handlebars.create(); - const compiled = new Map(); - - render = (filename, ...args) => { - if (!compiled.has(filename)) { - compiled.set(filename, hb.compile(fs.readFileSync(filename, "utf-8"))); + const compiled = new Set(); + const compileFile = filename => { + const name = "$" + path.relative(templates_path, filename); + if (!compiled.has(name)) { + compiled.add(name); + hb.registerPartial(name, hb.compile(fs.readFileSync(filename, "utf-8"), { zjsFilename: filename })); } - return compiled.get(filename)(...args); + return name; }; + class ZJavaScriptCompiler extends hb.JavaScriptCompiler { + nameLookup(parent, name, type) { + // Auto-register partials with relative paths, like handlebars-loader. + if (type === "partial" && name !== "@partial-block") { + name = compileFile(path.resolve(path.dirname(this.options.zjsFilename), name + ".hbs")); + } + return super.nameLookup(parent, name, type); + } + } + + ZJavaScriptCompiler.prototype.compiler = ZJavaScriptCompiler; + hb.JavaScriptCompiler = ZJavaScriptCompiler; + + render = (filename, ...args) => hb.partials[compileFile(filename)](...args); + return hb; };