2020-08-01 03:43:15 +02:00
|
|
|
"use strict";
|
|
|
|
|
2024-10-09 00:25:41 +02:00
|
|
|
const fs = require("node:fs");
|
|
|
|
const path = require("node:path");
|
2020-07-24 06:02:07 +02:00
|
|
|
|
2020-07-29 01:37:13 +02:00
|
|
|
const Handlebars = require("handlebars");
|
2020-07-16 22:40:18 +02:00
|
|
|
const {SourceMapConsumer, SourceNode} = require("source-map");
|
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
|
|
|
|
2020-12-01 00:19:42 +01:00
|
|
|
function compile_hbs(module, filename) {
|
2022-03-02 06:52:04 +01:00
|
|
|
const code = fs.readFileSync(filename, "utf8");
|
2023-03-22 02:18:19 +01:00
|
|
|
const pc = hb.precompile(code, {preventIndent: true, srcName: filename, strict: true});
|
2020-02-10 12:32:56 +01:00
|
|
|
const node = new SourceNode();
|
|
|
|
node.add([
|
2020-07-29 01:37:13 +02:00
|
|
|
'const Handlebars = require("handlebars/runtime");\n',
|
2021-06-14 21:20:56 +02:00
|
|
|
"module.exports = Handlebars.template(",
|
2020-07-29 01:37:13 +02:00
|
|
|
SourceNode.fromStringWithSourceMap(pc.code, new SourceMapConsumer(pc.map)),
|
|
|
|
");\n",
|
2020-02-10 12:32:56 +01:00
|
|
|
]);
|
|
|
|
const out = node.toStringWithSourceMap();
|
|
|
|
module._compile(
|
|
|
|
out.code +
|
|
|
|
"\n//# sourceMappingURL=data:application/json;charset=utf-8;base64," +
|
|
|
|
Buffer.from(out.map.toString()).toString("base64"),
|
2020-07-02 02:16:03 +02:00
|
|
|
filename,
|
2020-02-10 12:32:56 +01:00
|
|
|
);
|
2020-12-01 00:19:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
exports.hook_require = () => {
|
|
|
|
require.extensions[".hbs"] = compile_hbs;
|
2019-07-12 02:04:01 +02:00
|
|
|
};
|