seitime-frappe/esbuild/esbuild-plugin-html.js
Faris Ansari 226ad1d91a feat: New Build System based on esbuild
- Deprecate use of build.json
- *.bundle.js files placed anywhere in the public folder are bundled
- Built files are created in public/build folder which is gitignored

WIP
2021-04-22 07:01:36 +05:30

44 lines
1,020 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`
};
})
.catch(() => {
return {
contents: "",
warnings: [
{
text: `There was an error importing ${filepath}`
}
]
};
});
});
}
};
function scrub_html_template(content) {
content = content.replace(/\s/g, " ");
content = content.replace(/(<!--.*?-->)/g, "");
return content.replace("'", "'"); // eslint-disable-line
}