seitime-frappe/esbuild/frappe-html.js
2022-08-04 14:51:01 +05:30

44 lines
975 B
JavaScript

module.exports = {
name: "frappe-html",
setup(build) {
let path = require("path");
let fs = require("fs/promises");
build.onResolve({ filter: /\.html$/ }, (args) => {
return {
path: path.join(args.resolveDir, args.path),
namespace: "frappe-html",
};
});
build.onLoad({ filter: /.*/, namespace: "frappe-html" }, (args) => {
let filepath = args.path;
let filename = path.basename(filepath).split(".")[0];
return fs
.readFile(filepath, "utf-8")
.then((content) => {
content = scrub_html_template(content);
return {
contents: `\n\tfrappe.templates['${filename}'] = \`${content}\`;\n`,
watchFiles: [filepath],
};
})
.catch(() => {
return {
contents: "",
warnings: [
{
text: `There was an error importing ${filepath}`,
},
],
};
});
});
},
};
function scrub_html_template(content) {
content = content.replace(/`/g, "\\`");
return content;
}