@zilero/gulp-pug-compiler
Version:
A Gulp plugin for compiling Pug templates to HTML with support for layouts, includes, and custom options
7 lines (6 loc) • 2.38 kB
JavaScript
;var zod=require('zod'),v=require('@zilero/gulp-plugin-factory'),pug=require('pug'),consola=require('consola'),p=require('chalk');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var v__default=/*#__PURE__*/_interopDefault(v);var p__default=/*#__PURE__*/_interopDefault(p);zod.z.custom();zod.z.custom(e=>e&&typeof e=="object"&&"pipe"in e&&typeof e.pipe=="function",{message:"Expected a valid Transform stream"});var c=e=>{let s=e.path.join(".");switch(e.code){case "invalid_union":{let n=e.unionErrors.flatMap(({issues:t})=>t).filter(t=>t.code==="invalid_type"),o=n.map(({expected:t})=>t).filter((t,m,g)=>g.indexOf(t)===m).join(" or "),r=n[0]?.received??"unknown";return ["Validation failed:",` Path: ${p__default.default.gray(s)}`,` Expected: ${p__default.default.green(o)}`,` Received: ${p__default.default.yellow(r)}`].join(`
`)}case "invalid_type":return ["Validation failed:",` Path: ${p__default.default.gray(s)}`,` Expected: ${p__default.default.green(e.expected)}`,` Received: ${p__default.default.yellow(e.received)}`].join(`
`);case "unrecognized_keys":return ["Found extra keys:",...e.keys.map(n=>` \u2022 ${p__default.default.yellow(n)}`)].join(`
`);default:return [e.message,` Path: ${p__default.default.gray(s)}`].join(`
`)}};function u({name:e,schema:s,defaults:n}){return o=>{try{let r={...n,...o};return s.parse(r)}catch(r){throw r instanceof zod.z.ZodError?(consola.consola.box({title:p__default.default.red(`[${e} Error]`),message:r.errors.map(c).join(`
`),style:{borderColor:"red",borderStyle:"round",padding:1}}),new Error):r}}}var O=zod.z.function().args(zod.z.string()).returns(zod.z.union([zod.z.promise(zod.z.string()),zod.z.string()])).optional(),P=zod.z.custom(),C=zod.z.object({onAfterCompile:O.optional()}),f=zod.z.object({pugOptions:P.optional(),pluginOptions:C.optional()}).strict();var S={pluginOptions:{}},d=S;var a="GulpPugCompiler";var z=u({name:a,schema:f,defaults:d}),E=e=>{let{pugOptions:s={},pluginOptions:n={}}=z(e);return v__default.default({pluginName:a,onFile:async o=>{try{if(o.isBuffer()){let t=pug.compileFile(o.path,{...s,filename:o.path,basedir:o.base})();if(n.onAfterCompile){let m=await n.onAfterCompile(t);m&&(t=m);}o.contents=Buffer.from(t),o.extname=".html";}return o}catch(r){throw new Error(`An error occurred while processing file ${o.relative}.`,{cause:r})}}})},W=E;module.exports=W;