2019-07-12 02:04:01 +02:00
|
|
|
const fs = require("fs");
|
2020-02-10 12:32:56 +01:00
|
|
|
const Handlebars = require("handlebars/dist/cjs/handlebars.js");
|
2019-07-12 02:04:01 +02:00
|
|
|
const path = require("path");
|
2020-02-10 12:32:56 +01:00
|
|
|
const { SourceMapConsumer, SourceNode } = require("source-map");
|
2019-07-12 02:04:01 +02:00
|
|
|
|
|
|
|
const templates_path = path.resolve(__dirname, "../../static/templates");
|
|
|
|
|
2020-02-10 12:32:56 +01:00
|
|
|
exports.make_handlebars = () => Handlebars.create();
|
2019-07-12 02:04:01 +02:00
|
|
|
|
2020-02-10 12:32:56 +01:00
|
|
|
exports.stub_templates = stub => {
|
|
|
|
window.template_stub = stub;
|
|
|
|
};
|
2019-07-12 02:04:01 +02:00
|
|
|
|
2020-02-10 12:32:56 +01:00
|
|
|
const hb = Handlebars.create();
|
|
|
|
|
|
|
|
class ZJavaScriptCompiler extends hb.JavaScriptCompiler {
|
|
|
|
nameLookup(parent, name, type) {
|
|
|
|
// Auto-register partials with relative paths, like handlebars-loader.
|
|
|
|
if (type === "partial" && name !== "@partial-block") {
|
|
|
|
const filename = path.resolve(path.dirname(this.options.srcName), name + ".hbs");
|
|
|
|
return ["require(", JSON.stringify(filename), ")"];
|
2019-07-11 08:54:21 +02:00
|
|
|
}
|
2020-02-10 12:32:56 +01:00
|
|
|
return super.nameLookup(parent, name, type);
|
2019-07-11 08:54:21 +02:00
|
|
|
}
|
2020-02-10 12:32:56 +01:00
|
|
|
}
|
2019-07-11 08:54:21 +02:00
|
|
|
|
2020-02-10 12:32:56 +01:00
|
|
|
ZJavaScriptCompiler.prototype.compiler = ZJavaScriptCompiler;
|
|
|
|
hb.JavaScriptCompiler = ZJavaScriptCompiler;
|
2019-07-12 02:04:01 +02:00
|
|
|
|
|
|
|
require.extensions[".hbs"] = (module, filename) => {
|
2020-02-10 12:32:56 +01:00
|
|
|
const code = fs.readFileSync(filename, "utf-8");
|
|
|
|
const name = path.relative(templates_path, filename).slice(0, -".hbs".length);
|
|
|
|
const pc = hb.precompile(code, { preventIndent: true, srcName: filename });
|
|
|
|
const node = new SourceNode();
|
|
|
|
node.add([
|
|
|
|
"let hb, template;\n",
|
|
|
|
"module.exports = (...args) => {\n",
|
|
|
|
" if (window.template_stub !== undefined) {\n",
|
|
|
|
" return window.template_stub(",
|
|
|
|
JSON.stringify(name),
|
|
|
|
", ...args);\n",
|
|
|
|
" }\n",
|
|
|
|
" if (hb !== Handlebars) {\n",
|
|
|
|
" template = (hb = Handlebars).template(",
|
|
|
|
SourceNode.fromStringWithSourceMap(pc.code, new SourceMapConsumer(pc.map)),
|
|
|
|
");\n",
|
|
|
|
" }\n",
|
|
|
|
" return template(...args);\n",
|
|
|
|
"};\n",
|
|
|
|
]);
|
|
|
|
const out = node.toStringWithSourceMap();
|
|
|
|
module._compile(
|
|
|
|
out.code +
|
|
|
|
"\n//# sourceMappingURL=data:application/json;charset=utf-8;base64," +
|
|
|
|
Buffer.from(out.map.toString()).toString("base64"),
|
|
|
|
filename
|
|
|
|
);
|
2019-07-12 02:04:01 +02:00
|
|
|
};
|