hmpl-loader
Version:
Loader for files with hpml extension for webpack
83 lines (73 loc) • 1.74 kB
JavaScript
const path = require("path");
const schemaUtils = require("schema-utils");
const schema = {
type: "object",
properties: {
memo: {
type: "boolean",
},
autoBody: {
anyOf: [
{
type: "boolean",
},
{
type: "object",
properties: {
formData: { type: "boolean" },
},
additionalProperties: false,
},
],
},
allowedContentTypes: {
anyOf: [
{
type: "array",
items: { type: "string" },
},
{
type: "string",
enum: ["*"],
},
],
},
sanitize: {
type: "boolean",
},
disallowedTags: {
type: "array",
items: {
enum: ["script", "style", "iframe"],
},
},
sanitizeConfig: {
type: "object",
additionalProperties: true,
},
},
additionalProperties: false,
};
module.exports = function (source) {
if (this.cacheable) this.cacheable();
if (typeof source !== "string")
throw Error(
"Template was not found or the type of the passed value is not string"
);
const modulePath = JSON.stringify(
path.join("hmpl-js", "dist", "hmpl.runtime")
);
const result = [];
const template = JSON.stringify(source);
const options = this.getOptions();
schemaUtils.validate(schema, options, {
name: "hmpl-loader",
baseDataPath: "options",
});
const stringOptions = JSON.stringify(options);
result.push(`const hmpl = require(${modulePath});\n`);
result.push(`const template = hmpl.compile(${template},${stringOptions});\n`);
result.push(`module.exports = template;`);
return result.join("");
};
module.exports.seperable = true;