{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { join } from \"node:path\"\nimport type { Options as PugOptions, LocalsObject } from \"pug\"\nimport type { Logger, Plugin } from \"vite\"\nimport { compileFile } from \"pug\"\nimport pc from \"picocolors\"\n\ninterface PluginOptions extends PugOptions {\n  /**\n   * Look for pug files in the directory\n   * of currently compiled index.html\n   * (locally)\n   * instead of project root.\n   *\n   * Can accept a function to determine the option per-html-file.\n   */\n  localImports?: boolean | ((htmlfile: string) => boolean)\n}\n\nexport function pugs(html: string, pugger: (filename: string) => string, logger?: Pick<Logger, \"warn\">) {\n  return html.replace(/<pug.+?(file|src)=\"(.+?)\".*?\\/.*?>/gi, (_tag: string, attr: string, filename: string) => {\n    if (attr === \"file\" && logger) {\n      logger.warn(`the \\`file\\` attribute is deprecated, please include ${filename} with \\`src\\` instead`)\n    }\n    return pugger(filename)\n  })\n}\n\nexport default function pugPlugin(options?: PluginOptions, locals?: LocalsObject): Plugin {\n  return {\n    name: \"vite-plugin-pug\",\n\n    handleHotUpdate({ file, server }) {\n      if (file.endsWith(\".pug\")) {\n        server.config.logger.info(`pug’s not hot 🌭 ${file}`)\n        server.ws.send({\n          type: \"full-reload\"\n        })\n      }\n    },\n\n    transformIndexHtml: {\n      transform(html, { server, filename: htmlfile }) {\n        return pugs(\n          html,\n          filename => {\n            const compile = (filepath: string) => compileFile(filepath, options)(locals)\n            if (\n              (typeof options?.localImports === \"function\" && options.localImports(htmlfile)) ||\n              options?.localImports\n            ) {\n              // extract current directory from the html file path\n              const filedir = htmlfile.replace(/(.*)[\\\\\\/].*\\.html$/, \"$1\")\n\n              // apply current directory to the pug file imported from html\n              const filepath = join(filedir, filename)\n\n              return compile(filepath)\n            }\n\n            return compile(filename)\n          },\n          server?.config.logger\n        )\n      }\n    }\n  }\n}\n"],"mappings":"yaAAA,IAAAA,EAAA,GAAAC,EAAAD,EAAA,aAAAE,EAAA,SAAAC,IAAA,eAAAC,EAAAJ,GAAA,IAAAK,EAAqB,gBAGrBC,EAA4B,eAC5BC,EAAe,sBAcR,SAASJ,EAAKK,EAAcC,EAAsCC,EAA+B,CACtG,OAAOF,EAAK,QAAQ,uCAAwC,CAACG,EAAcC,EAAcC,KACnFD,IAAS,QAAUF,GACrBA,EAAO,KAAK,wDAAwDG,wBAA+B,EAE9FJ,EAAOI,CAAQ,EACvB,CACH,CAEe,SAARX,EAA2BY,EAAyBC,EAA+B,CACxF,MAAO,CACL,KAAM,kBAEN,gBAAgB,CAAE,KAAAC,EAAM,OAAAC,CAAO,EAAG,CAC5BD,EAAK,SAAS,MAAM,IACtBC,EAAO,OAAO,OAAO,KAAK,gCAAoBD,GAAM,EACpDC,EAAO,GAAG,KAAK,CACb,KAAM,aACR,CAAC,EAEL,EAEA,mBAAoB,CAClB,UAAUT,EAAM,CAAE,OAAAS,EAAQ,SAAUC,CAAS,EAAG,CAC9C,OAAOf,EACLK,EACAK,GAAY,CACV,IAAMM,EAAWC,MAAqB,eAAYA,EAAUN,CAAO,EAAEC,CAAM,EAC3E,GACG,OAAOD,GAAS,cAAiB,YAAcA,EAAQ,aAAaI,CAAQ,GAC7EJ,GAAS,aACT,CAEA,IAAMO,EAAUH,EAAS,QAAQ,sBAAuB,IAAI,EAGtDE,KAAW,QAAKC,EAASR,CAAQ,EAEvC,OAAOM,EAAQC,CAAQ,CACzB,CAEA,OAAOD,EAAQN,CAAQ,CACzB,EACAI,GAAQ,OAAO,MACjB,CACF,CACF,CACF,CACF","names":["src_exports","__export","pugPlugin","pugs","__toCommonJS","import_node_path","import_pug","import_picocolors","html","pugger","logger","_tag","attr","filename","options","locals","file","server","htmlfile","compile","filepath","filedir"]}