UNPKG

hmpl-loader

Version:

Loader for files with hpml extension for webpack

74 lines (70 loc) 1.65 kB
const path = require("path"); const schemaUtils = require("schema-utils"); const schema = { type: "object", properties: { memo: { type: "boolean", }, autoBody: { anyOf: [ { type: "object", properties: { formData: { type: "boolean", }, }, additionalProperties: false, }, { type: "boolean" }, ], }, allowedContentTypes: { anyOf: [ { type: "array", items: { type: "string", }, }, { enum: ["*"], }, ], }, sanitize: { type: "boolean", }, disallowedTags: { type: "array", items: { enum: ["script", "style", "iframe"], }, }, }, 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;