@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.02 kB
JavaScript
import {z as z$1}from'zod';import v from'@zilero/gulp-plugin-factory';import {compileFile}from'pug';import {consola}from'consola';import p from'chalk';z$1.custom();z$1.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.gray(s)}`,` Expected: ${p.green(o)}`,` Received: ${p.yellow(r)}`].join(`
`)}case "invalid_type":return ["Validation failed:",` Path: ${p.gray(s)}`,` Expected: ${p.green(e.expected)}`,` Received: ${p.yellow(e.received)}`].join(`
`);case "unrecognized_keys":return ["Found extra keys:",...e.keys.map(n=>` \u2022 ${p.yellow(n)}`)].join(`
`);default:return [e.message,` Path: ${p.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 z$1.ZodError?(consola.box({title:p.red(`[${e} Error]`),message:r.errors.map(c).join(`
`),style:{borderColor:"red",borderStyle:"round",padding:1}}),new Error):r}}}var O=z$1.function().args(z$1.string()).returns(z$1.union([z$1.promise(z$1.string()),z$1.string()])).optional(),P=z$1.custom(),C=z$1.object({onAfterCompile:O.optional()}),f=z$1.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({pluginName:a,onFile:async o=>{try{if(o.isBuffer()){let t=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;export{W as default};